TvInputManagerService.java revision e17b2dd7bcc137bf4d842a779e8d62c63957a978
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 17package com.android.server.tv; 18 19import android.app.ActivityManager; 20import android.content.BroadcastReceiver; 21import android.content.ComponentName; 22import android.content.ContentResolver; 23import android.content.ContentUris; 24import android.content.ContentValues; 25import android.content.Context; 26import android.content.Intent; 27import android.content.IntentFilter; 28import android.content.ServiceConnection; 29import android.content.pm.PackageManager; 30import android.content.pm.ResolveInfo; 31import android.content.pm.ServiceInfo; 32import android.database.Cursor; 33import android.graphics.Rect; 34import android.media.tv.ITvInputClient; 35import android.media.tv.ITvInputHardware; 36import android.media.tv.ITvInputHardwareCallback; 37import android.media.tv.ITvInputManager; 38import android.media.tv.ITvInputService; 39import android.media.tv.ITvInputServiceCallback; 40import android.media.tv.ITvInputSession; 41import android.media.tv.ITvInputSessionCallback; 42import android.media.tv.TvContract; 43import android.media.tv.TvInputHardwareInfo; 44import android.media.tv.TvInputInfo; 45import android.media.tv.TvInputService; 46import android.net.Uri; 47import android.os.Binder; 48import android.os.Bundle; 49import android.os.Handler; 50import android.os.IBinder; 51import android.os.Looper; 52import android.os.Message; 53import android.os.Process; 54import android.os.RemoteException; 55import android.os.UserHandle; 56import android.util.Slog; 57import android.util.SparseArray; 58import android.view.InputChannel; 59import android.view.Surface; 60 61import com.android.internal.content.PackageMonitor; 62import com.android.internal.os.SomeArgs; 63import com.android.server.IoThread; 64import com.android.server.SystemService; 65 66import org.xmlpull.v1.XmlPullParserException; 67 68import java.io.IOException; 69 70import java.util.ArrayList; 71import java.util.HashMap; 72import java.util.List; 73import java.util.Map; 74 75/** This class provides a system service that manages television inputs. */ 76public final class TvInputManagerService extends SystemService { 77 // STOPSHIP: Turn debugging off. 78 private static final boolean DEBUG = true; 79 private static final String TAG = "TvInputManagerService"; 80 81 private final Context mContext; 82 private final TvInputHardwareManager mTvInputHardwareManager; 83 84 private final ContentResolver mContentResolver; 85 86 // A global lock. 87 private final Object mLock = new Object(); 88 89 // ID of the current user. 90 private int mCurrentUserId = UserHandle.USER_OWNER; 91 92 // A map from user id to UserState. 93 private final SparseArray<UserState> mUserStates = new SparseArray<UserState>(); 94 95 private final Handler mLogHandler; 96 97 public TvInputManagerService(Context context) { 98 super(context); 99 100 mContext = context; 101 mContentResolver = context.getContentResolver(); 102 mLogHandler = new LogHandler(IoThread.get().getLooper()); 103 104 mTvInputHardwareManager = new TvInputHardwareManager(context); 105 registerBroadcastReceivers(); 106 107 synchronized (mLock) { 108 mUserStates.put(mCurrentUserId, new UserState()); 109 buildTvInputListLocked(mCurrentUserId); 110 } 111 } 112 113 @Override 114 public void onStart() { 115 publishBinderService(Context.TV_INPUT_SERVICE, new BinderService()); 116 } 117 118 private void registerBroadcastReceivers() { 119 PackageMonitor monitor = new PackageMonitor() { 120 @Override 121 public void onSomePackagesChanged() { 122 synchronized (mLock) { 123 buildTvInputListLocked(mCurrentUserId); 124 } 125 } 126 }; 127 monitor.register(mContext, null, UserHandle.ALL, true); 128 129 IntentFilter intentFilter = new IntentFilter(); 130 intentFilter.addAction(Intent.ACTION_USER_SWITCHED); 131 intentFilter.addAction(Intent.ACTION_USER_REMOVED); 132 mContext.registerReceiverAsUser(new BroadcastReceiver() { 133 @Override 134 public void onReceive(Context context, Intent intent) { 135 String action = intent.getAction(); 136 if (Intent.ACTION_USER_SWITCHED.equals(action)) { 137 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); 138 } else if (Intent.ACTION_USER_REMOVED.equals(action)) { 139 removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); 140 } 141 } 142 }, UserHandle.ALL, intentFilter, null, null); 143 } 144 145 private void buildTvInputListLocked(int userId) { 146 UserState userState = getUserStateLocked(userId); 147 userState.inputMap.clear(); 148 149 if (DEBUG) Slog.d(TAG, "buildTvInputList"); 150 PackageManager pm = mContext.getPackageManager(); 151 List<ResolveInfo> services = pm.queryIntentServices( 152 new Intent(TvInputService.SERVICE_INTERFACE), 153 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA); 154 for (ResolveInfo ri : services) { 155 ServiceInfo si = ri.serviceInfo; 156 if (!android.Manifest.permission.BIND_TV_INPUT.equals(si.permission)) { 157 Slog.w(TAG, "Skipping TV input " + si.name + ": it does not require the permission " 158 + android.Manifest.permission.BIND_TV_INPUT); 159 continue; 160 } 161 try { 162 TvInputInfo info = TvInputInfo.createTvInputInfo(mContext, ri); 163 if (DEBUG) Slog.d(TAG, "add " + info.getId()); 164 userState.inputMap.put(info.getId(), info); 165 } catch (IOException | XmlPullParserException e) { 166 Slog.e(TAG, "Can't load TV input " + si.name, e); 167 } 168 } 169 } 170 171 private void switchUser(int userId) { 172 synchronized (mLock) { 173 if (mCurrentUserId == userId) { 174 return; 175 } 176 // final int oldUserId = mCurrentUserId; 177 // TODO: Release services and sessions in the old user state, if needed. 178 mCurrentUserId = userId; 179 180 UserState userState = mUserStates.get(userId); 181 if (userState == null) { 182 userState = new UserState(); 183 } 184 mUserStates.put(userId, userState); 185 buildTvInputListLocked(userId); 186 } 187 } 188 189 private void removeUser(int userId) { 190 synchronized (mLock) { 191 UserState userState = mUserStates.get(userId); 192 if (userState == null) { 193 return; 194 } 195 // Release created sessions. 196 for (SessionState state : userState.sessionStateMap.values()) { 197 if (state.mSession != null) { 198 try { 199 state.mSession.release(); 200 } catch (RemoteException e) { 201 Slog.e(TAG, "error in release", e); 202 } 203 } 204 } 205 userState.sessionStateMap.clear(); 206 207 // Unregister all callbacks and unbind all services. 208 for (ServiceState serviceState : userState.serviceStateMap.values()) { 209 if (serviceState.mCallback != null) { 210 try { 211 serviceState.mService.unregisterCallback(serviceState.mCallback); 212 } catch (RemoteException e) { 213 Slog.e(TAG, "error in unregisterCallback", e); 214 } 215 } 216 serviceState.mClientTokens.clear(); 217 mContext.unbindService(serviceState.mConnection); 218 } 219 userState.serviceStateMap.clear(); 220 221 userState.clientStateMap.clear(); 222 223 mUserStates.remove(userId); 224 } 225 } 226 227 private UserState getUserStateLocked(int userId) { 228 UserState userState = mUserStates.get(userId); 229 if (userState == null) { 230 throw new IllegalStateException("User state not found for user ID " + userId); 231 } 232 return userState; 233 } 234 235 private ServiceState getServiceStateLocked(String inputId, int userId) { 236 UserState userState = getUserStateLocked(userId); 237 ServiceState serviceState = userState.serviceStateMap.get(inputId); 238 if (serviceState == null) { 239 throw new IllegalStateException("Service state not found for " + inputId + " (userId=" 240 + userId + ")"); 241 } 242 return serviceState; 243 } 244 245 private SessionState getSessionStateLocked(IBinder sessionToken, int callingUid, int userId) { 246 UserState userState = getUserStateLocked(userId); 247 SessionState sessionState = userState.sessionStateMap.get(sessionToken); 248 if (sessionState == null) { 249 throw new IllegalArgumentException("Session state not found for token " + sessionToken); 250 } 251 // Only the application that requested this session or the system can access it. 252 if (callingUid != Process.SYSTEM_UID && callingUid != sessionState.mCallingUid) { 253 throw new SecurityException("Illegal access to the session with token " + sessionToken 254 + " from uid " + callingUid); 255 } 256 return sessionState; 257 } 258 259 private ITvInputSession getSessionLocked(IBinder sessionToken, int callingUid, int userId) { 260 SessionState sessionState = getSessionStateLocked(sessionToken, callingUid, userId); 261 ITvInputSession session = sessionState.mSession; 262 if (session == null) { 263 throw new IllegalStateException("Session not yet created for token " + sessionToken); 264 } 265 return session; 266 } 267 268 private int resolveCallingUserId(int callingPid, int callingUid, int requestedUserId, 269 String methodName) { 270 return ActivityManager.handleIncomingUser(callingPid, callingUid, requestedUserId, false, 271 false, methodName, null); 272 } 273 274 private void updateServiceConnectionLocked(String inputId, int userId) { 275 UserState userState = getUserStateLocked(userId); 276 ServiceState serviceState = userState.serviceStateMap.get(inputId); 277 if (serviceState == null) { 278 return; 279 } 280 if (serviceState.mReconnecting) { 281 if (!serviceState.mSessionTokens.isEmpty()) { 282 // wait until all the sessions are removed. 283 return; 284 } 285 serviceState.mReconnecting = false; 286 } 287 boolean isStateEmpty = serviceState.mClientTokens.isEmpty() 288 && serviceState.mSessionTokens.isEmpty(); 289 if (serviceState.mService == null && !isStateEmpty && userId == mCurrentUserId) { 290 // This means that the service is not yet connected but its state indicates that we 291 // have pending requests. Then, connect the service. 292 if (serviceState.mBound) { 293 // We have already bound to the service so we don't try to bind again until after we 294 // unbind later on. 295 return; 296 } 297 if (DEBUG) { 298 Slog.d(TAG, "bindServiceAsUser(inputId=" + inputId + ", userId=" + userId 299 + ")"); 300 } 301 302 Intent i = new Intent(TvInputService.SERVICE_INTERFACE).setComponent( 303 userState.inputMap.get(inputId).getComponent()); 304 serviceState.mBound = mContext.bindServiceAsUser( 305 i, serviceState.mConnection, Context.BIND_AUTO_CREATE, new UserHandle(userId)); 306 } else if (serviceState.mService != null && isStateEmpty) { 307 // This means that the service is already connected but its state indicates that we have 308 // nothing to do with it. Then, disconnect the service. 309 if (DEBUG) { 310 Slog.d(TAG, "unbindService(inputId=" + inputId + ")"); 311 } 312 mContext.unbindService(serviceState.mConnection); 313 userState.serviceStateMap.remove(inputId); 314 } 315 } 316 317 private ClientState createClientStateLocked(IBinder clientToken, int userId) { 318 UserState userState = getUserStateLocked(userId); 319 ClientState clientState = new ClientState(clientToken, userId); 320 try { 321 clientToken.linkToDeath(clientState, 0); 322 } catch (RemoteException e) { 323 Slog.e(TAG, "Client is already died."); 324 } 325 userState.clientStateMap.put(clientToken, clientState); 326 return clientState; 327 } 328 329 private void createSessionInternalLocked(ITvInputService service, final IBinder sessionToken, 330 final int userId) { 331 final UserState userState = getUserStateLocked(userId); 332 final SessionState sessionState = userState.sessionStateMap.get(sessionToken); 333 if (DEBUG) { 334 Slog.d(TAG, "createSessionInternalLocked(inputId=" + sessionState.mInputId + ")"); 335 } 336 337 final InputChannel[] channels = InputChannel.openInputChannelPair(sessionToken.toString()); 338 339 // Set up a callback to send the session token. 340 ITvInputSessionCallback callback = new ITvInputSessionCallback.Stub() { 341 @Override 342 public void onSessionCreated(ITvInputSession session) { 343 if (DEBUG) { 344 Slog.d(TAG, "onSessionCreated(inputId=" + sessionState.mInputId + ")"); 345 } 346 synchronized (mLock) { 347 sessionState.mSession = session; 348 if (session == null) { 349 removeSessionStateLocked(sessionToken, userId); 350 sendSessionTokenToClientLocked(sessionState.mClient, sessionState.mInputId, 351 null, null, sessionState.mSeq, userId); 352 } else { 353 try { 354 session.asBinder().linkToDeath(sessionState, 0); 355 } catch (RemoteException e) { 356 Slog.e(TAG, "Session is already died."); 357 } 358 359 IBinder clientToken = sessionState.mClient.asBinder(); 360 ClientState clientState = userState.clientStateMap.get(clientToken); 361 if (clientState == null) { 362 clientState = createClientStateLocked(clientToken, userId); 363 } 364 clientState.mSessionTokens.add(sessionState.mSessionToken); 365 366 sendSessionTokenToClientLocked(sessionState.mClient, sessionState.mInputId, 367 sessionToken, channels[0], sessionState.mSeq, userId); 368 } 369 channels[0].dispose(); 370 } 371 } 372 373 @Override 374 public void onVideoStreamChanged(int width, int height, boolean interlaced) { 375 synchronized (mLock) { 376 if (DEBUG) { 377 Slog.d(TAG, "onVideoStreamChanged(" + width + ", " + height + ")"); 378 } 379 if (sessionState.mSession == null || sessionState.mClient == null) { 380 return; 381 } 382 try { 383 sessionState.mClient.onVideoStreamChanged(width, height, interlaced, 384 sessionState.mSeq); 385 } catch (RemoteException e) { 386 Slog.e(TAG, "error in onVideoStreamChanged"); 387 } 388 } 389 } 390 391 @Override 392 public void onAudioStreamChanged(int channelCount) { 393 synchronized (mLock) { 394 if (DEBUG) { 395 Slog.d(TAG, "onAudioStreamChanged(" + channelCount + ")"); 396 } 397 if (sessionState.mSession == null || sessionState.mClient == null) { 398 return; 399 } 400 try { 401 sessionState.mClient.onAudioStreamChanged(channelCount, sessionState.mSeq); 402 } catch (RemoteException e) { 403 Slog.e(TAG, "error in onAudioStreamChanged"); 404 } 405 } 406 } 407 408 @Override 409 public void onClosedCaptionStreamChanged(boolean hasClosedCaption) { 410 synchronized (mLock) { 411 if (DEBUG) { 412 Slog.d(TAG, "onClosedCaptionStreamChanged(" + hasClosedCaption + ")"); 413 } 414 if (sessionState.mSession == null || sessionState.mClient == null) { 415 return; 416 } 417 try { 418 sessionState.mClient.onClosedCaptionStreamChanged(hasClosedCaption, 419 sessionState.mSeq); 420 } catch (RemoteException e) { 421 Slog.e(TAG, "error in onClosedCaptionStreamChanged"); 422 } 423 } 424 } 425 426 @Override 427 public void onSessionEvent(String eventType, Bundle eventArgs) { 428 synchronized (mLock) { 429 if (DEBUG) { 430 Slog.d(TAG, "onEvent(what=" + eventType + ", data=" + eventArgs + ")"); 431 } 432 if (sessionState.mSession == null || sessionState.mClient == null) { 433 return; 434 } 435 try { 436 sessionState.mClient.onSessionEvent(eventType, eventArgs, 437 sessionState.mSeq); 438 } catch (RemoteException e) { 439 Slog.e(TAG, "error in onSessionEvent"); 440 } 441 } 442 } 443 }; 444 445 // Create a session. When failed, send a null token immediately. 446 try { 447 service.createSession(channels[1], callback); 448 } catch (RemoteException e) { 449 Slog.e(TAG, "error in createSession", e); 450 removeSessionStateLocked(sessionToken, userId); 451 sendSessionTokenToClientLocked(sessionState.mClient, sessionState.mInputId, null, null, 452 sessionState.mSeq, userId); 453 } 454 channels[1].dispose(); 455 } 456 457 private void sendSessionTokenToClientLocked(ITvInputClient client, String inputId, 458 IBinder sessionToken, InputChannel channel, int seq, int userId) { 459 try { 460 client.onSessionCreated(inputId, sessionToken, channel, seq); 461 } catch (RemoteException exception) { 462 Slog.e(TAG, "error in onSessionCreated", exception); 463 } 464 } 465 466 private void releaseSessionLocked(IBinder sessionToken, int callingUid, int userId) { 467 SessionState sessionState = getSessionStateLocked(sessionToken, callingUid, userId); 468 if (sessionState.mSession != null) { 469 try { 470 sessionState.mSession.release(); 471 } catch (RemoteException e) { 472 Slog.w(TAG, "session is already disapeared", e); 473 } 474 sessionState.mSession = null; 475 } 476 removeSessionStateLocked(sessionToken, userId); 477 } 478 479 private void removeSessionStateLocked(IBinder sessionToken, int userId) { 480 // Remove the session state from the global session state map of the current user. 481 UserState userState = getUserStateLocked(userId); 482 SessionState sessionState = userState.sessionStateMap.remove(sessionToken); 483 484 // Close the open log entry, if any. 485 if (sessionState.mLogUri != null) { 486 SomeArgs args = SomeArgs.obtain(); 487 args.arg1 = sessionState.mLogUri; 488 args.arg2 = System.currentTimeMillis(); 489 mLogHandler.obtainMessage(LogHandler.MSG_CLOSE_ENTRY, args).sendToTarget(); 490 } 491 492 // Also remove the session token from the session token list of the current client and 493 // service. 494 ClientState clientState = userState.clientStateMap.get(sessionState.mClient.asBinder()); 495 if (clientState != null) { 496 clientState.mSessionTokens.remove(sessionToken); 497 if (clientState.isEmpty()) { 498 userState.clientStateMap.remove(sessionState.mClient.asBinder()); 499 } 500 } 501 502 ServiceState serviceState = userState.serviceStateMap.get(sessionState.mInputId); 503 if (serviceState != null) { 504 serviceState.mSessionTokens.remove(sessionToken); 505 } 506 updateServiceConnectionLocked(sessionState.mInputId, userId); 507 } 508 509 private void unregisterCallbackInternalLocked(IBinder clientToken, String inputId, 510 int userId) { 511 UserState userState = getUserStateLocked(userId); 512 ClientState clientState = userState.clientStateMap.get(clientToken); 513 if (clientState != null) { 514 clientState.mInputIds.remove(inputId); 515 if (clientState.isEmpty()) { 516 userState.clientStateMap.remove(clientToken); 517 } 518 } 519 520 ServiceState serviceState = userState.serviceStateMap.get(inputId); 521 if (serviceState == null) { 522 return; 523 } 524 525 // Remove this client from the client list and unregister the callback. 526 serviceState.mClientTokens.remove(clientToken); 527 if (!serviceState.mClientTokens.isEmpty()) { 528 // We have other clients who want to keep the callback. Do this later. 529 return; 530 } 531 if (serviceState.mService == null || serviceState.mCallback == null) { 532 return; 533 } 534 try { 535 serviceState.mService.unregisterCallback(serviceState.mCallback); 536 } catch (RemoteException e) { 537 Slog.e(TAG, "error in unregisterCallback", e); 538 } finally { 539 serviceState.mCallback = null; 540 updateServiceConnectionLocked(inputId, userId); 541 } 542 } 543 544 private void broadcastServiceAvailabilityChangedLocked(ServiceState serviceState) { 545 for (IBinder clientToken : serviceState.mClientTokens) { 546 try { 547 ITvInputClient.Stub.asInterface(clientToken).onAvailabilityChanged( 548 serviceState.mTvInputInfo.getId(), serviceState.mAvailable); 549 } catch (RemoteException e) { 550 Slog.e(TAG, "error in onAvailabilityChanged", e); 551 } 552 } 553 } 554 555 private final class BinderService extends ITvInputManager.Stub { 556 @Override 557 public List<TvInputInfo> getTvInputList(int userId) { 558 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), 559 Binder.getCallingUid(), userId, "getTvInputList"); 560 final long identity = Binder.clearCallingIdentity(); 561 try { 562 synchronized (mLock) { 563 UserState userState = getUserStateLocked(resolvedUserId); 564 return new ArrayList<TvInputInfo>(userState.inputMap.values()); 565 } 566 } finally { 567 Binder.restoreCallingIdentity(identity); 568 } 569 } 570 571 @Override 572 public boolean getAvailability(final ITvInputClient client, final String inputId, 573 int userId) { 574 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), 575 Binder.getCallingUid(), userId, "getAvailability"); 576 final long identity = Binder.clearCallingIdentity(); 577 try { 578 synchronized (mLock) { 579 UserState userState = getUserStateLocked(resolvedUserId); 580 ServiceState serviceState = userState.serviceStateMap.get(inputId); 581 if (serviceState != null) { 582 // We already know the status of this input service. Return the cached 583 // status. 584 return serviceState.mAvailable; 585 } 586 } 587 } finally { 588 Binder.restoreCallingIdentity(identity); 589 } 590 return false; 591 } 592 593 @Override 594 public void registerCallback(final ITvInputClient client, final String inputId, 595 int userId) { 596 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), 597 Binder.getCallingUid(), userId, "registerCallback"); 598 final long identity = Binder.clearCallingIdentity(); 599 try { 600 synchronized (mLock) { 601 // Create a new service callback and add it to the callback map of the current 602 // service. 603 UserState userState = getUserStateLocked(resolvedUserId); 604 ServiceState serviceState = userState.serviceStateMap.get(inputId); 605 if (serviceState == null) { 606 serviceState = new ServiceState( 607 userState.inputMap.get(inputId), resolvedUserId); 608 userState.serviceStateMap.put(inputId, serviceState); 609 } 610 IBinder clientToken = client.asBinder(); 611 if (!serviceState.mClientTokens.contains(clientToken)) { 612 serviceState.mClientTokens.add(clientToken); 613 } 614 615 ClientState clientState = userState.clientStateMap.get(clientToken); 616 if (clientState == null) { 617 clientState = createClientStateLocked(clientToken, resolvedUserId); 618 } 619 if (!clientState.mInputIds.contains(inputId)) { 620 clientState.mInputIds.add(inputId); 621 } 622 623 if (serviceState.mService != null) { 624 if (serviceState.mCallback != null) { 625 // We already handled. 626 return; 627 } 628 serviceState.mCallback = new ServiceCallback(resolvedUserId); 629 try { 630 serviceState.mService.registerCallback(serviceState.mCallback); 631 } catch (RemoteException e) { 632 Slog.e(TAG, "error in registerCallback", e); 633 } 634 } else { 635 updateServiceConnectionLocked(inputId, resolvedUserId); 636 } 637 } 638 } finally { 639 Binder.restoreCallingIdentity(identity); 640 } 641 } 642 643 @Override 644 public void unregisterCallback(ITvInputClient client, String inputId, int userId) { 645 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), 646 Binder.getCallingUid(), userId, "unregisterCallback"); 647 final long identity = Binder.clearCallingIdentity(); 648 try { 649 synchronized (mLock) { 650 unregisterCallbackInternalLocked(client.asBinder(), inputId, resolvedUserId); 651 } 652 } finally { 653 Binder.restoreCallingIdentity(identity); 654 } 655 } 656 657 @Override 658 public void createSession(final ITvInputClient client, final String inputId, 659 int seq, int userId) { 660 final int callingUid = Binder.getCallingUid(); 661 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, 662 userId, "createSession"); 663 final long identity = Binder.clearCallingIdentity(); 664 try { 665 synchronized (mLock) { 666 UserState userState = getUserStateLocked(resolvedUserId); 667 ServiceState serviceState = userState.serviceStateMap.get(inputId); 668 if (serviceState == null) { 669 serviceState = new ServiceState( 670 userState.inputMap.get(inputId), resolvedUserId); 671 userState.serviceStateMap.put(inputId, serviceState); 672 } 673 // Send a null token immediately while reconnecting. 674 if (serviceState.mReconnecting == true) { 675 sendSessionTokenToClientLocked(client, inputId, null, null, seq, userId); 676 return; 677 } 678 679 // Create a new session token and a session state. 680 IBinder sessionToken = new Binder(); 681 SessionState sessionState = new SessionState(sessionToken, inputId, client, 682 seq, callingUid, resolvedUserId); 683 684 // Add them to the global session state map of the current user. 685 userState.sessionStateMap.put(sessionToken, sessionState); 686 687 // Also, add them to the session state map of the current service. 688 serviceState.mSessionTokens.add(sessionToken); 689 690 if (serviceState.mService != null) { 691 createSessionInternalLocked(serviceState.mService, sessionToken, 692 resolvedUserId); 693 } else { 694 updateServiceConnectionLocked(inputId, resolvedUserId); 695 } 696 } 697 } finally { 698 Binder.restoreCallingIdentity(identity); 699 } 700 } 701 702 @Override 703 public void releaseSession(IBinder sessionToken, int userId) { 704 final int callingUid = Binder.getCallingUid(); 705 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, 706 userId, "releaseSession"); 707 final long identity = Binder.clearCallingIdentity(); 708 try { 709 synchronized (mLock) { 710 releaseSessionLocked(sessionToken, callingUid, resolvedUserId); 711 } 712 } finally { 713 Binder.restoreCallingIdentity(identity); 714 } 715 } 716 717 @Override 718 public void setSurface(IBinder sessionToken, Surface surface, int userId) { 719 final int callingUid = Binder.getCallingUid(); 720 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, 721 userId, "setSurface"); 722 final long identity = Binder.clearCallingIdentity(); 723 try { 724 synchronized (mLock) { 725 try { 726 getSessionLocked(sessionToken, callingUid, resolvedUserId).setSurface( 727 surface); 728 } catch (RemoteException e) { 729 Slog.e(TAG, "error in setSurface", e); 730 } 731 } 732 } finally { 733 if (surface != null) { 734 // surface is not used in TvInputManagerService. 735 surface.release(); 736 } 737 Binder.restoreCallingIdentity(identity); 738 } 739 } 740 741 @Override 742 public void setVolume(IBinder sessionToken, float volume, int userId) { 743 final int callingUid = Binder.getCallingUid(); 744 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, 745 userId, "setVolume"); 746 final long identity = Binder.clearCallingIdentity(); 747 try { 748 synchronized (mLock) { 749 try { 750 getSessionLocked(sessionToken, callingUid, resolvedUserId).setVolume( 751 volume); 752 } catch (RemoteException e) { 753 Slog.e(TAG, "error in setVolume", e); 754 } 755 } 756 } finally { 757 Binder.restoreCallingIdentity(identity); 758 } 759 } 760 761 @Override 762 public void tune(IBinder sessionToken, final Uri channelUri, int userId) { 763 final int callingUid = Binder.getCallingUid(); 764 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, 765 userId, "tune"); 766 final long identity = Binder.clearCallingIdentity(); 767 try { 768 synchronized (mLock) { 769 try { 770 getSessionLocked(sessionToken, callingUid, resolvedUserId).tune(channelUri); 771 772 long currentTime = System.currentTimeMillis(); 773 long channelId = ContentUris.parseId(channelUri); 774 775 // Close the open log entry first, if any. 776 UserState userState = getUserStateLocked(resolvedUserId); 777 SessionState sessionState = userState.sessionStateMap.get(sessionToken); 778 if (sessionState.mLogUri != null) { 779 SomeArgs args = SomeArgs.obtain(); 780 args.arg1 = sessionState.mLogUri; 781 args.arg2 = currentTime; 782 mLogHandler.obtainMessage(LogHandler.MSG_CLOSE_ENTRY, args) 783 .sendToTarget(); 784 } 785 786 // Create a log entry and fill it later. 787 ContentValues values = new ContentValues(); 788 values.put(TvContract.WatchedPrograms.COLUMN_WATCH_START_TIME_UTC_MILLIS, 789 currentTime); 790 values.put(TvContract.WatchedPrograms.COLUMN_WATCH_END_TIME_UTC_MILLIS, 0); 791 values.put(TvContract.WatchedPrograms.COLUMN_CHANNEL_ID, channelId); 792 793 sessionState.mLogUri = mContentResolver.insert( 794 TvContract.WatchedPrograms.CONTENT_URI, values); 795 SomeArgs args = SomeArgs.obtain(); 796 args.arg1 = sessionState.mLogUri; 797 args.arg2 = ContentUris.parseId(channelUri); 798 args.arg3 = currentTime; 799 mLogHandler.obtainMessage(LogHandler.MSG_OPEN_ENTRY, args).sendToTarget(); 800 } catch (RemoteException e) { 801 Slog.e(TAG, "error in tune", e); 802 return; 803 } 804 } 805 } finally { 806 Binder.restoreCallingIdentity(identity); 807 } 808 } 809 810 @Override 811 public void createOverlayView(IBinder sessionToken, IBinder windowToken, Rect frame, 812 int userId) { 813 final int callingUid = Binder.getCallingUid(); 814 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, 815 userId, "createOverlayView"); 816 final long identity = Binder.clearCallingIdentity(); 817 try { 818 synchronized (mLock) { 819 try { 820 getSessionLocked(sessionToken, callingUid, resolvedUserId) 821 .createOverlayView(windowToken, frame); 822 } catch (RemoteException e) { 823 Slog.e(TAG, "error in createOverlayView", e); 824 } 825 } 826 } finally { 827 Binder.restoreCallingIdentity(identity); 828 } 829 } 830 831 @Override 832 public void relayoutOverlayView(IBinder sessionToken, Rect frame, int userId) { 833 final int callingUid = Binder.getCallingUid(); 834 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, 835 userId, "relayoutOverlayView"); 836 final long identity = Binder.clearCallingIdentity(); 837 try { 838 synchronized (mLock) { 839 try { 840 getSessionLocked(sessionToken, callingUid, resolvedUserId) 841 .relayoutOverlayView(frame); 842 } catch (RemoteException e) { 843 Slog.e(TAG, "error in relayoutOverlayView", e); 844 } 845 } 846 } finally { 847 Binder.restoreCallingIdentity(identity); 848 } 849 } 850 851 @Override 852 public void removeOverlayView(IBinder sessionToken, int userId) { 853 final int callingUid = Binder.getCallingUid(); 854 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, 855 userId, "removeOverlayView"); 856 final long identity = Binder.clearCallingIdentity(); 857 try { 858 synchronized (mLock) { 859 try { 860 getSessionLocked(sessionToken, callingUid, resolvedUserId) 861 .removeOverlayView(); 862 } catch (RemoteException e) { 863 Slog.e(TAG, "error in removeOverlayView", e); 864 } 865 } 866 } finally { 867 Binder.restoreCallingIdentity(identity); 868 } 869 } 870 871 @Override 872 public List<TvInputHardwareInfo> getHardwareList() throws RemoteException { 873 if (mContext.checkCallingPermission( 874 android.Manifest.permission.TV_INPUT_HARDWARE) 875 != PackageManager.PERMISSION_GRANTED) { 876 return null; 877 } 878 879 final long identity = Binder.clearCallingIdentity(); 880 try { 881 return mTvInputHardwareManager.getHardwareList(); 882 } finally { 883 Binder.restoreCallingIdentity(identity); 884 } 885 } 886 887 @Override 888 public ITvInputHardware acquireTvInputHardware(int deviceId, 889 ITvInputHardwareCallback callback, int userId) throws RemoteException { 890 if (mContext.checkCallingPermission( 891 android.Manifest.permission.TV_INPUT_HARDWARE) 892 != PackageManager.PERMISSION_GRANTED) { 893 return null; 894 } 895 896 final long identity = Binder.clearCallingIdentity(); 897 final int callingUid = Binder.getCallingUid(); 898 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, 899 userId, "acquireTvInputHardware"); 900 try { 901 return mTvInputHardwareManager.acquireHardware( 902 deviceId, callback, callingUid, resolvedUserId); 903 } finally { 904 Binder.restoreCallingIdentity(identity); 905 } 906 } 907 908 @Override 909 public void releaseTvInputHardware(int deviceId, ITvInputHardware hardware, int userId) 910 throws RemoteException { 911 if (mContext.checkCallingPermission( 912 android.Manifest.permission.TV_INPUT_HARDWARE) 913 != PackageManager.PERMISSION_GRANTED) { 914 return; 915 } 916 917 final long identity = Binder.clearCallingIdentity(); 918 final int callingUid = Binder.getCallingUid(); 919 final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, 920 userId, "releaseTvInputHardware"); 921 try { 922 mTvInputHardwareManager.releaseHardware( 923 deviceId, hardware, callingUid, resolvedUserId); 924 } finally { 925 Binder.restoreCallingIdentity(identity); 926 } 927 } 928 } 929 930 private static final class UserState { 931 // A mapping from the TV input id to its TvInputInfo. 932 private final Map<String, TvInputInfo> inputMap = new HashMap<String,TvInputInfo>(); 933 934 // A mapping from the token of a client to its state. 935 private final Map<IBinder, ClientState> clientStateMap = 936 new HashMap<IBinder, ClientState>(); 937 938 // A mapping from the name of a TV input service to its state. 939 private final Map<String, ServiceState> serviceStateMap = 940 new HashMap<String, ServiceState>(); 941 942 // A mapping from the token of a TV input session to its state. 943 private final Map<IBinder, SessionState> sessionStateMap = 944 new HashMap<IBinder, SessionState>(); 945 } 946 947 private final class ClientState implements IBinder.DeathRecipient { 948 private final List<String> mInputIds = new ArrayList<String>(); 949 private final List<IBinder> mSessionTokens = new ArrayList<IBinder>(); 950 951 private IBinder mClientToken; 952 private final int mUserId; 953 954 ClientState(IBinder clientToken, int userId) { 955 mClientToken = clientToken; 956 mUserId = userId; 957 } 958 959 public boolean isEmpty() { 960 return mInputIds.isEmpty() && mSessionTokens.isEmpty(); 961 } 962 963 @Override 964 public void binderDied() { 965 synchronized (mLock) { 966 UserState userState = getUserStateLocked(mUserId); 967 // DO NOT remove the client state of clientStateMap in this method. It will be 968 // removed in releaseSessionLocked() or unregisterCallbackInternalLocked(). 969 ClientState clientState = userState.clientStateMap.get(mClientToken); 970 if (clientState != null) { 971 while (clientState.mSessionTokens.size() > 0) { 972 releaseSessionLocked( 973 clientState.mSessionTokens.get(0), Process.SYSTEM_UID, mUserId); 974 } 975 while (clientState.mInputIds.size() > 0) { 976 unregisterCallbackInternalLocked( 977 mClientToken, clientState.mInputIds.get(0), mUserId); 978 } 979 } 980 mClientToken = null; 981 } 982 } 983 } 984 985 private final class ServiceState { 986 private final List<IBinder> mClientTokens = new ArrayList<IBinder>(); 987 private final List<IBinder> mSessionTokens = new ArrayList<IBinder>(); 988 private final ServiceConnection mConnection; 989 private final TvInputInfo mTvInputInfo; 990 991 private ITvInputService mService; 992 private ServiceCallback mCallback; 993 private boolean mBound; 994 private boolean mAvailable; 995 private boolean mReconnecting; 996 997 private ServiceState(TvInputInfo inputInfo, int userId) { 998 mTvInputInfo = inputInfo; 999 mConnection = new InputServiceConnection(inputInfo, userId); 1000 } 1001 } 1002 1003 private final class SessionState implements IBinder.DeathRecipient { 1004 private final String mInputId; 1005 private final ITvInputClient mClient; 1006 private final int mSeq; 1007 private final int mCallingUid; 1008 private final int mUserId; 1009 private final IBinder mSessionToken; 1010 private ITvInputSession mSession; 1011 private Uri mLogUri; 1012 1013 private SessionState(IBinder sessionToken, String inputId, ITvInputClient client, int seq, 1014 int callingUid, int userId) { 1015 mSessionToken = sessionToken; 1016 mInputId = inputId; 1017 mClient = client; 1018 mSeq = seq; 1019 mCallingUid = callingUid; 1020 mUserId = userId; 1021 } 1022 1023 @Override 1024 public void binderDied() { 1025 synchronized (mLock) { 1026 mSession = null; 1027 if (mClient != null) { 1028 try { 1029 mClient.onSessionReleased(mSeq); 1030 } catch(RemoteException e) { 1031 Slog.e(TAG, "error in onSessionReleased", e); 1032 } 1033 } 1034 removeSessionStateLocked(mSessionToken, mUserId); 1035 } 1036 } 1037 } 1038 1039 private final class InputServiceConnection implements ServiceConnection { 1040 private final TvInputInfo mTvInputInfo; 1041 private final int mUserId; 1042 1043 private InputServiceConnection(TvInputInfo inputInfo, int userId) { 1044 mUserId = userId; 1045 mTvInputInfo = inputInfo; 1046 } 1047 1048 @Override 1049 public void onServiceConnected(ComponentName name, IBinder service) { 1050 if (DEBUG) { 1051 Slog.d(TAG, "onServiceConnected(inputId=" + mTvInputInfo.getId() + ")"); 1052 } 1053 synchronized (mLock) { 1054 ServiceState serviceState = getServiceStateLocked(mTvInputInfo.getId(), mUserId); 1055 serviceState.mService = ITvInputService.Stub.asInterface(service); 1056 1057 // Register a callback, if we need to. 1058 if (!serviceState.mClientTokens.isEmpty() && serviceState.mCallback == null) { 1059 serviceState.mCallback = new ServiceCallback(mUserId); 1060 try { 1061 serviceState.mService.registerCallback(serviceState.mCallback); 1062 } catch (RemoteException e) { 1063 Slog.e(TAG, "error in registerCallback", e); 1064 } 1065 } 1066 1067 // And create sessions, if any. 1068 for (IBinder sessionToken : serviceState.mSessionTokens) { 1069 createSessionInternalLocked(serviceState.mService, sessionToken, mUserId); 1070 } 1071 } 1072 } 1073 1074 @Override 1075 public void onServiceDisconnected(ComponentName name) { 1076 if (DEBUG) { 1077 Slog.d(TAG, "onServiceDisconnected(inputId=" + mTvInputInfo.getId() + ")"); 1078 } 1079 if (!mTvInputInfo.getComponent().equals(name)) { 1080 throw new IllegalArgumentException("Mismatched ComponentName: " 1081 + mTvInputInfo.getComponent() + " (expected), " + name + " (actual)."); 1082 } 1083 synchronized (mLock) { 1084 UserState userState = getUserStateLocked(mUserId); 1085 ServiceState serviceState = userState.serviceStateMap.get(mTvInputInfo.getId()); 1086 if (serviceState != null) { 1087 serviceState.mReconnecting = true; 1088 serviceState.mBound = false; 1089 serviceState.mService = null; 1090 serviceState.mCallback = null; 1091 1092 // Send null tokens for not finishing create session events. 1093 for (IBinder sessionToken : serviceState.mSessionTokens) { 1094 SessionState sessionState = userState.sessionStateMap.get(sessionToken); 1095 if (sessionState.mSession == null) { 1096 removeSessionStateLocked(sessionToken, sessionState.mUserId); 1097 sendSessionTokenToClientLocked(sessionState.mClient, 1098 sessionState.mInputId, null, null, sessionState.mSeq, 1099 sessionState.mUserId); 1100 } 1101 } 1102 1103 if (serviceState.mAvailable) { 1104 serviceState.mAvailable = false; 1105 broadcastServiceAvailabilityChangedLocked(serviceState); 1106 } 1107 updateServiceConnectionLocked(mTvInputInfo.getId(), mUserId); 1108 } 1109 } 1110 } 1111 } 1112 1113 private final class ServiceCallback extends ITvInputServiceCallback.Stub { 1114 private final int mUserId; 1115 1116 ServiceCallback(int userId) { 1117 mUserId = userId; 1118 } 1119 1120 @Override 1121 public void onAvailabilityChanged(String inputId, boolean isAvailable) { 1122 if (DEBUG) { 1123 Slog.d(TAG, "onAvailabilityChanged(inputId=" + inputId + ", isAvailable=" 1124 + isAvailable + ")"); 1125 } 1126 synchronized (mLock) { 1127 ServiceState serviceState = getServiceStateLocked(inputId, mUserId); 1128 if (serviceState.mAvailable != isAvailable) { 1129 serviceState.mAvailable = isAvailable; 1130 broadcastServiceAvailabilityChangedLocked(serviceState); 1131 } 1132 } 1133 } 1134 } 1135 1136 private final class LogHandler extends Handler { 1137 private static final int MSG_OPEN_ENTRY = 1; 1138 private static final int MSG_UPDATE_ENTRY = 2; 1139 private static final int MSG_CLOSE_ENTRY = 3; 1140 1141 public LogHandler(Looper looper) { 1142 super(looper); 1143 } 1144 1145 @Override 1146 public void handleMessage(Message msg) { 1147 switch (msg.what) { 1148 case MSG_OPEN_ENTRY: { 1149 SomeArgs args = (SomeArgs) msg.obj; 1150 Uri uri = (Uri) args.arg1; 1151 long channelId = (long) args.arg2; 1152 long time = (long) args.arg3; 1153 onOpenEntry(uri, channelId, time); 1154 args.recycle(); 1155 return; 1156 } 1157 case MSG_UPDATE_ENTRY: { 1158 SomeArgs args = (SomeArgs) msg.obj; 1159 Uri uri = (Uri) args.arg1; 1160 long channelId = (long) args.arg2; 1161 long time = (long) args.arg3; 1162 onUpdateEntry(uri, channelId, time); 1163 args.recycle(); 1164 return; 1165 } 1166 case MSG_CLOSE_ENTRY: { 1167 SomeArgs args = (SomeArgs) msg.obj; 1168 Uri uri = (Uri) args.arg1; 1169 long time = (long) args.arg2; 1170 onCloseEntry(uri, time); 1171 args.recycle(); 1172 return; 1173 } 1174 default: { 1175 Slog.w(TAG, "Unhandled message code: " + msg.what); 1176 return; 1177 } 1178 } 1179 } 1180 1181 private void onOpenEntry(Uri uri, long channelId, long watchStarttime) { 1182 String[] projection = { 1183 TvContract.Programs.COLUMN_TITLE, 1184 TvContract.Programs.COLUMN_START_TIME_UTC_MILLIS, 1185 TvContract.Programs.COLUMN_END_TIME_UTC_MILLIS, 1186 TvContract.Programs.COLUMN_SHORT_DESCRIPTION 1187 }; 1188 String selection = TvContract.Programs.COLUMN_CHANNEL_ID + "=? AND " 1189 + TvContract.Programs.COLUMN_START_TIME_UTC_MILLIS + "<=? AND " 1190 + TvContract.Programs.COLUMN_END_TIME_UTC_MILLIS + ">?"; 1191 String[] selectionArgs = { 1192 String.valueOf(channelId), 1193 String.valueOf(watchStarttime), 1194 String.valueOf(watchStarttime) 1195 }; 1196 String sortOrder = TvContract.Programs.COLUMN_START_TIME_UTC_MILLIS + " ASC"; 1197 Cursor cursor = null; 1198 try { 1199 cursor = mContentResolver.query(TvContract.Programs.CONTENT_URI, projection, 1200 selection, selectionArgs, sortOrder); 1201 if (cursor != null && cursor.moveToNext()) { 1202 ContentValues values = new ContentValues(); 1203 values.put(TvContract.WatchedPrograms.COLUMN_TITLE, cursor.getString(0)); 1204 values.put(TvContract.WatchedPrograms.COLUMN_START_TIME_UTC_MILLIS, 1205 cursor.getLong(1)); 1206 long endTime = cursor.getLong(2); 1207 values.put(TvContract.WatchedPrograms.COLUMN_END_TIME_UTC_MILLIS, endTime); 1208 values.put(TvContract.WatchedPrograms.COLUMN_DESCRIPTION, cursor.getString(3)); 1209 mContentResolver.update(uri, values, null, null); 1210 1211 // Schedule an update when the current program ends. 1212 SomeArgs args = SomeArgs.obtain(); 1213 args.arg1 = uri; 1214 args.arg2 = channelId; 1215 args.arg3 = endTime; 1216 Message msg = obtainMessage(LogHandler.MSG_UPDATE_ENTRY, args); 1217 sendMessageDelayed(msg, endTime - System.currentTimeMillis()); 1218 } 1219 } finally { 1220 if (cursor != null) { 1221 cursor.close(); 1222 } 1223 } 1224 } 1225 1226 private void onUpdateEntry(Uri uri, long channelId, long time) { 1227 String[] projection = { 1228 TvContract.WatchedPrograms.COLUMN_WATCH_START_TIME_UTC_MILLIS, 1229 TvContract.WatchedPrograms.COLUMN_WATCH_END_TIME_UTC_MILLIS, 1230 TvContract.WatchedPrograms.COLUMN_TITLE, 1231 TvContract.WatchedPrograms.COLUMN_START_TIME_UTC_MILLIS, 1232 TvContract.WatchedPrograms.COLUMN_END_TIME_UTC_MILLIS, 1233 TvContract.WatchedPrograms.COLUMN_DESCRIPTION 1234 }; 1235 Cursor cursor = null; 1236 try { 1237 cursor = mContentResolver.query(uri, projection, null, null, null); 1238 if (cursor != null && cursor.moveToNext()) { 1239 long watchStartTime = cursor.getLong(0); 1240 long watchEndTime = cursor.getLong(1); 1241 String title = cursor.getString(2); 1242 long startTime = cursor.getLong(3); 1243 long endTime = cursor.getLong(4); 1244 String description = cursor.getString(5); 1245 1246 // Do nothing if the current log entry is already closed. 1247 if (watchEndTime > 0) { 1248 return; 1249 } 1250 1251 // The current program has just ended. Create a (complete) log entry off the 1252 // current entry. 1253 ContentValues values = new ContentValues(); 1254 values.put(TvContract.WatchedPrograms.COLUMN_WATCH_START_TIME_UTC_MILLIS, 1255 watchStartTime); 1256 values.put(TvContract.WatchedPrograms.COLUMN_WATCH_END_TIME_UTC_MILLIS, time); 1257 values.put(TvContract.WatchedPrograms.COLUMN_CHANNEL_ID, channelId); 1258 values.put(TvContract.WatchedPrograms.COLUMN_TITLE, title); 1259 values.put(TvContract.WatchedPrograms.COLUMN_START_TIME_UTC_MILLIS, startTime); 1260 values.put(TvContract.WatchedPrograms.COLUMN_END_TIME_UTC_MILLIS, endTime); 1261 values.put(TvContract.WatchedPrograms.COLUMN_DESCRIPTION, description); 1262 mContentResolver.insert(TvContract.WatchedPrograms.CONTENT_URI, values); 1263 } 1264 } finally { 1265 if (cursor != null) { 1266 cursor.close(); 1267 } 1268 } 1269 // Re-open the current log entry with the next program information. 1270 onOpenEntry(uri, channelId, time); 1271 } 1272 1273 private void onCloseEntry(Uri uri, long watchEndTime) { 1274 ContentValues values = new ContentValues(); 1275 values.put(TvContract.WatchedPrograms.COLUMN_WATCH_END_TIME_UTC_MILLIS, watchEndTime); 1276 mContentResolver.update(uri, values, null, null); 1277 } 1278 } 1279} 1280