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