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.provider.Settings; 27 import android.support.v14.preference.SwitchPreference; 28 import android.support.v7.preference.Preference; 29 30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 31 import com.android.internal.telephony.PhoneStateIntentReceiver; 32 import com.android.internal.telephony.TelephonyProperties; 33 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 34 import com.android.settingslib.WirelessUtils; 35 36 public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener { 37 38 private static final int EVENT_SERVICE_STATE_CHANGED = 3; 39 40 private final Context mContext; 41 private final SwitchPreference mSwitchPref; 42 private final MetricsFeatureProvider mMetricsFeatureProvider; 43 44 private PhoneStateIntentReceiver mPhoneStateReceiver; 45 46 private Handler mHandler = new Handler() { 47 @Override 48 public void handleMessage(Message msg) { 49 switch (msg.what) { 50 case EVENT_SERVICE_STATE_CHANGED: 51 onAirplaneModeChanged(); 52 break; 53 } 54 } 55 }; 56 57 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) { 58 @Override 59 public void onChange(boolean selfChange) { 60 onAirplaneModeChanged(); 61 } 62 }; 63 64 public AirplaneModeEnabler(Context context, SwitchPreference airplaneModeSwitchPreference, 65 MetricsFeatureProvider metricsFeatureProvider) { 66 67 mContext = context; 68 mSwitchPref = airplaneModeSwitchPreference; 69 mMetricsFeatureProvider = metricsFeatureProvider; 70 71 airplaneModeSwitchPreference.setPersistent(false); 72 73 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler); 74 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED); 75 } 76 77 public void resume() { 78 79 mSwitchPref.setChecked(WirelessUtils.isAirplaneModeOn(mContext)); 80 81 mPhoneStateReceiver.registerIntent(); 82 mSwitchPref.setOnPreferenceChangeListener(this); 83 mContext.getContentResolver().registerContentObserver( 84 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true, 85 mAirplaneModeObserver); 86 } 87 88 public void pause() { 89 mPhoneStateReceiver.unregisterIntent(); 90 mSwitchPref.setOnPreferenceChangeListener(null); 91 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver); 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 mSwitchPref.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 mSwitchPref.setChecked(WirelessUtils.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 Boolean value = (Boolean) newValue; 128 mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_AIRPLANE_TOGGLE, value); 129 setAirplaneModeOn(value); 130 } 131 return true; 132 } 133 134 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) { 135 if (isECMExit) { 136 // update database based on the current checkbox state 137 setAirplaneModeOn(isAirplaneModeOn); 138 } else { 139 // update summary 140 onAirplaneModeChanged(); 141 } 142 } 143 144 } 145