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