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)80 public void triggerRadioOnAndListen(RadioOnStateListener.Callback callback, 81 boolean forEmergencyCall, Phone phoneForEmergencyCall) { 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, 94 forEmergencyCall && phone == phoneForEmergencyCall); 95 } 96 97 powerOnRadio(forEmergencyCall, phoneForEmergencyCall); 98 } 99 /** 100 * Attempt to power on the radio (i.e. take the device out of airplane mode). We'll eventually 101 * get an onServiceStateChanged() callback when the radio successfully comes up. 102 */ powerOnRadio(boolean forEmergencyCall, Phone phoneForEmergencyCall)103 private void powerOnRadio(boolean forEmergencyCall, Phone phoneForEmergencyCall) { 104 105 // If airplane mode is on, we turn it off the same way that the Settings activity turns it 106 // off. 107 if (Settings.Global.getInt(mContext.getContentResolver(), 108 Settings.Global.AIRPLANE_MODE_ON, 0) > 0) { 109 Log.d(this, "==> Turning off airplane mode for emergency call."); 110 111 // Change the system setting 112 Settings.Global.putInt(mContext.getContentResolver(), 113 Settings.Global.AIRPLANE_MODE_ON, 0); 114 115 for (Phone phone : PhoneFactory.getPhones()) { 116 Log.d(this, "powerOnRadio, enabling Radio"); 117 phone.setRadioPower(true, forEmergencyCall, phone == phoneForEmergencyCall, false); 118 } 119 120 // Post the broadcast intend for change in airplane mode 121 // TODO: We really should not be in charge of sending this broadcast. 122 // If changing the setting is sufficient to trigger all of the rest of the logic, 123 // then that should also trigger the broadcast intent. 124 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 125 intent.putExtra("state", false); 126 mContext.sendBroadcastAsUser(intent, UserHandle.ALL); 127 } 128 } 129 130 /** 131 * This method is called from multiple Listeners on the Main Looper. 132 * Synchronization is not necessary. 133 */ 134 @Override onComplete(RadioOnStateListener listener, boolean isRadioReady)135 public void onComplete(RadioOnStateListener listener, boolean isRadioReady) { 136 mIsRadioOnCallingEnabled |= isRadioReady; 137 mInProgressListeners.remove(listener); 138 if (mCallback != null && mInProgressListeners.isEmpty()) { 139 mCallback.onComplete(null, mIsRadioOnCallingEnabled); 140 } 141 } 142 143 @Override isOkToCall(Phone phone, int serviceState)144 public boolean isOkToCall(Phone phone, int serviceState) { 145 return (mCallback == null) ? false : mCallback.isOkToCall(phone, serviceState); 146 } 147 } 148