1 /* 2 * Copyright (C) 2024 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.deviceconfig; 18 19 import android.annotation.Nullable; 20 import android.content.Context; 21 import android.content.pm.PackageInfo; 22 import android.content.pm.PackageManager; 23 import android.util.Log; 24 25 import com.android.internal.annotations.VisibleForTesting; 26 27 import java.util.List; 28 import java.util.Optional; 29 30 /** 31 * Helper class for the service resources package. 32 * 33 * @hide 34 */ 35 public class ServiceResourcesHelper { 36 private static final String TAG = "ServiceResourcesHelper"; 37 38 @VisibleForTesting 39 static final String RESOURCES_PACKAGE_SUFFIX = 40 ".android.server.deviceconfig.resources"; 41 42 @Nullable private static ServiceResourcesHelper sHelper; // singleton. 43 44 /** Provides an instance of the helper. */ get(Context context)45 public static ServiceResourcesHelper get(Context context) { 46 if (sHelper == null) { 47 sHelper = new ServiceResourcesHelper(context); 48 } 49 return sHelper; 50 } 51 52 /** Sets the helper instance for testing purposes. */ 53 @VisibleForTesting setInstanceForTest(ServiceResourcesHelper helper)54 public static void setInstanceForTest(ServiceResourcesHelper helper) { 55 sHelper = helper; 56 } 57 58 private final Optional<String> mServiceResourcesPackageName; 59 60 @VisibleForTesting ServiceResourcesHelper(Context context)61 public ServiceResourcesHelper(Context context) { 62 mServiceResourcesPackageName = 63 getSystemPackageNameBySuffix(context, RESOURCES_PACKAGE_SUFFIX); 64 } 65 66 /** 67 * Returns an {@link Optional} containing the service resources package name, or an empty 68 * {@link Optional} if the package cannot be found. 69 */ getResourcesPackageName()70 public Optional<String> getResourcesPackageName() { 71 return mServiceResourcesPackageName; 72 } 73 getSystemPackageNameBySuffix(Context context, String suffix)74 private Optional<String> getSystemPackageNameBySuffix(Context context, String suffix) { 75 PackageManager packageManager = context.getPackageManager(); 76 List<PackageInfo> installedSystemPackages = 77 packageManager.getInstalledPackages(PackageManager.MATCH_SYSTEM_ONLY); 78 79 String serviceResourcesPackageName = null; 80 for (PackageInfo packageInfo : installedSystemPackages) { 81 final String packageName = packageInfo.packageName; 82 if (packageInfo.packageName.endsWith(RESOURCES_PACKAGE_SUFFIX)) { 83 Log.i(TAG, "Found service resource package: " + packageName); 84 if (serviceResourcesPackageName != null) { 85 Log.w(TAG, "Multiple service resource packages found"); 86 } else { 87 serviceResourcesPackageName = packageName; 88 } 89 } 90 } 91 92 return Optional.ofNullable(serviceResourcesPackageName); 93 } 94 }