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.os.UserHandle; 27 import android.provider.Settings.Secure; 28 import android.text.TextUtils; 29 import android.util.ArraySet; 30 31 import com.android.internal.annotations.VisibleForTesting; 32 import com.android.systemui.Prefs; 33 import com.android.systemui.Prefs.Key; 34 import com.android.systemui.util.UserAwareController; 35 36 import java.util.Arrays; 37 import java.util.Collection; 38 import java.util.Collections; 39 40 import javax.inject.Inject; 41 42 public class AutoAddTracker implements UserAwareController { 43 44 private static final String[][] CONVERT_PREFS = { 45 {Key.QS_HOTSPOT_ADDED, HOTSPOT}, 46 {Key.QS_DATA_SAVER_ADDED, SAVER}, 47 {Key.QS_INVERT_COLORS_ADDED, INVERSION}, 48 {Key.QS_WORK_ADDED, WORK}, 49 {Key.QS_NIGHTDISPLAY_ADDED, NIGHT}, 50 }; 51 52 private final ArraySet<String> mAutoAdded; 53 private final Context mContext; 54 private int mUserId; 55 AutoAddTracker(Context context, int userId)56 public AutoAddTracker(Context context, int userId) { 57 mContext = context; 58 mUserId = userId; 59 mAutoAdded = new ArraySet<>(getAdded()); 60 } 61 62 /** 63 * Init method must be called after construction to start listening 64 */ initialize()65 public void initialize() { 66 // TODO: remove migration code and shared preferences keys after P release 67 if (mUserId == UserHandle.USER_SYSTEM) { 68 for (String[] convertPref : CONVERT_PREFS) { 69 if (Prefs.getBoolean(mContext, convertPref[0], false)) { 70 setTileAdded(convertPref[1]); 71 Prefs.remove(mContext, convertPref[0]); 72 } 73 } 74 } 75 mContext.getContentResolver().registerContentObserver( 76 Secure.getUriFor(Secure.QS_AUTO_ADDED_TILES), false, mObserver, 77 UserHandle.USER_ALL); 78 } 79 80 @Override changeUser(UserHandle newUser)81 public void changeUser(UserHandle newUser) { 82 if (newUser.getIdentifier() == mUserId) { 83 return; 84 } 85 mUserId = newUser.getIdentifier(); 86 mAutoAdded.clear(); 87 mAutoAdded.addAll(getAdded()); 88 } 89 90 @Override getCurrentUserId()91 public int getCurrentUserId() { 92 return mUserId; 93 } 94 isAdded(String tile)95 public boolean isAdded(String tile) { 96 return mAutoAdded.contains(tile); 97 } 98 setTileAdded(String tile)99 public void setTileAdded(String tile) { 100 if (mAutoAdded.add(tile)) { 101 saveTiles(); 102 } 103 } 104 setTileRemoved(String tile)105 public void setTileRemoved(String tile) { 106 if (mAutoAdded.remove(tile)) { 107 saveTiles(); 108 } 109 } 110 destroy()111 public void destroy() { 112 mContext.getContentResolver().unregisterContentObserver(mObserver); 113 } 114 saveTiles()115 private void saveTiles() { 116 Secure.putStringForUser(mContext.getContentResolver(), Secure.QS_AUTO_ADDED_TILES, 117 TextUtils.join(",", mAutoAdded), mUserId); 118 } 119 getAdded()120 private Collection<String> getAdded() { 121 String current = Secure.getStringForUser(mContext.getContentResolver(), 122 Secure.QS_AUTO_ADDED_TILES, mUserId); 123 if (current == null) { 124 return Collections.emptyList(); 125 } 126 return Arrays.asList(current.split(",")); 127 } 128 129 @VisibleForTesting 130 protected final ContentObserver mObserver = new ContentObserver(new Handler()) { 131 @Override 132 public void onChange(boolean selfChange) { 133 mAutoAdded.clear(); 134 mAutoAdded.addAll(getAdded()); 135 } 136 }; 137 138 public static class Builder { 139 private final Context mContext; 140 private int mUserId; 141 142 @Inject Builder(Context context)143 public Builder(Context context) { 144 mContext = context; 145 } 146 setUserId(int userId)147 public Builder setUserId(int userId) { 148 mUserId = userId; 149 return this; 150 } 151 build()152 public AutoAddTracker build() { 153 return new AutoAddTracker(mContext, mUserId); 154 } 155 } 156 } 157