blob: f105712260fd7b2d7804460dd180d1d6cea01afa [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
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
17package com.android.settings;
18
19import com.android.internal.telephony.PhoneStateIntentReceiver;
20
21import android.content.Context;
22import android.content.Intent;
23import android.os.Handler;
24import android.os.Message;
25import android.preference.CheckBoxPreference;
26import android.preference.Preference;
27import android.provider.Settings;
28import android.telephony.ServiceState;
29
30public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
31
32 private final Context mContext;
33
34 private PhoneStateIntentReceiver mPhoneStateReceiver;
35
36 private final CheckBoxPreference mCheckBoxPref;
37
38 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
39
40 private Handler mHandler = new Handler() {
41 @Override
42 public void handleMessage(Message msg) {
43 switch (msg.what) {
44 case EVENT_SERVICE_STATE_CHANGED:
45 onAirplaneModeChanged();
46 break;
47 }
48 }
49 };
50
51 public AirplaneModeEnabler(Context context, CheckBoxPreference airplaneModeCheckBoxPreference) {
52
53 mContext = context;
54 mCheckBoxPref = airplaneModeCheckBoxPreference;
55
56 airplaneModeCheckBoxPreference.setPersistent(false);
57
58 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
59 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
60 }
61
62 public void resume() {
63
64 // This is the widget enabled state, not the preference toggled state
65 mCheckBoxPref.setEnabled(true);
66 mCheckBoxPref.setChecked(isAirplaneModeOn(mContext));
67
68 mPhoneStateReceiver.registerIntent();
69 mCheckBoxPref.setOnPreferenceChangeListener(this);
70 }
71
72 public void pause() {
73 mPhoneStateReceiver.unregisterIntent();
74 mCheckBoxPref.setOnPreferenceChangeListener(null);
75 }
76
77 static boolean isAirplaneModeOn(Context context) {
78 return Settings.System.getInt(context.getContentResolver(),
79 Settings.System.AIRPLANE_MODE_ON, 0) != 0;
80 }
81
82 private void setAirplaneModeOn(boolean enabling) {
83
84 mCheckBoxPref.setEnabled(false);
85 mCheckBoxPref.setSummary(enabling ? R.string.airplane_mode_turning_on
86 : R.string.airplane_mode_turning_off);
87
88 // Change the system setting
89 Settings.System.putInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
90 enabling ? 1 : 0);
91
92 // Post the intent
93 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
94 intent.putExtra("state", enabling);
95 mContext.sendBroadcast(intent);
96 }
97
98 /**
99 * Called when we've received confirmation that the airplane mode was set.
100 */
101 private void onAirplaneModeChanged() {
102 ServiceState serviceState = mPhoneStateReceiver.getServiceState();
103 boolean airplaneModeEnabled = serviceState.getState() == ServiceState.STATE_POWER_OFF;
104 mCheckBoxPref.setChecked(airplaneModeEnabled);
105 mCheckBoxPref.setSummary(airplaneModeEnabled ? null :
106 mContext.getString(R.string.airplane_mode_summary));
107 mCheckBoxPref.setEnabled(true);
108 }
109
110 /**
111 * Called when someone clicks on the checkbox preference.
112 */
113 public boolean onPreferenceChange(Preference preference, Object newValue) {
114 setAirplaneModeOn((Boolean) newValue);
115 return true;
116 }
117
118}