1 /* 2 * Copyright (C) 2022 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.settings.accessibility; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.SharedPreferences; 22 import android.text.TextUtils; 23 24 import androidx.annotation.NonNull; 25 26 import java.util.StringJoiner; 27 28 /** Provides utility methods to accessibility quick settings only. */ 29 final class AccessibilityQuickSettingUtils { 30 31 private static final String ACCESSIBILITY_PERF = "accessibility_prefs"; 32 private static final String KEY_TILE_SERVICE_SHOWN = "tile_service_shown"; 33 private static final char COMPONENT_NAME_SEPARATOR = ':'; 34 private static final TextUtils.SimpleStringSplitter sStringColonSplitter = 35 new TextUtils.SimpleStringSplitter(COMPONENT_NAME_SEPARATOR); 36 37 /** 38 * Opts in component name into {@link AccessibilityQuickSettingUtils#KEY_TILE_SERVICE_SHOWN} 39 * colon-separated string in {@link SharedPreferences}. 40 * 41 * @param context The current context. 42 * @param componentName The component name that need to be opted in SharedPreferences. 43 */ optInValueToSharedPreferences(Context context, @NonNull ComponentName componentName)44 public static void optInValueToSharedPreferences(Context context, 45 @NonNull ComponentName componentName) { 46 final String targetString = getFromSharedPreferences(context); 47 if (hasValueInSharedPreferences(targetString, componentName)) { 48 return; 49 } 50 51 final StringJoiner joiner = new StringJoiner(String.valueOf(COMPONENT_NAME_SEPARATOR)); 52 if (!TextUtils.isEmpty(targetString)) { 53 joiner.add(targetString); 54 } 55 joiner.add(componentName.flattenToString()); 56 57 SharedPreferences.Editor editor = getSharedPreferences(context).edit(); 58 editor.putString(KEY_TILE_SERVICE_SHOWN, joiner.toString()).apply(); 59 } 60 61 /** 62 * Returns if component name existed in {@link 63 * AccessibilityQuickSettingUtils#KEY_TILE_SERVICE_SHOWN} string in {@link SharedPreferences}. 64 * 65 * @param context The current context. 66 * @param componentName The component name that need to be checked existed in SharedPreferences. 67 * @return {@code true} if componentName existed in SharedPreferences. 68 */ hasValueInSharedPreferences(Context context, @NonNull ComponentName componentName)69 public static boolean hasValueInSharedPreferences(Context context, 70 @NonNull ComponentName componentName) { 71 final String targetString = getFromSharedPreferences(context); 72 return hasValueInSharedPreferences(targetString, componentName); 73 } 74 hasValueInSharedPreferences(String targetString, @NonNull ComponentName componentName)75 private static boolean hasValueInSharedPreferences(String targetString, 76 @NonNull ComponentName componentName) { 77 if (TextUtils.isEmpty(targetString)) { 78 return false; 79 } 80 81 sStringColonSplitter.setString(targetString); 82 83 while (sStringColonSplitter.hasNext()) { 84 final String name = sStringColonSplitter.next(); 85 if (TextUtils.equals(componentName.flattenToString(), name)) { 86 return true; 87 } 88 } 89 return false; 90 } 91 getFromSharedPreferences(Context context)92 private static String getFromSharedPreferences(Context context) { 93 return getSharedPreferences(context).getString(KEY_TILE_SERVICE_SHOWN, ""); 94 } 95 getSharedPreferences(Context context)96 private static SharedPreferences getSharedPreferences(Context context) { 97 return context.getSharedPreferences(ACCESSIBILITY_PERF, Context.MODE_PRIVATE); 98 } 99 AccessibilityQuickSettingUtils()100 private AccessibilityQuickSettingUtils(){} 101 } 102