1 /* 2 * Copyright (C) 2023 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.dream; 18 19 import android.content.Context; 20 import android.provider.Settings; 21 22 import androidx.preference.Preference; 23 24 import com.android.internal.annotations.VisibleForTesting; 25 import com.android.settings.R; 26 import com.android.settings.core.TogglePreferenceController; 27 import com.android.settingslib.dream.DreamBackend; 28 29 /** 30 * Controller for the {@link androidx.preference.SwitchPreference} which controls if dream 31 * overlays should be enabled. 32 */ 33 public class DreamHomeControlsPreferenceController extends TogglePreferenceController { 34 private final DreamBackend mBackend; 35 DreamHomeControlsPreferenceController(Context context, String key)36 public DreamHomeControlsPreferenceController(Context context, String key) { 37 this(context, key, DreamBackend.getInstance(context)); 38 } 39 40 @VisibleForTesting DreamHomeControlsPreferenceController(Context context, String key, DreamBackend dreamBackend)41 public DreamHomeControlsPreferenceController(Context context, String key, 42 DreamBackend dreamBackend) { 43 super(context, key); 44 mBackend = dreamBackend; 45 } 46 47 @Override getAvailabilityStatus()48 public int getAvailabilityStatus() { 49 final boolean supported = 50 mBackend.getSupportedComplications() 51 .contains(DreamBackend.COMPLICATION_TYPE_HOME_CONTROLS); 52 53 return controlsEnabledOnLockscreen() ? (supported ? AVAILABLE : CONDITIONALLY_UNAVAILABLE) 54 : DISABLED_DEPENDENT_SETTING; 55 } 56 57 @Override updateState(Preference preference)58 public void updateState(Preference preference) { 59 super.updateState(preference); 60 preference.setEnabled(getAvailabilityStatus() == AVAILABLE); 61 refreshSummary(preference); 62 } 63 64 @Override isChecked()65 public boolean isChecked() { 66 return controlsEnabledOnLockscreen() && mBackend.getEnabledComplications().contains( 67 DreamBackend.COMPLICATION_TYPE_HOME_CONTROLS); 68 } 69 70 @Override setChecked(boolean isChecked)71 public boolean setChecked(boolean isChecked) { 72 mBackend.setHomeControlsEnabled(isChecked); 73 return true; 74 } 75 controlsEnabledOnLockscreen()76 private boolean controlsEnabledOnLockscreen() { 77 return Settings.Secure.getInt( 78 mContext.getContentResolver(), 79 Settings.Secure.LOCKSCREEN_SHOW_CONTROLS, 0) != 0; 80 } 81 82 @Override getSliceHighlightMenuRes()83 public int getSliceHighlightMenuRes() { 84 return R.string.menu_key_display; 85 } 86 } 87