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