1 /* 2 * Copyright (C) 2020 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.internal.accessibility.util; 18 19 import static android.provider.Settings.Secure.ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU; 20 import static android.provider.Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_ALL; 21 import static android.provider.Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN; 22 import static android.provider.Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW; 23 import static android.view.accessibility.AccessibilityManager.ACCESSIBILITY_BUTTON; 24 import static android.view.accessibility.AccessibilityManager.ACCESSIBILITY_SHORTCUT_KEY; 25 26 import static com.android.internal.accessibility.AccessibilityShortcutController.MAGNIFICATION_COMPONENT_NAME; 27 import static com.android.internal.util.FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED__SERVICE_STATUS__DISABLED; 28 import static com.android.internal.util.FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED__SERVICE_STATUS__ENABLED; 29 import static com.android.internal.util.FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED__SERVICE_STATUS__UNKNOWN; 30 import static com.android.internal.util.FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__A11Y_BUTTON; 31 import static com.android.internal.util.FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__A11Y_BUTTON_LONG_PRESS; 32 import static com.android.internal.util.FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__A11Y_FLOATING_MENU; 33 import static com.android.internal.util.FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__TRIPLE_TAP; 34 import static com.android.internal.util.FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__UNKNOWN_TYPE; 35 import static com.android.internal.util.FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__VOLUME_KEY; 36 import static com.android.internal.util.FrameworkStatsLog.MAGNIFICATION_USAGE_REPORTED__ACTIVATED_MODE__MAGNIFICATION_ALL; 37 import static com.android.internal.util.FrameworkStatsLog.MAGNIFICATION_USAGE_REPORTED__ACTIVATED_MODE__MAGNIFICATION_FULL_SCREEN; 38 import static com.android.internal.util.FrameworkStatsLog.MAGNIFICATION_USAGE_REPORTED__ACTIVATED_MODE__MAGNIFICATION_UNKNOWN_MODE; 39 import static com.android.internal.util.FrameworkStatsLog.MAGNIFICATION_USAGE_REPORTED__ACTIVATED_MODE__MAGNIFICATION_WINDOW; 40 41 import android.content.ComponentName; 42 import android.content.Context; 43 import android.provider.Settings; 44 import android.view.accessibility.AccessibilityManager; 45 import android.view.accessibility.AccessibilityManager.ShortcutType; 46 47 import com.android.internal.util.FrameworkStatsLog; 48 49 /** Methods for logging accessibility states. */ 50 public final class AccessibilityStatsLogUtils { 51 private static final int UNKNOWN_STATUS = 52 ACCESSIBILITY_SHORTCUT_REPORTED__SERVICE_STATUS__UNKNOWN; 53 AccessibilityStatsLogUtils()54 private AccessibilityStatsLogUtils() {} 55 56 /** 57 * Logs accessibility feature name that is assigned to the given {@code shortcutType}. 58 * Calls this when clicking the shortcut {@link AccessibilityManager#ACCESSIBILITY_BUTTON} or 59 * {@link AccessibilityManager#ACCESSIBILITY_SHORTCUT_KEY}. 60 * 61 * @param context context used to retrieve the {@link Settings} provider 62 * @param componentName component name of the accessibility feature 63 * @param shortcutType accessibility shortcut type 64 */ logAccessibilityShortcutActivated(Context context, ComponentName componentName, @ShortcutType int shortcutType)65 public static void logAccessibilityShortcutActivated(Context context, 66 ComponentName componentName, @ShortcutType int shortcutType) { 67 logAccessibilityShortcutActivatedInternal(componentName, 68 convertToLoggingShortcutType(context, shortcutType), UNKNOWN_STATUS); 69 } 70 71 /** 72 * Logs accessibility feature name that is assigned to the given {@code shortcutType} and the 73 * {@code serviceEnabled} status. 74 * Calls this when clicking the shortcut {@link AccessibilityManager#ACCESSIBILITY_BUTTON} 75 * or {@link AccessibilityManager#ACCESSIBILITY_SHORTCUT_KEY}. 76 * 77 * @param context context used to retrieve the {@link Settings} provider 78 * @param componentName component name of the accessibility feature 79 * @param shortcutType accessibility shortcut type 80 * @param serviceEnabled {@code true} if the service is enabled 81 */ logAccessibilityShortcutActivated(Context context, ComponentName componentName, @ShortcutType int shortcutType, boolean serviceEnabled)82 public static void logAccessibilityShortcutActivated(Context context, 83 ComponentName componentName, @ShortcutType int shortcutType, boolean serviceEnabled) { 84 logAccessibilityShortcutActivatedInternal(componentName, 85 convertToLoggingShortcutType(context, shortcutType), 86 convertToLoggingServiceStatus(serviceEnabled)); 87 } 88 89 /** 90 * Logs accessibility feature name that is assigned to the given {@code loggingShortcutType} and 91 * {@code loggingServiceStatus} code. 92 * 93 * @param componentName component name of the accessibility feature 94 * @param loggingShortcutType accessibility shortcut type for logging. 0 denotes 95 * unknown_type, 1 denotes accessibility button, 2 denotes volume 96 * key, 3 denotes triple tap on the screen, 4 denotes long press on 97 * accessibility button, 5 denotes accessibility floating menu. 98 * @param loggingServiceStatus The service status code for logging. 0 denotes unknown_status, 1 99 * denotes enabled, 2 denotes disabled. 100 */ logAccessibilityShortcutActivatedInternal(ComponentName componentName, int loggingShortcutType, int loggingServiceStatus)101 private static void logAccessibilityShortcutActivatedInternal(ComponentName componentName, 102 int loggingShortcutType, int loggingServiceStatus) { 103 FrameworkStatsLog.write(FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED, 104 componentName.flattenToString(), loggingShortcutType, loggingServiceStatus); 105 } 106 107 /** 108 * Logs magnification that is assigned to the triple tap shortcut. Calls this when triggering 109 * the magnification triple tap shortcut. 110 */ logMagnificationTripleTap(boolean enabled)111 public static void logMagnificationTripleTap(boolean enabled) { 112 FrameworkStatsLog.write(FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED, 113 MAGNIFICATION_COMPONENT_NAME.flattenToString(), 114 ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__TRIPLE_TAP, 115 convertToLoggingServiceStatus(enabled)); 116 } 117 118 /** 119 * Logs accessibility feature name that is assigned to the long pressed accessibility button 120 * shortcut. Calls this when clicking the long pressed accessibility button shortcut. 121 * 122 * @param componentName The component name of the accessibility feature. 123 */ logAccessibilityButtonLongPressStatus(ComponentName componentName)124 public static void logAccessibilityButtonLongPressStatus(ComponentName componentName) { 125 FrameworkStatsLog.write(FrameworkStatsLog.ACCESSIBILITY_SHORTCUT_REPORTED, 126 componentName.flattenToString(), 127 ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__A11Y_BUTTON_LONG_PRESS, 128 UNKNOWN_STATUS); 129 } 130 131 /** 132 * Logs the magnification activated mode and its duration of the usage. 133 * Calls this when the magnification is disabled. 134 * 135 * @param mode The activated magnification mode. 136 * @param duration The duration in milliseconds during the magnification is activated. 137 */ logMagnificationUsageState(int mode, long duration)138 public static void logMagnificationUsageState(int mode, long duration) { 139 FrameworkStatsLog.write(FrameworkStatsLog.MAGNIFICATION_USAGE_REPORTED, 140 convertToLoggingMagnificationMode(mode), 141 duration); 142 } 143 144 /** 145 * Logs the activated mode of the magnification when the IME window is shown on the screen. 146 * Calls this when the magnification is enabled and the IME window is shown on the screen. 147 * 148 * @param mode The activated magnification mode. 149 */ logMagnificationModeWithImeOn(int mode)150 public static void logMagnificationModeWithImeOn(int mode) { 151 FrameworkStatsLog.write(FrameworkStatsLog.MAGNIFICATION_MODE_WITH_IME_ON_REPORTED, 152 convertToLoggingMagnificationMode(mode)); 153 } 154 isFloatingMenuEnabled(Context context)155 private static boolean isFloatingMenuEnabled(Context context) { 156 return Settings.Secure.getInt(context.getContentResolver(), 157 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, /* def= */ -1) 158 == ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU; 159 } 160 convertToLoggingShortcutType(Context context, @ShortcutType int shortcutType)161 private static int convertToLoggingShortcutType(Context context, 162 @ShortcutType int shortcutType) { 163 switch (shortcutType) { 164 case ACCESSIBILITY_BUTTON: 165 return isFloatingMenuEnabled(context) 166 ? ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__A11Y_FLOATING_MENU 167 : ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__A11Y_BUTTON; 168 case ACCESSIBILITY_SHORTCUT_KEY: 169 return ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__VOLUME_KEY; 170 } 171 return ACCESSIBILITY_SHORTCUT_REPORTED__SHORTCUT_TYPE__UNKNOWN_TYPE; 172 } 173 convertToLoggingServiceStatus(boolean enabled)174 private static int convertToLoggingServiceStatus(boolean enabled) { 175 return enabled ? ACCESSIBILITY_SHORTCUT_REPORTED__SERVICE_STATUS__ENABLED 176 : ACCESSIBILITY_SHORTCUT_REPORTED__SERVICE_STATUS__DISABLED; 177 } 178 convertToLoggingMagnificationMode(int mode)179 private static int convertToLoggingMagnificationMode(int mode) { 180 switch (mode) { 181 case ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN: 182 return MAGNIFICATION_USAGE_REPORTED__ACTIVATED_MODE__MAGNIFICATION_FULL_SCREEN; 183 case ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW: 184 return MAGNIFICATION_USAGE_REPORTED__ACTIVATED_MODE__MAGNIFICATION_WINDOW; 185 case ACCESSIBILITY_MAGNIFICATION_MODE_ALL: 186 return MAGNIFICATION_USAGE_REPORTED__ACTIVATED_MODE__MAGNIFICATION_ALL; 187 188 default: 189 return MAGNIFICATION_USAGE_REPORTED__ACTIVATED_MODE__MAGNIFICATION_UNKNOWN_MODE; 190 } 191 } 192 } 193