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