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
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;
24import android.os.AsyncTask;
25import android.os.RemoteException;
26import android.os.ServiceManager;
27import android.provider.Settings;
28import android.util.Slog;
29import android.widget.CompoundButton;
30
31public class AirplaneModeController extends BroadcastReceiver
32        implements CompoundButton.OnCheckedChangeListener {
33    private static final String TAG = "StatusBar.AirplaneModeController";
34
35    private Context mContext;
36    private CompoundButton mCheckBox;
37
38    private boolean mAirplaneMode;
39
40    public AirplaneModeController(Context context, CompoundButton checkbox) {
41        mContext = context;
42        mAirplaneMode = getAirplaneMode();
43        mCheckBox = checkbox;
44        checkbox.setChecked(mAirplaneMode);
45        checkbox.setOnCheckedChangeListener(this);
46
47        IntentFilter filter = new IntentFilter();
48        filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
49        context.registerReceiver(this, filter);
50
51    }
52
53    public void release() {
54        mContext.unregisterReceiver(this);
55    }
56
57    public void onCheckedChanged(CompoundButton view, boolean checked) {
58        if (checked != mAirplaneMode) {
59            mAirplaneMode = checked;
60            unsafe(checked);
61        }
62    }
63
64    public void onReceive(Context context, Intent intent) {
65        if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
66            final boolean enabled = intent.getBooleanExtra("state", false);
67            if (enabled != mAirplaneMode) {
68                mAirplaneMode = enabled;
69                mCheckBox.setChecked(enabled);
70            }
71        }
72    }
73
74    private boolean getAirplaneMode() {
75        ContentResolver cr = mContext.getContentResolver();
76        return 0 != Settings.System.getInt(cr, Settings.System.AIRPLANE_MODE_ON, 0);
77    }
78
79    // TODO: Fix this racy API by adding something better to TelephonyManager or
80    // ConnectivityService.
81    private void unsafe(final boolean enabled) {
82        AsyncTask.execute(new Runnable() {
83                public void run() {
84                    Settings.System.putInt(
85                            mContext.getContentResolver(),
86                            Settings.System.AIRPLANE_MODE_ON,
87                            enabled ? 1 : 0);
88                    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
89                    intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
90                    intent.putExtra("state", enabled);
91                    mContext.sendBroadcast(intent);
92                }
93            });
94    }
95}
96
97