Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2007 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.settings;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.database.ContentObserver;
     22 import android.os.Handler;
     23 import android.os.Message;
     24 import android.os.SystemProperties;
     25 import android.os.UserHandle;
     26 import android.preference.CheckBoxPreference;
     27 import android.preference.Preference;
     28 import android.preference.SwitchPreference;
     29 import android.provider.Settings;
     30 
     31 import com.android.internal.telephony.PhoneStateIntentReceiver;
     32 import com.android.internal.telephony.TelephonyProperties;
     33 
     34 public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
     35 
     36     private final Context mContext;
     37 
     38     private PhoneStateIntentReceiver mPhoneStateReceiver;
     39 
     40     private final SwitchPreference mSwitchPref;
     41 
     42     private static final int EVENT_SERVICE_STATE_CHANGED = 3;
     43 
     44     private Handler mHandler = new Handler() {
     45         @Override
     46         public void handleMessage(Message msg) {
     47             switch (msg.what) {
     48                 case EVENT_SERVICE_STATE_CHANGED:
     49                     onAirplaneModeChanged();
     50                     break;
     51             }
     52         }
     53     };
     54 
     55     private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
     56         @Override
     57         public void onChange(boolean selfChange) {
     58             onAirplaneModeChanged();
     59         }
     60     };
     61 
     62     public AirplaneModeEnabler(Context context, SwitchPreference airplaneModeCheckBoxPreference) {
     63 
     64         mContext = context;
     65         mSwitchPref = airplaneModeCheckBoxPreference;
     66 
     67         airplaneModeCheckBoxPreference.setPersistent(false);
     68 
     69         mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
     70         mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
     71     }
     72 
     73     public void resume() {
     74 
     75         mSwitchPref.setChecked(isAirplaneModeOn(mContext));
     76 
     77         mPhoneStateReceiver.registerIntent();
     78         mSwitchPref.setOnPreferenceChangeListener(this);
     79         mContext.getContentResolver().registerContentObserver(
     80                 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
     81                 mAirplaneModeObserver);
     82     }
     83 
     84     public void pause() {
     85         mPhoneStateReceiver.unregisterIntent();
     86         mSwitchPref.setOnPreferenceChangeListener(null);
     87         mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
     88     }
     89 
     90     public static boolean isAirplaneModeOn(Context context) {
     91         return Settings.Global.getInt(context.getContentResolver(),
     92                 Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
     93     }
     94 
     95     private void setAirplaneModeOn(boolean enabling) {
     96         // Change the system setting
     97         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
     98                                 enabling ? 1 : 0);
     99         // Update the UI to reflect system setting
    100         mSwitchPref.setChecked(enabling);
    101 
    102         // Post the intent
    103         Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    104         intent.putExtra("state", enabling);
    105         mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
    106     }
    107 
    108     /**
    109      * Called when we've received confirmation that the airplane mode was set.
    110      * TODO: We update the checkbox summary when we get notified
    111      * that mobile radio is powered up/down. We should not have dependency
    112      * on one radio alone. We need to do the following:
    113      * - handle the case of wifi/bluetooth failures
    114      * - mobile does not send failure notification, fail on timeout.
    115      */
    116     private void onAirplaneModeChanged() {
    117         mSwitchPref.setChecked(isAirplaneModeOn(mContext));
    118     }
    119 
    120     /**
    121      * Called when someone clicks on the checkbox preference.
    122      */
    123     public boolean onPreferenceChange(Preference preference, Object newValue) {
    124         if (Boolean.parseBoolean(
    125                     SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
    126             // In ECM mode, do not update database at this point
    127         } else {
    128             setAirplaneModeOn((Boolean) newValue);
    129         }
    130         return true;
    131     }
    132 
    133     public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
    134         if (isECMExit) {
    135             // update database based on the current checkbox state
    136             setAirplaneModeOn(isAirplaneModeOn);
    137         } else {
    138             // update summary
    139             onAirplaneModeChanged();
    140         }
    141     }
    142 
    143 }
    144