1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.settings.display; 18 19 import static com.android.settings.display.AdaptiveSleepPreferenceController.hasSufficientPermission; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.net.Uri; 24 import android.text.TextUtils; 25 26 import androidx.preference.Preference; 27 28 import com.android.settings.core.BasePreferenceController; 29 30 public class AdaptiveSleepPermissionPreferenceController extends BasePreferenceController { 31 final static String PREF_NAME = "adaptive_sleep_permission"; 32 private final Intent mIntent; 33 AdaptiveSleepPermissionPreferenceController(Context context, String key)34 public AdaptiveSleepPermissionPreferenceController(Context context, String key) { 35 super(context, key); 36 final String packageName = context.getPackageManager().getAttentionServicePackageName(); 37 mIntent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 38 mIntent.setData(Uri.parse("package:" + packageName)); 39 } 40 41 @Override 42 @AvailabilityStatus getAvailabilityStatus()43 public int getAvailabilityStatus() { 44 return AVAILABLE_UNSEARCHABLE; 45 } 46 47 @Override handlePreferenceTreeClick(Preference preference)48 public boolean handlePreferenceTreeClick(Preference preference) { 49 if (TextUtils.equals(getPreferenceKey(), preference.getKey())) { 50 mContext.startActivity(mIntent); 51 return true; 52 } 53 return super.handlePreferenceTreeClick(preference); 54 } 55 56 @Override updateState(Preference preference)57 public void updateState(Preference preference) { 58 super.updateState(preference); 59 if (TextUtils.equals(getPreferenceKey(), preference.getKey())) { 60 preference.setVisible(!hasSufficientPermission(mContext.getPackageManager())); 61 } 62 } 63 } 64