blob: 3c8276d9bfa8cf9f0565882835bf740f3df608e0 [file] [log] [blame]
Joe Onoratoa8e34182010-11-26 13:19:58 -08001/*
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
17package com.android.systemui.statusbar.policy;
18
19import android.content.BroadcastReceiver;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
Joe Onoratoc07d7c12011-01-16 13:10:11 -080024import android.os.AsyncTask;
Joe Onoratoa8e34182010-11-26 13:19:58 -080025import android.os.RemoteException;
26import android.os.ServiceManager;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070027import android.os.UserHandle;
Joe Onoratoa8e34182010-11-26 13:19:58 -080028import android.provider.Settings;
29import android.util.Slog;
30import android.widget.CompoundButton;
31
32public 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 Onoratoa8e34182010-11-26 13:19:58 -080059 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 Tatec09cdce2012-09-10 16:50:14 -070077 return 0 != Settings.Global.getInt(cr, Settings.Global.AIRPLANE_MODE_ON, 0);
Joe Onoratoa8e34182010-11-26 13:19:58 -080078 }
79
80 // TODO: Fix this racy API by adding something better to TelephonyManager or
81 // ConnectivityService.
Joe Onoratoc07d7c12011-01-16 13:10:11 -080082 private void unsafe(final boolean enabled) {
83 AsyncTask.execute(new Runnable() {
84 public void run() {
Christopher Tatec09cdce2012-09-10 16:50:14 -070085 Settings.Global.putInt(
Joe Onoratoc07d7c12011-01-16 13:10:11 -080086 mContext.getContentResolver(),
Christopher Tatec09cdce2012-09-10 16:50:14 -070087 Settings.Global.AIRPLANE_MODE_ON,
Joe Onoratoc07d7c12011-01-16 13:10:11 -080088 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 Hackborn5ac72a22012-08-29 18:32:08 -070092 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Joe Onoratoc07d7c12011-01-16 13:10:11 -080093 }
94 });
Joe Onoratoa8e34182010-11-26 13:19:58 -080095 }
96}
97