1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.display;
15 
16 import static android.os.UserManager.DISALLOW_SET_WALLPAPER;
17 
18 import android.content.ComponentName;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ResolveInfo;
23 import android.os.UserHandle;
24 import android.text.TextUtils;
25 import android.util.Log;
26 
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.BasePreferenceController;
32 import com.android.settingslib.RestrictedLockUtilsInternal;
33 import com.android.settingslib.RestrictedPreference;
34 
35 import java.util.List;
36 
37 public class WallpaperPreferenceController extends BasePreferenceController {
38     private static final String TAG = "WallpaperPrefController";
39 
40     private final String mWallpaperPackage;
41     private final String mWallpaperClass;
42     private final String mStylesAndWallpaperClass;
43 
WallpaperPreferenceController(Context context, String key)44     public WallpaperPreferenceController(Context context, String key) {
45         super(context, key);
46         mWallpaperPackage = mContext.getString(R.string.config_wallpaper_picker_package);
47         mWallpaperClass = mContext.getString(R.string.config_wallpaper_picker_class);
48         mStylesAndWallpaperClass =
49                 mContext.getString(R.string.config_styles_and_wallpaper_picker_class);
50     }
51 
52     @Override
displayPreference(PreferenceScreen screen)53     public void displayPreference(PreferenceScreen screen) {
54         super.displayPreference(screen);
55         Preference preference = screen.findPreference(getPreferenceKey());
56         preference.setTitle(getTitle());
57     }
58 
getTitle()59     public String getTitle() {
60         return mContext.getString(areStylesAvailable()
61                 ? R.string.style_and_wallpaper_settings_title : R.string.wallpaper_settings_title);
62     }
63 
getComponentName()64     public ComponentName getComponentName() {
65         return new ComponentName(mWallpaperPackage,
66                 areStylesAvailable() ? mStylesAndWallpaperClass : mWallpaperClass);
67     }
68 
getKeywords()69     public String getKeywords() {
70         StringBuilder sb = new StringBuilder(mContext.getString(R.string.keywords_wallpaper));
71         if (areStylesAvailable()) {
72             sb.append(", ").append(mContext.getString(R.string.keywords_styles));
73         }
74         return sb.toString();
75     }
76 
77     @Override
getAvailabilityStatus()78     public int getAvailabilityStatus() {
79         if (TextUtils.isEmpty(mWallpaperPackage) || TextUtils.isEmpty(mWallpaperClass)) {
80             Log.e(TAG, "No Wallpaper picker specified!");
81             return UNSUPPORTED_ON_DEVICE;
82         }
83         return canResolveWallpaperComponent(mWallpaperClass)
84                 ? AVAILABLE_UNSEARCHABLE : CONDITIONALLY_UNAVAILABLE;
85     }
86 
87     @Override
updateState(Preference preference)88     public void updateState(Preference preference) {
89         disablePreferenceIfManaged((RestrictedPreference) preference);
90     }
91 
92     @Override
handlePreferenceTreeClick(Preference preference)93     public boolean handlePreferenceTreeClick(Preference preference) {
94         if (getPreferenceKey().equals(preference.getKey())) {
95             preference.getContext().startActivity(new Intent().setComponent(getComponentName()));
96             return true;
97         }
98         return super.handlePreferenceTreeClick(preference);
99     }
100 
101     /** Returns whether Styles & Wallpaper is enabled and available. */
areStylesAvailable()102     public boolean areStylesAvailable() {
103         return !TextUtils.isEmpty(mStylesAndWallpaperClass)
104                 && canResolveWallpaperComponent(mStylesAndWallpaperClass);
105     }
106 
canResolveWallpaperComponent(String className)107     private boolean canResolveWallpaperComponent(String className) {
108         final ComponentName componentName = new ComponentName(mWallpaperPackage, className);
109         final PackageManager pm = mContext.getPackageManager();
110         final Intent intent = new Intent().setComponent(componentName);
111         final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0 /* flags */);
112         return resolveInfos != null && !resolveInfos.isEmpty();
113     }
114 
disablePreferenceIfManaged(RestrictedPreference pref)115     private void disablePreferenceIfManaged(RestrictedPreference pref) {
116         final String restriction = DISALLOW_SET_WALLPAPER;
117         if (pref != null) {
118             pref.setDisabledByAdmin(null);
119             if (RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext,
120                     restriction, UserHandle.myUserId())) {
121                 pref.setEnabled(false);
122             } else {
123                 pref.checkRestrictionAndSetDisabled(restriction);
124             }
125         }
126     }
127 }
128