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