• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.accessibility;
18 
19 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
21 
22 import android.content.Context;
23 import android.provider.Settings;
24 
25 import androidx.lifecycle.Lifecycle;
26 import androidx.lifecycle.LifecycleObserver;
27 import androidx.lifecycle.OnLifecycleEvent;
28 import androidx.preference.PreferenceScreen;
29 import androidx.preference.SwitchPreference;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.TogglePreferenceController;
33 
34 /**
35  * Controller that accesses and switches the preference status of the magnification joystick feature
36  */
37 public class MagnificationJoystickPreferenceController extends TogglePreferenceController
38         implements LifecycleObserver {
39 
40     private static final String TAG =
41             MagnificationJoystickPreferenceController.class.getSimpleName();
42     static final String PREF_KEY = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_JOYSTICK_ENABLED;
43 
44     private SwitchPreference mSwitchPreference;
45 
MagnificationJoystickPreferenceController(Context context, String preferenceKey)46     public MagnificationJoystickPreferenceController(Context context, String preferenceKey) {
47         super(context, preferenceKey);
48     }
49 
50     @Override
getAvailabilityStatus()51     public int getAvailabilityStatus() {
52         return AVAILABLE;
53     }
54 
55     @Override
isChecked()56     public boolean isChecked() {
57         return Settings.Secure.getInt(mContext.getContentResolver(),
58                 Settings.Secure.ACCESSIBILITY_MAGNIFICATION_JOYSTICK_ENABLED, OFF) == ON;
59     }
60 
61     @Override
setChecked(boolean isChecked)62     public boolean setChecked(boolean isChecked) {
63         return Settings.Secure.putInt(mContext.getContentResolver(),
64                 Settings.Secure.ACCESSIBILITY_MAGNIFICATION_JOYSTICK_ENABLED,
65                 (isChecked ? ON : OFF));
66     }
67 
68     @Override
getSliceHighlightMenuRes()69     public int getSliceHighlightMenuRes() {
70         return R.string.menu_key_accessibility;
71     }
72 
73     @Override
displayPreference(PreferenceScreen screen)74     public void displayPreference(PreferenceScreen screen) {
75         super.displayPreference(screen);
76         mSwitchPreference = screen.findPreference(getPreferenceKey());
77     }
78 
79     // TODO(b/186731461): Remove it when this controller is used in DashBoardFragment only.
80     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
onResume()81     void onResume() {
82         updateState();
83     }
84 
85     /**
86      * Updates the state of preference components which has been displayed by
87      * {@link MagnificationJoystickPreferenceController#displayPreference}.
88      */
updateState()89     void updateState() {
90         updateState(mSwitchPreference);
91     }
92 }
93