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