• 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             mListeners = new ArrayList<>(2);
54         }
55         int activeModems = TelephonyManager.from(mContext).getActiveModemCount();
56         // Add new listeners if active modem count increased.
57         while (mListeners.size() < activeModems) {
58             mListeners.add(new RadioOnStateListener());
59         }
60         // Clean up listeners if active modem count decreased.
61         while (mListeners.size() > activeModems) {
62             mListeners.get(mListeners.size() - 1).cleanup();
63             mListeners.remove(mListeners.size() - 1);
64         }
65     }
66     /**
67      * Starts the "turn on radio" sequence. This is the (single) external API of the
68      * RadioOnHelper class.
69      *
70      * This method kicks off the following sequence:
71      * - Power on the radio for each Phone
72      * - Listen for radio events telling us the radio has come up.
73      * - Retry if we've gone a significant amount of time without any response from the radio.
74      * - Finally, clean up any leftover state.
75      *
76      * This method is safe to call from any thread, since it simply posts a message to the
77      * RadioOnHelper's handler (thus ensuring that the rest of the sequence is entirely
78      * serialized, and runs on the main looper.)
79      */
triggerRadioOnAndListen(RadioOnStateListener.Callback callback, boolean forEmergencyCall, Phone phoneForEmergencyCall, boolean isTestEmergencyNumber)80     public void triggerRadioOnAndListen(RadioOnStateListener.Callback callback,
81             boolean forEmergencyCall, Phone phoneForEmergencyCall, boolean isTestEmergencyNumber) {
82         setupListeners();
83         mCallback = callback;
84         mInProgressListeners.clear();
85         mIsRadioOnCallingEnabled = false;
86         for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); i++) {
87             Phone phone = PhoneFactory.getPhone(i);
88             if (phone == null) {
89                 continue;
90             }
91 
92             mInProgressListeners.add(mListeners.get(i));
93             mListeners.get(i).waitForRadioOn(phone, this, forEmergencyCall, forEmergencyCall
94                     && phone == phoneForEmergencyCall);
95         }
96         powerOnRadio(forEmergencyCall, phoneForEmergencyCall, isTestEmergencyNumber);
97     }
98     /**
99      * Attempt to power on the radio (i.e. take the device out of airplane mode). We'll eventually
100      * get an onServiceStateChanged() callback when the radio successfully comes up.
101      */
powerOnRadio(boolean forEmergencyCall, Phone phoneForEmergencyCall, boolean isTestEmergencyNumber)102     private void powerOnRadio(boolean forEmergencyCall, Phone phoneForEmergencyCall,
103             boolean isTestEmergencyNumber) {
104 
105         // Always try to turn on the radio here independent of APM setting - if we got here in the
106         // first place, the radio is off independent of APM setting.
107         for (Phone phone : PhoneFactory.getPhones()) {
108             Log.d(this, "powerOnRadio, enabling Radio");
109             if (isTestEmergencyNumber) {
110                 phone.setRadioPowerOnForTestEmergencyCall(phone == phoneForEmergencyCall);
111             } else {
112                 phone.setRadioPower(true, forEmergencyCall, phone == phoneForEmergencyCall,
113                         false);
114             }
115         }
116 
117         // If airplane mode is on, we turn it off the same way that the Settings activity turns it
118         // off to keep the setting in sync.
119         if (Settings.Global.getInt(mContext.getContentResolver(),
120                 Settings.Global.AIRPLANE_MODE_ON, 0) > 0) {
121             Log.d(this, "==> Turning off airplane mode for emergency call.");
122 
123             // Change the system setting
124             Settings.Global.putInt(mContext.getContentResolver(),
125                     Settings.Global.AIRPLANE_MODE_ON, 0);
126 
127             // Post the broadcast intend for change in airplane mode
128             // TODO: We really should not be in charge of sending this broadcast.
129             // If changing the setting is sufficient to trigger all of the rest of the logic,
130             // then that should also trigger the broadcast intent.
131             Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
132             intent.putExtra("state", false);
133             mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
134         }
135     }
136 
137     /**
138      * This method is called from multiple Listeners on the Main Looper.
139      * Synchronization is not necessary.
140      */
141     @Override
onComplete(RadioOnStateListener listener, boolean isRadioReady)142     public void onComplete(RadioOnStateListener listener, boolean isRadioReady) {
143         mIsRadioOnCallingEnabled |= isRadioReady;
144         mInProgressListeners.remove(listener);
145         if (mCallback != null && mInProgressListeners.isEmpty()) {
146             mCallback.onComplete(null, mIsRadioOnCallingEnabled);
147         }
148     }
149 
150     @Override
isOkToCall(Phone phone, int serviceState)151     public boolean isOkToCall(Phone phone, int serviceState) {
152         return (mCallback == null) ? false : mCallback.isOkToCall(phone, serviceState);
153     }
154 }
155