blob: a1789a5a1d630b016effb32b12f062f43b48f4d7 [file] [log] [blame]
Jason Monk9a4ce132016-01-21 15:27:17 -05001/*
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
15package com.android.systemui.qs.tiles;
16
Jason Monk76c67aa2016-02-19 14:49:42 -050017import android.content.Intent;
Jason Monk9a4ce132016-01-21 15:27:17 -050018import com.android.internal.logging.MetricsProto.MetricsEvent;
19import com.android.systemui.R;
20import com.android.systemui.qs.QSTile;
21import com.android.systemui.statusbar.policy.DataSaverController;
22
23public class DataSaverTile extends QSTile<QSTile.BooleanState> implements
24 DataSaverController.Listener{
25
26 private final DataSaverController mDataSaverController;
27
28 public DataSaverTile(Host host) {
29 super(host);
30 mDataSaverController = host.getNetworkController().getDataSaverController();
31 }
32
33 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050034 public BooleanState newTileState() {
Jason Monk9a4ce132016-01-21 15:27:17 -050035 return new BooleanState();
36 }
37
38 @Override
39 public void setListening(boolean listening) {
40 if (listening) {
41 mDataSaverController.addListener(this);
42 } else {
43 mDataSaverController.remListener(this);
44 }
45 }
46
47 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050048 public Intent getLongClickIntent() {
49 return CellularTile.CELLULAR_SETTINGS;
50 }
51
52 @Override
Jason Monk9a4ce132016-01-21 15:27:17 -050053 protected void handleClick() {
54 mState.value = !mDataSaverController.isDataSaverEnabled();
55 mDataSaverController.setDataSaverEnabled(mState.value);
56 refreshState(mState.value);
57 }
58
59 @Override
60 protected void handleUpdateState(BooleanState state, Object arg) {
61 state.value = arg instanceof Boolean ? (Boolean) arg
62 : mDataSaverController.isDataSaverEnabled();
63 state.label = mContext.getString(R.string.data_saver);
64 state.contentDescription = mContext.getString(state.value ?
65 R.string.accessibility_data_saver_on : R.string.accessibility_data_saver_off);
66 state.icon = ResourceIcon.get(state.value ? R.drawable.ic_data_saver
67 : R.drawable.ic_data_saver_off);
68 }
69
70 @Override
71 public int getMetricsCategory() {
72 return MetricsEvent.QS_DATA_SAVER;
73 }
74
75 @Override
76 public void onDataSaverChanged(boolean isDataSaving) {
77 refreshState(isDataSaving);
78 }
79}