summaryrefslogtreecommitdiffstats
path: root/tests/ThemesTest/src/com/example/themetests/MainActivity.java
blob: 2793272374c39c6673b9801212634a96b28872be (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright 2014 The Android Open Source 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 com.example.themetests;

import android.app.Activity;
import android.app.ComposedIconInfo;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageInfo;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.content.pm.ThemeUtils;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.ThemeConfig;
import android.database.Cursor;
import android.os.Bundle;

import android.os.ServiceManager;
import android.provider.ThemesContract.ThemesColumns;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";

    private ListView mThemeList;
    private Button mDetachButton;
    private ImageView mImage;
    private ImageView mIcon;

    private Resources mResources;
    private AssetManager mAssets;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mThemeList = (ListView) findViewById(R.id.theme_list);
        mDetachButton = (Button) findViewById(R.id.detach_assets);
        mDetachButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                detachThemeAssets(mResources, mAssets);
                mThemeList.setEnabled(true);
                mDetachButton.setEnabled(false);
                updateImages();
            }
        });
        mImage = (ImageView) findViewById(R.id.image);
        mIcon = (ImageView) findViewById(R.id.icon);
    }

    @Override
    protected void onResume() {
        super.onResume();
        PackageManager pm = getPackageManager();
        Context ctx = null;
        try {
            ctx = createPackageContext("com.android.systemui", 0);
            mAssets = ctx.getAssets();
            mResources = ctx.getResources();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        updateImages();
        loadThemes();
    }

    private void loadThemes() {
        String[] columns = {ThemesColumns._ID, ThemesColumns.TITLE, ThemesColumns.PKG_NAME};
        String selection = ThemesColumns.PRESENT_AS_THEME + "=? AND " +
                ThemesColumns.PKG_NAME + "<>?";
        String[] selectionArgs = {"1", ThemeConfig.SYSTEM_DEFAULT};
        Cursor c = getContentResolver().query(ThemesColumns.CONTENT_URI, columns, selection,
                selectionArgs, null);
        if (c != null) {
            ThemeAdapter adapter = new ThemeAdapter(this, c, 0);
            mThemeList.setAdapter(adapter);
            mThemeList.setOnItemClickListener(mThemeClicked);
        }
    }

    private boolean attachThemeAssets(Resources res, AssetManager assets, String pkgName) {
        final PackageManager pm = getPackageManager();
        PackageInfo piTheme = null;
        PackageInfo piAndroid = null;
        PackageInfo piTarget = null;
        PackageInfo piIcon = null;

        String basePackageName = null;
        int count = assets.getBasePackageCount();
        if (count > 1) {
            basePackageName  = assets.getBasePackageName(1);
        } else if (count == 1) {
            basePackageName  = assets.getBasePackageName(0);
        } else {
            return false;
        }

        try {
            piTheme = pm.getPackageInfo(pkgName, 0);
            piAndroid = getPackageManager().getPackageInfo("android", 0);
            piTarget = getPackageManager().getPackageInfo(basePackageName, 0);
            piIcon = getPackageManager().getPackageInfo(pkgName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        if (piTheme == null || piTheme.applicationInfo == null ||
                piAndroid == null || piAndroid.applicationInfo == null ||
                piTheme.mOverlayTargets == null) {
            return false;
        }

        String themePackageName = pkgName;
        String themePath = piTheme.applicationInfo.publicSourceDir;
        if (!piTarget.isThemeApk && piTheme.mOverlayTargets.contains(basePackageName)) {
            String targetPackagePath = piTarget.applicationInfo.sourceDir;
            String prefixPath = ThemeUtils.getOverlayPathToTarget(basePackageName);

            String resCachePath = ThemeUtils.getResDir(basePackageName, piTheme);
            String resTablePath = resCachePath + "/resources.arsc";
            String resApkPath = resCachePath + "/resources.apk";
            int cookie = assets.addOverlayPath(themePath, resTablePath, resApkPath,
                    targetPackagePath, prefixPath);

            if (cookie != 0) {
                assets.setThemePackageName(themePackageName);
                assets.addThemeCookie(cookie);
            }
        }

        if (!piTarget.isThemeApk && piTheme.mOverlayTargets.contains("android")) {
            String resCachePath= ThemeUtils.getResDir(piAndroid.packageName, piTheme);
            String prefixPath = ThemeUtils.getOverlayPathToTarget(piAndroid.packageName);
            String targetPackagePath = piAndroid.applicationInfo.publicSourceDir;
            String resTablePath = resCachePath + "/resources.arsc";
            String resApkPath = resCachePath + "/resources.apk";
            int cookie = assets.addOverlayPath(themePath, resTablePath,
                    resApkPath, targetPackagePath, prefixPath);
            if (cookie != 0 && !assets.getThemeCookies().contains(cookie)) {
                assets.setThemePackageName(themePackageName);
                assets.addThemeCookie(cookie);
            }
        }

        if (piIcon != null) {
            String themeIconPath =  piIcon.applicationInfo.publicSourceDir;
            String prefixPath = ThemeUtils.ICONS_PATH;
            String iconDir = ThemeUtils.getIconPackDir(pkgName);
            String resTablePath = iconDir + "/resources.arsc";
            String resApkPath = iconDir + "/resources.apk";

            // Legacy Icon packs have everything in their APK
            if (piIcon.isLegacyIconPackApk) {
                prefixPath = "";
                resApkPath = "";
                resTablePath = "";
            }

            int cookie = assets.addIconPath(themeIconPath, resTablePath, resApkPath, prefixPath,
                    Resources.THEME_ICON_PKG_ID);
            if (cookie != 0) {
                assets.setIconPackCookie(cookie);
                assets.setIconPackageName(pkgName);
                setActivityIcons(res);
            }
        }

        res.updateStringCache();
        return true;
    }

    private void detachThemeAssets(Resources res, AssetManager assets) {
        String themePackageName = assets.getThemePackageName();
        String iconPackageName = assets.getIconPackageName();
        String commonResPackageName = assets.getCommonResPackageName();

        //Remove Icon pack if it exists
        if (!TextUtils.isEmpty(iconPackageName) && assets.getIconPackCookie() > 0) {
            assets.removeOverlayPath(iconPackageName, assets.getIconPackCookie());
            assets.setIconPackageName(null);
            assets.setIconPackCookie(0);
        }
        //Remove common resources if it exists
        if (!TextUtils.isEmpty(commonResPackageName) && assets.getCommonResCookie() > 0) {
            assets.removeOverlayPath(commonResPackageName, assets.getCommonResCookie());
            assets.setCommonResPackageName(null);
            assets.setCommonResCookie(0);
        }
        final List<Integer> themeCookies = assets.getThemeCookies();
        if (!TextUtils.isEmpty(themePackageName) && !themeCookies.isEmpty()) {
            // remove overlays in reverse order
            for (int i = themeCookies.size() - 1; i >= 0; i--) {
                assets.removeOverlayPath(themePackageName, themeCookies.get(i));
            }
        }
        assets.getThemeCookies().clear();
        assets.setThemePackageName(null);
        //res.updateStringCache();
    }

    private void setActivityIcons(Resources r, String themePkgName) {
        SparseArray<PackageItemInfo> iconResources = new SparseArray<PackageItemInfo>();
        String pkgName = r.getAssets().getAppName();
        PackageInfo pkgInfo = null;
        ApplicationInfo appInfo = null;

        try {
            pkgInfo = getPackageManager().getPackageInfo(pkgName, PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            return;
        }

        final ThemeConfig themeConfig = r.getConfiguration().themeConfig;
        if (pkgName != null && themeConfig != null &&
                pkgName.equals(themeConfig.getIconPackPkgName())) {
            return;
        }

        //Map application icon
        if (pkgInfo != null && pkgInfo.applicationInfo != null) {
            appInfo = pkgInfo.applicationInfo;
            if (appInfo.themedIcon != 0 || iconResources.get(appInfo.icon) == null) {
                iconResources.put(appInfo.icon, appInfo);
            }
        }

        //Map activity icons.
        if (pkgInfo != null && pkgInfo.activities != null) {
            for (ActivityInfo ai : pkgInfo.activities) {
                if (ai.icon != 0 && (ai.themedIcon != 0 || iconResources.get(ai.icon) == null)) {
                    iconResources.put(ai.icon, ai);
                } else if (appInfo != null && appInfo.icon != 0 &&
                        (ai.themedIcon != 0 || iconResources.get(appInfo.icon) == null)) {
                    iconResources.put(appInfo.icon, ai);
                }
            }
        }

        r.setIconResources(iconResources);
        final IPackageManager pm = IPackageManager.Stub.asInterface(
                ServiceManager.getService("package"));
        try {
            ComposedIconInfo iconInfo = pm.getComposedIconInfo();
            r.setComposedIconInfo(iconInfo);
        } catch (Exception e) {
        }
    }

    AdapterView.OnItemClickListener mThemeClicked = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String pkgName = (String) view.getTag();
            if (attachThemeAssets(mResources, mAssets, pkgName)) {
                mThemeList.setEnabled(false);
                mDetachButton.setEnabled(true);
                updateImages();
            }
        }
    };

    private void updateImages() {
        int resId = mResources.getIdentifier("ic_sysbar_home", "drawable", "com.android.systemui");
        if (resId != 0) {
            mImage.setImageDrawable(mResources.getDrawable(resId));
        }
        resId = mResources.getIdentifier("icon", "drawable", "com.android.systemui");
        if (resId != 0) {
            mIcon.setImageDrawable(mResources.getDrawable(resId));
        }
    }

    class ThemeAdapter extends CursorAdapter {
        public ThemeAdapter(Context context, Cursor c, int flags) {
            super(context, c, flags);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View v = inflater.inflate(R.layout.theme_list_item, parent, false);
            return v;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            int titleIdx = cursor.getColumnIndex(ThemesColumns.TITLE);
            int pkgIdx = cursor.getColumnIndex(ThemesColumns.PKG_NAME);
            String title = cursor.getString(titleIdx);
            String pkgName = cursor.getString(pkgIdx);
            TextView tv = (TextView) view.findViewById(R.id.theme_title);
            tv.setText(title);
            view.setTag(pkgName);
        }
    }
}