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 private static final String LAUNCHED_SETTINGS = "app_launched_settings"; 40 41 private final String mWallpaperPackage; 42 private final String mWallpaperClass; 43 private final String mStylesAndWallpaperClass; 44 private final String mWallpaperActionName; 45 private final String mStylesAndWallpaperActionName; 46 private final String mWallpaperLaunchExtra; 47 WallpaperPreferenceController(Context context, String key)48 public WallpaperPreferenceController(Context context, String key) { 49 super(context, key); 50 mWallpaperPackage = mContext.getString(R.string.config_wallpaper_picker_package); 51 mWallpaperClass = mContext.getString(R.string.config_wallpaper_picker_class); 52 mStylesAndWallpaperClass = 53 mContext.getString(R.string.config_styles_and_wallpaper_picker_class); 54 mWallpaperActionName = mContext.getString(R.string.config_wallpaper_picker_action); 55 mStylesAndWallpaperActionName = 56 mContext.getString(R.string.config_styles_and_wallpaper_picker_action); 57 mWallpaperLaunchExtra = mContext.getString(R.string.config_wallpaper_picker_launch_extra); 58 } 59 60 @Override displayPreference(PreferenceScreen screen)61 public void displayPreference(PreferenceScreen screen) { 62 super.displayPreference(screen); 63 Preference preference = screen.findPreference(getPreferenceKey()); 64 preference.setTitle(getTitle()); 65 } 66 getTitle()67 public String getTitle() { 68 return mContext.getString(areStylesAvailable() 69 ? R.string.style_and_wallpaper_settings_title : R.string.wallpaper_settings_title); 70 } 71 getComponentName()72 public ComponentName getComponentName() { 73 return new ComponentName(mWallpaperPackage, getComponentClassString()); 74 } 75 getComponentClassString()76 public String getComponentClassString() { 77 return areStylesAvailable() ? mStylesAndWallpaperClass : mWallpaperClass; 78 } 79 getComponentActionName()80 public String getComponentActionName() { 81 return areStylesAvailable() ? mStylesAndWallpaperActionName : mWallpaperActionName; 82 } 83 getKeywords()84 public String getKeywords() { 85 StringBuilder sb = new StringBuilder(mContext.getString(R.string.keywords_wallpaper)); 86 if (areStylesAvailable()) { 87 sb.append(", ").append(mContext.getString(R.string.keywords_styles)); 88 } 89 return sb.toString(); 90 } 91 92 @Override getAvailabilityStatus()93 public int getAvailabilityStatus() { 94 if ((TextUtils.isEmpty(mWallpaperClass) && TextUtils.isEmpty(mStylesAndWallpaperClass)) 95 || TextUtils.isEmpty(mWallpaperPackage)) { 96 Log.e(TAG, "No Wallpaper picker specified!"); 97 return UNSUPPORTED_ON_DEVICE; 98 } 99 return canResolveWallpaperComponent(getComponentClassString()) 100 ? AVAILABLE_UNSEARCHABLE : CONDITIONALLY_UNAVAILABLE; 101 } 102 103 @Override updateState(Preference preference)104 public void updateState(Preference preference) { 105 disablePreferenceIfManaged((RestrictedPreference) preference); 106 } 107 108 @Override handlePreferenceTreeClick(Preference preference)109 public boolean handlePreferenceTreeClick(Preference preference) { 110 if (getPreferenceKey().equals(preference.getKey())) { 111 final Intent intent = new Intent().setComponent( 112 getComponentName()).putExtra(mWallpaperLaunchExtra, LAUNCHED_SETTINGS); 113 if (areStylesAvailable()) { 114 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 115 } 116 preference.getContext().startActivity(intent); 117 return true; 118 } 119 return super.handlePreferenceTreeClick(preference); 120 } 121 122 /** Returns whether Styles & Wallpaper is enabled and available. */ areStylesAvailable()123 public boolean areStylesAvailable() { 124 return !TextUtils.isEmpty(mStylesAndWallpaperClass) 125 && canResolveWallpaperComponent(mStylesAndWallpaperClass); 126 } 127 canResolveWallpaperComponent(String className)128 private boolean canResolveWallpaperComponent(String className) { 129 final ComponentName componentName = new ComponentName(mWallpaperPackage, className); 130 final PackageManager pm = mContext.getPackageManager(); 131 final Intent intent = new Intent().setComponent(componentName); 132 final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0 /* flags */); 133 return resolveInfos != null && !resolveInfos.isEmpty(); 134 } 135 disablePreferenceIfManaged(RestrictedPreference pref)136 private void disablePreferenceIfManaged(RestrictedPreference pref) { 137 final String restriction = DISALLOW_SET_WALLPAPER; 138 if (pref != null) { 139 pref.setDisabledByAdmin(null); 140 if (RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext, 141 restriction, UserHandle.myUserId())) { 142 pref.setEnabled(false); 143 } else { 144 pref.checkRestrictionAndSetDisabled(restriction); 145 } 146 } 147 } 148 } 149