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 com.android.internal.telephony.PhoneStateIntentReceiver; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.os.Handler; 24 import android.os.Message; 25 import android.os.SystemProperties; 26 import android.preference.CheckBoxPreference; 27 import android.preference.Preference; 28 import android.provider.Settings; 29 import android.telephony.ServiceState; 30 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 AirplaneModeEnabler(Context context, CheckBoxPreference airplaneModeCheckBoxPreference)54 public AirplaneModeEnabler(Context context, CheckBoxPreference airplaneModeCheckBoxPreference) { 55 56 mContext = context; 57 mCheckBoxPref = airplaneModeCheckBoxPreference; 58 59 airplaneModeCheckBoxPreference.setPersistent(false); 60 61 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler); 62 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED); 63 } 64 resume()65 public void resume() { 66 67 // This is the widget enabled state, not the preference toggled state 68 mCheckBoxPref.setEnabled(true); 69 mCheckBoxPref.setChecked(isAirplaneModeOn(mContext)); 70 71 mPhoneStateReceiver.registerIntent(); 72 mCheckBoxPref.setOnPreferenceChangeListener(this); 73 } 74 pause()75 public void pause() { 76 mPhoneStateReceiver.unregisterIntent(); 77 mCheckBoxPref.setOnPreferenceChangeListener(null); 78 } 79 isAirplaneModeOn(Context context)80 public static boolean isAirplaneModeOn(Context context) { 81 return Settings.System.getInt(context.getContentResolver(), 82 Settings.System.AIRPLANE_MODE_ON, 0) != 0; 83 } 84 setAirplaneModeOn(boolean enabling)85 private void setAirplaneModeOn(boolean enabling) { 86 87 mCheckBoxPref.setEnabled(false); 88 mCheckBoxPref.setSummary(enabling ? R.string.airplane_mode_turning_on 89 : R.string.airplane_mode_turning_off); 90 91 // Change the system setting 92 Settings.System.putInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 93 enabling ? 1 : 0); 94 95 // Post the intent 96 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 97 intent.putExtra("state", enabling); 98 mContext.sendBroadcast(intent); 99 } 100 101 /** 102 * Called when we've received confirmation that the airplane mode was set. 103 */ onAirplaneModeChanged()104 private void onAirplaneModeChanged() { 105 ServiceState serviceState = mPhoneStateReceiver.getServiceState(); 106 boolean airplaneModeEnabled = serviceState.getState() == ServiceState.STATE_POWER_OFF; 107 mCheckBoxPref.setChecked(airplaneModeEnabled); 108 mCheckBoxPref.setSummary(airplaneModeEnabled ? null : 109 mContext.getString(R.string.airplane_mode_summary)); 110 mCheckBoxPref.setEnabled(true); 111 } 112 113 /** 114 * Called when someone clicks on the checkbox preference. 115 */ onPreferenceChange(Preference preference, Object newValue)116 public boolean onPreferenceChange(Preference preference, Object newValue) { 117 if (Boolean.parseBoolean( 118 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) { 119 // In ECM mode, do not update database at this point 120 } else { 121 setAirplaneModeOn((Boolean) newValue); 122 } 123 return true; 124 } 125 setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn)126 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) { 127 if (isECMExit) { 128 // update database based on the current checkbox state 129 setAirplaneModeOn(isAirplaneModeOn); 130 } else { 131 // update checkbox state based on database value 132 onAirplaneModeChanged(); 133 } 134 } 135 136 } 137