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