1/*
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;
27import com.android.systemui.statusbar.policy.NightModeController;
28
29/**
30 * Manages which tiles should be automatically added to QS.
31 */
32public class AutoTileManager {
33
34    private final Context mContext;
35    private final QSTileHost mHost;
36    private final Handler mHandler;
37
38    public AutoTileManager(Context context, QSTileHost host) {
39        mContext = context;
40        mHost = host;
41        mHandler = new Handler(mHost.getLooper());
42        if (!Prefs.getBoolean(context, Key.QS_HOTSPOT_ADDED, false)) {
43            host.getHotspotController().addCallback(mHotspotCallback);
44        }
45        if (!Prefs.getBoolean(context, Key.QS_DATA_SAVER_ADDED, false)) {
46            host.getNetworkController().getDataSaverController().addListener(mDataSaverListener);
47        }
48        if (!Prefs.getBoolean(context, Key.QS_INVERT_COLORS_ADDED, false)) {
49            mColorsSetting = new SecureSetting(mContext, mHandler,
50                    Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) {
51                @Override
52                protected void handleValueChanged(int value, boolean observedChange) {
53                    if (value != 0) {
54                        mHost.addTile("inversion");
55                        Prefs.putBoolean(mContext, Key.QS_INVERT_COLORS_ADDED, true);
56                        mHandler.post(new Runnable() {
57                            @Override
58                            public void run() {
59                                mColorsSetting.setListening(false);
60                            }
61                        });
62                    }
63                }
64            };
65            mColorsSetting.setListening(true);
66        }
67        if (!Prefs.getBoolean(context, Key.QS_WORK_ADDED, false)) {
68            host.getManagedProfileController().addCallback(mProfileCallback);
69        }
70        if (!Prefs.getBoolean(context, Key.QS_NIGHT_ADDED, false)) {
71            host.getNightModeController().addListener(mNightModeListener);
72        }
73    }
74
75    public void destroy() {
76        // TODO: Remove any registered listeners.
77    }
78
79    private final NightModeController.Listener mNightModeListener =
80            new NightModeController.Listener() {
81        @Override
82        public void onNightModeChanged() {
83            if (mHost.getNightModeController().isEnabled()) {
84                mHost.addTile("night");
85                Prefs.putBoolean(mContext, Key.QS_NIGHT_ADDED, true);
86                mHandler.post(new Runnable() {
87                    @Override
88                    public void run() {
89                        mHost.getNightModeController().removeListener(mNightModeListener);
90                    }
91                });
92            }
93        }
94
95        @Override
96        public void onTwilightAutoChanged() { }
97    };
98
99    private final ManagedProfileController.Callback mProfileCallback =
100            new ManagedProfileController.Callback() {
101                @Override
102                public void onManagedProfileChanged() {
103                    if (mHost.getManagedProfileController().hasActiveProfile()) {
104                        mHost.addTile("work");
105                        Prefs.putBoolean(mContext, Key.QS_WORK_ADDED, true);
106                        mHandler.post(new Runnable() {
107                            @Override
108                            public void run() {
109                                mHost.getManagedProfileController().removeCallback(
110                                        mProfileCallback);
111                            }
112                        });
113                    }
114                }
115
116                @Override
117                public void onManagedProfileRemoved() {
118                }
119            };
120
121    private SecureSetting mColorsSetting;
122
123    private final DataSaverController.Listener mDataSaverListener = new Listener() {
124        @Override
125        public void onDataSaverChanged(boolean isDataSaving) {
126            if (isDataSaving) {
127                mHost.addTile("saver");
128                Prefs.putBoolean(mContext, Key.QS_DATA_SAVER_ADDED, true);
129                mHandler.post(new Runnable() {
130                    @Override
131                    public void run() {
132                        mHost.getNetworkController().getDataSaverController().remListener(
133                                mDataSaverListener);
134                    }
135                });
136            }
137        }
138    };
139
140    private final HotspotController.Callback mHotspotCallback = new Callback() {
141        @Override
142        public void onHotspotChanged(boolean enabled) {
143            if (enabled) {
144                mHost.addTile("hotspot");
145                Prefs.putBoolean(mContext, Key.QS_HOTSPOT_ADDED, true);
146                mHandler.post(new Runnable() {
147                    @Override
148                    public void run() {
149                        mHost.getHotspotController().removeCallback(mHotspotCallback);
150                    }
151                });
152            }
153        }
154    };
155}
156