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.os.UserHandle; 28 import android.text.format.TimeMigrationUtils; 29 import android.util.Slog; 30 31 class BlobStoreUtils { 32 private static final String DESC_RES_TYPE_STRING = "string"; 33 34 @Nullable getPackageResources(@onNull Context context, @NonNull String packageName, int userId)35 static Resources getPackageResources(@NonNull Context context, 36 @NonNull String packageName, int userId) { 37 try { 38 return context.createContextAsUser(UserHandle.of(userId), /* flags */ 0) 39 .getPackageManager().getResourcesForApplication(packageName); 40 } catch (PackageManager.NameNotFoundException e) { 41 Slog.d(TAG, "Unknown package in user " + userId + ": " 42 + packageName, e); 43 return null; 44 } 45 } 46 47 @IdRes getDescriptionResourceId(@onNull Resources resources, @NonNull String resourceEntryName, @NonNull String packageName)48 static int getDescriptionResourceId(@NonNull Resources resources, 49 @NonNull String resourceEntryName, @NonNull String packageName) { 50 return resources.getIdentifier(resourceEntryName, DESC_RES_TYPE_STRING, packageName); 51 } 52 53 @IdRes getDescriptionResourceId(@onNull Context context, @NonNull String resourceEntryName, @NonNull String packageName, int userId)54 static int getDescriptionResourceId(@NonNull Context context, 55 @NonNull String resourceEntryName, @NonNull String packageName, int userId) { 56 final Resources resources = getPackageResources(context, packageName, userId); 57 return resources == null 58 ? Resources.ID_NULL 59 : getDescriptionResourceId(resources, resourceEntryName, packageName); 60 } 61 62 @NonNull formatTime(long timeMs)63 static String formatTime(long timeMs) { 64 return TimeMigrationUtils.formatMillisWithFixedFormat(timeMs); 65 } 66 } 67