Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | package com.android.systemui.qs.tiles; |
| 16 | |
Rajeev Kumar | a8981f3 | 2017-07-19 18:12:47 -0700 | [diff] [blame] | 17 | import android.content.DialogInterface.OnClickListener; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 18 | import android.content.Intent; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 19 | import android.service.quicksettings.Tile; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 20 | import android.widget.Switch; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 21 | |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 22 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 23 | import com.android.systemui.Dependency; |
Felipe Leme | 0281a99 | 2016-04-27 10:22:32 -0700 | [diff] [blame] | 24 | import com.android.systemui.Prefs; |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 25 | import com.android.systemui.R; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 26 | import com.android.systemui.plugins.qs.QSTile.BooleanState; |
Rajeev Kumar | a8981f3 | 2017-07-19 18:12:47 -0700 | [diff] [blame] | 27 | import com.android.systemui.qs.QSHost; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 28 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
Felipe Leme | 0281a99 | 2016-04-27 10:22:32 -0700 | [diff] [blame] | 29 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 30 | import com.android.systemui.statusbar.policy.DataSaverController; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 31 | import com.android.systemui.statusbar.policy.NetworkController; |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 32 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 33 | public class DataSaverTile extends QSTileImpl<BooleanState> implements |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 34 | DataSaverController.Listener{ |
| 35 | |
| 36 | private final DataSaverController mDataSaverController; |
| 37 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 38 | public DataSaverTile(QSHost host) { |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 39 | super(host); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 40 | mDataSaverController = Dependency.get(NetworkController.class).getDataSaverController(); |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 44 | public BooleanState newTileState() { |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 45 | return new BooleanState(); |
| 46 | } |
| 47 | |
| 48 | @Override |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 49 | public void handleSetListening(boolean listening) { |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 50 | if (listening) { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 51 | mDataSaverController.addCallback(this); |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 52 | } else { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 53 | mDataSaverController.removeCallback(this); |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 58 | public Intent getLongClickIntent() { |
Rohan Shah | 53f3f15 | 2018-02-28 15:13:31 -0800 | [diff] [blame] | 59 | return CellularTile.getCellularSettingIntent(); |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 60 | } |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 61 | @Override |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 62 | protected void handleClick() { |
Felipe Leme | ac0dbbb | 2016-05-03 15:58:35 -0700 | [diff] [blame] | 63 | if (mState.value |
| 64 | || Prefs.getBoolean(mContext, Prefs.Key.QS_DATA_SAVER_DIALOG_SHOWN, false)) { |
Felipe Leme | 0281a99 | 2016-04-27 10:22:32 -0700 | [diff] [blame] | 65 | // 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, |
Rajeev Kumar | a8981f3 | 2017-07-19 18:12:47 -0700 | [diff] [blame] | 74 | (OnClickListener) (dialogInterface, which) -> toggleDataSaver()); |
Felipe Leme | 0281a99 | 2016-04-27 10:22:32 -0700 | [diff] [blame] | 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 Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 82 | mState.value = !mDataSaverController.isDataSaverEnabled(); |
| 83 | mDataSaverController.setDataSaverEnabled(mState.value); |
| 84 | refreshState(mState.value); |
| 85 | } |
| 86 | |
| 87 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 88 | public CharSequence getTileLabel() { |
| 89 | return mContext.getString(R.string.data_saver); |
| 90 | } |
| 91 | |
| 92 | @Override |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 93 | protected void handleUpdateState(BooleanState state, Object arg) { |
| 94 | state.value = arg instanceof Boolean ? (Boolean) arg |
| 95 | : mDataSaverController.isDataSaverEnabled(); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 96 | state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 97 | state.label = mContext.getString(R.string.data_saver); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 98 | state.contentDescription = state.label; |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 99 | state.icon = ResourceIcon.get(state.value ? R.drawable.ic_data_saver |
| 100 | : R.drawable.ic_data_saver_off); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 101 | state.expandedAccessibilityClassName = Switch.class.getName(); |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public int getMetricsCategory() { |
| 106 | return MetricsEvent.QS_DATA_SAVER; |
| 107 | } |
| 108 | |
| 109 | @Override |
Felipe Leme | 3e5e841 | 2016-03-09 15:44:23 -0800 | [diff] [blame] | 110 | 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 Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 119 | public void onDataSaverChanged(boolean isDataSaving) { |
| 120 | refreshState(isDataSaving); |
| 121 | } |
Rajeev Kumar | a8981f3 | 2017-07-19 18:12:47 -0700 | [diff] [blame] | 122 | } |