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