summaryrefslogtreecommitdiff
path: root/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java
blob: bc56d18eeb941711392c320a392f4fd11b696611 (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
/*
 * Copyright (C) 2022 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.android.safetycenter.resources;

import static java.util.Objects.requireNonNull;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.util.Log;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;

import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * Wrapper for context to override getResources method. Resources for the Safety Center that need to
 * be fetched from the dedicated resources APK.
 */
public class SafetyCenterResourcesContext extends ContextWrapper {
    private static final String TAG = "SafetyCenterResContext";

    /** Intent action that is used to identify the Safety Center resources APK */
    private static final String RESOURCES_APK_ACTION =
            "com.android.safetycenter.intent.action.SAFETY_CENTER_RESOURCES_APK";

    /** Permission APEX name */
    private static final String APEX_MODULE_NAME = "com.android.permission";

    /**
     * The path where the Permission apex is mounted. Current value = "/apex/com.android.permission"
     */
    private static final String APEX_MODULE_PATH =
            new File("/apex", APEX_MODULE_NAME).getAbsolutePath();

    /** Raw XML config resource name */
    private static final String CONFIG_NAME = "safety_center_config";

    /** Intent action that is used to identify the Safety Center resources APK */
    private final String mResourcesApkAction;

    /** The path where the Safety Center resources APK is expected to be installed */
    @Nullable private final String mResourcesApkPath;

    /** Raw XML config resource name */
    private final String mConfigName;

    /** Specific flags used for retrieving resolve info */
    private final int mFlags;

    /**
     * Whether we should fallback with an empty string when calling {@link #getStringByName} for a
     * string resource that does not exist.
     */
    private final boolean mShouldFallbackIfNamedResourceNotFound;

    // Cached package name and resources from the resources APK
    @Nullable private String mResourcesApkPkgName;
    @Nullable private AssetManager mAssetsFromApk;
    @Nullable private Resources mResourcesFromApk;
    @Nullable private Resources.Theme mThemeFromApk;

    public SafetyCenterResourcesContext(Context contextBase) {
        this(contextBase, /* shouldFallbackIfNamedResourceNotFound */ true);
    }

    private SafetyCenterResourcesContext(
            Context contextBase, boolean shouldFallbackIfNamedResourceNotFound) {
        this(
                contextBase,
                RESOURCES_APK_ACTION,
                APEX_MODULE_PATH,
                CONFIG_NAME,
                PackageManager.MATCH_SYSTEM_ONLY,
                shouldFallbackIfNamedResourceNotFound);
    }

    @VisibleForTesting
    SafetyCenterResourcesContext(
            Context contextBase,
            String resourcesApkAction,
            @Nullable String resourcesApkPath,
            String configName,
            int flags,
            boolean shouldFallbackIfNamedResourceNotFound) {
        super(contextBase);
        mResourcesApkAction = requireNonNull(resourcesApkAction);
        mResourcesApkPath = resourcesApkPath;
        mConfigName = requireNonNull(configName);
        mFlags = flags;
        mShouldFallbackIfNamedResourceNotFound = shouldFallbackIfNamedResourceNotFound;
    }

    /** Creates a new {@link SafetyCenterResourcesContext} for testing. */
    @VisibleForTesting
    public static SafetyCenterResourcesContext forTests(Context contextBase) {
        return new SafetyCenterResourcesContext(
                contextBase, /* shouldFallbackIfNamedResourceNotFound */ false);
    }

    /** Get the package name of the Safety Center resources APK. */
    @VisibleForTesting
    @Nullable
    String getResourcesApkPkgName() {
        if (mResourcesApkPkgName != null) {
            return mResourcesApkPkgName;
        }

        List<ResolveInfo> resolveInfos =
                getPackageManager().queryIntentActivities(new Intent(mResourcesApkAction), mFlags);

        if (resolveInfos.size() > 1) {
            // multiple apps found, log a warning, but continue
            Log.w(TAG, "Found > 1 APK that can resolve Safety Center resources APK intent:");
            final int resolveInfosSize = resolveInfos.size();
            for (int i = 0; i < resolveInfosSize; i++) {
                ResolveInfo resolveInfo = resolveInfos.get(i);
                Log.w(
                        TAG,
                        String.format(
                                "- pkg:%s at:%s",
                                resolveInfo.activityInfo.applicationInfo.packageName,
                                resolveInfo.activityInfo.applicationInfo.sourceDir));
            }
        }

        ResolveInfo info = null;
        // Assume the first good ResolveInfo is the one we're looking for
        final int resolveInfosSize = resolveInfos.size();
        for (int i = 0; i < resolveInfosSize; i++) {
            ResolveInfo resolveInfo = resolveInfos.get(i);
            if (mResourcesApkPath != null
                    && !resolveInfo.activityInfo.applicationInfo.sourceDir.startsWith(
                            mResourcesApkPath)) {
                // skip apps that don't live in the Permission apex
                continue;
            }
            info = resolveInfo;
            break;
        }

        if (info == null) {
            // Resource APK not loaded yet, print a stack trace to see where this is called from
            Log.e(
                    TAG,
                    "Attempted to fetch resources before Safety Center resources APK is loaded!",
                    new IllegalStateException());
            return null;
        }

        mResourcesApkPkgName = info.activityInfo.applicationInfo.packageName;
        Log.i(TAG, "Found Safety Center resources APK at: " + mResourcesApkPkgName);
        return mResourcesApkPkgName;
    }

    /**
     * Gets the raw XML resource representing the Safety Center configuration from the Safety Center
     * resources APK.
     */
    @Nullable
    public InputStream getSafetyCenterConfig() {
        String resourcePkgName = getResourcesApkPkgName();
        if (resourcePkgName == null) {
            return null;
        }
        Resources resources = getResources();
        if (resources == null) {
            return null;
        }
        int id = resources.getIdentifier(mConfigName, "raw", resourcePkgName);
        if (id == Resources.ID_NULL) {
            return null;
        }
        return resources.openRawResource(id);
    }

    /**
     * Returns an optional {@link String} resource from the given {@code stringId}.
     *
     * <p>Returns {@code null} if {@code stringId} is equal to {@link Resources#ID_NULL}. Otherwise,
     * throws a {@link Resources.NotFoundException} if the resource cannot be accessed.
     */
    @Nullable
    public String getOptionalString(@StringRes int stringId) {
        if (stringId == Resources.ID_NULL) {
            return null;
        }
        return getString(stringId);
    }

    /** Same as {@link #getOptionalString(int)} but with the given {@code formatArgs}. */
    @Nullable
    public String getOptionalString(@StringRes int stringId, Object... formatArgs) {
        if (stringId == Resources.ID_NULL) {
            return null;
        }
        return getString(stringId, formatArgs);
    }

    /** Same as {@link #getOptionalString(int)} but using the string name rather than ID. */
    @Nullable
    public String getOptionalStringByName(String name) {
        return getOptionalString(getStringRes(name));
    }

    /**
     * Gets a string resource by name from the Safety Center resources APK, and returns an empty
     * string if the resource does not exist (or throws a {@link Resources.NotFoundException} if
     * {@link #mShouldFallbackIfNamedResourceNotFound} is {@code false}).
     */
    public String getStringByName(String name) {
        int id = getStringRes(name);
        return maybeFallbackIfNamedResourceIsNull(name, getOptionalString(id));
    }

    /** Same as {@link #getStringByName(String)} but with the given {@code formatArgs}. */
    public String getStringByName(String name, Object... formatArgs) {
        int id = getStringRes(name);
        return maybeFallbackIfNamedResourceIsNull(name, getOptionalString(id, formatArgs));
    }

    private String maybeFallbackIfNamedResourceIsNull(String name, @Nullable String value) {
        if (value != null) {
            return value;
        }
        if (!mShouldFallbackIfNamedResourceNotFound) {
            throw new Resources.NotFoundException();
        }
        Log.w(TAG, "String resource " + name + " not found");
        return "";
    }

    @StringRes
    private int getStringRes(String name) {
        return getResId(name, "string");
    }

    private int getResId(String name, String type) {
        String resourcePkgName = getResourcesApkPkgName();
        if (resourcePkgName == null) {
            return Resources.ID_NULL;
        }
        Resources resources = getResources();
        if (resources == null) {
            return Resources.ID_NULL;
        }
        // TODO(b/227738283): profile the performance of this operation and consider adding caching
        //  or finding some alternative solution.
        return resources.getIdentifier(name, type, resourcePkgName);
    }

    @Nullable
    private Context getResourcesApkContext() {
        String name = getResourcesApkPkgName();
        if (name == null) {
            return null;
        }
        try {
            return createPackageContext(name, 0);
        } catch (PackageManager.NameNotFoundException e) {
            Log.wtf(TAG, "Failed to load resources", e);
        }
        return null;
    }

    /** Retrieve assets held in the Safety Center resources APK. */
    @Override
    public AssetManager getAssets() {
        if (mAssetsFromApk == null) {
            Context resourcesApkContext = getResourcesApkContext();
            if (resourcesApkContext != null) {
                mAssetsFromApk = resourcesApkContext.getAssets();
            }
        }
        return mAssetsFromApk;
    }

    /** Retrieve resources held in the Safety Center resources APK. */
    @Override
    public Resources getResources() {
        if (mResourcesFromApk == null) {
            Context resourcesApkContext = getResourcesApkContext();
            if (resourcesApkContext != null) {
                mResourcesFromApk = resourcesApkContext.getResources();
            }
        }
        return mResourcesFromApk;
    }

    /** Retrieve theme held in the Safety Center resources APK. */
    @Override
    public Resources.Theme getTheme() {
        if (mThemeFromApk == null) {
            Context resourcesApkContext = getResourcesApkContext();
            if (resourcesApkContext != null) {
                mThemeFromApk = resourcesApkContext.getTheme();
            }
        }
        return mThemeFromApk;
    }

    /**
     * Gets a drawable resource by name from the Safety Center resources APK. Returns a null
     * drawable if the resource does not exist (or throws a {@link Resources.NotFoundException} if
     * {@link #mShouldFallbackIfNamedResourceNotFound} is {@code false}).
     *
     * @param name the identifier for this drawable resource
     * @param theme the theme used to style the drawable attributes, may be {@code null}
     */
    @Nullable
    public Drawable getDrawableByName(String name, @Nullable Resources.Theme theme) {
        int resId = getResId(name, "drawable");
        if (resId != Resources.ID_NULL) {
            return getResources().getDrawable(resId, theme);
        }

        if (!mShouldFallbackIfNamedResourceNotFound) {
            throw new Resources.NotFoundException();
        }

        Log.w(TAG, "Drawable resource " + name + " not found");
        return null;
    }

    /**
     * Returns an {@link Icon} instance containing a drawable with the given name. If no such
     * drawable exists, returns {@code null} or throws {@link Resources.NotFoundException}.
     */
    @Nullable
    public Icon getIconByDrawableName(String drawableResName) {
        int resId = getResId(drawableResName, "drawable");
        if (resId != Resources.ID_NULL) {
            return Icon.createWithResource(getResourcesApkPkgName(), resId);
        }

        if (!mShouldFallbackIfNamedResourceNotFound) {
            throw new Resources.NotFoundException();
        }

        Log.w(TAG, "Drawable resource " + drawableResName + " not found");
        return null;
    }

    /**
     * Gets a color by resource name
     */
    @ColorInt
    @Nullable
    public Integer getColorByName(String name) {
        int resId = getResId(name, "color");
        if (resId != Resources.ID_NULL) {
            return getResources().getColor(resId, getTheme());
        }

        if (!mShouldFallbackIfNamedResourceNotFound) {
            throw new Resources.NotFoundException();
        }

        Log.w(TAG, "Color resource " + name + " not found");
        return null;
    }
}