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