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