AirplaneModeTile.java revision 383db5ebcc3a4a615faf249bf4f126f42e80b82e
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.BroadcastReceiver; 20import android.content.Context; 21import android.content.Intent; 22import android.content.IntentFilter; 23import android.net.ConnectivityManager; 24import android.provider.Settings; 25import android.provider.Settings.Global; 26import android.widget.Switch; 27 28import com.android.internal.logging.MetricsLogger; 29import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 30import com.android.systemui.R; 31import com.android.systemui.qs.GlobalSetting; 32import com.android.systemui.qs.QSTile; 33 34/** Quick settings tile: Airplane mode **/ 35public class AirplaneModeTile extends QSTile<QSTile.BooleanState> { 36 private final AnimationIcon mEnable = 37 new AnimationIcon(R.drawable.ic_signal_airplane_enable_animation, 38 R.drawable.ic_signal_airplane_disable); 39 private final AnimationIcon mDisable = 40 new AnimationIcon(R.drawable.ic_signal_airplane_disable_animation, 41 R.drawable.ic_signal_airplane_enable); 42 private final GlobalSetting mSetting; 43 44 private boolean mListening; 45 46 public AirplaneModeTile(Host host) { 47 super(host); 48 49 mSetting = new GlobalSetting(mContext, mHandler, Global.AIRPLANE_MODE_ON) { 50 @Override 51 protected void handleValueChanged(int value) { 52 handleRefreshState(value); 53 } 54 }; 55 } 56 57 @Override 58 public BooleanState newTileState() { 59 return new BooleanState(); 60 } 61 62 @Override 63 public void handleClick() { 64 MetricsLogger.action(mContext, getMetricsCategory(), !mState.value); 65 setEnabled(!mState.value); 66 } 67 68 private void setEnabled(boolean enabled) { 69 final ConnectivityManager mgr = 70 (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 71 mgr.setAirplaneMode(enabled); 72 } 73 74 @Override 75 public Intent getLongClickIntent() { 76 return new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS); 77 } 78 79 @Override 80 public CharSequence getTileLabel() { 81 return mContext.getString(R.string.airplane_mode); 82 } 83 84 @Override 85 protected void handleUpdateState(BooleanState state, Object arg) { 86 final int value = arg instanceof Integer ? (Integer)arg : mSetting.getValue(); 87 final boolean airplaneMode = value != 0; 88 state.value = airplaneMode; 89 state.label = mContext.getString(R.string.airplane_mode); 90 if (airplaneMode) { 91 state.icon = mEnable; 92 } else { 93 state.icon = mDisable; 94 } 95 state.contentDescription = state.label; 96 state.minimalAccessibilityClassName = state.expandedAccessibilityClassName 97 = Switch.class.getName(); 98 } 99 100 @Override 101 public int getMetricsCategory() { 102 return MetricsEvent.QS_AIRPLANEMODE; 103 } 104 105 @Override 106 protected String composeChangeAnnouncement() { 107 if (mState.value) { 108 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_on); 109 } else { 110 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_off); 111 } 112 } 113 114 public void setListening(boolean listening) { 115 if (mListening == listening) return; 116 mListening = listening; 117 if (listening) { 118 final IntentFilter filter = new IntentFilter(); 119 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); 120 mContext.registerReceiver(mReceiver, filter); 121 } else { 122 mContext.unregisterReceiver(mReceiver); 123 } 124 mSetting.setListening(listening); 125 } 126 127 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 128 @Override 129 public void onReceive(Context context, Intent intent) { 130 if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) { 131 refreshState(); 132 } 133 } 134 }; 135} 136