1 /* 2 * Copyright (C) 2021 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.gestures; 18 19 import android.content.Context; 20 import android.net.Uri; 21 22 import androidx.preference.Preference; 23 import androidx.preference.PreferenceScreen; 24 25 import com.android.settings.core.BasePreferenceController; 26 import com.android.settingslib.core.lifecycle.LifecycleObserver; 27 import com.android.settingslib.core.lifecycle.events.OnStart; 28 import com.android.settingslib.core.lifecycle.events.OnStop; 29 import com.android.settingslib.widget.RadioButtonPreference; 30 31 /** 32 * The controller to handle one-handed mode pull screen into reach preference. 33 **/ 34 public class OneHandedActionPullDownPrefController extends BasePreferenceController 35 implements OneHandedSettingsUtils.TogglesCallback, LifecycleObserver, OnStart, OnStop { 36 37 private final OneHandedSettingsUtils mUtils; 38 39 private Preference mPreference; 40 OneHandedActionPullDownPrefController(Context context, String key)41 public OneHandedActionPullDownPrefController(Context context, String key) { 42 super(context, key); 43 mUtils = new OneHandedSettingsUtils(context); 44 } 45 46 @Override updateState(Preference preference)47 public void updateState(Preference preference) { 48 super.updateState(preference); 49 if (preference instanceof RadioButtonPreference) { 50 ((RadioButtonPreference) preference).setChecked( 51 !OneHandedSettingsUtils.isSwipeDownNotificationEnabled(mContext)); 52 } 53 } 54 55 @Override getAvailabilityStatus()56 public int getAvailabilityStatus() { 57 return (OneHandedSettingsUtils.isSupportOneHandedMode() 58 && OneHandedSettingsUtils.canEnableController(mContext)) 59 ? AVAILABLE : DISABLED_DEPENDENT_SETTING; 60 } 61 62 @Override handlePreferenceTreeClick(Preference preference)63 public boolean handlePreferenceTreeClick(Preference preference) { 64 if (!getPreferenceKey().equals(preference.getKey())) { 65 return false; 66 } 67 OneHandedSettingsUtils.setSwipeDownNotificationEnabled(mContext, false); 68 if (preference instanceof RadioButtonPreference) { 69 ((RadioButtonPreference) preference).setChecked(true); 70 } 71 return true; 72 } 73 74 @Override displayPreference(PreferenceScreen screen)75 public void displayPreference(PreferenceScreen screen) { 76 super.displayPreference(screen); 77 mPreference = screen.findPreference(getPreferenceKey()); 78 } 79 80 @Override onStart()81 public void onStart() { 82 mUtils.registerToggleAwareObserver(this); 83 } 84 85 @Override onStop()86 public void onStop() { 87 mUtils.unregisterToggleAwareObserver(); 88 } 89 90 @Override onChange(Uri uri)91 public void onChange(Uri uri) { 92 if (mPreference == null) { 93 return; 94 } 95 if (uri.equals(OneHandedSettingsUtils.ONE_HANDED_MODE_ENABLED_URI) 96 || uri.equals(OneHandedSettingsUtils.SHORTCUT_ENABLED_URI)) { 97 mPreference.setEnabled(OneHandedSettingsUtils.canEnableController(mContext)); 98 } else if (uri.equals(OneHandedSettingsUtils.SHOW_NOTIFICATION_ENABLED_URI)) { 99 updateState(mPreference); 100 } 101 } 102 } 103