• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             final Intent intent = new Intent().setComponent(getComponentName());
96             if (areStylesAvailable()) {
97                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
98             }
99             preference.getContext().startActivity(intent);
100             return true;
101         }
102         return super.handlePreferenceTreeClick(preference);
103     }
104 
105     /** Returns whether Styles & Wallpaper is enabled and available. */
areStylesAvailable()106     public boolean areStylesAvailable() {
107         return !TextUtils.isEmpty(mStylesAndWallpaperClass)
108                 && canResolveWallpaperComponent(mStylesAndWallpaperClass);
109     }
110 
canResolveWallpaperComponent(String className)111     private boolean canResolveWallpaperComponent(String className) {
112         final ComponentName componentName = new ComponentName(mWallpaperPackage, className);
113         final PackageManager pm = mContext.getPackageManager();
114         final Intent intent = new Intent().setComponent(componentName);
115         final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0 /* flags */);
116         return resolveInfos != null && !resolveInfos.isEmpty();
117     }
118 
disablePreferenceIfManaged(RestrictedPreference pref)119     private void disablePreferenceIfManaged(RestrictedPreference pref) {
120         final String restriction = DISALLOW_SET_WALLPAPER;
121         if (pref != null) {
122             pref.setDisabledByAdmin(null);
123             if (RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext,
124                     restriction, UserHandle.myUserId())) {
125                 pref.setEnabled(false);
126             } else {
127                 pref.checkRestrictionAndSetDisabled(restriction);
128             }
129         }
130     }
131 }
132