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 // TODO: remove migration code and shared preferences keys after P release 61 if (mUserId == UserHandle.USER_SYSTEM) { 62 for (String[] convertPref : CONVERT_PREFS) { 63 if (Prefs.getBoolean(context, convertPref[0], false)) { 64 setTileAdded(convertPref[1]); 65 Prefs.remove(context, convertPref[0]); 66 } 67 } 68 } 69 mContext.getContentResolver().registerContentObserver( 70 Secure.getUriFor(Secure.QS_AUTO_ADDED_TILES), false, mObserver, 71 UserHandle.USER_ALL); 72 } 73 74 @Override changeUser(UserHandle newUser)75 public void changeUser(UserHandle newUser) { 76 if (newUser.getIdentifier() == mUserId) { 77 return; 78 } 79 mUserId = newUser.getIdentifier(); 80 mAutoAdded.clear(); 81 mAutoAdded.addAll(getAdded()); 82 } 83 84 @Override getCurrentUserId()85 public int getCurrentUserId() { 86 return mUserId; 87 } 88 isAdded(String tile)89 public boolean isAdded(String tile) { 90 return mAutoAdded.contains(tile); 91 } 92 setTileAdded(String tile)93 public void setTileAdded(String tile) { 94 if (mAutoAdded.add(tile)) { 95 saveTiles(); 96 } 97 } 98 setTileRemoved(String tile)99 public void setTileRemoved(String tile) { 100 if (mAutoAdded.remove(tile)) { 101 saveTiles(); 102 } 103 } 104 destroy()105 public void destroy() { 106 mContext.getContentResolver().unregisterContentObserver(mObserver); 107 } 108 saveTiles()109 private void saveTiles() { 110 Secure.putStringForUser(mContext.getContentResolver(), Secure.QS_AUTO_ADDED_TILES, 111 TextUtils.join(",", mAutoAdded), mUserId); 112 } 113 getAdded()114 private Collection<String> getAdded() { 115 String current = Secure.getStringForUser(mContext.getContentResolver(), 116 Secure.QS_AUTO_ADDED_TILES, mUserId); 117 if (current == null) { 118 return Collections.emptyList(); 119 } 120 return Arrays.asList(current.split(",")); 121 } 122 123 @VisibleForTesting 124 protected final ContentObserver mObserver = new ContentObserver(new Handler()) { 125 @Override 126 public void onChange(boolean selfChange) { 127 mAutoAdded.clear(); 128 mAutoAdded.addAll(getAdded()); 129 } 130 }; 131 132 public static class Builder { 133 private final Context mContext; 134 private int mUserId; 135 136 @Inject Builder(Context context)137 public Builder(Context context) { 138 mContext = context; 139 } 140 setUserId(int userId)141 public Builder setUserId(int userId) { 142 mUserId = userId; 143 return this; 144 } 145 build()146 public AutoAddTracker build() { 147 return new AutoAddTracker(mContext, mUserId); 148 } 149 } 150 } 151