Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 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 | |
Doris Ling | 4427cd7 | 2017-07-25 10:38:22 -0700 | [diff] [blame] | 16 | import android.content.ComponentName; |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 17 | import android.content.Context; |
Doris Ling | 4427cd7 | 2017-07-25 10:38:22 -0700 | [diff] [blame] | 18 | import android.content.Intent; |
| 19 | import android.content.pm.PackageManager; |
| 20 | import android.content.pm.ResolveInfo; |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 21 | import android.os.UserHandle; |
| 22 | import android.support.v7.preference.Preference; |
Doris Ling | 4427cd7 | 2017-07-25 10:38:22 -0700 | [diff] [blame] | 23 | import android.text.TextUtils; |
| 24 | import android.util.Log; |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 25 | |
Doris Ling | 4427cd7 | 2017-07-25 10:38:22 -0700 | [diff] [blame] | 26 | import com.android.settings.R; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 27 | import com.android.settings.core.PreferenceControllerMixin; |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 28 | import com.android.settingslib.RestrictedLockUtils; |
| 29 | import com.android.settingslib.RestrictedPreference; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 30 | import com.android.settingslib.core.AbstractPreferenceController; |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 31 | |
| 32 | import static android.os.UserManager.DISALLOW_SET_WALLPAPER; |
| 33 | |
Doris Ling | 4427cd7 | 2017-07-25 10:38:22 -0700 | [diff] [blame] | 34 | import java.util.List; |
| 35 | |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 36 | public class WallpaperPreferenceController extends AbstractPreferenceController implements |
| 37 | PreferenceControllerMixin { |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 38 | |
Doris Ling | 4427cd7 | 2017-07-25 10:38:22 -0700 | [diff] [blame] | 39 | private static final String TAG = "WallpaperPrefController"; |
| 40 | |
Andrew Sapperstein | ebaab6a | 2017-06-28 17:16:57 -0700 | [diff] [blame] | 41 | public static final String KEY_WALLPAPER = "wallpaper"; |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 42 | |
Doris Ling | 4427cd7 | 2017-07-25 10:38:22 -0700 | [diff] [blame] | 43 | private final String mWallpaperPackage; |
| 44 | private final String mWallpaperClass; |
| 45 | |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 46 | public WallpaperPreferenceController(Context context) { |
| 47 | super(context); |
Doris Ling | 4427cd7 | 2017-07-25 10:38:22 -0700 | [diff] [blame] | 48 | mWallpaperPackage = mContext.getString(R.string.config_wallpaper_picker_package); |
| 49 | mWallpaperClass = mContext.getString(R.string.config_wallpaper_picker_class); |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
Fan Zhang | 242da31 | 2016-10-25 16:38:22 -0700 | [diff] [blame] | 53 | public boolean isAvailable() { |
Doris Ling | 4427cd7 | 2017-07-25 10:38:22 -0700 | [diff] [blame] | 54 | 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 Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
Fan Zhang | db1112a | 2016-10-18 12:58:31 -0700 | [diff] [blame] | 69 | public String getPreferenceKey() { |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 70 | return KEY_WALLPAPER; |
| 71 | } |
| 72 | |
| 73 | @Override |
Fan Zhang | db1112a | 2016-10-18 12:58:31 -0700 | [diff] [blame] | 74 | public void updateState(Preference preference) { |
| 75 | disablePreferenceIfManaged((RestrictedPreference) preference); |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Fan Zhang | db1112a | 2016-10-18 12:58:31 -0700 | [diff] [blame] | 78 | private void disablePreferenceIfManaged(RestrictedPreference pref) { |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 79 | 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 | } |