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.os.UserHandle; 28import 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) { 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(); 77 return 0 != Settings.Global.getInt(cr, Settings.Global.AIRPLANE_MODE_ON, 0); 78 } 79 80 // TODO: Fix this racy API by adding something better to TelephonyManager or 81 // ConnectivityService. 82 private void unsafe(final boolean enabled) { 83 AsyncTask.execute(new Runnable() { 84 public void run() { 85 Settings.Global.putInt( 86 mContext.getContentResolver(), 87 Settings.Global.AIRPLANE_MODE_ON, 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); 92 mContext.sendBroadcastAsUser(intent, UserHandle.ALL); 93 } 94 }); 95 } 96} 97 98