DndTile.java revision 4f1163c83e79da52b2be7ff6b10163441895bc26
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.qs.tiles;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.SharedPreferences;
24import android.provider.Settings;
25import android.provider.Settings.Global;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.View.OnAttachStateChangeListener;
29import android.view.ViewGroup;
30
31import com.android.systemui.R;
32import com.android.systemui.qs.QSTile;
33import com.android.systemui.statusbar.policy.ZenModeController;
34import com.android.systemui.volume.ZenModePanel;
35
36/** Quick settings tile: Do not disturb **/
37public class DndTile extends QSTile<QSTile.BooleanState> {
38    private static final Intent ZEN_SETTINGS = new Intent(Settings.ACTION_ZEN_MODE_SETTINGS);
39
40    private static final String ACTION_SET_VISIBLE = "com.android.systemui.dndtile.SET_VISIBLE";
41    private static final String EXTRA_VISIBLE = "visible";
42    private static final String PREF_KEY_VISIBLE = "DndTileVisible";
43    private static final String PREF_KEY_COMBINED_ICON = "DndTileCombinedIcon";
44
45    private final ZenModeController mController;
46    private final DndDetailAdapter mDetailAdapter;
47
48    private boolean mListening;
49    private boolean mVisible;
50    private boolean mShowingDetail;
51
52    public DndTile(Host host) {
53        super(host);
54        mController = host.getZenModeController();
55        mDetailAdapter = new DndDetailAdapter();
56        mVisible = isVisible(host.getContext());
57        mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_SET_VISIBLE));
58    }
59
60    public static void setVisible(Context context, boolean visible) {
61        context.sendBroadcast(new Intent(DndTile.ACTION_SET_VISIBLE)
62                .putExtra(DndTile.EXTRA_VISIBLE, visible));
63    }
64
65    public static boolean isVisible(Context context) {
66        return getSharedPrefs(context).getBoolean(PREF_KEY_VISIBLE, false);
67    }
68
69    public static void setCombinedIcon(Context context, boolean combined) {
70        getSharedPrefs(context).edit().putBoolean(PREF_KEY_COMBINED_ICON, combined).commit();
71    }
72
73    public static boolean isCombinedIcon(Context context) {
74        return getSharedPrefs(context).getBoolean(PREF_KEY_COMBINED_ICON, false);
75    }
76
77    @Override
78    public DetailAdapter getDetailAdapter() {
79        return mDetailAdapter;
80    }
81
82    @Override
83    protected BooleanState newTileState() {
84        return new BooleanState();
85    }
86
87    @Override
88    public void handleClick() {
89        if (mState.value) {
90            mController.setZen(Global.ZEN_MODE_OFF);
91        } else {
92            mController.setZen(Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
93            showDetail(true);
94        }
95    }
96
97    @Override
98    protected void handleUpdateState(BooleanState state, Object arg) {
99        final int zen = arg instanceof Integer ? (Integer) arg : mController.getZen();
100        state.value = zen != Global.ZEN_MODE_OFF;
101        state.visible = mVisible;
102        switch (zen) {
103            case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
104                state.icon = ResourceIcon.get(R.drawable.ic_qs_dnd_on);
105                state.label = mContext.getString(R.string.quick_settings_dnd_priority_label);
106                state.contentDescription = mContext.getString(
107                        R.string.accessibility_quick_settings_dnd_priority_on);
108                break;
109            case Global.ZEN_MODE_NO_INTERRUPTIONS:
110                state.icon = ResourceIcon.get(R.drawable.ic_qs_dnd_on);
111                state.label = mContext.getString(R.string.quick_settings_dnd_none_label);
112                state.contentDescription = mContext.getString(
113                        R.string.accessibility_quick_settings_dnd_none_on);
114                break;
115            case Global.ZEN_MODE_ALARMS:
116                state.icon = ResourceIcon.get(R.drawable.ic_qs_dnd_on);
117                state.label = mContext.getString(R.string.quick_settings_dnd_alarms_label);
118                state.contentDescription = mContext.getString(
119                        R.string.accessibility_quick_settings_dnd_alarms_on);
120                break;
121            default:
122                state.icon = ResourceIcon.get(R.drawable.ic_qs_dnd_off);
123                state.label = mContext.getString(R.string.quick_settings_dnd_label);
124                state.contentDescription =  mContext.getString(
125                        R.string.accessibility_quick_settings_dnd_off);
126                break;
127        }
128        if (mShowingDetail && !state.value) {
129            showDetail(false);
130        }
131    }
132
133    @Override
134    protected String composeChangeAnnouncement() {
135        if (mState.value) {
136            return mContext.getString(R.string.accessibility_quick_settings_dnd_changed_on);
137        } else {
138            return mContext.getString(R.string.accessibility_quick_settings_dnd_changed_off);
139        }
140    }
141
142    @Override
143    public void setListening(boolean listening) {
144        if (mListening == listening) return;
145        mListening = listening;
146        if (mListening) {
147            mController.addCallback(mZenCallback);
148        } else {
149            mController.removeCallback(mZenCallback);
150        }
151    }
152
153    private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() {
154        public void onZenChanged(int zen) {
155            refreshState(zen);
156        }
157    };
158
159    private static SharedPreferences getSharedPrefs(Context context) {
160        return context.getSharedPreferences(context.getPackageName(), 0);
161    }
162
163    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
164        @Override
165        public void onReceive(Context context, Intent intent) {
166            mVisible = intent.getBooleanExtra(EXTRA_VISIBLE, false);
167            getSharedPrefs(mContext).edit().putBoolean(PREF_KEY_VISIBLE, mVisible).commit();
168            refreshState();
169        }
170    };
171
172    private final class DndDetailAdapter implements DetailAdapter, OnAttachStateChangeListener {
173
174        @Override
175        public int getTitle() {
176            return R.string.quick_settings_dnd_label;
177        }
178
179        @Override
180        public Boolean getToggleState() {
181            return mState.value;
182        }
183
184        @Override
185        public Intent getSettingsIntent() {
186            return ZEN_SETTINGS;
187        }
188
189        @Override
190        public void setToggleState(boolean state) {
191            if (!state) {
192                mController.setZen(Global.ZEN_MODE_OFF);
193                showDetail(false);
194            }
195        }
196
197        @Override
198        public View createDetailView(Context context, View convertView, ViewGroup parent) {
199            final ZenModePanel zmp = convertView != null ? (ZenModePanel) convertView
200                    : (ZenModePanel) LayoutInflater.from(context).inflate(
201                            R.layout.zen_mode_panel, parent, false);
202            if (convertView == null) {
203                zmp.init(mController);
204                zmp.setEmbedded(true);
205                zmp.addOnAttachStateChangeListener(this);
206            }
207            return zmp;
208        }
209
210        @Override
211        public void onViewAttachedToWindow(View v) {
212            mShowingDetail = true;
213        }
214
215        @Override
216        public void onViewDetachedFromWindow(View v) {
217            mShowingDetail = false;
218        }
219    }
220}
221