blob: b74247960fc716fd6df318d28658a23199e6562f [file] [log] [blame]
Jason Monkc3f42c12016-02-05 12:33:13 -05001/*
2 * Copyright (C) 2016 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
15package com.android.systemui.statusbar.phone;
16
17import android.content.Context;
18import android.os.Handler;
19import android.provider.Settings.Secure;
20import com.android.systemui.Prefs;
21import com.android.systemui.Prefs.Key;
22import com.android.systemui.qs.SecureSetting;
23import com.android.systemui.statusbar.policy.DataSaverController;
24import com.android.systemui.statusbar.policy.DataSaverController.Listener;
25import com.android.systemui.statusbar.policy.HotspotController;
26import com.android.systemui.statusbar.policy.HotspotController.Callback;
27
28/**
29 * Manages which tiles should be automatically added to QS.
30 */
31public class AutoTileManager {
32
33 private final Context mContext;
34 private final QSTileHost mHost;
35 private final Handler mHandler;
36
37 public AutoTileManager(Context context, QSTileHost host) {
38 mContext = context;
39 mHost = host;
40 mHandler = new Handler(mHost.getLooper());
41 if (!Prefs.getBoolean(context, Key.QS_HOTSPOT_ADDED, false)) {
42 host.getHotspotController().addCallback(mHotspotCallback);
43 }
44 if (!Prefs.getBoolean(context, Key.QS_DATA_SAVER_ADDED, false)) {
45 host.getNetworkController().getDataSaverController().addListener(mDataSaverListener);
46 }
47 if (!Prefs.getBoolean(context, Key.QS_INVERT_COLORS_ADDED, false)) {
48 mColorsSetting = new SecureSetting(mContext, mHandler,
49 Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) {
50 @Override
51 protected void handleValueChanged(int value, boolean observedChange) {
52 if (value != 0) {
53 mHost.addTile("inversion");
54 Prefs.putBoolean(mContext, Key.QS_INVERT_COLORS_ADDED, true);
55 mHandler.post(new Runnable() {
56 @Override
57 public void run() {
58 mColorsSetting.setListening(false);
59 }
60 });
61 }
62 }
63 };
64 mColorsSetting.setListening(true);
65 }
66 if (!Prefs.getBoolean(context, Key.QS_WORK_ADDED, false)) {
67 host.getManagedProfileController().addCallback(mProfileCallback);
68 }
69 }
70
71 public void destroy() {
72 // TODO: Remove any registered listeners.
73 }
74
75 private final ManagedProfileController.Callback mProfileCallback =
76 new ManagedProfileController.Callback() {
77 @Override
78 public void onManagedProfileChanged() {
79 if (mHost.getManagedProfileController().hasActiveProfile()) {
80 mHost.addTile("work");
81 Prefs.putBoolean(mContext, Key.QS_WORK_ADDED, true);
82 mHandler.post(new Runnable() {
83 @Override
84 public void run() {
85 mHost.getManagedProfileController().removeCallback(
86 mProfileCallback);
87 }
88 });
89 }
90 }
91
92 @Override
93 public void onManagedProfileRemoved() {
94 }
95 };
96
97 private SecureSetting mColorsSetting;
98
99 private final DataSaverController.Listener mDataSaverListener = new Listener() {
100 @Override
101 public void onDataSaverChanged(boolean isDataSaving) {
102 if (isDataSaving) {
103 mHost.addTile("saver");
104 Prefs.putBoolean(mContext, Key.QS_DATA_SAVER_ADDED, true);
105 mHandler.post(new Runnable() {
106 @Override
107 public void run() {
108 mHost.getNetworkController().getDataSaverController().remListener(
109 mDataSaverListener);
110 }
111 });
112 }
113 }
114 };
115
116 private final HotspotController.Callback mHotspotCallback = new Callback() {
117 @Override
118 public void onHotspotChanged(boolean enabled) {
119 if (enabled) {
120 mHost.addTile("hotspot");
121 Prefs.putBoolean(mContext, Key.QS_HOTSPOT_ADDED, true);
122 mHandler.post(new Runnable() {
123 @Override
124 public void run() {
125 mHost.getHotspotController().removeCallback(mHotspotCallback);
126 }
127 });
128 }
129 }
130 };
131}