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; |
Felipe Leme | 7d822ff | 2016-04-21 14:21:45 -0700 | [diff] [blame] | 19 | |
| 20 | import com.android.internal.logging.MetricsLogger; |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 21 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
Felipe Leme | 0281a99 | 2016-04-27 10:22:32 -0700 | [diff] [blame^] | 22 | import com.android.systemui.Prefs; |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 23 | import com.android.systemui.R; |
| 24 | import com.android.systemui.qs.QSTile; |
Felipe Leme | 0281a99 | 2016-04-27 10:22:32 -0700 | [diff] [blame^] | 25 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 26 | import com.android.systemui.statusbar.policy.DataSaverController; |
| 27 | |
| 28 | public 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 Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 39 | public BooleanState newTileState() { |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 40 | 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 Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 53 | public Intent getLongClickIntent() { |
| 54 | return CellularTile.CELLULAR_SETTINGS; |
| 55 | } |
| 56 | |
| 57 | @Override |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 58 | protected void handleClick() { |
Felipe Leme | 0281a99 | 2016-04-27 10:22:32 -0700 | [diff] [blame^] | 59 | 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 Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 82 | mState.value = !mDataSaverController.isDataSaverEnabled(); |
Felipe Leme | 7d822ff | 2016-04-21 14:21:45 -0700 | [diff] [blame] | 83 | MetricsLogger.action(mContext, getMetricsCategory(), mState.value); |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 84 | mDataSaverController.setDataSaverEnabled(mState.value); |
| 85 | refreshState(mState.value); |
| 86 | } |
| 87 | |
| 88 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 89 | public CharSequence getTileLabel() { |
| 90 | return mContext.getString(R.string.data_saver); |
| 91 | } |
| 92 | |
| 93 | @Override |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 94 | 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 Monk | f23aa99 | 2016-01-22 16:45:21 -0500 | [diff] [blame] | 98 | state.contentDescription = mContext.getString(state.value |
| 99 | ? R.string.accessibility_data_saver_on : R.string.accessibility_data_saver_off); |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 100 | 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 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 | } |
| 122 | } |