summaryrefslogtreecommitdiffstats
path: root/core/java/android/content/pm/ThemeUtils.java
blob: e41523c9bc1ad960e26c564f55a3399ec417ea7c (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*
 * 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 android.content.pm;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.IntentFilter;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.ThemeConfig;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.FileUtils;
import android.os.SystemProperties;
import android.provider.MediaStore;
import android.provider.Settings;
import android.provider.ThemesContract;
import android.provider.ThemesContract.ThemesColumns;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.jar.StrictJarFile;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import static android.content.res.ThemeConfig.SYSTEM_DEFAULT;

/**
 * @hide
 */
public class ThemeUtils {
    private static final String TAG = "ThemeUtils";

    /* Path inside a theme APK to the overlay folder */
    public static final String OVERLAY_PATH = "assets/overlays/";
    public static final String ICONS_PATH = "assets/icons/";
    public static final String COMMON_RES_PATH = "assets/overlays/common/";
    public static final String FONT_XML = "fonts.xml";
    public static final String RESOURCE_CACHE_DIR = "/data/resource-cache/";
    public static final String IDMAP_SUFFIX = "@idmap";
    public static final String COMMON_RES_SUFFIX = ".common";
    public static final String COMMON_RES_TARGET = "common";
    public static final String ICON_HASH_FILENAME = "hash";

    // path to external theme resources, i.e. bootanimation.zip
    public static final String SYSTEM_THEME_PATH = "/data/system/theme";
    public static final String SYSTEM_THEME_FONT_PATH = SYSTEM_THEME_PATH + File.separator + "fonts";
    public static final String SYSTEM_THEME_RINGTONE_PATH = SYSTEM_THEME_PATH
            + File.separator + "ringtones";
    public static final String SYSTEM_THEME_NOTIFICATION_PATH = SYSTEM_THEME_PATH
            + File.separator + "notifications";
    public static final String SYSTEM_THEME_ALARM_PATH = SYSTEM_THEME_PATH
            + File.separator + "alarms";
    public static final String SYSTEM_THEME_ICON_CACHE_DIR = SYSTEM_THEME_PATH
            + File.separator + "icons";
    // internal path to bootanimation.zip inside theme apk
    public static final String THEME_BOOTANIMATION_PATH = "assets/bootanimation/bootanimation.zip";

    public static final String SYSTEM_MEDIA_PATH = "/system/media/audio";
    public static final String SYSTEM_ALARMS_PATH = SYSTEM_MEDIA_PATH + File.separator
            + "alarms";
    public static final String SYSTEM_RINGTONES_PATH = SYSTEM_MEDIA_PATH + File.separator
            + "ringtones";
    public static final String SYSTEM_NOTIFICATIONS_PATH = SYSTEM_MEDIA_PATH + File.separator
            + "notifications";

    // path to asset lockscreen and wallpapers directory
    public static final String LOCKSCREEN_WALLPAPER_PATH = "lockscreen";
    public static final String WALLPAPER_PATH = "wallpapers";

    private static final String MEDIA_CONTENT_URI = "content://media/internal/audio/media";

    // Constants for theme change broadcast
    public static final String ACTION_THEME_CHANGED = "org.cyanogenmod.intent.action.THEME_CHANGED";
    public static final String CATEGORY_THEME_COMPONENT_PREFIX = "org.cyanogenmod.intent.category.";
    public static final String EXTRA_COMPONENTS = "components";
    public static final String EXTRA_REQUEST_TYPE = "request_type";
    public static final String EXTRA_UPDATE_TIME = "update_time";

    public static final int SYSTEM_TARGET_API = 0;

    // Package name for any app which does not have a specific theme applied
    private static final String DEFAULT_PKG = "default";

    private static final String SETTINGS_DB =
            "/data/data/com.android.providers.settings/databases/settings.db";
    private static final String SETTINGS_SECURE_TABLE = "secure";
    private static final String MANIFEST_NAME = "META-INF/MANIFEST.MF";

    /**
     * IDMAP hash version code used to alter the resulting hash and force recreating
     * of the idmap.  This value should be changed whenever there is a need to force
     * an update to all idmaps.
     */
    private static final byte IDMAP_HASH_VERSION = 3;

    // Actions in manifests which identify legacy icon packs
    public static final String[] sSupportedActions = new String[] {
            "org.adw.launcher.THEMES",
            "com.gau.go.launcherex.theme",
            "com.novalauncher.THEME"
    };

    // Categories in manifests which identify legacy icon packs
    public static final String[] sSupportedCategories = new String[] {
            "com.fede.launcher.THEME_ICONPACK",
            "com.anddoes.launcher.THEME",
            "com.teslacoilsw.launcher.THEME"
    };


    /**
     * Get the root path of the resource cache for the given theme
     * @param themePkgName
     * @return Root resource cache path for the given theme
     */
    public static String getOverlayResourceCacheDir(String themePkgName) {
        return RESOURCE_CACHE_DIR + themePkgName;
    }

    /**
     * Get the path of the resource cache for the given target and theme
     * @param targetPkgName
     * @param themePkg
     * @return Path to the resource cache for this target and theme
     */
    public static String getTargetCacheDir(String targetPkgName, PackageInfo themePkg) {
        return getTargetCacheDir(targetPkgName, themePkg.packageName);
    }

    public static String getTargetCacheDir(String targetPkgName, PackageParser.Package themePkg) {
        return getTargetCacheDir(targetPkgName, themePkg.packageName);
    }

    public static String getTargetCacheDir(String targetPkgName, String themePkgName) {
        return getOverlayResourceCacheDir(themePkgName) + File.separator + targetPkgName;
    }

    /**
     * Get the path to the icons for the given theme
     * @param pkgName
     * @return
     */
    public static String getIconPackDir(String pkgName) {
      return getOverlayResourceCacheDir(pkgName) + File.separator + "icons";
    }

    public static String getIconHashFile(String pkgName) {
        return getIconPackDir(pkgName) + File.separator  +  ICON_HASH_FILENAME;
    }

    public static String getIconPackApkPath(String pkgName) {
        return getIconPackDir(pkgName) + "/resources.apk";
    }

    public static String getIconPackResPath(String pkgName) {
        return getIconPackDir(pkgName) + "/resources.arsc";
    }

    public static String getIdmapPath(String targetPkgName, String overlayPkgName) {
        return getTargetCacheDir(targetPkgName, overlayPkgName) + File.separator + "idmap";
    }

    public static String getOverlayPathToTarget(String targetPkgName) {
        StringBuilder sb = new StringBuilder();
        sb.append(OVERLAY_PATH);
        sb.append(targetPkgName);
        sb.append('/');
        return sb.toString();
    }

    public static String getCommonPackageName(String themePackageName) {
        if (TextUtils.isEmpty(themePackageName)) return null;

        return COMMON_RES_TARGET;
    }

    public static void createCacheDirIfNotExists() throws IOException {
        File file = new File(RESOURCE_CACHE_DIR);
        if (!file.exists() && !file.mkdir()) {
            throw new IOException("Could not create dir: " + file.toString());
        }
        FileUtils.setPermissions(file, FileUtils.S_IRWXU
                | FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH, -1, -1);
    }

    public static void createResourcesDirIfNotExists(String targetPkgName, String overlayPkgName)
            throws IOException {
        createDirIfNotExists(getOverlayResourceCacheDir(overlayPkgName));
        File file = new File(getTargetCacheDir(targetPkgName, overlayPkgName));
        if (!file.exists() && !file.mkdir()) {
            throw new IOException("Could not create dir: " + file.toString());
        }
        FileUtils.setPermissions(file, FileUtils.S_IRWXU
                | FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH, -1, -1);
    }

    public static void createIconDirIfNotExists(String pkgName) throws IOException {
        createDirIfNotExists(getOverlayResourceCacheDir(pkgName));
        File file = new File(getIconPackDir(pkgName));
        if (!file.exists() && !file.mkdir()) {
            throw new IOException("Could not create dir: " + file.toString());
        }
        FileUtils.setPermissions(file, FileUtils.S_IRWXU
                | FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH, -1, -1);
    }

    private static boolean dirExists(String dirPath) {
        final File dir = new File(dirPath);
        return dir.exists() && dir.isDirectory();
    }

    private static void createDirIfNotExists(String dirPath) {
        if (!dirExists(dirPath)) {
            File dir = new File(dirPath);
            if (dir.mkdir()) {
                FileUtils.setPermissions(dir, FileUtils.S_IRWXU |
                        FileUtils.S_IRWXG| FileUtils.S_IROTH | FileUtils.S_IXOTH, -1, -1);
            }
        }
    }

    /**
     * Create SYSTEM_THEME_PATH directory if it does not exist
     */
    public static void createThemeDirIfNotExists() {
        createDirIfNotExists(SYSTEM_THEME_PATH);
    }

    /**
     * Create SYSTEM_FONT_PATH directory if it does not exist
     */
    public static void createFontDirIfNotExists() {
        createDirIfNotExists(SYSTEM_THEME_FONT_PATH);
    }

    /**
     * Create SYSTEM_THEME_RINGTONE_PATH directory if it does not exist
     */
    public static void createRingtoneDirIfNotExists() {
        createDirIfNotExists(SYSTEM_THEME_RINGTONE_PATH);
    }

    /**
     * Create SYSTEM_THEME_NOTIFICATION_PATH directory if it does not exist
     */
    public static void createNotificationDirIfNotExists() {
        createDirIfNotExists(SYSTEM_THEME_NOTIFICATION_PATH);
    }

    /**
     * Create SYSTEM_THEME_ALARM_PATH directory if it does not exist
     */
    public static void createAlarmDirIfNotExists() {
        createDirIfNotExists(SYSTEM_THEME_ALARM_PATH);
    }

    /**
     * Create SYSTEM_THEME_ICON_CACHE_DIR directory if it does not exist
     */
    public static void createIconCacheDirIfNotExists() {
        createDirIfNotExists(SYSTEM_THEME_ICON_CACHE_DIR);
    }

    public static void clearIconCache() {
        FileUtils.deleteContents(new File(SYSTEM_THEME_ICON_CACHE_DIR));
    }

    public static InputStream getInputStreamFromAsset(Context ctx, String path) throws IOException {
        if (ctx == null || path == null)
            return null;
        InputStream is = null;
        String ASSET_BASE = "file:///android_asset/";
        path = path.substring(ASSET_BASE.length());
        AssetManager assets = ctx.getAssets();
        is = assets.open(path);
        return is;
    }

    public static void closeQuietly(InputStream stream) {
        if (stream == null)
            return;
        try {
            stream.close();
        } catch (IOException e) {
        }
    }

    public static void closeQuietly(OutputStream stream) {
        if (stream == null)
            return;
        try {
            stream.close();
        } catch (IOException e) {
        }
    }

    /**
     * Scale the boot animation to better fit the device by editing the desc.txt found
     * in the bootanimation.zip
     * @param context Context to use for getting an instance of the WindowManager
     * @param input InputStream of the original bootanimation.zip
     * @param dst Path to store the newly created bootanimation.zip
     * @throws IOException
     */
    public static void copyAndScaleBootAnimation(Context context, InputStream input, String dst)
            throws IOException {
        final OutputStream os = new FileOutputStream(dst);
        final ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
        final ZipInputStream bootAni = new ZipInputStream(new BufferedInputStream(input));
        ZipEntry ze;

        zos.setMethod(ZipOutputStream.STORED);
        final byte[] bytes = new byte[4096];
        int len;
        while ((ze = bootAni.getNextEntry()) != null) {
            ZipEntry entry = new ZipEntry(ze.getName());
            entry.setMethod(ZipEntry.STORED);
            entry.setCrc(ze.getCrc());
            entry.setSize(ze.getSize());
            entry.setCompressedSize(ze.getSize());
            if (!ze.getName().equals("desc.txt")) {
                // just copy this entry straight over into the output zip
                zos.putNextEntry(entry);
                while ((len = bootAni.read(bytes)) > 0) {
                    zos.write(bytes, 0, len);
                }
            } else {
                String line;
                BufferedReader reader = new BufferedReader(new InputStreamReader(bootAni));
                final String[] info = reader.readLine().split(" ");

                int scaledWidth;
                int scaledHeight;
                WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
                DisplayMetrics dm = new DisplayMetrics();
                wm.getDefaultDisplay().getRealMetrics(dm);
                // just in case the device is in landscape orientation we will
                // swap the values since most (if not all) animations are portrait
                if (dm.widthPixels > dm.heightPixels) {
                    scaledWidth = dm.heightPixels;
                    scaledHeight = dm.widthPixels;
                } else {
                    scaledWidth = dm.widthPixels;
                    scaledHeight = dm.heightPixels;
                }

                int width = Integer.parseInt(info[0]);
                int height = Integer.parseInt(info[1]);

                if (width == height)
                    scaledHeight = scaledWidth;
                else {
                    // adjust scaledHeight to retain original aspect ratio
                    float scale = (float)scaledWidth / (float)width;
                    int newHeight = (int)((float)height * scale);
                    if (newHeight < scaledHeight)
                        scaledHeight = newHeight;
                }

                CRC32 crc32 = new CRC32();
                int size = 0;
                ByteBuffer buffer = ByteBuffer.wrap(bytes);
                line = String.format("%d %d %s\n", scaledWidth, scaledHeight, info[2]);
                buffer.put(line.getBytes());
                size += line.getBytes().length;
                crc32.update(line.getBytes());
                while ((line = reader.readLine()) != null) {
                    line = String.format("%s\n", line);
                    buffer.put(line.getBytes());
                    size += line.getBytes().length;
                    crc32.update(line.getBytes());
                }
                entry.setCrc(crc32.getValue());
                entry.setSize(size);
                entry.setCompressedSize(size);
                zos.putNextEntry(entry);
                zos.write(buffer.array(), 0, size);
            }
            zos.closeEntry();
        }
        zos.close();
    }

    public static boolean isValidAudible(String fileName) {
        return (fileName != null &&
                (fileName.endsWith(".mp3") || fileName.endsWith(".ogg")));
    }

    public static boolean setAudible(Context context, File ringtone, int type, String name) {
        final String path = ringtone.getAbsolutePath();
        final String mimeType = name.endsWith(".ogg") ? "audio/ogg" : "audio/mp3";
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, path);
        values.put(MediaStore.MediaColumns.TITLE, name);
        values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
        values.put(MediaStore.MediaColumns.SIZE, ringtone.length());
        values.put(MediaStore.Audio.Media.IS_RINGTONE, type == RingtoneManager.TYPE_RINGTONE);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION,
                type == RingtoneManager.TYPE_NOTIFICATION);
        values.put(MediaStore.Audio.Media.IS_ALARM, type == RingtoneManager.TYPE_ALARM);
        values.put(MediaStore.Audio.Media.IS_MUSIC, false);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(path);
        Uri newUri = null;
        Cursor c = context.getContentResolver().query(uri,
                new String[] {MediaStore.MediaColumns._ID},
                MediaStore.MediaColumns.DATA + "='" + path + "'",
                null, null);
        if (c != null && c.getCount() > 0) {
            c.moveToFirst();
            long id = c.getLong(0);
            c.close();
            newUri = Uri.withAppendedPath(Uri.parse(MEDIA_CONTENT_URI), "" + id);
            context.getContentResolver().update(uri, values,
                    MediaStore.MediaColumns._ID + "=" + id, null);
        }
        if (newUri == null)
            newUri = context.getContentResolver().insert(uri, values);
        try {
            RingtoneManager.setActualDefaultRingtoneUri(context, type, newUri);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static boolean setDefaultAudible(Context context, int type) {
        final String audiblePath = getDefaultAudiblePath(type);
        if (audiblePath != null) {
            Uri uri = MediaStore.Audio.Media.getContentUriForPath(audiblePath);
            Cursor c = context.getContentResolver().query(uri,
                    new String[] {MediaStore.MediaColumns._ID},
                    MediaStore.MediaColumns.DATA + "='" + audiblePath + "'",
                    null, null);
            if (c != null && c.getCount() > 0) {
                c.moveToFirst();
                long id = c.getLong(0);
                c.close();
                uri = Uri.withAppendedPath(
                        Uri.parse(MEDIA_CONTENT_URI), "" + id);
            }
            if (uri != null)
                RingtoneManager.setActualDefaultRingtoneUri(context, type, uri);
        } else {
            return false;
        }
        return true;
    }

    public static String getDefaultAudiblePath(int type) {
        final String name;
        final String path;
        switch (type) {
            case RingtoneManager.TYPE_ALARM:
                name = SystemProperties.get("ro.config.alarm_alert", null);
                path = name != null ? SYSTEM_ALARMS_PATH + File.separator + name : null;
                break;
            case RingtoneManager.TYPE_NOTIFICATION:
                name = SystemProperties.get("ro.config.notification_sound", null);
                path = name != null ? SYSTEM_NOTIFICATIONS_PATH + File.separator + name : null;
                break;
            case RingtoneManager.TYPE_RINGTONE:
                name = SystemProperties.get("ro.config.ringtone", null);
                path = name != null ? SYSTEM_RINGTONES_PATH + File.separator + name : null;
                break;
            default:
                path = null;
                break;
        }
        return path;
    }

    public static void clearAudibles(Context context, String audiblePath) {
        final File audibleDir = new File(audiblePath);
        if (audibleDir.exists()) {
            String[] files = audibleDir.list();
            final ContentResolver resolver = context.getContentResolver();
            for (String s : files) {
                final String filePath = audiblePath + File.separator + s;
                Uri uri = MediaStore.Audio.Media.getContentUriForPath(filePath);
                resolver.delete(uri, MediaStore.MediaColumns.DATA + "=\""
                        + filePath + "\"", null);
                (new File(filePath)).delete();
            }
        }
    }

    public static Context createUiContext(final Context context) {
        try {
            Context uiContext = context.createPackageContext("com.android.systemui",
                    Context.CONTEXT_RESTRICTED);
            return new ThemedUiContext(uiContext, context.getApplicationContext());
        } catch (PackageManager.NameNotFoundException e) {
        }

        return null;
    }

    public static void registerThemeChangeReceiver(final Context context,
            final BroadcastReceiver receiver) {
        IntentFilter filter = new IntentFilter(ACTION_THEME_CHANGED);

        context.registerReceiver(receiver, filter);
    }

    public static String getLockscreenWallpaperPath(AssetManager assetManager) throws IOException {
        String[] assets = assetManager.list(LOCKSCREEN_WALLPAPER_PATH);
        String asset = getFirstNonEmptyAsset(assets);
        if (asset == null) return null;
        return LOCKSCREEN_WALLPAPER_PATH + File.separator + asset;
    }

    public static String getWallpaperPath(AssetManager assetManager) throws IOException {
        String[] assets = assetManager.list(WALLPAPER_PATH);
        String asset = getFirstNonEmptyAsset(assets);
        if (asset == null) return null;
        return WALLPAPER_PATH + File.separator + asset;
    }

    public static List<String> getWallpaperPathList(AssetManager assetManager)
            throws IOException {
        List<String> wallpaperList = new ArrayList<String>();
        String[] assets = assetManager.list(WALLPAPER_PATH);
        for (String asset : assets) {
            if (!TextUtils.isEmpty(asset)) {
                wallpaperList.add(WALLPAPER_PATH + File.separator + asset);
            }
        }
        return wallpaperList;
    }

    // Returns the first non-empty asset name. Empty assets can occur if the APK is built
    // with folders included as zip entries in the APK. Searching for files inside "folderName" via
    // assetManager.list("folderName") can cause these entries to be included as empty strings.
    private static String getFirstNonEmptyAsset(String[] assets) {
        if (assets == null) return null;
        String filename = null;
        for(String asset : assets) {
            if (!TextUtils.isEmpty(asset)) {
                filename = asset;
                break;
            }
        }
        return filename;
    }

    public static String getDefaultThemePackageName(Context context) {
        final String defaultThemePkg = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.DEFAULT_THEME_PACKAGE);
        if (!TextUtils.isEmpty(defaultThemePkg)) {
            PackageManager pm = context.getPackageManager();
            try {
                if (pm.getPackageInfo(defaultThemePkg, 0) != null) {
                    return defaultThemePkg;
                }
            } catch (PackageManager.NameNotFoundException e) {
                // doesn't exist so system will be default
                Log.w(TAG, "Default theme " + defaultThemePkg + " not found", e);
            }
        }

        return SYSTEM_DEFAULT;
    }

    private static class ThemedUiContext extends ContextWrapper {
        private Context mAppContext;

        public ThemedUiContext(Context context, Context appContext) {
            super(context);
            mAppContext = appContext;
        }

        @Override
        public Context getApplicationContext() {
            return mAppContext;
        }

        @Override
        public String getPackageName() {
            return mAppContext.getPackageName();
        }
    }

    // Returns a mutable list of all theme components
    public static List<String> getAllComponents() {
        List<String> components = new ArrayList<String>(9);
        components.add(ThemesColumns.MODIFIES_FONTS);
        components.add(ThemesColumns.MODIFIES_LAUNCHER);
        components.add(ThemesColumns.MODIFIES_ALARMS);
        components.add(ThemesColumns.MODIFIES_BOOT_ANIM);
        components.add(ThemesColumns.MODIFIES_ICONS);
        components.add(ThemesColumns.MODIFIES_LOCKSCREEN);
        components.add(ThemesColumns.MODIFIES_NOTIFICATIONS);
        components.add(ThemesColumns.MODIFIES_OVERLAYS);
        components.add(ThemesColumns.MODIFIES_RINGTONES);
        components.add(ThemesColumns.MODIFIES_STATUS_BAR);
        components.add(ThemesColumns.MODIFIES_NAVIGATION_BAR);
        components.add(ThemesColumns.MODIFIES_LIVE_LOCK_SCREEN);
        return components;
    }

    /**
     *  Returns a mutable list of all the theme components supported by a given package
     *  NOTE: This queries the themes content provider. If there isn't a provider installed
     *  or if it is too early in the boot process this method will not work.
     */
    public static List<String> getSupportedComponents(Context context, String pkgName) {
        List<String> supportedComponents = new ArrayList<String>();

        String selection = ThemesContract.ThemesColumns.PKG_NAME + "= ?";
        String[] selectionArgs = new String[]{ pkgName };
        Cursor c = context.getContentResolver().query(ThemesContract.ThemesColumns.CONTENT_URI,
                null, selection, selectionArgs, null);

        if (c != null) {
            if (c.moveToFirst()) {
                List<String> allComponents = getAllComponents();
                for (String component : allComponents) {
                    int index = c.getColumnIndex(component);
                    if (c.getInt(index) == 1) {
                        supportedComponents.add(component);
                    }
                }
            }
            c.close();
        }
        return supportedComponents;
    }

    /**
     * Get the components from the default theme.  If the default theme is not SYSTEM then any
     * components that are not in the default theme will come from SYSTEM to create a complete
     * component map.
     * @param context
     * @return
     */
    public static Map<String, String> getDefaultComponents(Context context) {
        String defaultThemePkg = getDefaultThemePackageName(context);
        List<String> defaultComponents = null;
        List<String> systemComponents = getSupportedComponents(context, SYSTEM_DEFAULT);
        if (!SYSTEM_DEFAULT.equals(defaultThemePkg)) {
            defaultComponents = getSupportedComponents(context, defaultThemePkg);
        }

        Map<String, String> componentMap = new HashMap<String, String>(systemComponents.size());
        if (defaultComponents != null) {
            for (String component : defaultComponents) {
                componentMap.put(component, defaultThemePkg);
            }
        }
        for (String component : systemComponents) {
            if (!componentMap.containsKey(component)) {
                componentMap.put(component, SYSTEM_DEFAULT);
            }
        }

        return componentMap;
    }

    /**
     * Takes an existing component map and adds any missing components from the default
     * map of components.
     * @param context
     * @param componentMap An existing component map
     */
    public static void completeComponentMap(Context context,
            Map<String, String> componentMap) {
        if (componentMap == null) return;

        Map<String, String> defaultComponents = getDefaultComponents(context);
        for (String component : defaultComponents.keySet()) {
            if (!componentMap.containsKey(component)) {
                componentMap.put(component, defaultComponents.get(component));
            }
        }
    }

    /**
     * Convenience method to determine if a theme component is a per app theme and not a standard
     * component.
     * @param component
     * @return
     */
    public static boolean isPerAppThemeComponent(String component) {
        return !(DEFAULT_PKG.equals(component)
                || ThemeConfig.SYSTEMUI_STATUS_BAR_PKG.equals(component)
                || ThemeConfig.SYSTEMUI_NAVBAR_PKG.equals(component));
    }

    /**
     * Get a 32 bit hashcode for the given package.
     * @param pkg
     * @return
     */
    public static int getPackageHashCode(PackageParser.Package pkg, StrictJarFile jarFile) {
        int hash = pkg.manifestDigest != null ? pkg.manifestDigest.hashCode() : 0;
        final ZipEntry je = jarFile.findEntry(MANIFEST_NAME);
        if (je != null) {
            try {
                try {
                    ManifestDigest digest = ManifestDigest.fromInputStream(
                        jarFile.getInputStream(je));
                    if (digest != null) {
                        hash += digest.hashCode();
                    }
                } finally {
                    jarFile.close();
                }
            } catch (IOException | RuntimeException e) {
                // Failed to generate digest from manifest.mf
            }
        }
        hash = 31 * hash + IDMAP_HASH_VERSION;
        return hash;
    }
}