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