Sudheer Shanka | 130d79c | 2020-03-04 13:54:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.blob; |
| 18 | |
| 19 | import static com.android.server.blob.BlobStoreConfig.TAG; |
| 20 | |
| 21 | import android.annotation.IdRes; |
| 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
| 24 | import android.content.Context; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.res.Resources; |
| 27 | import android.util.Slog; |
| 28 | |
| 29 | class 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 Shanka | c6c7994 | 2020-03-12 13:20:46 -0700 | [diff] [blame^] | 50 | |
| 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 Shanka | 130d79c | 2020-03-04 13:54:36 -0800 | [diff] [blame] | 59 | } |