• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.accessibility;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 
23 import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
24 
25 import java.util.HashSet;
26 import java.util.Set;
27 
28 /** Static utility methods relating to {@link PreferredShortcut} */
29 public final class PreferredShortcuts {
30 
31     private static final String ACCESSIBILITY_PERF = "accessibility_prefs";
32     private static final String USER_SHORTCUT_TYPE = "user_shortcut_type";
33 
34     /**
35      * Retrieves {@link UserShortcutType} for the given {@code componentName} from
36      * SharedPreferences.
37      *
38      * @param context       {@link Context} to access the {@link SharedPreferences}
39      * @param componentName Name of the service or activity, should be the format of {@link
40      *                      ComponentName#flattenToString()}.
41      * @param defaultType   See {@link UserShortcutType}
42      * @return {@link UserShortcutType}
43      */
retrieveUserShortcutType(Context context, String componentName, int defaultType)44     public static int retrieveUserShortcutType(Context context, String componentName,
45             int defaultType) {
46         if (componentName == null) {
47             return defaultType;
48         }
49 
50         // Create a mutable set to modify
51         final Set<String> info = new HashSet<>(getFromSharedPreferences(context));
52         info.removeIf(str -> !str.contains(componentName));
53 
54         if (info.isEmpty()) {
55             return defaultType;
56         }
57 
58         final String str = info.stream().findFirst().get();
59         final PreferredShortcut shortcut = PreferredShortcut.fromString(str);
60         return shortcut.getType();
61     }
62 
63     /**
64      * Saves a {@link PreferredShortcut} which containing {@link ComponentName#flattenToString()}
65      * and {@link UserShortcutType} in SharedPreferences.
66      *
67      * @param context  {@link Context} to access the {@link SharedPreferences}
68      * @param shortcut Contains {@link ComponentName#flattenToString()} and {@link UserShortcutType}
69      */
saveUserShortcutType(Context context, PreferredShortcut shortcut)70     public static void saveUserShortcutType(Context context, PreferredShortcut shortcut) {
71         final String componentName = shortcut.getComponentName();
72         if (componentName == null) {
73             return;
74         }
75 
76         // Create a mutable set to modify
77         final Set<String> info = new HashSet<>(getFromSharedPreferences(context));
78         info.removeIf(str -> str.contains(componentName));
79         info.add(shortcut.toString());
80         saveToSharedPreferences(context, info);
81     }
82 
83     /**
84      * Returns a immutable set of {@link PreferredShortcut#toString()} list from
85      * SharedPreferences.
86      */
getFromSharedPreferences(Context context)87     private static Set<String> getFromSharedPreferences(Context context) {
88         return getSharedPreferences(context).getStringSet(USER_SHORTCUT_TYPE, Set.of());
89     }
90 
91     /** Sets a set of {@link PreferredShortcut#toString()} list into SharedPreferences. */
saveToSharedPreferences(Context context, Set<String> data)92     private static void saveToSharedPreferences(Context context, Set<String> data) {
93         SharedPreferences.Editor editor = getSharedPreferences(context).edit();
94         editor.putStringSet(USER_SHORTCUT_TYPE, data).apply();
95     }
96 
getSharedPreferences(Context context)97     private static SharedPreferences getSharedPreferences(Context context) {
98         return context.getSharedPreferences(ACCESSIBILITY_PERF, Context.MODE_PRIVATE);
99     }
100 
PreferredShortcuts()101     private PreferredShortcuts() {}
102 }
103