1 /* 2 * Copyright (C) 2017 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 package com.android.wallpaper.module; 17 18 import android.content.Context; 19 import android.content.Intent; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageManager; 22 import android.content.pm.PackageManager.NameNotFoundException; 23 import android.content.pm.ResolveInfo; 24 import android.content.res.Resources; 25 import android.util.Log; 26 import android.util.Pair; 27 28 import androidx.annotation.Nullable; 29 30 import dagger.hilt.android.qualifiers.ApplicationContext; 31 32 import java.io.File; 33 34 import javax.inject.Inject; 35 import javax.inject.Singleton; 36 37 /** 38 * Provides content from the partner customization apk on the device (if there is one). 39 */ 40 41 @Singleton 42 public class DefaultPartnerProvider implements PartnerProvider { 43 44 private final String mPackageName; 45 private final Resources mResources; 46 47 @Inject DefaultPartnerProvider(@pplicationContext Context ctx)48 public DefaultPartnerProvider(@ApplicationContext Context ctx) { 49 Pair<String, Resources> apkInfo = findSystemApk(ctx.getPackageManager()); 50 if (apkInfo != null) { 51 mPackageName = apkInfo.first; 52 mResources = apkInfo.second; 53 } else { 54 mPackageName = null; 55 mResources = null; 56 } 57 } 58 59 /** 60 * Finds the partner customization APK in the system directory. 61 * 62 * @param pm 63 * @return Pair of the package name and the Resources for the APK, or null if the APK isn't found 64 * on the device. 65 */ 66 @Nullable findSystemApk(PackageManager pm)67 protected Pair<String, Resources> findSystemApk(PackageManager pm) { 68 final Intent intent = new Intent(PartnerProvider.ACTION_PARTNER_CUSTOMIZATION); 69 for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) { 70 if (info.activityInfo != null 71 && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { 72 final String packageName = info.activityInfo.packageName; 73 try { 74 final Resources res = pm.getResourcesForApplication(packageName); 75 return Pair.create(packageName, res); 76 } catch (NameNotFoundException e) { 77 Log.w("DefaultPartnerProvider", "Failed to find resources for " + packageName); 78 } 79 } 80 } 81 return null; 82 } 83 84 @Override getResources()85 public Resources getResources() { 86 return mResources; 87 } 88 89 @Override getLegacyWallpaperDirectory()90 public File getLegacyWallpaperDirectory() { 91 int resId = 0; 92 Resources res = getResources(); 93 // Resources may be null if no partner customization APK has been placed on the system image, so 94 // check if null before calling Resources#getIdentifier. 95 if (res != null) { 96 resId = res.getIdentifier(PartnerProvider.RES_LEGACY_SYSTEM_WALLPAPER_DIR, 97 "string", mPackageName); 98 } 99 return (resId != 0) ? new File(res.getString(resId)) : null; 100 } 101 102 @Override getPackageName()103 public String getPackageName() { 104 return mPackageName; 105 } 106 107 @Override shouldHideDefaultWallpaper()108 public boolean shouldHideDefaultWallpaper() { 109 Resources res = getResources(); 110 // Resources may be null if no partner customization APK has been placed on the system image, so 111 // check if null before calling Resources#getIdentifier. 112 if (res != null) { 113 final int resId = res.getIdentifier( 114 RES_DEFAULT_WALLPAPER_HIDDEN, /* defType */ "bool", mPackageName); 115 return resId != 0 && res.getBoolean(resId); 116 } 117 return false; 118 } 119 } 120