Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 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.telecomm; |
| 18 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 19 | import android.app.AppOpsManager; |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Santos Cordon | fd86bd4 | 2014-07-19 14:59:04 -0700 | [diff] [blame] | 22 | |
| 23 | import android.content.Intent; |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 24 | import android.content.res.Resources; |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 25 | import android.os.Binder; |
Santos Cordon | fd86bd4 | 2014-07-19 14:59:04 -0700 | [diff] [blame] | 26 | import android.os.Bundle; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
Santos Cordon | fd86bd4 | 2014-07-19 14:59:04 -0700 | [diff] [blame] | 28 | import android.os.IBinder; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 29 | import android.os.Looper; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 30 | import android.os.Message; |
| 31 | import android.os.ServiceManager; |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 32 | import android.phone.PhoneManager; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 33 | import android.telecomm.CallState; |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 34 | import android.telecomm.PhoneAccount; |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 35 | import android.telecomm.PhoneAccountHandle; |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 36 | import android.telecomm.TelecommManager; |
| 37 | import android.telephony.TelephonyManager; |
| 38 | |
| 39 | import com.android.internal.telecomm.ITelecommService; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 40 | |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 41 | import java.util.List; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Implementation of the ITelecomm interface. |
| 45 | */ |
| 46 | public class TelecommServiceImpl extends ITelecommService.Stub { |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 47 | private static final String TELEPHONY_PACKAGE_NAME = "com.android.phone"; |
| 48 | |
Santos Cordon | fd86bd4 | 2014-07-19 14:59:04 -0700 | [diff] [blame] | 49 | /** ${inheritDoc} */ |
| 50 | @Override |
| 51 | public IBinder asBinder() { |
| 52 | return super.asBinder(); |
| 53 | } |
| 54 | |
| 55 | /** |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 56 | * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the |
| 57 | * request after sending. The main thread will notify the request when it is complete. |
| 58 | */ |
| 59 | private static final class MainThreadRequest { |
| 60 | /** The result of the request that is run on the main thread */ |
| 61 | public Object result; |
| 62 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * A handler that processes messages on the main thread in the phone process. Since many |
| 66 | * of the Phone calls are not thread safe this is needed to shuttle the requests from the |
| 67 | * inbound binder threads to the main thread in the phone process. |
| 68 | */ |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 69 | private final class MainThreadHandler extends Handler { |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 70 | @Override |
| 71 | public void handleMessage(Message msg) { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 72 | if (msg.obj instanceof MainThreadRequest) { |
| 73 | MainThreadRequest request = (MainThreadRequest) msg.obj; |
| 74 | Object result = null; |
| 75 | switch (msg.what) { |
| 76 | case MSG_SILENCE_RINGER: |
| 77 | mCallsManager.getRinger().silence(); |
| 78 | break; |
| 79 | case MSG_SHOW_CALL_SCREEN: |
| 80 | mCallsManager.getInCallController().bringToForeground(msg.arg1 == 1); |
| 81 | break; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 82 | case MSG_END_CALL: |
| 83 | result = endCallInternal(); |
| 84 | break; |
| 85 | case MSG_ACCEPT_RINGING_CALL: |
| 86 | acceptRingingCallInternal(); |
| 87 | break; |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 88 | case MSG_CANCEL_MISSED_CALLS_NOTIFICATION: |
| 89 | mMissedCallNotifier.clearMissedCalls(); |
| 90 | break; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 91 | case MSG_IS_TTY_SUPPORTED: |
| 92 | result = mCallsManager.isTtySupported(); |
| 93 | break; |
| 94 | case MSG_GET_CURRENT_TTY_MODE: |
| 95 | result = mCallsManager.getCurrentTtyMode(); |
| 96 | break; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | if (result != null) { |
| 100 | request.result = result; |
| 101 | synchronized(request) { |
| 102 | request.notifyAll(); |
| 103 | } |
| 104 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 107 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 108 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 109 | /** Private constructor; @see init() */ |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 110 | private static final String TAG = TelecommServiceImpl.class.getSimpleName(); |
| 111 | |
| 112 | private static final String SERVICE_NAME = "telecomm"; |
| 113 | |
| 114 | private static final int MSG_SILENCE_RINGER = 1; |
| 115 | private static final int MSG_SHOW_CALL_SCREEN = 2; |
Santos Cordon | 626697a | 2014-07-10 22:36:37 -0700 | [diff] [blame] | 116 | private static final int MSG_END_CALL = 3; |
| 117 | private static final int MSG_ACCEPT_RINGING_CALL = 4; |
| 118 | private static final int MSG_CANCEL_MISSED_CALLS_NOTIFICATION = 5; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 119 | private static final int MSG_IS_TTY_SUPPORTED = 6; |
| 120 | private static final int MSG_GET_CURRENT_TTY_MODE = 7; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 121 | |
| 122 | /** The singleton instance. */ |
| 123 | private static TelecommServiceImpl sInstance; |
| 124 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 125 | private final MainThreadHandler mMainThreadHandler = new MainThreadHandler(); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 126 | private final CallsManager mCallsManager = CallsManager.getInstance(); |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 127 | private final MissedCallNotifier mMissedCallNotifier; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 128 | private final PhoneAccountRegistrar mPhoneAccountRegistrar; |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 129 | private final AppOpsManager mAppOpsManager; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 130 | |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 131 | private TelecommServiceImpl( |
| 132 | MissedCallNotifier missedCallNotifier, PhoneAccountRegistrar phoneAccountRegistrar) { |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 133 | mMissedCallNotifier = missedCallNotifier; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 134 | mPhoneAccountRegistrar = phoneAccountRegistrar; |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 135 | mAppOpsManager = |
| 136 | (AppOpsManager) TelecommApp.getInstance().getSystemService(Context.APP_OPS_SERVICE); |
| 137 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 138 | publish(); |
| 139 | } |
| 140 | |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Initialize the singleton TelecommServiceImpl instance. |
| 143 | * This is only done once, at startup, from TelecommApp.onCreate(). |
| 144 | */ |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 145 | static TelecommServiceImpl init( |
| 146 | MissedCallNotifier missedCallNotifier, PhoneAccountRegistrar phoneAccountRegistrar) { |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 147 | synchronized (TelecommServiceImpl.class) { |
| 148 | if (sInstance == null) { |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 149 | sInstance = new TelecommServiceImpl(missedCallNotifier, phoneAccountRegistrar); |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 150 | } else { |
| 151 | Log.wtf(TAG, "init() called multiple times! sInstance %s", sInstance); |
| 152 | } |
| 153 | return sInstance; |
| 154 | } |
| 155 | } |
| 156 | |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 157 | // |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 158 | // Implementation of the ITelecommService interface. |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 159 | // |
| 160 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 161 | @Override |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 162 | public PhoneAccountHandle getDefaultOutgoingPhoneAccount() { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 163 | try { |
| 164 | return mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(); |
| 165 | } catch (Exception e) { |
| 166 | Log.e(this, e, "getDefaultOutgoingPhoneAccount"); |
| 167 | throw e; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 172 | public List<PhoneAccountHandle> getEnabledPhoneAccounts() { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 173 | try { |
| 174 | return mPhoneAccountRegistrar.getEnabledPhoneAccounts(); |
| 175 | } catch (Exception e) { |
| 176 | Log.e(this, e, "getEnabledPhoneAccounts"); |
| 177 | throw e; |
| 178 | } |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 182 | public PhoneAccount getPhoneAccount(PhoneAccountHandle accountHandle) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 183 | try { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 184 | return mPhoneAccountRegistrar.getPhoneAccount(accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 185 | } catch (Exception e) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 186 | Log.e(this, e, "getPhoneAccount %s", accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 187 | throw e; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 188 | } |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 192 | public void registerPhoneAccount(PhoneAccount account) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 193 | try { |
| 194 | enforceModifyPermissionOrCallingPackage( |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 195 | account.getAccountHandle().getComponentName().getPackageName()); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 196 | if (PhoneAccountRegistrar.has(account, PhoneAccount.CAPABILITY_CALL_PROVIDER) || |
| 197 | PhoneAccountRegistrar.has(account, PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 198 | enforceModifyPermissionOrCallingPackage(TELEPHONY_PACKAGE_NAME); |
| 199 | } |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 200 | mPhoneAccountRegistrar.registerPhoneAccount(account); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 201 | } catch (Exception e) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 202 | Log.e(this, e, "registerPhoneAccount %s", account); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 203 | throw e; |
| 204 | } |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 208 | public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 209 | try { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 210 | enforceModifyPermissionOrCallingPackage( |
| 211 | accountHandle.getComponentName().getPackageName()); |
| 212 | mPhoneAccountRegistrar.unregisterPhoneAccount(accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 213 | } catch (Exception e) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 214 | Log.e(this, e, "unregisterPhoneAccount %s", accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 215 | throw e; |
| 216 | } |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void clearAccounts(String packageName) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 221 | try { |
| 222 | enforceModifyPermissionOrCallingPackage(packageName); |
| 223 | mPhoneAccountRegistrar.clearAccounts(packageName); |
| 224 | } catch (Exception e) { |
| 225 | Log.e(this, e, "clearAccounts %s", packageName); |
| 226 | throw e; |
| 227 | } |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 230 | /** |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 231 | * @see TelecommManager#silenceRinger |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 232 | */ |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 233 | @Override |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 234 | public void silenceRinger() { |
| 235 | Log.d(this, "silenceRinger"); |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 236 | enforceModifyPermission(); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 237 | sendRequestAsync(MSG_SILENCE_RINGER, 0); |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 240 | /** |
| 241 | * @see TelecommManager#getDefaultPhoneApp |
| 242 | */ |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 243 | @Override |
| 244 | public ComponentName getDefaultPhoneApp() { |
| 245 | Resources resources = TelecommApp.getInstance().getResources(); |
| 246 | return new ComponentName( |
| 247 | resources.getString(R.string.ui_default_package), |
| 248 | resources.getString(R.string.dialer_default_class)); |
| 249 | } |
| 250 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 251 | /** |
| 252 | * @see TelecommManager#isInAPhoneCall |
| 253 | */ |
| 254 | @Override |
| 255 | public boolean isInAPhoneCall() { |
| 256 | enforceReadPermission(); |
Santos Cordon | 626697a | 2014-07-10 22:36:37 -0700 | [diff] [blame] | 257 | // Do not use sendRequest() with this method since it could cause a deadlock with |
| 258 | // audio service, which we call into from the main thread: AudioManager.setMode(). |
| 259 | return mCallsManager.hasAnyCalls(); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
| 263 | * @see TelecommManager#isRinging |
| 264 | */ |
| 265 | @Override |
| 266 | public boolean isRinging() { |
| 267 | enforceReadPermission(); |
Santos Cordon | 626697a | 2014-07-10 22:36:37 -0700 | [diff] [blame] | 268 | return mCallsManager.hasRingingCall(); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * @see TelecommManager#endCall |
| 273 | */ |
| 274 | @Override |
| 275 | public boolean endCall() { |
| 276 | enforceModifyPermission(); |
| 277 | return (boolean) sendRequest(MSG_END_CALL); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @see TelecommManager#acceptRingingCall |
| 282 | */ |
| 283 | @Override |
| 284 | public void acceptRingingCall() { |
| 285 | enforceModifyPermission(); |
| 286 | sendRequestAsync(MSG_ACCEPT_RINGING_CALL, 0); |
| 287 | } |
| 288 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 289 | /** |
| 290 | * @see PhoneManager#showCallScreen |
| 291 | */ |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 292 | @Override |
| 293 | public void showCallScreen(boolean showDialpad) { |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 294 | enforceReadPermissionOrDefaultDialer(); |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 295 | sendRequestAsync(MSG_SHOW_CALL_SCREEN, showDialpad ? 1 : 0); |
| 296 | } |
| 297 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 298 | /** |
| 299 | * @see PhoneManager#cancelMissedCallsNotification |
| 300 | */ |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 301 | @Override |
| 302 | public void cancelMissedCallsNotification() { |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 303 | enforceModifyPermissionOrDefaultDialer(); |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 304 | sendRequestAsync(MSG_CANCEL_MISSED_CALLS_NOTIFICATION, 0); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 307 | /** |
| 308 | * @see PhoneManager#handlePinMmi |
| 309 | */ |
| 310 | @Override |
| 311 | public boolean handlePinMmi(String dialString) { |
| 312 | enforceModifyPermissionOrDefaultDialer(); |
| 313 | |
| 314 | // Switch identity so that TelephonyManager checks Telecomm's permissions instead. |
| 315 | long token = Binder.clearCallingIdentity(); |
| 316 | boolean retval = getTelephonyManager().handlePinMmi(dialString); |
| 317 | Binder.restoreCallingIdentity(token); |
| 318 | |
| 319 | return retval; |
| 320 | } |
| 321 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 322 | /** |
| 323 | * @see TelecommManager#isTtySupported |
| 324 | */ |
| 325 | @Override |
| 326 | public boolean isTtySupported() { |
| 327 | enforceReadPermission(); |
| 328 | return (boolean) sendRequest(MSG_IS_TTY_SUPPORTED); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * @see TelecommManager#getCurrentTtyMode |
| 333 | */ |
| 334 | @Override |
| 335 | public int getCurrentTtyMode() { |
| 336 | enforceReadPermission(); |
| 337 | return (int) sendRequest(MSG_GET_CURRENT_TTY_MODE); |
| 338 | } |
| 339 | |
Santos Cordon | fd86bd4 | 2014-07-19 14:59:04 -0700 | [diff] [blame] | 340 | /** |
| 341 | * @see TelecommManager#addNewIncomingCall |
| 342 | */ |
| 343 | @Override |
| 344 | public void addNewIncomingCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) { |
| 345 | if (phoneAccountHandle != null && phoneAccountHandle.getComponentName() != null) { |
| 346 | mAppOpsManager.checkPackage( |
| 347 | Binder.getCallingUid(), phoneAccountHandle.getComponentName().getPackageName()); |
| 348 | |
| 349 | Intent intent = new Intent(TelecommManager.ACTION_INCOMING_CALL); |
| 350 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 351 | intent.putExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle); |
| 352 | if (extras != null) { |
| 353 | intent.putExtra(TelecommManager.EXTRA_INCOMING_CALL_EXTRAS, extras); |
| 354 | } |
| 355 | |
| 356 | long token = Binder.clearCallingIdentity(); |
| 357 | TelecommApp.getInstance().startActivity(intent); |
| 358 | Binder.restoreCallingIdentity(token); |
| 359 | } |
| 360 | } |
| 361 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 362 | // |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 363 | // Supporting methods for the ITelecommService interface implementation. |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 364 | // |
| 365 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 366 | private void acceptRingingCallInternal() { |
| 367 | Call call = mCallsManager.getFirstCallWithState(CallState.RINGING); |
| 368 | if (call != null) { |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 369 | call.answer(call.getVideoState()); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | private boolean endCallInternal() { |
| 374 | // Always operate on the foreground call if one exists, otherwise get the first call in |
| 375 | // priority order by call-state. |
| 376 | Call call = mCallsManager.getForegroundCall(); |
| 377 | if (call == null) { |
| 378 | call = mCallsManager.getFirstCallWithState( |
| 379 | CallState.ACTIVE, |
| 380 | CallState.DIALING, |
| 381 | CallState.RINGING, |
| 382 | CallState.ON_HOLD); |
| 383 | } |
| 384 | |
| 385 | if (call != null) { |
| 386 | if (call.getState() == CallState.RINGING) { |
| 387 | call.reject(false /* rejectWithMessage */, null); |
| 388 | } else { |
| 389 | call.disconnect(); |
| 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | return false; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Make sure the caller has the MODIFY_PHONE_STATE permission. |
| 399 | * |
| 400 | * @throws SecurityException if the caller does not have the required permission |
| 401 | */ |
| 402 | private void enforceModifyPermission() { |
| 403 | TelecommApp.getInstance().enforceCallingOrSelfPermission( |
| 404 | android.Manifest.permission.MODIFY_PHONE_STATE, null); |
| 405 | } |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 406 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 407 | private void enforceModifyPermissionOrDefaultDialer() { |
| 408 | if (!isDefaultDialerCalling()) { |
| 409 | enforceModifyPermission(); |
| 410 | } |
| 411 | } |
| 412 | |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 413 | private void enforceModifyPermissionOrCallingPackage(String packageName) { |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame^] | 414 | // TODO: Use a new telecomm permission for this instead of reusing modify. |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 415 | try { |
| 416 | enforceModifyPermission(); |
| 417 | } catch (SecurityException e) { |
| 418 | enforceCallingPackage(packageName); |
| 419 | } |
| 420 | } |
| 421 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 422 | private void enforceReadPermission() { |
| 423 | TelecommApp.getInstance().enforceCallingOrSelfPermission( |
| 424 | android.Manifest.permission.READ_PHONE_STATE, null); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 425 | } |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 426 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 427 | private void enforceReadPermissionOrDefaultDialer() { |
| 428 | if (!isDefaultDialerCalling()) { |
| 429 | enforceReadPermission(); |
| 430 | } |
| 431 | } |
| 432 | |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 433 | private void enforceCallingPackage(String packageName) { |
| 434 | mAppOpsManager.checkPackage(Binder.getCallingUid(), packageName); |
| 435 | } |
| 436 | |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 437 | private void showCallScreenInternal(boolean showDialpad) { |
| 438 | CallsManager.getInstance().getInCallController().bringToForeground(showDialpad); |
| 439 | } |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 440 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 441 | private boolean isDefaultDialerCalling() { |
| 442 | ComponentName defaultDialerComponent = getDefaultPhoneApp(); |
| 443 | if (defaultDialerComponent != null) { |
| 444 | try { |
| 445 | mAppOpsManager.checkPackage( |
| 446 | Binder.getCallingUid(), defaultDialerComponent.getPackageName()); |
| 447 | return true; |
| 448 | } catch (SecurityException e) { |
| 449 | Log.e(TAG, e, "Could not get default dialer."); |
| 450 | } |
| 451 | } |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | private TelephonyManager getTelephonyManager() { |
| 456 | return (TelephonyManager) |
| 457 | TelecommApp.getInstance().getSystemService(Context.TELEPHONY_SERVICE); |
| 458 | } |
| 459 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 460 | private void publish() { |
| 461 | Log.d(this, "publish: %s", this); |
| 462 | ServiceManager.addService(SERVICE_NAME, this); |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 463 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 464 | |
| 465 | private MainThreadRequest sendRequestAsync(int command, int arg1) { |
| 466 | MainThreadRequest request = new MainThreadRequest(); |
| 467 | mMainThreadHandler.obtainMessage(command, arg1, 0, request).sendToTarget(); |
| 468 | return request; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Posts the specified command to be executed on the main thread, waits for the request to |
| 473 | * complete, and returns the result. |
| 474 | */ |
| 475 | private Object sendRequest(int command) { |
| 476 | if (Looper.myLooper() == mMainThreadHandler.getLooper()) { |
Sailesh Nepal | bca199f | 2014-07-02 15:57:44 -0700 | [diff] [blame] | 477 | MainThreadRequest request = new MainThreadRequest(); |
| 478 | mMainThreadHandler.handleMessage(mMainThreadHandler.obtainMessage(command, request)); |
| 479 | return request.result; |
| 480 | } else { |
| 481 | MainThreadRequest request = sendRequestAsync(command, 0); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 482 | |
Sailesh Nepal | bca199f | 2014-07-02 15:57:44 -0700 | [diff] [blame] | 483 | // Wait for the request to complete |
| 484 | synchronized (request) { |
| 485 | while (request.result == null) { |
| 486 | try { |
| 487 | request.wait(); |
| 488 | } catch (InterruptedException e) { |
| 489 | // Do nothing, go back and wait until the request is complete |
| 490 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
Sailesh Nepal | bca199f | 2014-07-02 15:57:44 -0700 | [diff] [blame] | 493 | return request.result; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 494 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 495 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 496 | } |