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