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             // TODO(b/130759285): Create a new string keywords_styles_and_wallpaper
73             sb.append(", ").append(mContext.getString(R.string.theme_customization_category))
74                     .append(", ").append(mContext.getString(R.string.keywords_dark_ui_mode));
75         }
76         return sb.toString();
77     }
78 
79     @Override
getAvailabilityStatus()80     public int getAvailabilityStatus() {
81         if (TextUtils.isEmpty(mWallpaperPackage) || TextUtils.isEmpty(mWallpaperClass)) {
82             Log.e(TAG, "No Wallpaper picker specified!");
83             return UNSUPPORTED_ON_DEVICE;
84         }
85         return canResolveWallpaperComponent(mWallpaperClass)
86                 ? AVAILABLE_UNSEARCHABLE : CONDITIONALLY_UNAVAILABLE;
87     }
88 
89     @Override
updateState(Preference preference)90     public void updateState(Preference preference) {
91         disablePreferenceIfManaged((RestrictedPreference) preference);
92     }
93 
94     @Override
handlePreferenceTreeClick(Preference preference)95     public boolean handlePreferenceTreeClick(Preference preference) {
96         if (getPreferenceKey().equals(preference.getKey())) {
97             preference.getContext().startActivity(new Intent().setComponent(getComponentName()));
98             return true;
99         }
100         return super.handlePreferenceTreeClick(preference);
101     }
102 
103     /** Returns whether Styles & Wallpaper is enabled and available. */
areStylesAvailable()104     public boolean areStylesAvailable() {
105         return !TextUtils.isEmpty(mStylesAndWallpaperClass)
106                 && canResolveWallpaperComponent(mStylesAndWallpaperClass);
107     }
108 
canResolveWallpaperComponent(String className)109     private boolean canResolveWallpaperComponent(String className) {
110         final ComponentName componentName = new ComponentName(mWallpaperPackage, className);
111         final PackageManager pm = mContext.getPackageManager();
112         final Intent intent = new Intent().setComponent(componentName);
113         final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0 /* flags */);
114         return resolveInfos != null && !resolveInfos.isEmpty();
115     }
116 
disablePreferenceIfManaged(RestrictedPreference pref)117     private void disablePreferenceIfManaged(RestrictedPreference pref) {
118         final String restriction = DISALLOW_SET_WALLPAPER;
119         if (pref != null) {
120             pref.setDisabledByAdmin(null);
121             if (RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext,
122                     restriction, UserHandle.myUserId())) {
123                 pref.setEnabled(false);
124             } else {
125                 pref.checkRestrictionAndSetDisabled(restriction);
126             }
127         }
128     }
129 }
130