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.PreferenceScreen; 23 24 import com.android.settings.widget.SettingsMainSwitchPreferenceController; 25 import com.android.settingslib.core.lifecycle.LifecycleObserver; 26 import com.android.settingslib.core.lifecycle.events.OnStart; 27 import com.android.settingslib.core.lifecycle.events.OnStop; 28 import com.android.settingslib.widget.MainSwitchPreference; 29 30 /** 31 * The controller to handle one-handed mode main switch enable or disable state. 32 **/ 33 public class OneHandedMainSwitchPreferenceController extends 34 SettingsMainSwitchPreferenceController implements OneHandedSettingsUtils.TogglesCallback, 35 LifecycleObserver, OnStart, OnStop { 36 37 private final OneHandedSettingsUtils mUtils; 38 39 private MainSwitchPreference mPreference; 40 OneHandedMainSwitchPreferenceController(Context context, String preferenceKey)41 public OneHandedMainSwitchPreferenceController(Context context, String preferenceKey) { 42 super(context, preferenceKey); 43 mUtils = new OneHandedSettingsUtils(context); 44 } 45 46 @Override getAvailabilityStatus()47 public int getAvailabilityStatus() { 48 return (OneHandedSettingsUtils.isSupportOneHandedMode() 49 && OneHandedSettingsUtils.getNavigationBarMode(mContext) != 0 /* 3-button mode */) 50 ? AVAILABLE : DISABLED_DEPENDENT_SETTING; 51 } 52 53 @Override isChecked()54 public boolean isChecked() { 55 return OneHandedSettingsUtils.isOneHandedModeEnabled(mContext); 56 } 57 58 @Override setChecked(boolean isChecked)59 public boolean setChecked(boolean isChecked) { 60 if (isChecked) { 61 // Set default value for TapsAppToExit and Timeout 62 OneHandedSettingsUtils.setTapsAppToExitEnabled(mContext, true); 63 OneHandedSettingsUtils.setTimeoutValue(mContext, 64 OneHandedSettingsUtils.OneHandedTimeout.MEDIUM.getValue()); 65 } 66 OneHandedSettingsUtils.setOneHandedModeEnabled(mContext, isChecked); 67 return true; 68 } 69 70 @Override displayPreference(PreferenceScreen screen)71 public void displayPreference(PreferenceScreen screen) { 72 super.displayPreference(screen); 73 mPreference = screen.findPreference(getPreferenceKey()); 74 } 75 76 @Override onStart()77 public void onStart() { 78 mUtils.registerToggleAwareObserver(this); 79 } 80 81 @Override onStop()82 public void onStop() { 83 mUtils.unregisterToggleAwareObserver(); 84 } 85 86 @Override onChange(Uri uri)87 public void onChange(Uri uri) { 88 if (mPreference == null) { 89 return; 90 } 91 if (uri.equals(OneHandedSettingsUtils.ONE_HANDED_MODE_ENABLED_URI)) { 92 mPreference.setChecked(isChecked()); 93 } 94 } 95 } 96