blob: aadc8e74c1230a6bc9a56273c05ff4248005e9dd [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;
Jason Monk32508852017-01-18 09:17:13 -050019import android.service.quicksettings.Tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040020import android.widget.Switch;
Felipe Leme7d822ff2016-04-21 14:21:45 -070021
22import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010023import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Felipe Leme0281a992016-04-27 10:22:32 -070024import com.android.systemui.Prefs;
Jason Monk9a4ce132016-01-21 15:27:17 -050025import com.android.systemui.R;
26import com.android.systemui.qs.QSTile;
Felipe Leme0281a992016-04-27 10:22:32 -070027import com.android.systemui.statusbar.phone.SystemUIDialog;
Jason Monk9a4ce132016-01-21 15:27:17 -050028import com.android.systemui.statusbar.policy.DataSaverController;
29
30public class DataSaverTile extends QSTile<QSTile.BooleanState> implements
31 DataSaverController.Listener{
32
33 private final DataSaverController mDataSaverController;
34
35 public DataSaverTile(Host host) {
36 super(host);
37 mDataSaverController = host.getNetworkController().getDataSaverController();
38 }
39
40 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050041 public BooleanState newTileState() {
Jason Monk9a4ce132016-01-21 15:27:17 -050042 return new BooleanState();
43 }
44
45 @Override
46 public void setListening(boolean listening) {
47 if (listening) {
Jason Monk88529052016-11-04 13:29:58 -040048 mDataSaverController.addCallback(this);
Jason Monk9a4ce132016-01-21 15:27:17 -050049 } else {
Jason Monk88529052016-11-04 13:29:58 -040050 mDataSaverController.removeCallback(this);
Jason Monk9a4ce132016-01-21 15:27:17 -050051 }
52 }
53
54 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050055 public Intent getLongClickIntent() {
56 return CellularTile.CELLULAR_SETTINGS;
57 }
58
59 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -050060 protected void handleClick() {
Felipe Lemeac0dbbb2016-05-03 15:58:35 -070061 if (mState.value
62 || Prefs.getBoolean(mContext, Prefs.Key.QS_DATA_SAVER_DIALOG_SHOWN, false)) {
Felipe Leme0281a992016-04-27 10:22:32 -070063 // Do it right away.
64 toggleDataSaver();
65 return;
66 }
67 // Shows dialog first
68 SystemUIDialog dialog = new SystemUIDialog(mContext);
69 dialog.setTitle(com.android.internal.R.string.data_saver_enable_title);
70 dialog.setMessage(com.android.internal.R.string.data_saver_description);
71 dialog.setPositiveButton(com.android.internal.R.string.data_saver_enable_button,
72 new DialogInterface.OnClickListener() {
73 @Override
74 public void onClick(DialogInterface dialog, int which) {
75 toggleDataSaver();
76 }
77 });
78 dialog.setNegativeButton(com.android.internal.R.string.cancel, null);
79 dialog.setShowForAllUsers(true);
80 dialog.show();
81 Prefs.putBoolean(mContext, Prefs.Key.QS_DATA_SAVER_DIALOG_SHOWN, true);
82 }
83
84 private void toggleDataSaver() {
Jason Monk9a4ce132016-01-21 15:27:17 -050085 mState.value = !mDataSaverController.isDataSaverEnabled();
Felipe Leme7d822ff2016-04-21 14:21:45 -070086 MetricsLogger.action(mContext, getMetricsCategory(), mState.value);
Jason Monk9a4ce132016-01-21 15:27:17 -050087 mDataSaverController.setDataSaverEnabled(mState.value);
88 refreshState(mState.value);
89 }
90
91 @Override
Jason Monk39c98e62016-03-16 09:18:35 -040092 public CharSequence getTileLabel() {
93 return mContext.getString(R.string.data_saver);
94 }
95
96 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -050097 protected void handleUpdateState(BooleanState state, Object arg) {
98 state.value = arg instanceof Boolean ? (Boolean) arg
99 : mDataSaverController.isDataSaverEnabled();
Jason Monk32508852017-01-18 09:17:13 -0500100 state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
Jason Monk9a4ce132016-01-21 15:27:17 -0500101 state.label = mContext.getString(R.string.data_saver);
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400102 state.contentDescription = state.label;
Jason Monk9a4ce132016-01-21 15:27:17 -0500103 state.icon = ResourceIcon.get(state.value ? R.drawable.ic_data_saver
104 : R.drawable.ic_data_saver_off);
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400105 state.minimalAccessibilityClassName = state.expandedAccessibilityClassName
106 = Switch.class.getName();
Jason Monk9a4ce132016-01-21 15:27:17 -0500107 }
108
109 @Override
110 public int getMetricsCategory() {
111 return MetricsEvent.QS_DATA_SAVER;
112 }
113
114 @Override
Felipe Leme3e5e8412016-03-09 15:44:23 -0800115 protected String composeChangeAnnouncement() {
116 if (mState.value) {
117 return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_on);
118 } else {
119 return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_off);
120 }
121 }
122
123 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -0500124 public void onDataSaverChanged(boolean isDataSaving) {
125 refreshState(isDataSaving);
126 }
127}