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