blob: 9410b5a0194528b8fa8ccbcbdbeae33560ef4235 [file] [log] [blame]
Fan Zhang66b573a2016-10-06 16:33:13 -07001/*
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 */
14package com.android.settings.display;
15
Doris Ling4427cd72017-07-25 10:38:22 -070016import android.content.ComponentName;
Fan Zhang66b573a2016-10-06 16:33:13 -070017import android.content.Context;
Doris Ling4427cd72017-07-25 10:38:22 -070018import android.content.Intent;
19import android.content.pm.PackageManager;
20import android.content.pm.ResolveInfo;
Fan Zhang66b573a2016-10-06 16:33:13 -070021import android.os.UserHandle;
22import android.support.v7.preference.Preference;
Doris Ling4427cd72017-07-25 10:38:22 -070023import android.text.TextUtils;
24import android.util.Log;
Fan Zhang66b573a2016-10-06 16:33:13 -070025
Doris Ling4427cd72017-07-25 10:38:22 -070026import com.android.settings.R;
Tony Mantler1d583e12017-06-13 13:09:25 -070027import com.android.settings.core.PreferenceControllerMixin;
Fan Zhang66b573a2016-10-06 16:33:13 -070028import com.android.settingslib.RestrictedLockUtils;
29import com.android.settingslib.RestrictedPreference;
Tony Mantler1d583e12017-06-13 13:09:25 -070030import com.android.settingslib.core.AbstractPreferenceController;
Fan Zhang66b573a2016-10-06 16:33:13 -070031
32import static android.os.UserManager.DISALLOW_SET_WALLPAPER;
33
Doris Ling4427cd72017-07-25 10:38:22 -070034import java.util.List;
35
Tony Mantler1d583e12017-06-13 13:09:25 -070036public class WallpaperPreferenceController extends AbstractPreferenceController implements
37 PreferenceControllerMixin {
Fan Zhang66b573a2016-10-06 16:33:13 -070038
Doris Ling4427cd72017-07-25 10:38:22 -070039 private static final String TAG = "WallpaperPrefController";
40
Andrew Sappersteinebaab6a2017-06-28 17:16:57 -070041 public static final String KEY_WALLPAPER = "wallpaper";
Fan Zhang66b573a2016-10-06 16:33:13 -070042
Doris Ling4427cd72017-07-25 10:38:22 -070043 private final String mWallpaperPackage;
44 private final String mWallpaperClass;
45
Fan Zhang66b573a2016-10-06 16:33:13 -070046 public WallpaperPreferenceController(Context context) {
47 super(context);
Doris Ling4427cd72017-07-25 10:38:22 -070048 mWallpaperPackage = mContext.getString(R.string.config_wallpaper_picker_package);
49 mWallpaperClass = mContext.getString(R.string.config_wallpaper_picker_class);
Fan Zhang66b573a2016-10-06 16:33:13 -070050 }
51
52 @Override
Fan Zhang242da312016-10-25 16:38:22 -070053 public boolean isAvailable() {
Doris Ling4427cd72017-07-25 10:38:22 -070054 if (TextUtils.isEmpty(mWallpaperPackage) || TextUtils.isEmpty(mWallpaperClass)) {
55 Log.e(TAG, "No Wallpaper picker specified!");
56 return false;
57 }
58 final ComponentName componentName =
59 new ComponentName(mWallpaperPackage, mWallpaperClass);
60 final PackageManager pm = mContext.getPackageManager();
61 final Intent intent = new Intent();
62 intent.setComponent(componentName);
63 final List<ResolveInfo> resolveInfos =
64 pm.queryIntentActivities(intent, 0 /* flags */);
65 return resolveInfos != null && resolveInfos.size() != 0;
Fan Zhang66b573a2016-10-06 16:33:13 -070066 }
67
68 @Override
Fan Zhangdb1112a2016-10-18 12:58:31 -070069 public String getPreferenceKey() {
Fan Zhang66b573a2016-10-06 16:33:13 -070070 return KEY_WALLPAPER;
71 }
72
73 @Override
Fan Zhangdb1112a2016-10-18 12:58:31 -070074 public void updateState(Preference preference) {
75 disablePreferenceIfManaged((RestrictedPreference) preference);
Fan Zhang66b573a2016-10-06 16:33:13 -070076 }
77
Fan Zhangdb1112a2016-10-18 12:58:31 -070078 private void disablePreferenceIfManaged(RestrictedPreference pref) {
Fan Zhang66b573a2016-10-06 16:33:13 -070079 final String restriction = DISALLOW_SET_WALLPAPER;
80 if (pref != null) {
81 pref.setDisabledByAdmin(null);
82 if (RestrictedLockUtils.hasBaseUserRestriction(mContext,
83 restriction, UserHandle.myUserId())) {
84 pref.setEnabled(false);
85 } else {
86 pref.checkRestrictionAndSetDisabled(restriction);
87 }
88 }
89 }
90}