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 17 package com.android.systemui.qs.tiles; 18 19 import android.provider.Settings.Secure; 20 21 import com.android.internal.logging.MetricsLogger; 22 import com.android.systemui.Prefs; 23 import com.android.systemui.R; 24 import com.android.systemui.qs.QSTile; 25 import com.android.systemui.qs.SecureSetting; 26 import com.android.systemui.qs.UsageTracker; 27 28 /** Quick settings tile: Invert colors **/ 29 public class ColorInversionTile extends QSTile<QSTile.BooleanState> { 30 31 private final AnimationIcon mEnable 32 = new AnimationIcon(R.drawable.ic_invert_colors_enable_animation); 33 private final AnimationIcon mDisable 34 = new AnimationIcon(R.drawable.ic_invert_colors_disable_animation); 35 private final SecureSetting mSetting; 36 private final UsageTracker mUsageTracker; 37 38 private boolean mListening; 39 40 public ColorInversionTile(Host host) { 41 super(host); 42 43 mSetting = new SecureSetting(mContext, mHandler, 44 Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) { 45 @Override 46 protected void handleValueChanged(int value, boolean observedChange) { 47 if (value != 0 || observedChange) { 48 mUsageTracker.trackUsage(); 49 } 50 if (mListening) { 51 handleRefreshState(value); 52 } 53 } 54 }; 55 mUsageTracker = new UsageTracker(host.getContext(), 56 Prefs.Key.COLOR_INVERSION_TILE_LAST_USED, ColorInversionTile.class, 57 R.integer.days_to_show_color_inversion_tile); 58 if (mSetting.getValue() != 0 && !mUsageTracker.isRecentlyUsed()) { 59 mUsageTracker.trackUsage(); 60 } 61 mUsageTracker.setListening(true); 62 mSetting.setListening(true); 63 } 64 65 @Override 66 protected void handleDestroy() { 67 super.handleDestroy(); 68 mUsageTracker.setListening(false); 69 mSetting.setListening(false); 70 } 71 72 @Override 73 protected BooleanState newTileState() { 74 return new BooleanState(); 75 } 76 77 @Override 78 public void setListening(boolean listening) { 79 mListening = listening; 80 } 81 82 @Override 83 protected void handleUserSwitch(int newUserId) { 84 mSetting.setUserId(newUserId); 85 handleRefreshState(mSetting.getValue()); 86 } 87 88 @Override 89 protected void handleClick() { 90 MetricsLogger.action(mContext, getMetricsCategory(), !mState.value); 91 mSetting.setValue(mState.value ? 0 : 1); 92 mEnable.setAllowAnimation(true); 93 mDisable.setAllowAnimation(true); 94 } 95 96 @Override 97 protected void handleLongClick() { 98 if (mState.value) return; // don't allow usage reset if inversion is active 99 final String title = mContext.getString(R.string.quick_settings_reset_confirmation_title, 100 mState.label); 101 mUsageTracker.showResetConfirmation(title, new Runnable() { 102 @Override 103 public void run() { 104 refreshState(); 105 } 106 }); 107 } 108 109 @Override 110 protected void handleUpdateState(BooleanState state, Object arg) { 111 final int value = arg instanceof Integer ? (Integer) arg : mSetting.getValue(); 112 final boolean enabled = value != 0; 113 state.visible = enabled || mUsageTracker.isRecentlyUsed(); 114 state.value = enabled; 115 state.label = mContext.getString(R.string.quick_settings_inversion_label); 116 state.icon = enabled ? mEnable : mDisable; 117 } 118 119 @Override 120 public int getMetricsCategory() { 121 return MetricsLogger.QS_COLORINVERSION; 122 } 123 124 @Override 125 protected String composeChangeAnnouncement() { 126 if (mState.value) { 127 return mContext.getString( 128 R.string.accessibility_quick_settings_color_inversion_changed_on); 129 } else { 130 return mContext.getString( 131 R.string.accessibility_quick_settings_color_inversion_changed_off); 132 } 133 } 134 } 135