• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.qs;
16 
17 import static com.android.systemui.statusbar.phone.AutoTileManager.HOTSPOT;
18 import static com.android.systemui.statusbar.phone.AutoTileManager.INVERSION;
19 import static com.android.systemui.statusbar.phone.AutoTileManager.NIGHT;
20 import static com.android.systemui.statusbar.phone.AutoTileManager.SAVER;
21 import static com.android.systemui.statusbar.phone.AutoTileManager.WORK;
22 
23 import android.content.Context;
24 import android.database.ContentObserver;
25 import android.os.Handler;
26 import android.provider.Settings.Secure;
27 import android.text.TextUtils;
28 import android.util.ArraySet;
29 
30 import com.android.internal.annotations.VisibleForTesting;
31 import com.android.systemui.Prefs;
32 import com.android.systemui.Prefs.Key;
33 
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Collections;
37 
38 public class AutoAddTracker {
39 
40     private static final String[][] CONVERT_PREFS = {
41             {Key.QS_HOTSPOT_ADDED, HOTSPOT},
42             {Key.QS_DATA_SAVER_ADDED, SAVER},
43             {Key.QS_INVERT_COLORS_ADDED, INVERSION},
44             {Key.QS_WORK_ADDED, WORK},
45             {Key.QS_NIGHTDISPLAY_ADDED, NIGHT},
46     };
47 
48     private final ArraySet<String> mAutoAdded;
49     private final Context mContext;
50 
AutoAddTracker(Context context)51     public AutoAddTracker(Context context) {
52         mContext = context;
53         mAutoAdded = new ArraySet<>(getAdded());
54         for (String[] convertPref : CONVERT_PREFS) {
55             if (Prefs.getBoolean(context, convertPref[0], false)) {
56                 setTileAdded(convertPref[1]);
57                 Prefs.putBoolean(context, convertPref[0], false);
58             }
59         }
60         mContext.getContentResolver().registerContentObserver(
61                 Secure.getUriFor(Secure.QS_AUTO_ADDED_TILES), false, mObserver);
62     }
63 
isAdded(String tile)64     public boolean isAdded(String tile) {
65         return mAutoAdded.contains(tile);
66     }
67 
setTileAdded(String tile)68     public void setTileAdded(String tile) {
69         if (mAutoAdded.add(tile)) {
70             saveTiles();
71         }
72     }
73 
destroy()74     public void destroy() {
75         mContext.getContentResolver().unregisterContentObserver(mObserver);
76     }
77 
saveTiles()78     private void saveTiles() {
79         Secure.putString(mContext.getContentResolver(), Secure.QS_AUTO_ADDED_TILES,
80                 TextUtils.join(",", mAutoAdded));
81     }
82 
getAdded()83     private Collection<String> getAdded() {
84         String current = Secure.getString(mContext.getContentResolver(), Secure.QS_AUTO_ADDED_TILES);
85         if (current == null) {
86             return Collections.emptyList();
87         }
88         return Arrays.asList(current.split(","));
89     }
90 
91     @VisibleForTesting
92     protected final ContentObserver mObserver = new ContentObserver(new Handler()) {
93         @Override
94         public void onChange(boolean selfChange) {
95             mAutoAdded.addAll(getAdded());
96         }
97     };
98 }
99