aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/lockclock/misc/IconUtils.java
blob: 437d0759d69eb16c8422ebb1034fdb3c472a5f92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (C) 2013 The CyanogenMod Project (DvTonder)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cyanogenmod.lockclock.misc;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.Log;
import com.cyanogenmod.lockclock.R;

public class IconUtils {
    private static final String TAG = "IconUtils";
    private static boolean D = Constants.DEBUG;

    public static int getWeatherIconResource(Context context, String iconSet, int conditionCode) {
        if (iconSet.startsWith("ext:") || iconSet.equals(Constants.MONOCHROME)) {
            return 0;
        }

        final Resources res = context.getResources();
        final int resId = res.getIdentifier("weather_" + iconSet + "_" + conditionCode,
                "drawable", context.getPackageName());

        if (resId != 0) {
            return resId;
        }

        // Use the default color set unknown icon
        return R.drawable.weather_color_na;
    }

    public static Bitmap getWeatherIconBitmap(Context context, String iconSet,
            int color, int conditionCode) {
        return getWeatherIconBitmap(context, iconSet, color, conditionCode, 0);
    }

    public static Bitmap getWeatherIconBitmap(Context context, String iconSet,
            int color, int conditionCode, int density) {
        boolean isMonoSet = Constants.MONOCHROME.equals(iconSet);
        Resources res = null;
        int resId = 0;

        if (iconSet.startsWith("ext:")) {
            String packageName = iconSet.substring(4);
            try {
                res = context.getPackageManager().getResourcesForApplication(packageName);
                resId = res.getIdentifier("weather_" + conditionCode, "drawable", packageName);
            } catch (PackageManager.NameNotFoundException e) {
                // fall back to colored icons
                iconSet = Constants.COLOR_STD;
            }
        }
        if (resId == 0) {
            String identifier = isMonoSet
                    ? "weather_" + conditionCode : "weather_" + iconSet + "_" + conditionCode;
            res = context.getResources();
            resId = res.getIdentifier(identifier, "drawable", context.getPackageName());
        }

        if (resId == 0) {
            resId = isMonoSet ? R.drawable.weather_na : R.drawable.weather_color_na;
        }

        return getOverlaidBitmap(res, resId, isMonoSet ? color : 0, density);
    }

    public static Bitmap getOverlaidBitmap(Resources res, int resId, int color) {
        return getOverlaidBitmap(res, resId, color, 0);
    }

    public static Bitmap getOverlaidBitmap(Resources res, int resId, int color, int density) {
        Bitmap src = getBitmapFromResource(res, resId, density);
        if (color == 0 || src == null) {
            return src;
        }

        final Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(dest);
        final Paint paint = new Paint();

        // Overlay the selected color and set the imageview
        paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
        c.drawBitmap(src, 0, 0, paint);
        return dest;
    }

    public static Bitmap getBitmapFromResource(Resources res, int resId, int density) {
        if (density == 0) {
            if (D) Log.d(TAG, "Decoding resource id = " + resId + " for default density");
            return BitmapFactory.decodeResource(res, resId);
        }

        if (D) Log.d(TAG, "Decoding resource id = " + resId + " for density = " + density);
        Drawable d = res.getDrawableForDensity(resId, density);
        if (d instanceof BitmapDrawable) {
            BitmapDrawable bd = (BitmapDrawable) d;
            return bd.getBitmap();
        }

        Bitmap result = Bitmap.createBitmap(d.getIntrinsicWidth(),
                d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        d.setBounds(0, 0, result.getWidth(), result.getHeight());
        d.draw(canvas);
        canvas.setBitmap(null);

        return result;
    }

    public static int getNextHigherDensity(Context context) {
        Resources res = context.getResources();
        int density = res.getDisplayMetrics().densityDpi;

        if (density == DisplayMetrics.DENSITY_LOW) {
            return DisplayMetrics.DENSITY_MEDIUM;
        } else if (density == DisplayMetrics.DENSITY_MEDIUM) {
            return DisplayMetrics.DENSITY_HIGH;
        } else if (density == DisplayMetrics.DENSITY_HIGH) {
            return DisplayMetrics.DENSITY_XHIGH;
        } else if (density == DisplayMetrics.DENSITY_XHIGH) {
            return DisplayMetrics.DENSITY_XXHIGH;
        } else if (density == DisplayMetrics.DENSITY_XXHIGH) {
            return DisplayMetrics.DENSITY_XXXHIGH;
        }

        // fallback: use current density
        return density;
    }
}