summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/themes/provider/util/SystemUiPreviewGenerator.java
blob: d53ad2fe8efaa42377ec812e777d9750ff1453ed (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * 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 org.cyanogenmod.themes.provider.util;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.widget.FrameLayout;
import org.cyanogenmod.themes.provider.view.BatteryMeterView;

import org.cyanogenmod.themes.provider.R;

public class SystemUiPreviewGenerator {
    public static final String SYSTEMUI_PACKAGE = "com.android.systemui";
    private static final String WIFI_DRAWABLE = "stat_sys_wifi_signal_3_fully";
    private static final String SIGNAL_DRAWABLE = "stat_sys_signal_3_fully";
    private static final String BLUETOOTH_DRAWABLE = "stat_sys_data_bluetooth_connected";
    private static final String STATUS_BAR_BATTERY_WIDTH = "status_bar_battery_width";
    private static final String STATUS_BAR_BATTERY_HEIGHT = "status_bar_battery_height";
    private static final String STATUS_BAR_CLOCK_COLOR = "status_bar_clock_color";
    private static final String SYSTEM_BAR_BACKGROUND = "system_bar_background";
    private static final String STATUS_BAR_BACKGROUND_OPAQUE = "status_bar_background_opaque";
    private static final String NAVIGATION_BAR_BACKGROUND_OPAQUE
            = "navigation_bar_background_opaque";
    private static final String IC_SYSBAR_BACK = "ic_sysbar_back";
    private static final String IC_SYSBAR_HOME = "ic_sysbar_home";
    private static final String IC_SYSBAR_RECENT = "ic_sysbar_recent";
    private static final String STATUS_BAR_ICON_SIZE = "status_bar_icon_size";

    private Context mContext;

    public SystemUiPreviewGenerator(Context context) {
        mContext = context;
    }

    /**
     * Loads the necessary resources for the given theme.
     * @param pkgName Package name for the theme to use
     * @return
     * @throws NameNotFoundException
     */
    public SystemUiItems generateSystemUiItems(String pkgName) throws NameNotFoundException {
        PackageManager pm = mContext.getPackageManager();
        Resources res = pm.getThemedResourcesForApplication(SYSTEMUI_PACKAGE, pkgName);
        int iconSize = res.getDimensionPixelSize(res.getIdentifier(STATUS_BAR_ICON_SIZE, "dimen",
                        SYSTEMUI_PACKAGE));

        SystemUiItems items = new SystemUiItems();
        Drawable d = res.getDrawable(res.getIdentifier(BLUETOOTH_DRAWABLE, "drawable",
                SYSTEMUI_PACKAGE));
        items.bluetoothIcon = renderDrawableToBitmap(d, iconSize);

        d = res.getDrawable(res.getIdentifier(WIFI_DRAWABLE, "drawable", SYSTEMUI_PACKAGE));
        items.wifiIcon = renderDrawableToBitmap(d, iconSize);

        d = res.getDrawable(res.getIdentifier(SIGNAL_DRAWABLE, "drawable", SYSTEMUI_PACKAGE));
        items.signalIcon = renderDrawableToBitmap(d, iconSize);

        items.clockColor = res.getColor(res.getIdentifier(STATUS_BAR_CLOCK_COLOR, "color",
                SYSTEMUI_PACKAGE));
        // wifi margin no longer used in systemui
        items.wifiMarginEnd = 0;
        generateBatteryPreviews(res, items);
        generateBackgroundPreviews(res, items);

        items.navbarBack = BitmapFactory.decodeResource(res, res.getIdentifier(IC_SYSBAR_BACK,
                "drawable", SYSTEMUI_PACKAGE));
        items.navbarHome = BitmapFactory.decodeResource(res, res.getIdentifier(IC_SYSBAR_HOME,
                "drawable", SYSTEMUI_PACKAGE));
        items.navbarRecent = BitmapFactory.decodeResource(res, res.getIdentifier(IC_SYSBAR_RECENT,
                "drawable", SYSTEMUI_PACKAGE));

        return items;
    }

    private Bitmap renderDrawableToBitmap(Drawable d, int iconSize) {
        if (d instanceof VectorDrawable) {
            return renderVectorDrawableToBitmap((VectorDrawable) d, iconSize);
        } else if (d instanceof BitmapDrawable) {
            return ((BitmapDrawable) d).getBitmap();
        }

        return null;
    }

    private Bitmap renderVectorDrawableToBitmap(VectorDrawable d, int iconSize) {
        Bitmap bmp = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        d.setBounds(0, 0, iconSize, iconSize);
        d.draw(canvas);

        return bmp;
    }

    /**
     * Generates the various battery types using the provided resources.
     * @param res
     * @param items
     */
    private void generateBatteryPreviews(Resources res, SystemUiItems items) {
        FrameLayout view = new FrameLayout(mContext);
        BatteryMeterView battery = new BatteryMeterView(mContext, res);
        view.addView(battery,
                new FrameLayout.LayoutParams(mContext.getResources().getDimensionPixelSize(
                        R.dimen.status_bar_battery_width),
                        mContext.getResources().getDimensionPixelSize(
                                R.dimen.status_bar_battery_height)));
        battery.setMode(BatteryMeterView.BatteryMeterMode.BATTERY_METER_ICON_PORTRAIT);
        items.batteryPortrait = LayoutRenderUtils.renderViewToBitmap(view);
        battery.setMode(BatteryMeterView.BatteryMeterMode.BATTERY_METER_ICON_LANDSCAPE);
        items.batteryLandscape = LayoutRenderUtils.renderViewToBitmap(view);
        battery.setMode(BatteryMeterView.BatteryMeterMode.BATTERY_METER_CIRCLE);
        items.batteryCircle = LayoutRenderUtils.renderViewToBitmap(view);
    }

    private void generateBackgroundPreviews(Resources res, SystemUiItems items) {
        int width = Math.max(res.getDisplayMetrics().widthPixels,
                res.getDisplayMetrics().heightPixels);
        int height = mContext.getResources().getDimensionPixelSize(
                R.dimen.status_bar_battery_height);
        Drawable defaultBackground = res.getDrawable(
                res.getIdentifier(SYSTEM_BAR_BACKGROUND, "drawable", SYSTEMUI_PACKAGE));
        defaultBackground.setBounds(0, 0, width, height);

        Drawable statusbarBackground = null;
        int resId = res.getIdentifier(STATUS_BAR_BACKGROUND_OPAQUE, "color", SYSTEMUI_PACKAGE);
        if (resId != 0) {
            statusbarBackground = new ColorDrawable(res.getColor(resId));
            statusbarBackground.setBounds(0, 0, width, height);
        }
        items.statusbarBackground = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(items.statusbarBackground);
        if (statusbarBackground != null) {
            statusbarBackground.draw(canvas);
        } else {
            defaultBackground.draw(canvas);
        }

        Drawable navbarBackground = null;
        resId = res.getIdentifier(NAVIGATION_BAR_BACKGROUND_OPAQUE, "color", SYSTEMUI_PACKAGE);
        if (resId != 0) {
            navbarBackground = new ColorDrawable(res.getColor(resId));
            navbarBackground.setBounds(0, 0, width, height);
        }
        items.navbarBackground = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        canvas.setBitmap(items.navbarBackground);
        if (navbarBackground != null) {
            navbarBackground.draw(canvas);
        } else {
            defaultBackground.draw(canvas);
        }
    }

    public class SystemUiItems {
        /**
         * Status bar items
         */
        public Bitmap bluetoothIcon;
        public Bitmap wifiIcon;
        public Bitmap signalIcon;
        public Bitmap batteryPortrait;
        public Bitmap batteryLandscape;
        public Bitmap batteryCircle;
        public Bitmap statusbarBackground;
        public int clockColor;
        public int wifiMarginEnd;

        /**
         * Navigation bar items
         */
        public Bitmap navbarBackground;
        public Bitmap navbarBack;
        public Bitmap navbarHome;
        public Bitmap navbarRecent;
    }
}