blob: b22e1ec5d53301b0c516634a9b7fa9da98aa395b [file] [log] [blame]
Jason Monk9a4ce132016-01-21 15:27:17 -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.qs.tiles;
16
Felipe Leme0281a992016-04-27 10:22:32 -070017import android.content.DialogInterface;
Jason Monk76c67aa2016-02-19 14:49:42 -050018import android.content.Intent;
Felipe Leme7d822ff2016-04-21 14:21:45 -070019
20import com.android.internal.logging.MetricsLogger;
Jason Monk9a4ce132016-01-21 15:27:17 -050021import com.android.internal.logging.MetricsProto.MetricsEvent;
Felipe Leme0281a992016-04-27 10:22:32 -070022import com.android.systemui.Prefs;
Jason Monk9a4ce132016-01-21 15:27:17 -050023import com.android.systemui.R;
24import com.android.systemui.qs.QSTile;
Felipe Leme0281a992016-04-27 10:22:32 -070025import com.android.systemui.statusbar.phone.SystemUIDialog;
Jason Monk9a4ce132016-01-21 15:27:17 -050026import com.android.systemui.statusbar.policy.DataSaverController;
27
28public class DataSaverTile extends QSTile<QSTile.BooleanState> implements
29 DataSaverController.Listener{
30
31 private final DataSaverController mDataSaverController;
32
33 public DataSaverTile(Host host) {
34 super(host);
35 mDataSaverController = host.getNetworkController().getDataSaverController();
36 }
37
38 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050039 public BooleanState newTileState() {
Jason Monk9a4ce132016-01-21 15:27:17 -050040 return new BooleanState();
41 }
42
43 @Override
44 public void setListening(boolean listening) {
45 if (listening) {
46 mDataSaverController.addListener(this);
47 } else {
48 mDataSaverController.remListener(this);
49 }
50 }
51
52 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050053 public Intent getLongClickIntent() {
54 return CellularTile.CELLULAR_SETTINGS;
55 }
56
57 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -050058 protected void handleClick() {
Felipe Lemeac0dbbb2016-05-03 15:58:35 -070059 if (mState.value
60 || Prefs.getBoolean(mContext, Prefs.Key.QS_DATA_SAVER_DIALOG_SHOWN, false)) {
Felipe Leme0281a992016-04-27 10:22:32 -070061 // Do it right away.
62 toggleDataSaver();
63 return;
64 }
65 // Shows dialog first
66 SystemUIDialog dialog = new SystemUIDialog(mContext);
67 dialog.setTitle(com.android.internal.R.string.data_saver_enable_title);
68 dialog.setMessage(com.android.internal.R.string.data_saver_description);
69 dialog.setPositiveButton(com.android.internal.R.string.data_saver_enable_button,
70 new DialogInterface.OnClickListener() {
71 @Override
72 public void onClick(DialogInterface dialog, int which) {
73 toggleDataSaver();
74 }
75 });
76 dialog.setNegativeButton(com.android.internal.R.string.cancel, null);
77 dialog.setShowForAllUsers(true);
78 dialog.show();
79 Prefs.putBoolean(mContext, Prefs.Key.QS_DATA_SAVER_DIALOG_SHOWN, true);
80 }
81
82 private void toggleDataSaver() {
Jason Monk9a4ce132016-01-21 15:27:17 -050083 mState.value = !mDataSaverController.isDataSaverEnabled();
Felipe Leme7d822ff2016-04-21 14:21:45 -070084 MetricsLogger.action(mContext, getMetricsCategory(), mState.value);
Jason Monk9a4ce132016-01-21 15:27:17 -050085 mDataSaverController.setDataSaverEnabled(mState.value);
86 refreshState(mState.value);
87 }
88
89 @Override
Jason Monk39c98e62016-03-16 09:18:35 -040090 public CharSequence getTileLabel() {
91 return mContext.getString(R.string.data_saver);
92 }
93
94 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -050095 protected void handleUpdateState(BooleanState state, Object arg) {
96 state.value = arg instanceof Boolean ? (Boolean) arg
97 : mDataSaverController.isDataSaverEnabled();
98 state.label = mContext.getString(R.string.data_saver);
Jason Monkf23aa992016-01-22 16:45:21 -050099 state.contentDescription = mContext.getString(state.value
100 ? R.string.accessibility_data_saver_on : R.string.accessibility_data_saver_off);
Jason Monk9a4ce132016-01-21 15:27:17 -0500101 state.icon = ResourceIcon.get(state.value ? R.drawable.ic_data_saver
102 : R.drawable.ic_data_saver_off);
103 }
104
105 @Override
106 public int getMetricsCategory() {
107 return MetricsEvent.QS_DATA_SAVER;
108 }
109
110 @Override
Felipe Leme3e5e8412016-03-09 15:44:23 -0800111 protected String composeChangeAnnouncement() {
112 if (mState.value) {
113 return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_on);
114 } else {
115 return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_off);
116 }
117 }
118
119 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -0500120 public void onDataSaverChanged(boolean isDataSaving) {
121 refreshState(isDataSaving);
122 }
123}