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.systemui.accessibility;
18 
19 import static android.provider.Settings.Secure.ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU;
20 import static android.provider.Settings.Secure.ACCESSIBILITY_BUTTON_MODE_GESTURE;
21 import static android.provider.Settings.Secure.ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR;
22 
23 import android.annotation.IntDef;
24 import android.annotation.MainThread;
25 import android.content.Context;
26 import android.provider.Settings;
27 import android.util.Log;
28 
29 import com.android.systemui.dagger.SysUISingleton;
30 
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33 
34 import javax.inject.Inject;
35 
36 /**
37  * Observes changes of the accessibility button mode
38  * {@link Settings.Secure#ACCESSIBILITY_BUTTON_MODE} and notify its listeners.
39  */
40 @MainThread
41 @SysUISingleton
42 public class AccessibilityButtonModeObserver extends
43         SecureSettingsContentObserver<AccessibilityButtonModeObserver.ModeChangedListener> {
44 
45     private static final String TAG = "A11yButtonModeObserver";
46 
47     private static final int ACCESSIBILITY_BUTTON_MODE_DEFAULT =
48             ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR;
49 
50     @Retention(RetentionPolicy.SOURCE)
51     @IntDef({
52             ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR,
53             ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU,
54             ACCESSIBILITY_BUTTON_MODE_GESTURE
55     })
56     public @interface AccessibilityButtonMode {}
57 
58     /** Listener for accessibility button mode changes. */
59     public interface ModeChangedListener {
60 
61         /**
62          * Called when accessibility button mode changes.
63          *
64          * @param mode Current accessibility button mode
65          */
onAccessibilityButtonModeChanged(@ccessibilityButtonMode int mode)66         void onAccessibilityButtonModeChanged(@AccessibilityButtonMode int mode);
67     }
68 
69     @Inject
AccessibilityButtonModeObserver(Context context)70     public AccessibilityButtonModeObserver(Context context) {
71         super(context, Settings.Secure.ACCESSIBILITY_BUTTON_MODE);
72     }
73 
74     @Override
onValueChanged(ModeChangedListener listener, String value)75     void onValueChanged(ModeChangedListener listener, String value) {
76         final int mode = parseAccessibilityButtonMode(value);
77         listener.onAccessibilityButtonModeChanged(mode);
78     }
79 
80     /**
81      * Gets the current accessibility button mode from the current user's settings.
82      *
83      * See {@link Settings.Secure#ACCESSIBILITY_BUTTON_MODE}.
84      */
getCurrentAccessibilityButtonMode()85     public int getCurrentAccessibilityButtonMode() {
86         final String value = getSettingsValue();
87 
88         return parseAccessibilityButtonMode(value);
89     }
90 
parseAccessibilityButtonMode(String value)91     private int parseAccessibilityButtonMode(String value) {
92         int mode;
93 
94         try {
95             mode = Integer.parseInt(value);
96         } catch (NumberFormatException e) {
97             Log.e(TAG, "Invalid string for  " + e);
98             mode = ACCESSIBILITY_BUTTON_MODE_DEFAULT;
99         }
100 
101         return mode;
102     }
103 }
104