blob: 65432dc9ec31066e1a472641e3a85f1968fb968f [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;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040019import android.widget.Switch;
Felipe Leme7d822ff2016-04-21 14:21:45 -070020
21import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010022import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Felipe Leme0281a992016-04-27 10:22:32 -070023import com.android.systemui.Prefs;
Jason Monk9a4ce132016-01-21 15:27:17 -050024import com.android.systemui.R;
25import com.android.systemui.qs.QSTile;
Felipe Leme0281a992016-04-27 10:22:32 -070026import com.android.systemui.statusbar.phone.SystemUIDialog;
Jason Monk9a4ce132016-01-21 15:27:17 -050027import com.android.systemui.statusbar.policy.DataSaverController;
28
29public class DataSaverTile extends QSTile<QSTile.BooleanState> implements
30 DataSaverController.Listener{
31
32 private final DataSaverController mDataSaverController;
33
34 public DataSaverTile(Host host) {
35 super(host);
36 mDataSaverController = host.getNetworkController().getDataSaverController();
37 }
38
39 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050040 public BooleanState newTileState() {
Jason Monk9a4ce132016-01-21 15:27:17 -050041 return new BooleanState();
42 }
43
44 @Override
45 public void setListening(boolean listening) {
46 if (listening) {
Jason Monk88529052016-11-04 13:29:58 -040047 mDataSaverController.addCallback(this);
Jason Monk9a4ce132016-01-21 15:27:17 -050048 } else {
Jason Monk88529052016-11-04 13:29:58 -040049 mDataSaverController.removeCallback(this);
Jason Monk9a4ce132016-01-21 15:27:17 -050050 }
51 }
52
53 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050054 public Intent getLongClickIntent() {
55 return CellularTile.CELLULAR_SETTINGS;
56 }
57
58 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -050059 protected void handleClick() {
Felipe Lemeac0dbbb2016-05-03 15:58:35 -070060 if (mState.value
61 || Prefs.getBoolean(mContext, Prefs.Key.QS_DATA_SAVER_DIALOG_SHOWN, false)) {
Felipe Leme0281a992016-04-27 10:22:32 -070062 // Do it right away.
63 toggleDataSaver();
64 return;
65 }
66 // Shows dialog first
67 SystemUIDialog dialog = new SystemUIDialog(mContext);
68 dialog.setTitle(com.android.internal.R.string.data_saver_enable_title);
69 dialog.setMessage(com.android.internal.R.string.data_saver_description);
70 dialog.setPositiveButton(com.android.internal.R.string.data_saver_enable_button,
71 new DialogInterface.OnClickListener() {
72 @Override
73 public void onClick(DialogInterface dialog, int which) {
74 toggleDataSaver();
75 }
76 });
77 dialog.setNegativeButton(com.android.internal.R.string.cancel, null);
78 dialog.setShowForAllUsers(true);
79 dialog.show();
80 Prefs.putBoolean(mContext, Prefs.Key.QS_DATA_SAVER_DIALOG_SHOWN, true);
81 }
82
83 private void toggleDataSaver() {
Jason Monk9a4ce132016-01-21 15:27:17 -050084 mState.value = !mDataSaverController.isDataSaverEnabled();
Felipe Leme7d822ff2016-04-21 14:21:45 -070085 MetricsLogger.action(mContext, getMetricsCategory(), mState.value);
Jason Monk9a4ce132016-01-21 15:27:17 -050086 mDataSaverController.setDataSaverEnabled(mState.value);
87 refreshState(mState.value);
88 }
89
90 @Override
Jason Monk39c98e62016-03-16 09:18:35 -040091 public CharSequence getTileLabel() {
92 return mContext.getString(R.string.data_saver);
93 }
94
95 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -050096 protected void handleUpdateState(BooleanState state, Object arg) {
97 state.value = arg instanceof Boolean ? (Boolean) arg
98 : mDataSaverController.isDataSaverEnabled();
99 state.label = mContext.getString(R.string.data_saver);
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400100 state.contentDescription = state.label;
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);
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400103 state.minimalAccessibilityClassName = state.expandedAccessibilityClassName
104 = Switch.class.getName();
Jason Monk9a4ce132016-01-21 15:27:17 -0500105 }
106
107 @Override
108 public int getMetricsCategory() {
109 return MetricsEvent.QS_DATA_SAVER;
110 }
111
112 @Override
Felipe Leme3e5e8412016-03-09 15:44:23 -0800113 protected String composeChangeAnnouncement() {
114 if (mState.value) {
115 return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_on);
116 } else {
117 return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_off);
118 }
119 }
120
121 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -0500122 public void onDataSaverChanged(boolean isDataSaving) {
123 refreshState(isDataSaving);
124 }
125}