Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.systemui.statusbar.policy; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Joe Onorato | c07d7c1 | 2011-01-16 13:10:11 -0800 | [diff] [blame] | 24 | import android.os.AsyncTask; |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 25 | import android.os.RemoteException; |
| 26 | import android.os.ServiceManager; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 27 | import android.os.UserHandle; |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 28 | import android.provider.Settings; |
| 29 | import android.util.Slog; |
| 30 | import android.widget.CompoundButton; |
| 31 | |
| 32 | public class AirplaneModeController extends BroadcastReceiver |
| 33 | implements CompoundButton.OnCheckedChangeListener { |
| 34 | private static final String TAG = "StatusBar.AirplaneModeController"; |
| 35 | |
| 36 | private Context mContext; |
| 37 | private CompoundButton mCheckBox; |
| 38 | |
| 39 | private boolean mAirplaneMode; |
| 40 | |
| 41 | public AirplaneModeController(Context context, CompoundButton checkbox) { |
| 42 | mContext = context; |
| 43 | mAirplaneMode = getAirplaneMode(); |
| 44 | mCheckBox = checkbox; |
| 45 | checkbox.setChecked(mAirplaneMode); |
| 46 | checkbox.setOnCheckedChangeListener(this); |
| 47 | |
| 48 | IntentFilter filter = new IntentFilter(); |
| 49 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 50 | context.registerReceiver(this, filter); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | public void release() { |
| 55 | mContext.unregisterReceiver(this); |
| 56 | } |
| 57 | |
| 58 | public void onCheckedChanged(CompoundButton view, boolean checked) { |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 59 | if (checked != mAirplaneMode) { |
| 60 | mAirplaneMode = checked; |
| 61 | unsafe(checked); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public void onReceive(Context context, Intent intent) { |
| 66 | if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) { |
| 67 | final boolean enabled = intent.getBooleanExtra("state", false); |
| 68 | if (enabled != mAirplaneMode) { |
| 69 | mAirplaneMode = enabled; |
| 70 | mCheckBox.setChecked(enabled); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private boolean getAirplaneMode() { |
| 76 | ContentResolver cr = mContext.getContentResolver(); |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 77 | return 0 != Settings.Global.getInt(cr, Settings.Global.AIRPLANE_MODE_ON, 0); |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // TODO: Fix this racy API by adding something better to TelephonyManager or |
| 81 | // ConnectivityService. |
Joe Onorato | c07d7c1 | 2011-01-16 13:10:11 -0800 | [diff] [blame] | 82 | private void unsafe(final boolean enabled) { |
| 83 | AsyncTask.execute(new Runnable() { |
| 84 | public void run() { |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 85 | Settings.Global.putInt( |
Joe Onorato | c07d7c1 | 2011-01-16 13:10:11 -0800 | [diff] [blame] | 86 | mContext.getContentResolver(), |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 87 | Settings.Global.AIRPLANE_MODE_ON, |
Joe Onorato | c07d7c1 | 2011-01-16 13:10:11 -0800 | [diff] [blame] | 88 | enabled ? 1 : 0); |
| 89 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 90 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 91 | intent.putExtra("state", enabled); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 92 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Joe Onorato | c07d7c1 | 2011-01-16 13:10:11 -0800 | [diff] [blame] | 93 | } |
| 94 | }); |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |