DataSaverTile.java revision 39c98e652fd3e0e3ea3a334d60ba6ee979985978
19a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk/*
29a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk * Copyright (C) 2016 The Android Open Source Project
39a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk *
49a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
59a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk * except in compliance with the License. You may obtain a copy of the License at
69a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk *
79a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk *      http://www.apache.org/licenses/LICENSE-2.0
89a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk *
99a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk * Unless required by applicable law or agreed to in writing, software distributed under the
109a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
119a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk * KIND, either express or implied. See the License for the specific language governing
129a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk * permissions and limitations under the License.
139a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk */
149a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk
159a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monkpackage com.android.systemui.qs.tiles;
169a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk
1776c67aa361f65dfb2f5e03d06cc1ccebce9cecd9Jason Monkimport android.content.Intent;
189a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monkimport com.android.internal.logging.MetricsProto.MetricsEvent;
199a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monkimport com.android.systemui.R;
209a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monkimport com.android.systemui.qs.QSTile;
219a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monkimport com.android.systemui.statusbar.policy.DataSaverController;
229a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk
239a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monkpublic class DataSaverTile extends QSTile<QSTile.BooleanState> implements
249a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        DataSaverController.Listener{
259a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk
269a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    private final DataSaverController mDataSaverController;
279a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk
289a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    public DataSaverTile(Host host) {
299a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        super(host);
309a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        mDataSaverController = host.getNetworkController().getDataSaverController();
319a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    }
329a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk
339a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    @Override
3462b63a02d7ca630e3ad39991ea6550cab57e5d22Jason Monk    public BooleanState newTileState() {
359a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        return new BooleanState();
369a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    }
379a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk
389a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    @Override
399a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    public void setListening(boolean listening) {
409a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        if (listening) {
419a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk            mDataSaverController.addListener(this);
429a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        } else {
439a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk            mDataSaverController.remListener(this);
449a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        }
459a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    }
469a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk
479a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    @Override
4876c67aa361f65dfb2f5e03d06cc1ccebce9cecd9Jason Monk    public Intent getLongClickIntent() {
4976c67aa361f65dfb2f5e03d06cc1ccebce9cecd9Jason Monk        return CellularTile.CELLULAR_SETTINGS;
5076c67aa361f65dfb2f5e03d06cc1ccebce9cecd9Jason Monk    }
5176c67aa361f65dfb2f5e03d06cc1ccebce9cecd9Jason Monk
5276c67aa361f65dfb2f5e03d06cc1ccebce9cecd9Jason Monk    @Override
539a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    protected void handleClick() {
549a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        mState.value = !mDataSaverController.isDataSaverEnabled();
559a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        mDataSaverController.setDataSaverEnabled(mState.value);
569a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        refreshState(mState.value);
579a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    }
589a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk
599a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    @Override
6039c98e652fd3e0e3ea3a334d60ba6ee979985978Jason Monk    public CharSequence getTileLabel() {
6139c98e652fd3e0e3ea3a334d60ba6ee979985978Jason Monk        return mContext.getString(R.string.data_saver);
6239c98e652fd3e0e3ea3a334d60ba6ee979985978Jason Monk    }
6339c98e652fd3e0e3ea3a334d60ba6ee979985978Jason Monk
6439c98e652fd3e0e3ea3a334d60ba6ee979985978Jason Monk    @Override
659a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    protected void handleUpdateState(BooleanState state, Object arg) {
669a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        state.value = arg instanceof Boolean ? (Boolean) arg
679a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk                : mDataSaverController.isDataSaverEnabled();
689a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        state.label = mContext.getString(R.string.data_saver);
69f23aa99270606b227d007b8f7c1619f564c55b0fJason Monk        state.contentDescription = mContext.getString(state.value
70f23aa99270606b227d007b8f7c1619f564c55b0fJason Monk                ? R.string.accessibility_data_saver_on : R.string.accessibility_data_saver_off);
719a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        state.icon = ResourceIcon.get(state.value ? R.drawable.ic_data_saver
729a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk                : R.drawable.ic_data_saver_off);
739a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    }
749a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk
759a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    @Override
769a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    public int getMetricsCategory() {
779a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        return MetricsEvent.QS_DATA_SAVER;
789a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    }
799a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk
809a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    @Override
813e5e84152f6c07b4fada3fea2e6d00aa59624b80Felipe Leme    protected String composeChangeAnnouncement() {
823e5e84152f6c07b4fada3fea2e6d00aa59624b80Felipe Leme        if (mState.value) {
833e5e84152f6c07b4fada3fea2e6d00aa59624b80Felipe Leme            return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_on);
843e5e84152f6c07b4fada3fea2e6d00aa59624b80Felipe Leme        } else {
853e5e84152f6c07b4fada3fea2e6d00aa59624b80Felipe Leme            return mContext.getString(R.string.accessibility_quick_settings_data_saver_changed_off);
863e5e84152f6c07b4fada3fea2e6d00aa59624b80Felipe Leme        }
873e5e84152f6c07b4fada3fea2e6d00aa59624b80Felipe Leme    }
883e5e84152f6c07b4fada3fea2e6d00aa59624b80Felipe Leme
893e5e84152f6c07b4fada3fea2e6d00aa59624b80Felipe Leme    @Override
909a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    public void onDataSaverChanged(boolean isDataSaving) {
919a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk        refreshState(isDataSaving);
929a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk    }
939a4ce137eb5b73f9d7b097a26ae1c523c5b070e9Jason Monk}