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