OSDN Git Service

am 2d68f1b5: (-s ours) am 54e21852: Merge "Trim unwanted whitespace from translations...
[android-x86/packages-apps-Settings.git] / src / com / android / settings / AirplaneModeEnabler.java
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
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
65     public void resume() {
66         
67         mCheckBoxPref.setChecked(isAirplaneModeOn(mContext));
68
69         mPhoneStateReceiver.registerIntent();
70         mCheckBoxPref.setOnPreferenceChangeListener(this);
71     }
72     
73     public void pause() {
74         mPhoneStateReceiver.unregisterIntent();
75         mCheckBoxPref.setOnPreferenceChangeListener(null);
76     }
77     
78     public static boolean isAirplaneModeOn(Context context) {
79         return Settings.System.getInt(context.getContentResolver(),
80                 Settings.System.AIRPLANE_MODE_ON, 0) != 0;
81     }
82
83     private void setAirplaneModeOn(boolean enabling) {
84         
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         // Update the UI to reflect system setting
92         mCheckBoxPref.setChecked(enabling);
93         
94         // Post the intent
95         Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
96         intent.putExtra("state", enabling);
97         mContext.sendBroadcast(intent);
98     }
99
100     /**
101      * Called when we've received confirmation that the airplane mode was set.
102      * TODO: We update the checkbox summary when we get notified
103      * that mobile radio is powered up/down. We should not have dependency
104      * on one radio alone. We need to do the following:
105      * - handle the case of wifi/bluetooth failures
106      * - mobile does not send failure notification, fail on timeout.
107      */
108     private void onAirplaneModeChanged() {
109         ServiceState serviceState = mPhoneStateReceiver.getServiceState();
110         boolean airplaneModeEnabled = serviceState.getState() == ServiceState.STATE_POWER_OFF;
111         mCheckBoxPref.setSummary(airplaneModeEnabled ? null : 
112                 mContext.getString(R.string.airplane_mode_summary));            
113     }
114     
115     /**
116      * Called when someone clicks on the checkbox preference.
117      */
118     public boolean onPreferenceChange(Preference preference, Object newValue) {
119         if (Boolean.parseBoolean(
120                     SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
121             // In ECM mode, do not update database at this point
122         } else {
123             setAirplaneModeOn((Boolean) newValue);
124         }
125         return true;
126     }
127
128     public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
129         if (isECMExit) {
130             // update database based on the current checkbox state
131             setAirplaneModeOn(isAirplaneModeOn);
132         } else {
133             // update summary
134             onAirplaneModeChanged();
135         }
136     }
137
138 }