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