1 package com.android.customization.model;
2 
3 import android.content.Context;
4 import android.content.pm.ApplicationInfo;
5 import android.content.pm.PackageManager;
6 import android.content.pm.PackageManager.NameNotFoundException;
7 import android.content.res.Resources;
8 import android.graphics.drawable.Drawable;
9 import android.text.TextUtils;
10 import android.util.Log;
11 
12 import androidx.annotation.ColorInt;
13 
14 public abstract class ResourcesApkProvider {
15     private static final String TAG = "ResourcesApkProvider";
16 
17     protected final Context mContext;
18     protected final String mStubPackageName;
19     protected final Resources mStubApkResources;
20 
ResourcesApkProvider(Context context, String stubPackageName)21     public ResourcesApkProvider(Context context, String stubPackageName) {
22         mContext = context;
23         mStubPackageName = stubPackageName;
24         if (TextUtils.isEmpty(mStubPackageName)) {
25             mStubApkResources = null;
26         } else {
27             Resources apkResources = null;
28             try {
29                 PackageManager pm = mContext.getPackageManager();
30                 ApplicationInfo stubAppInfo = pm.getApplicationInfo(mStubPackageName,
31                         PackageManager.GET_META_DATA | PackageManager.MATCH_SYSTEM_ONLY);
32                 if (stubAppInfo != null) {
33                     apkResources = pm.getResourcesForApplication(stubAppInfo);
34                 }
35             } catch (NameNotFoundException e) {
36                 Log.w(TAG, String.format("Stub APK for %s not found.", mStubPackageName));
37             } finally {
38                 mStubApkResources = apkResources;
39             }
40         }
41     }
42 
getItemsFromStub(String arrayName)43     protected String[] getItemsFromStub(String arrayName) {
44         int themesListResId = mStubApkResources.getIdentifier(arrayName, "array",  mStubPackageName);
45         return mStubApkResources.getStringArray(themesListResId);
46     }
47 
getItemStringFromStub(String prefix, String itemName)48     protected String getItemStringFromStub(String prefix, String itemName) {
49         int resourceId = mStubApkResources.getIdentifier(String.format("%s%s", prefix, itemName),
50                 "string", mStubPackageName);
51         return mStubApkResources.getString(resourceId);
52     }
53 
getItemDrawableFromStub(String prefix, String itemName)54     protected Drawable getItemDrawableFromStub(String prefix, String itemName) {
55         int resourceId = mStubApkResources.getIdentifier(String.format("%s%s", prefix, itemName),
56                 "drawable", mStubPackageName);
57         return mStubApkResources.getDrawable(resourceId, null);
58     }
59 
60     @ColorInt
getItemColorFromStub(String prefix, String itemName)61     protected int getItemColorFromStub(String prefix, String itemName) {
62         int resourceId = mStubApkResources.getIdentifier(String.format("%s%s", prefix, itemName),
63                 "color", mStubPackageName);
64         return mStubApkResources.getColor(resourceId, null);
65     }
66 
isAvailable()67     public boolean isAvailable() {
68         return mStubApkResources != null;
69     }
70 }
71