1 /* 2 * Copyright (C) 2022 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.tv.settings.customization; 18 19 import static androidx.core.content.res.ResourcesCompat.ID_NULL; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 import android.content.res.Resources; 26 import android.graphics.drawable.Drawable; 27 import android.util.Log; 28 29 import androidx.annotation.Nullable; 30 31 import java.util.List; 32 33 /** 34 * Singleton class that is aware finds the settings customization apk available in the 35 * system image. Exposes methods to get resources from this package. 36 */ 37 public final class Partner { 38 private static final String TAG = "Partner"; 39 40 private static final String CUSTOMIZATION_ACTION = 41 "com.tv.settings.action.PARTNER_CUSTOMIZATION"; 42 43 private Resources mResources; 44 private String mPackage; 45 private static Partner sPartner = null; 46 Partner(Context context)47 private Partner(Context context) { 48 try { 49 final PackageManager packageManager = context.getPackageManager(); 50 final Intent intent = new Intent(CUSTOMIZATION_ACTION); 51 final List<ResolveInfo> resolveInfoList = packageManager 52 .queryBroadcastReceivers(intent, 0); 53 if (resolveInfoList == null || resolveInfoList.isEmpty()) { 54 Log.i(TAG, "Partner customization apk not found"); 55 } else { 56 mPackage = resolveInfoList.get(0).activityInfo.packageName; 57 Log.i(TAG, "Found partner customization apk: " + mPackage); 58 mResources = context.getPackageManager().getResourcesForApplication( 59 mPackage); 60 } 61 } catch (PackageManager.NameNotFoundException nameNotFoundException) { 62 Log.e(TAG, "Error in getting resources of partner customization apk"); 63 } 64 } 65 getInstance(Context context)66 public static Partner getInstance(Context context) { 67 if (sPartner == null) { 68 sPartner = new Partner(context); 69 } 70 return sPartner; 71 } 72 isCustomizationPackageProvided()73 public boolean isCustomizationPackageProvided() { 74 return mPackage != null && mResources != null; 75 } 76 77 @Nullable getInteger(String name)78 public Integer getInteger(String name) { 79 final Integer id = getIdentifier(name, "integer"); 80 if (id == null) { 81 Log.i(TAG, "Unable to find resource id of integer: " + name); 82 return null; 83 } 84 return mResources.getInteger(id); 85 } 86 87 @Nullable getIdentifier(String name, String defType)88 private Integer getIdentifier(String name, String defType) { 89 if (mResources == null) { 90 Log.i(TAG, "Partner customization resource reference is null"); 91 return null; 92 } 93 Integer id = mResources.getIdentifier(name, defType, mPackage); 94 if (id == null || id == ID_NULL) { 95 return null; 96 } 97 return id; 98 } 99 100 @Nullable getString(String name)101 public String getString(String name) { 102 final Integer id = getIdentifier(name, "string"); 103 if (id == null) { 104 Log.i(TAG, "Unable to find resource id of string: " + name); 105 return null; 106 } 107 return mResources.getString(id); 108 } 109 110 @Nullable getDrawable(String name, Resources.Theme theme)111 public Drawable getDrawable(String name, Resources.Theme theme) { 112 final Integer id = getIdentifier(name, "drawable"); 113 if (id == null) { 114 Log.i(TAG, "Unable to find resource id of drawable: " + name); 115 return null; 116 } 117 return mResources.getDrawable(id, theme); 118 } 119 120 @Nullable getArray(String name)121 public String[] getArray(String name) { 122 final Integer id = getIdentifier(name, "array"); 123 if (id == null) { 124 Log.i(TAG, "Unable to find resource id of array: " + name); 125 return null; 126 } 127 return mResources.getStringArray(id); 128 } 129 } 130