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