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