Home | History | Annotate | Download | only in qs
      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 
     51     public AutoAddTracker(Context context) {
     52         mContext = context;
     53         mAutoAdded = new ArraySet<>(getAdded());
     54         // TODO: remove migration code and shared preferences keys after P release
     55         for (String[] convertPref : CONVERT_PREFS) {
     56             if (Prefs.getBoolean(context, convertPref[0], false)) {
     57                 setTileAdded(convertPref[1]);
     58                 Prefs.remove(context, convertPref[0]);
     59             }
     60         }
     61         mContext.getContentResolver().registerContentObserver(
     62                 Secure.getUriFor(Secure.QS_AUTO_ADDED_TILES), false, mObserver);
     63     }
     64 
     65     public boolean isAdded(String tile) {
     66         return mAutoAdded.contains(tile);
     67     }
     68 
     69     public void setTileAdded(String tile) {
     70         if (mAutoAdded.add(tile)) {
     71             saveTiles();
     72         }
     73     }
     74 
     75     public void destroy() {
     76         mContext.getContentResolver().unregisterContentObserver(mObserver);
     77     }
     78 
     79     private void saveTiles() {
     80         Secure.putString(mContext.getContentResolver(), Secure.QS_AUTO_ADDED_TILES,
     81                 TextUtils.join(",", mAutoAdded));
     82     }
     83 
     84     private Collection<String> getAdded() {
     85         String current = Secure.getString(mContext.getContentResolver(), Secure.QS_AUTO_ADDED_TILES);
     86         if (current == null) {
     87             return Collections.emptyList();
     88         }
     89         return Arrays.asList(current.split(","));
     90     }
     91 
     92     @VisibleForTesting
     93     protected final ContentObserver mObserver = new ContentObserver(new Handler()) {
     94         @Override
     95         public void onChange(boolean selfChange) {
     96             mAutoAdded.addAll(getAdded());
     97         }
     98     };
     99 }
    100