blob: 0ce805ea41b29e904cc53b661b024213ebfcbfb7 [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 Leme0281a992016-04-27 10:22:32 -070059 if (Prefs.getBoolean(mContext, Prefs.Key.QS_DATA_SAVER_DIALOG_SHOWN, false)) {
60 // Do it right away.
61 toggleDataSaver();
62 return;
63 }
64 // Shows dialog first
65 SystemUIDialog dialog = new SystemUIDialog(mContext);
66 dialog.setTitle(com.android.internal.R.string.data_saver_enable_title);
67 dialog.setMessage(com.android.internal.R.string.data_saver_description);
68 dialog.setPositiveButton(com.android.internal.R.string.data_saver_enable_button,
69 new DialogInterface.OnClickListener() {
70 @Override
71 public void onClick(DialogInterface dialog, int which) {
72 toggleDataSaver();
73 }
74 });
75 dialog.setNegativeButton(com.android.internal.R.string.cancel, null);
76 dialog.setShowForAllUsers(true);
77 dialog.show();
78 Prefs.putBoolean(mContext, Prefs.Key.QS_DATA_SAVER_DIALOG_SHOWN, true);
79 }
80
81 private void toggleDataSaver() {
Jason Monk9a4ce132016-01-21 15:27:17 -050082 mState.value = !mDataSaverController.isDataSaverEnabled();
Felipe Leme7d822ff2016-04-21 14:21:45 -070083 MetricsLogger.action(mContext, getMetricsCategory(), mState.value);
Jason Monk9a4ce132016-01-21 15:27:17 -050084 mDataSaverController.setDataSaverEnabled(mState.value);
85 refreshState(mState.value);
86 }
87
88 @Override
Jason Monk39c98e62016-03-16 09:18:35 -040089 public CharSequence getTileLabel() {
90 return mContext.getString(R.string.data_saver);
91 }
92
93 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -050094 protected void handleUpdateState(BooleanState state, Object arg) {
95 state.value = arg instanceof Boolean ? (Boolean) arg
96 : mDataSaverController.isDataSaverEnabled();
97 state.label = mContext.getString(R.string.data_saver);
Jason Monkf23aa992016-01-22 16:45:21 -050098 state.contentDescription = mContext.getString(state.value
99 ? R.string.accessibility_data_saver_on : R.string.accessibility_data_saver_off);
Jason Monk9a4ce132016-01-21 15:27:17 -0500100 state.icon = ResourceIcon.get(state.value ? R.drawable.ic_data_saver
101 : R.drawable.ic_data_saver_off);
102 }
103
104 @Override
105 public int getMetricsCategory() {
106 return MetricsEvent.QS_DATA_SAVER;
107 }
108
109 @Override
Felipe Leme3e5e8412016-03-09 15:44:23 -0800110 protected String composeChangeAnnouncement() {
111 if (mState.value) {
112 return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_on);
113 } else {
114 return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_off);
115 }
116 }
117
118 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -0500119 public void onDataSaverChanged(boolean isDataSaving) {
120 refreshState(isDataSaving);
121 }
122}