blob: 412fe3dee93eb63db6e57d77f7243e4b5bb5a4bd [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;
Jason Monk9c7844c2017-01-18 15:21:53 -050024import com.android.systemui.Dependency;
Felipe Leme0281a992016-04-27 10:22:32 -070025import com.android.systemui.Prefs;
Jason Monk9a4ce132016-01-21 15:27:17 -050026import com.android.systemui.R;
27import com.android.systemui.qs.QSTile;
Felipe Leme0281a992016-04-27 10:22:32 -070028import com.android.systemui.statusbar.phone.SystemUIDialog;
Jason Monk9a4ce132016-01-21 15:27:17 -050029import com.android.systemui.statusbar.policy.DataSaverController;
Jason Monk9c7844c2017-01-18 15:21:53 -050030import com.android.systemui.statusbar.policy.NetworkController;
Jason Monk9a4ce132016-01-21 15:27:17 -050031
32public class DataSaverTile extends QSTile<QSTile.BooleanState> implements
33 DataSaverController.Listener{
34
35 private final DataSaverController mDataSaverController;
36
37 public DataSaverTile(Host host) {
38 super(host);
Jason Monk9c7844c2017-01-18 15:21:53 -050039 mDataSaverController = Dependency.get(NetworkController.class).getDataSaverController();
Jason Monk9a4ce132016-01-21 15:27:17 -050040 }
41
42 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050043 public BooleanState newTileState() {
Jason Monk9a4ce132016-01-21 15:27:17 -050044 return new BooleanState();
45 }
46
47 @Override
48 public void setListening(boolean listening) {
49 if (listening) {
Jason Monk88529052016-11-04 13:29:58 -040050 mDataSaverController.addCallback(this);
Jason Monk9a4ce132016-01-21 15:27:17 -050051 } else {
Jason Monk88529052016-11-04 13:29:58 -040052 mDataSaverController.removeCallback(this);
Jason Monk9a4ce132016-01-21 15:27:17 -050053 }
54 }
55
56 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050057 public Intent getLongClickIntent() {
58 return CellularTile.CELLULAR_SETTINGS;
59 }
60
61 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -050062 protected void handleClick() {
Felipe Lemeac0dbbb2016-05-03 15:58:35 -070063 if (mState.value
64 || Prefs.getBoolean(mContext, Prefs.Key.QS_DATA_SAVER_DIALOG_SHOWN, false)) {
Felipe Leme0281a992016-04-27 10:22:32 -070065 // Do it right away.
66 toggleDataSaver();
67 return;
68 }
69 // Shows dialog first
70 SystemUIDialog dialog = new SystemUIDialog(mContext);
71 dialog.setTitle(com.android.internal.R.string.data_saver_enable_title);
72 dialog.setMessage(com.android.internal.R.string.data_saver_description);
73 dialog.setPositiveButton(com.android.internal.R.string.data_saver_enable_button,
74 new DialogInterface.OnClickListener() {
75 @Override
76 public void onClick(DialogInterface dialog, int which) {
77 toggleDataSaver();
78 }
79 });
80 dialog.setNegativeButton(com.android.internal.R.string.cancel, null);
81 dialog.setShowForAllUsers(true);
82 dialog.show();
83 Prefs.putBoolean(mContext, Prefs.Key.QS_DATA_SAVER_DIALOG_SHOWN, true);
84 }
85
86 private void toggleDataSaver() {
Jason Monk9a4ce132016-01-21 15:27:17 -050087 mState.value = !mDataSaverController.isDataSaverEnabled();
Felipe Leme7d822ff2016-04-21 14:21:45 -070088 MetricsLogger.action(mContext, getMetricsCategory(), mState.value);
Jason Monk9a4ce132016-01-21 15:27:17 -050089 mDataSaverController.setDataSaverEnabled(mState.value);
90 refreshState(mState.value);
91 }
92
93 @Override
Jason Monk39c98e62016-03-16 09:18:35 -040094 public CharSequence getTileLabel() {
95 return mContext.getString(R.string.data_saver);
96 }
97
98 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -050099 protected void handleUpdateState(BooleanState state, Object arg) {
100 state.value = arg instanceof Boolean ? (Boolean) arg
101 : mDataSaverController.isDataSaverEnabled();
Jason Monk32508852017-01-18 09:17:13 -0500102 state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
Jason Monk9a4ce132016-01-21 15:27:17 -0500103 state.label = mContext.getString(R.string.data_saver);
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400104 state.contentDescription = state.label;
Jason Monk9a4ce132016-01-21 15:27:17 -0500105 state.icon = ResourceIcon.get(state.value ? R.drawable.ic_data_saver
106 : R.drawable.ic_data_saver_off);
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400107 state.minimalAccessibilityClassName = state.expandedAccessibilityClassName
108 = Switch.class.getName();
Jason Monk9a4ce132016-01-21 15:27:17 -0500109 }
110
111 @Override
112 public int getMetricsCategory() {
113 return MetricsEvent.QS_DATA_SAVER;
114 }
115
116 @Override
Felipe Leme3e5e8412016-03-09 15:44:23 -0800117 protected String composeChangeAnnouncement() {
118 if (mState.value) {
119 return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_on);
120 } else {
121 return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_off);
122 }
123 }
124
125 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -0500126 public void onDataSaverChanged(boolean isDataSaving) {
127 refreshState(isDataSaving);
128 }
129}