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 | |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 19 | import com.google.android.collect.Lists; |
| 20 | |
| 21 | import com.android.internal.telecomm.ITelecommService; |
| 22 | |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 25 | import android.content.res.Resources; |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 28 | import android.os.Looper; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 29 | import android.os.Message; |
| 30 | import android.os.ServiceManager; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 31 | import android.telecomm.CallState; |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 32 | import android.text.TextUtils; |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 33 | import android.telecomm.PhoneAccount; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 34 | |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 35 | import java.util.List; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Implementation of the ITelecomm interface. |
| 39 | */ |
| 40 | public class TelecommServiceImpl extends ITelecommService.Stub { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 41 | /** |
| 42 | * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the |
| 43 | * request after sending. The main thread will notify the request when it is complete. |
| 44 | */ |
| 45 | private static final class MainThreadRequest { |
| 46 | /** The result of the request that is run on the main thread */ |
| 47 | public Object result; |
| 48 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * A handler that processes messages on the main thread in the phone process. Since many |
| 52 | * of the Phone calls are not thread safe this is needed to shuttle the requests from the |
| 53 | * inbound binder threads to the main thread in the phone process. |
| 54 | */ |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 55 | private final class MainThreadHandler extends Handler { |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 56 | @Override |
| 57 | public void handleMessage(Message msg) { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 58 | if (msg.obj instanceof MainThreadRequest) { |
| 59 | MainThreadRequest request = (MainThreadRequest) msg.obj; |
| 60 | Object result = null; |
| 61 | switch (msg.what) { |
| 62 | case MSG_SILENCE_RINGER: |
| 63 | mCallsManager.getRinger().silence(); |
| 64 | break; |
| 65 | case MSG_SHOW_CALL_SCREEN: |
| 66 | mCallsManager.getInCallController().bringToForeground(msg.arg1 == 1); |
| 67 | break; |
| 68 | case MSG_IS_IN_A_PHONE_CALL: |
| 69 | result = mCallsManager.hasAnyCalls(); |
| 70 | break; |
| 71 | case MSG_IS_RINGING: |
| 72 | result = mCallsManager.hasRingingCall(); |
| 73 | break; |
| 74 | case MSG_END_CALL: |
| 75 | result = endCallInternal(); |
| 76 | break; |
| 77 | case MSG_ACCEPT_RINGING_CALL: |
| 78 | acceptRingingCallInternal(); |
| 79 | break; |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 80 | case MSG_CANCEL_MISSED_CALLS_NOTIFICATION: |
| 81 | mMissedCallNotifier.clearMissedCalls(); |
| 82 | break; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (result != null) { |
| 86 | request.result = result; |
| 87 | synchronized(request) { |
| 88 | request.notifyAll(); |
| 89 | } |
| 90 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 93 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 94 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 95 | /** Private constructor; @see init() */ |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 96 | private static final String TAG = TelecommServiceImpl.class.getSimpleName(); |
| 97 | |
| 98 | private static final String SERVICE_NAME = "telecomm"; |
| 99 | |
| 100 | private static final int MSG_SILENCE_RINGER = 1; |
| 101 | private static final int MSG_SHOW_CALL_SCREEN = 2; |
| 102 | private static final int MSG_IS_IN_A_PHONE_CALL = 3; |
| 103 | private static final int MSG_IS_RINGING = 4; |
| 104 | private static final int MSG_END_CALL = 5; |
| 105 | private static final int MSG_ACCEPT_RINGING_CALL = 6; |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 106 | private static final int MSG_CANCEL_MISSED_CALLS_NOTIFICATION = 7; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 107 | |
| 108 | /** The singleton instance. */ |
| 109 | private static TelecommServiceImpl sInstance; |
| 110 | |
| 111 | private final CallsManager mCallsManager = CallsManager.getInstance(); |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 112 | private final MissedCallNotifier mMissedCallNotifier; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 113 | private final MainThreadHandler mMainThreadHandler = new MainThreadHandler(); |
| 114 | |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 115 | private TelecommServiceImpl(MissedCallNotifier missedCallNotifier) { |
| 116 | mMissedCallNotifier = missedCallNotifier; |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 117 | publish(); |
| 118 | } |
| 119 | |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Initialize the singleton TelecommServiceImpl instance. |
| 122 | * This is only done once, at startup, from TelecommApp.onCreate(). |
| 123 | */ |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 124 | static TelecommServiceImpl init(MissedCallNotifier missedCallNotifier) { |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 125 | synchronized (TelecommServiceImpl.class) { |
| 126 | if (sInstance == null) { |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 127 | sInstance = new TelecommServiceImpl(missedCallNotifier); |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 128 | } else { |
| 129 | Log.wtf(TAG, "init() called multiple times! sInstance %s", sInstance); |
| 130 | } |
| 131 | return sInstance; |
| 132 | } |
| 133 | } |
| 134 | |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 135 | // |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 136 | // Implementation of the ITelecommService interface. |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 137 | // |
| 138 | |
| 139 | @Override |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 140 | public List<PhoneAccount> getAccounts() { |
| 141 | // TODO (STOPSHIP): Static list of Accounts for testing and UX work only. |
| 142 | ComponentName componentName = new ComponentName( |
| 143 | "com.android.telecomm", |
| 144 | TelecommServiceImpl.class.getName()); // This field is a no-op |
| 145 | Context app = TelecommApp.getInstance(); |
| 146 | |
| 147 | return Lists.newArrayList( |
| 148 | new PhoneAccount( |
| 149 | componentName, |
| 150 | "account0", |
| 151 | Uri.parse("tel:999-555-1212"), |
| 152 | app.getString(R.string.test_account_0_label), |
| 153 | app.getString(R.string.test_account_0_short_description), |
| 154 | true, |
| 155 | true), |
| 156 | new PhoneAccount( |
| 157 | componentName, |
| 158 | "account1", |
| 159 | Uri.parse("tel:333-111-2222"), |
| 160 | app.getString(R.string.test_account_1_label), |
| 161 | app.getString(R.string.test_account_1_short_description), |
| 162 | true, |
| 163 | false), |
| 164 | new PhoneAccount( |
| 165 | componentName, |
| 166 | "account2", |
| 167 | Uri.parse("mailto:two@example.com"), |
| 168 | app.getString(R.string.test_account_2_label), |
| 169 | app.getString(R.string.test_account_2_short_description), |
| 170 | true, |
| 171 | false), |
| 172 | new PhoneAccount( |
| 173 | componentName, |
| 174 | "account3", |
| 175 | Uri.parse("mailto:three@example.com"), |
| 176 | app.getString(R.string.test_account_3_label), |
| 177 | app.getString(R.string.test_account_3_short_description), |
| 178 | true, |
| 179 | false) |
| 180 | ); |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 184 | public void setEnabled(PhoneAccount account, boolean enabled) { |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 185 | // Enforce MODIFY_PHONE_STATE ? |
| 186 | // TODO |
| 187 | } |
| 188 | |
| 189 | @Override |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 190 | public void setSystemDefault(PhoneAccount account) { |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 191 | // Enforce MODIFY_PHONE_STATE ? |
| 192 | // TODO |
| 193 | } |
| 194 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 195 | /** |
| 196 | * @see TelecommManager#silenceringer |
| 197 | */ |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 198 | @Override |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 199 | public void silenceRinger() { |
| 200 | Log.d(this, "silenceRinger"); |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 201 | enforceModifyPermission(); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 202 | sendRequestAsync(MSG_SILENCE_RINGER, 0); |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 205 | /** |
| 206 | * @see TelecommManager#getDefaultPhoneApp |
| 207 | */ |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 208 | @Override |
| 209 | public ComponentName getDefaultPhoneApp() { |
| 210 | Resources resources = TelecommApp.getInstance().getResources(); |
| 211 | return new ComponentName( |
| 212 | resources.getString(R.string.ui_default_package), |
| 213 | resources.getString(R.string.dialer_default_class)); |
| 214 | } |
| 215 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 216 | /** |
| 217 | * @see TelecommManager#isInAPhoneCall |
| 218 | */ |
| 219 | @Override |
| 220 | public boolean isInAPhoneCall() { |
| 221 | enforceReadPermission(); |
| 222 | return (boolean) sendRequest(MSG_IS_IN_A_PHONE_CALL); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * @see TelecommManager#isRinging |
| 227 | */ |
| 228 | @Override |
| 229 | public boolean isRinging() { |
| 230 | enforceReadPermission(); |
| 231 | return (boolean) sendRequest(MSG_IS_RINGING); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @see TelecommManager#endCall |
| 236 | */ |
| 237 | @Override |
| 238 | public boolean endCall() { |
| 239 | enforceModifyPermission(); |
| 240 | return (boolean) sendRequest(MSG_END_CALL); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @see TelecommManager#acceptRingingCall |
| 245 | */ |
| 246 | @Override |
| 247 | public void acceptRingingCall() { |
| 248 | enforceModifyPermission(); |
| 249 | sendRequestAsync(MSG_ACCEPT_RINGING_CALL, 0); |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void showCallScreen(boolean showDialpad) { |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 254 | sendRequestAsync(MSG_SHOW_CALL_SCREEN, showDialpad ? 1 : 0); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void cancelMissedCallsNotification() { |
| 259 | sendRequestAsync(MSG_CANCEL_MISSED_CALLS_NOTIFICATION, 0); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 262 | // |
| 263 | // Supporting methods for the ITelephony interface implementation. |
| 264 | // |
| 265 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 266 | private void acceptRingingCallInternal() { |
| 267 | Call call = mCallsManager.getFirstCallWithState(CallState.RINGING); |
| 268 | if (call != null) { |
| 269 | call.answer(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | private boolean endCallInternal() { |
| 274 | // Always operate on the foreground call if one exists, otherwise get the first call in |
| 275 | // priority order by call-state. |
| 276 | Call call = mCallsManager.getForegroundCall(); |
| 277 | if (call == null) { |
| 278 | call = mCallsManager.getFirstCallWithState( |
| 279 | CallState.ACTIVE, |
| 280 | CallState.DIALING, |
| 281 | CallState.RINGING, |
| 282 | CallState.ON_HOLD); |
| 283 | } |
| 284 | |
| 285 | if (call != null) { |
| 286 | if (call.getState() == CallState.RINGING) { |
| 287 | call.reject(false /* rejectWithMessage */, null); |
| 288 | } else { |
| 289 | call.disconnect(); |
| 290 | } |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | return false; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Make sure the caller has the MODIFY_PHONE_STATE permission. |
| 299 | * |
| 300 | * @throws SecurityException if the caller does not have the required permission |
| 301 | */ |
| 302 | private void enforceModifyPermission() { |
| 303 | TelecommApp.getInstance().enforceCallingOrSelfPermission( |
| 304 | android.Manifest.permission.MODIFY_PHONE_STATE, null); |
| 305 | } |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 306 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 307 | private void enforceReadPermission() { |
| 308 | TelecommApp.getInstance().enforceCallingOrSelfPermission( |
| 309 | android.Manifest.permission.READ_PHONE_STATE, null); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 310 | } |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 311 | |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 312 | private void showCallScreenInternal(boolean showDialpad) { |
| 313 | CallsManager.getInstance().getInCallController().bringToForeground(showDialpad); |
| 314 | } |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 315 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 316 | private void publish() { |
| 317 | Log.d(this, "publish: %s", this); |
| 318 | ServiceManager.addService(SERVICE_NAME, this); |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 319 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 320 | |
| 321 | private MainThreadRequest sendRequestAsync(int command, int arg1) { |
| 322 | MainThreadRequest request = new MainThreadRequest(); |
| 323 | mMainThreadHandler.obtainMessage(command, arg1, 0, request).sendToTarget(); |
| 324 | return request; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Posts the specified command to be executed on the main thread, waits for the request to |
| 329 | * complete, and returns the result. |
| 330 | */ |
| 331 | private Object sendRequest(int command) { |
| 332 | if (Looper.myLooper() == mMainThreadHandler.getLooper()) { |
Sailesh Nepal | bca199f | 2014-07-02 15:57:44 -0700 | [diff] [blame] | 333 | MainThreadRequest request = new MainThreadRequest(); |
| 334 | mMainThreadHandler.handleMessage(mMainThreadHandler.obtainMessage(command, request)); |
| 335 | return request.result; |
| 336 | } else { |
| 337 | MainThreadRequest request = sendRequestAsync(command, 0); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 338 | |
Sailesh Nepal | bca199f | 2014-07-02 15:57:44 -0700 | [diff] [blame] | 339 | // Wait for the request to complete |
| 340 | synchronized (request) { |
| 341 | while (request.result == null) { |
| 342 | try { |
| 343 | request.wait(); |
| 344 | } catch (InterruptedException e) { |
| 345 | // Do nothing, go back and wait until the request is complete |
| 346 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
Sailesh Nepal | bca199f | 2014-07-02 15:57:44 -0700 | [diff] [blame] | 349 | return request.result; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 350 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 351 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 352 | } |