1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.qs.tiles;
18
19import android.content.Intent;
20import android.provider.Settings;
21import android.provider.Settings.Secure;
22import android.service.quicksettings.Tile;
23import android.widget.Switch;
24
25import com.android.internal.logging.MetricsLogger;
26import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
27import com.android.systemui.R;
28import com.android.systemui.qs.QSHost;
29import com.android.systemui.plugins.qs.QSTile.BooleanState;
30import com.android.systemui.qs.tileimpl.QSTileImpl;
31import com.android.systemui.qs.SecureSetting;
32
33/** Quick settings tile: Invert colors **/
34public class ColorInversionTile extends QSTileImpl<BooleanState> {
35
36    private final AnimationIcon mEnable
37            = new AnimationIcon(R.drawable.ic_invert_colors_enable_animation,
38            R.drawable.ic_invert_colors_disable);
39    private final AnimationIcon mDisable
40            = new AnimationIcon(R.drawable.ic_invert_colors_disable_animation,
41            R.drawable.ic_invert_colors_enable);
42    private final SecureSetting mSetting;
43
44    private boolean mListening;
45
46    public ColorInversionTile(QSHost host) {
47        super(host);
48
49        mSetting = new SecureSetting(mContext, mHandler,
50                Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) {
51            @Override
52            protected void handleValueChanged(int value, boolean observedChange) {
53                handleRefreshState(value);
54            }
55        };
56    }
57
58    @Override
59    protected void handleDestroy() {
60        super.handleDestroy();
61        mSetting.setListening(false);
62    }
63
64    @Override
65    public BooleanState newTileState() {
66        return new BooleanState();
67    }
68
69    @Override
70    public void setListening(boolean listening) {
71        mSetting.setListening(listening);
72    }
73
74    @Override
75    protected void handleUserSwitch(int newUserId) {
76        mSetting.setUserId(newUserId);
77        handleRefreshState(mSetting.getValue());
78    }
79
80    @Override
81    public Intent getLongClickIntent() {
82        return new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
83    }
84
85    @Override
86    protected void handleClick() {
87        mSetting.setValue(mState.value ? 0 : 1);
88    }
89
90    @Override
91    public CharSequence getTileLabel() {
92        return mContext.getString(R.string.quick_settings_inversion_label);
93    }
94
95    @Override
96    protected void handleUpdateState(BooleanState state, Object arg) {
97        final int value = arg instanceof Integer ? (Integer) arg : mSetting.getValue();
98        final boolean enabled = value != 0;
99        state.value = enabled;
100        state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
101        state.label = mContext.getString(R.string.quick_settings_inversion_label);
102        state.icon = enabled ? mEnable : mDisable;
103        state.expandedAccessibilityClassName = Switch.class.getName();
104        state.contentDescription = state.label;
105    }
106
107    @Override
108    public int getMetricsCategory() {
109        return MetricsEvent.QS_COLORINVERSION;
110    }
111
112    @Override
113    protected String composeChangeAnnouncement() {
114        if (mState.value) {
115            return mContext.getString(
116                    R.string.accessibility_quick_settings_color_inversion_changed_on);
117        } else {
118            return mContext.getString(
119                    R.string.accessibility_quick_settings_color_inversion_changed_off);
120        }
121    }
122}
123