Home | History | Annotate | Download | only in settings
      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 android.content.Context;
     20 import android.content.Intent;
     21 import android.database.ContentObserver;
     22 import android.os.Handler;
     23 import android.os.Looper;
     24 import android.os.Message;
     25 import android.os.SystemProperties;
     26 import android.os.UserHandle;
     27 import android.provider.Settings;
     28 
     29 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     30 import com.android.internal.telephony.PhoneStateIntentReceiver;
     31 import com.android.internal.telephony.TelephonyProperties;
     32 import com.android.settingslib.WirelessUtils;
     33 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
     34 
     35 public class AirplaneModeEnabler {
     36 
     37     private static final int EVENT_SERVICE_STATE_CHANGED = 3;
     38 
     39     private final Context mContext;
     40     private final MetricsFeatureProvider mMetricsFeatureProvider;
     41 
     42     private PhoneStateIntentReceiver mPhoneStateReceiver;
     43 
     44     private OnAirplaneModeChangedListener mOnAirplaneModeChangedListener;
     45 
     46     public interface OnAirplaneModeChangedListener {
     47         /**
     48          * Called when airplane mode status is changed.
     49          *
     50          * @param isAirplaneModeOn the airplane mode is on
     51          */
     52         void onAirplaneModeChanged(boolean isAirplaneModeOn);
     53     }
     54 
     55     private Handler mHandler = new Handler(Looper.getMainLooper()) {
     56         @Override
     57         public void handleMessage(Message msg) {
     58             switch (msg.what) {
     59                 case EVENT_SERVICE_STATE_CHANGED:
     60                     onAirplaneModeChanged();
     61                     break;
     62             }
     63         }
     64     };
     65 
     66     private ContentObserver mAirplaneModeObserver = new ContentObserver(
     67             new Handler(Looper.getMainLooper())) {
     68         @Override
     69         public void onChange(boolean selfChange) {
     70             onAirplaneModeChanged();
     71         }
     72     };
     73 
     74     public AirplaneModeEnabler(Context context, MetricsFeatureProvider metricsFeatureProvider,
     75             OnAirplaneModeChangedListener listener) {
     76 
     77         mContext = context;
     78         mMetricsFeatureProvider = metricsFeatureProvider;
     79         mOnAirplaneModeChangedListener = listener;
     80 
     81         mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
     82         mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
     83     }
     84 
     85     public void resume() {
     86         mPhoneStateReceiver.registerIntent();
     87         mContext.getContentResolver().registerContentObserver(
     88                 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
     89                 mAirplaneModeObserver);
     90     }
     91 
     92     public void pause() {
     93         mPhoneStateReceiver.unregisterIntent();
     94         mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
     95     }
     96 
     97     private void setAirplaneModeOn(boolean enabling) {
     98         // Change the system setting
     99         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
    100                 enabling ? 1 : 0);
    101 
    102         // Notify listener the system setting is changed.
    103         if (mOnAirplaneModeChangedListener != null) {
    104             mOnAirplaneModeChangedListener.onAirplaneModeChanged(enabling);
    105         }
    106 
    107         // Post the intent
    108         Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    109         intent.putExtra("state", enabling);
    110         mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
    111     }
    112 
    113     /**
    114      * Called when we've received confirmation that the airplane mode was set.
    115      * TODO: We update the checkbox summary when we get notified
    116      * that mobile radio is powered up/down. We should not have dependency
    117      * on one radio alone. We need to do the following:
    118      * - handle the case of wifi/bluetooth failures
    119      * - mobile does not send failure notification, fail on timeout.
    120      */
    121     private void onAirplaneModeChanged() {
    122         if (mOnAirplaneModeChangedListener != null) {
    123             mOnAirplaneModeChangedListener.onAirplaneModeChanged(isAirplaneModeOn());
    124         }
    125     }
    126 
    127     public void setAirplaneMode(boolean isAirplaneModeOn) {
    128         if (Boolean.parseBoolean(
    129                 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
    130             // In ECM mode, do not update database at this point
    131         } else {
    132             mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_AIRPLANE_TOGGLE,
    133                     isAirplaneModeOn);
    134             setAirplaneModeOn(isAirplaneModeOn);
    135         }
    136     }
    137 
    138     public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
    139         if (isECMExit) {
    140             // update database based on the current checkbox state
    141             setAirplaneModeOn(isAirplaneModeOn);
    142         } else {
    143             // update summary
    144             onAirplaneModeChanged();
    145         }
    146     }
    147 
    148     public boolean isAirplaneModeOn() {
    149         return WirelessUtils.isAirplaneModeOn(mContext);
    150     }
    151 }
    152