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 import javax.inject.Inject;
39 
40 public class AutoAddTracker {
41 
42     private static final String[][] CONVERT_PREFS = {
43             {Key.QS_HOTSPOT_ADDED, HOTSPOT},
44             {Key.QS_DATA_SAVER_ADDED, SAVER},
45             {Key.QS_INVERT_COLORS_ADDED, INVERSION},
46             {Key.QS_WORK_ADDED, WORK},
47             {Key.QS_NIGHTDISPLAY_ADDED, NIGHT},
48     };
49 
50     private final ArraySet<String> mAutoAdded;
51     private final Context mContext;
52 
53     @Inject
AutoAddTracker(Context context)54     public AutoAddTracker(Context context) {
55         mContext = context;
56         mAutoAdded = new ArraySet<>(getAdded());
57         // TODO: remove migration code and shared preferences keys after P release
58         for (String[] convertPref : CONVERT_PREFS) {
59             if (Prefs.getBoolean(context, convertPref[0], false)) {
60                 setTileAdded(convertPref[1]);
61                 Prefs.remove(context, convertPref[0]);
62             }
63         }
64         mContext.getContentResolver().registerContentObserver(
65                 Secure.getUriFor(Secure.QS_AUTO_ADDED_TILES), false, mObserver);
66     }
67 
isAdded(String tile)68     public boolean isAdded(String tile) {
69         return mAutoAdded.contains(tile);
70     }
71 
setTileAdded(String tile)72     public void setTileAdded(String tile) {
73         if (mAutoAdded.add(tile)) {
74             saveTiles();
75         }
76     }
77 
setTileRemoved(String tile)78     public void setTileRemoved(String tile) {
79         if (mAutoAdded.remove(tile)) {
80             saveTiles();
81         }
82     }
83 
destroy()84     public void destroy() {
85         mContext.getContentResolver().unregisterContentObserver(mObserver);
86     }
87 
saveTiles()88     private void saveTiles() {
89         Secure.putString(mContext.getContentResolver(), Secure.QS_AUTO_ADDED_TILES,
90                 TextUtils.join(",", mAutoAdded));
91     }
92 
getAdded()93     private Collection<String> getAdded() {
94         String current = Secure.getString(mContext.getContentResolver(), Secure.QS_AUTO_ADDED_TILES);
95         if (current == null) {
96             return Collections.emptyList();
97         }
98         return Arrays.asList(current.split(","));
99     }
100 
101     @VisibleForTesting
102     protected final ContentObserver mObserver = new ContentObserver(new Handler()) {
103         @Override
104         public void onChange(boolean selfChange) {
105             mAutoAdded.addAll(getAdded());
106         }
107     };
108 }
109