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 | |
| 17 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
| 18 | import com.android.systemui.R; |
| 19 | import com.android.systemui.qs.QSTile; |
| 20 | import com.android.systemui.statusbar.policy.DataSaverController; |
| 21 | |
| 22 | public class DataSaverTile extends QSTile<QSTile.BooleanState> implements |
| 23 | DataSaverController.Listener{ |
| 24 | |
| 25 | private final DataSaverController mDataSaverController; |
| 26 | |
| 27 | public DataSaverTile(Host host) { |
| 28 | super(host); |
| 29 | mDataSaverController = host.getNetworkController().getDataSaverController(); |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | protected BooleanState newTileState() { |
| 34 | return new BooleanState(); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public void setListening(boolean listening) { |
| 39 | if (listening) { |
| 40 | mDataSaverController.addListener(this); |
| 41 | } else { |
| 42 | mDataSaverController.remListener(this); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | protected void handleClick() { |
| 48 | mState.value = !mDataSaverController.isDataSaverEnabled(); |
| 49 | mDataSaverController.setDataSaverEnabled(mState.value); |
| 50 | refreshState(mState.value); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected void handleUpdateState(BooleanState state, Object arg) { |
| 55 | state.value = arg instanceof Boolean ? (Boolean) arg |
| 56 | : mDataSaverController.isDataSaverEnabled(); |
| 57 | state.label = mContext.getString(R.string.data_saver); |
| 58 | state.contentDescription = mContext.getString(state.value ? |
| 59 | R.string.accessibility_data_saver_on : R.string.accessibility_data_saver_off); |
| 60 | state.icon = ResourceIcon.get(state.value ? R.drawable.ic_data_saver |
| 61 | : R.drawable.ic_data_saver_off); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int getMetricsCategory() { |
| 66 | return MetricsEvent.QS_DATA_SAVER; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onDataSaverChanged(boolean isDataSaving) { |
| 71 | refreshState(isDataSaving); |
| 72 | } |
| 73 | } |