• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.services.telephony;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.UserHandle;
22 import android.provider.Settings;
23 import android.telephony.TelephonyManager;
24 
25 import com.android.internal.telephony.Phone;
26 import com.android.internal.telephony.PhoneFactory;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /**
32  * Helper class that implements special behavior related to emergency calls or making phone calls
33  * when the radio is in the POWER_OFF STATE. Specifically, this class handles the case of the user
34  * trying to dial an emergency number while the radio is off (i.e. the device is in airplane mode)
35  * or a normal number while the radio is off (because of the device is on Bluetooth), by turning the
36  * radio back on, waiting for it to come up, and then retrying the call.
37  */
38 public class RadioOnHelper implements RadioOnStateListener.Callback {
39 
40     private final Context mContext;
41     private RadioOnStateListener.Callback mCallback;
42     private List<RadioOnStateListener> mListeners;
43     private List<RadioOnStateListener> mInProgressListeners;
44     private boolean mIsRadioOnCallingEnabled;
45 
RadioOnHelper(Context context)46     public RadioOnHelper(Context context) {
47         mContext = context;
48         mInProgressListeners = new ArrayList<>(2);
49     }
50 
setupListeners()51     private void setupListeners() {
52         if (mListeners != null) {
53             return;
54         }
55         mListeners = new ArrayList<>(2);
56         for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
57             mListeners.add(new RadioOnStateListener());
58         }
59     }
60     /**
61      * Starts the "turn on radio" sequence. This is the (single) external API of the
62      * RadioOnHelper class.
63      *
64      * This method kicks off the following sequence:
65      * - Power on the radio for each Phone
66      * - Listen for radio events telling us the radio has come up.
67      * - Retry if we've gone a significant amount of time without any response from the radio.
68      * - Finally, clean up any leftover state.
69      *
70      * This method is safe to call from any thread, since it simply posts a message to the
71      * RadioOnHelper's handler (thus ensuring that the rest of the sequence is entirely
72      * serialized, and runs on the main looper.)
73      */
triggerRadioOnAndListen(RadioOnStateListener.Callback callback)74     public void triggerRadioOnAndListen(RadioOnStateListener.Callback callback) {
75         setupListeners();
76         mCallback = callback;
77         mInProgressListeners.clear();
78         mIsRadioOnCallingEnabled = false;
79         for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
80             Phone phone = PhoneFactory.getPhone(i);
81             if (phone == null) {
82                 continue;
83             }
84 
85             mInProgressListeners.add(mListeners.get(i));
86             mListeners.get(i).waitForRadioOn(phone, this);
87         }
88 
89         powerOnRadio();
90     }
91     /**
92      * Attempt to power on the radio (i.e. take the device out of airplane mode). We'll eventually
93      * get an onServiceStateChanged() callback when the radio successfully comes up.
94      */
powerOnRadio()95     private void powerOnRadio() {
96 
97         // If airplane mode is on, we turn it off the same way that the Settings activity turns it
98         // off.
99         if (Settings.Global.getInt(mContext.getContentResolver(),
100                 Settings.Global.AIRPLANE_MODE_ON, 0) > 0) {
101             Log.d(this, "==> Turning off airplane mode for emergency call.");
102 
103             // Change the system setting
104             Settings.Global.putInt(mContext.getContentResolver(),
105                     Settings.Global.AIRPLANE_MODE_ON, 0);
106 
107             // Post the broadcast intend for change in airplane mode
108             // TODO: We really should not be in charge of sending this broadcast.
109             // If changing the setting is sufficient to trigger all of the rest of the logic,
110             // then that should also trigger the broadcast intent.
111             Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
112             intent.putExtra("state", false);
113             mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
114         }
115     }
116 
117     /**
118      * This method is called from multiple Listeners on the Main Looper.
119      * Synchronization is not necessary.
120      */
121     @Override
onComplete(RadioOnStateListener listener, boolean isRadioReady)122     public void onComplete(RadioOnStateListener listener, boolean isRadioReady) {
123         mIsRadioOnCallingEnabled |= isRadioReady;
124         mInProgressListeners.remove(listener);
125         if (mCallback != null && mInProgressListeners.isEmpty()) {
126             mCallback.onComplete(null, mIsRadioOnCallingEnabled);
127         }
128     }
129 
130     @Override
isOkToCall(Phone phone, int serviceState)131     public boolean isOkToCall(Phone phone, int serviceState) {
132         return (mCallback == null) ? false : mCallback.isOkToCall(phone, serviceState);
133     }
134 }
135