Home | History | Annotate | Download | only in display
      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 android.content.ComponentName;
     17 import android.content.Context;
     18 import android.content.Intent;
     19 import android.content.pm.PackageManager;
     20 import android.content.pm.ResolveInfo;
     21 import android.os.UserHandle;
     22 import android.support.v7.preference.Preference;
     23 import android.text.TextUtils;
     24 import android.util.Log;
     25 
     26 import com.android.settings.R;
     27 import com.android.settings.core.PreferenceControllerMixin;
     28 import com.android.settingslib.RestrictedLockUtils;
     29 import com.android.settingslib.RestrictedPreference;
     30 import com.android.settingslib.core.AbstractPreferenceController;
     31 
     32 import static android.os.UserManager.DISALLOW_SET_WALLPAPER;
     33 
     34 import java.util.List;
     35 
     36 public class WallpaperPreferenceController extends AbstractPreferenceController implements
     37         PreferenceControllerMixin {
     38 
     39     private static final String TAG = "WallpaperPrefController";
     40 
     41     public static final String KEY_WALLPAPER = "wallpaper";
     42 
     43     private final String mWallpaperPackage;
     44     private final String mWallpaperClass;
     45 
     46     public WallpaperPreferenceController(Context context) {
     47         super(context);
     48         mWallpaperPackage = mContext.getString(R.string.config_wallpaper_picker_package);
     49         mWallpaperClass = mContext.getString(R.string.config_wallpaper_picker_class);
     50     }
     51 
     52     @Override
     53     public boolean isAvailable() {
     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;
     66     }
     67 
     68     @Override
     69     public String getPreferenceKey() {
     70         return KEY_WALLPAPER;
     71     }
     72 
     73     @Override
     74     public void updateState(Preference preference) {
     75         disablePreferenceIfManaged((RestrictedPreference) preference);
     76     }
     77 
     78     private void disablePreferenceIfManaged(RestrictedPreference pref) {
     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 }
     91