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