blob: fabce766c237d5f4fbcee075d3ddc77d401fa5bb [file] [log] [blame]
Sudheer Shanka130d79c2020-03-04 13:54:36 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.blob;
18
19import static com.android.server.blob.BlobStoreConfig.TAG;
20
21import android.annotation.IdRes;
22import android.annotation.NonNull;
23import android.annotation.Nullable;
24import android.content.Context;
25import android.content.pm.PackageManager;
26import android.content.res.Resources;
27import android.util.Slog;
28
29class BlobStoreUtils {
30 private static final String DESC_RES_TYPE_STRING = "string";
31
32 @Nullable
33 static Resources getPackageResources(@NonNull Context context,
34 @NonNull String packageName, int userId) {
35 try {
36 return context.getPackageManager()
37 .getResourcesForApplicationAsUser(packageName, userId);
38 } catch (PackageManager.NameNotFoundException e) {
39 Slog.d(TAG, "Unknown package in user " + userId + ": "
40 + packageName, e);
41 return null;
42 }
43 }
44
45 @IdRes
46 static int getDescriptionResourceId(@NonNull Resources resources,
47 @NonNull String resourceEntryName, @NonNull String packageName) {
48 return resources.getIdentifier(resourceEntryName, DESC_RES_TYPE_STRING, packageName);
49 }
Sudheer Shankac6c79942020-03-12 13:20:46 -070050
51 @IdRes
52 static int getDescriptionResourceId(@NonNull Context context,
53 @NonNull String resourceEntryName, @NonNull String packageName, int userId) {
54 final Resources resources = getPackageResources(context, packageName, userId);
55 return resources == null
56 ? Resources.ID_NULL
57 : getDescriptionResourceId(resources, resourceEntryName, packageName);
58 }
Sudheer Shanka130d79c2020-03-04 13:54:36 -080059}