Home | History | Annotate | Download | only in tiles
      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.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.net.ConnectivityManager;
     24 import android.provider.Settings.Global;
     25 
     26 import com.android.systemui.R;
     27 import com.android.systemui.qs.GlobalSetting;
     28 import com.android.systemui.qs.QSTile;
     29 
     30 /** Quick settings tile: Airplane mode **/
     31 public class AirplaneModeTile extends QSTile<QSTile.BooleanState> {
     32     private final AnimationIcon mEnable =
     33             new AnimationIcon(R.drawable.ic_signal_airplane_enable_animation);
     34     private final AnimationIcon mDisable =
     35             new AnimationIcon(R.drawable.ic_signal_airplane_disable_animation);
     36     private final GlobalSetting mSetting;
     37 
     38     private boolean mListening;
     39 
     40     public AirplaneModeTile(Host host) {
     41         super(host);
     42 
     43         mSetting = new GlobalSetting(mContext, mHandler, Global.AIRPLANE_MODE_ON) {
     44             @Override
     45             protected void handleValueChanged(int value) {
     46                 handleRefreshState(value);
     47             }
     48         };
     49     }
     50 
     51     @Override
     52     protected BooleanState newTileState() {
     53         return new BooleanState();
     54     }
     55 
     56     @Override
     57     public void handleClick() {
     58         setEnabled(!mState.value);
     59         mEnable.setAllowAnimation(true);
     60         mDisable.setAllowAnimation(true);
     61     }
     62 
     63     private void setEnabled(boolean enabled) {
     64         final ConnectivityManager mgr =
     65                 (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
     66         mgr.setAirplaneMode(enabled);
     67     }
     68 
     69     @Override
     70     protected void handleUpdateState(BooleanState state, Object arg) {
     71         final int value = arg instanceof Integer ? (Integer)arg : mSetting.getValue();
     72         final boolean airplaneMode = value != 0;
     73         state.value = airplaneMode;
     74         state.visible = true;
     75         state.label = mContext.getString(R.string.quick_settings_airplane_mode_label);
     76         if (airplaneMode) {
     77             state.icon = mEnable;
     78             state.contentDescription =  mContext.getString(
     79                     R.string.accessibility_quick_settings_airplane_on);
     80         } else {
     81             state.icon = mDisable;
     82             state.contentDescription =  mContext.getString(
     83                     R.string.accessibility_quick_settings_airplane_off);
     84         }
     85     }
     86 
     87     @Override
     88     protected String composeChangeAnnouncement() {
     89         if (mState.value) {
     90             return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_on);
     91         } else {
     92             return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_off);
     93         }
     94     }
     95 
     96     public void setListening(boolean listening) {
     97         if (mListening == listening) return;
     98         mListening = listening;
     99         if (listening) {
    100             final IntentFilter filter = new IntentFilter();
    101             filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    102             mContext.registerReceiver(mReceiver, filter);
    103         } else {
    104             mContext.unregisterReceiver(mReceiver);
    105         }
    106         mSetting.setListening(listening);
    107     }
    108 
    109     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    110         @Override
    111         public void onReceive(Context context, Intent intent) {
    112             if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
    113                 refreshState();
    114             }
    115         }
    116     };
    117 }
    118