2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.media.tv;
19 import android.content.Context;
20 import android.graphics.Rect;
21 import android.net.Uri;
22 import android.os.IBinder;
23 import android.os.Looper;
24 import android.os.Message;
25 import android.util.Log;
26 import android.view.InputChannel;
27 import android.view.InputEvent;
28 import android.view.InputEventReceiver;
29 import android.view.Surface;
31 import com.android.internal.os.HandlerCaller;
32 import com.android.internal.os.SomeArgs;
35 * Implements the internal ITvInputSession interface to convert incoming calls on to it back to
36 * calls on the public TvInputSession interface, scheduling them on the main thread of the process.
40 public class ITvInputSessionWrapper extends ITvInputSession.Stub implements HandlerCaller.Callback {
41 private static final String TAG = "TvInputSessionWrapper";
43 private static final int DO_RELEASE = 1;
44 private static final int DO_SET_SURFACE = 2;
45 private static final int DO_SET_VOLUME = 3;
46 private static final int DO_TUNE = 4;
47 private static final int DO_CREATE_OVERLAY_VIEW = 5;
48 private static final int DO_RELAYOUT_OVERLAY_VIEW = 6;
49 private static final int DO_REMOVE_OVERLAY_VIEW = 7;
51 private final HandlerCaller mCaller;
53 private TvInputService.Session mTvInputSessionImpl;
54 private InputChannel mChannel;
55 private TvInputEventReceiver mReceiver;
57 public ITvInputSessionWrapper(Context context, TvInputService.Session sessionImpl,
58 InputChannel channel) {
59 mCaller = new HandlerCaller(context, null, this, true /* asyncHandler */);
60 mTvInputSessionImpl = sessionImpl;
62 if (channel != null) {
63 mReceiver = new TvInputEventReceiver(channel, context.getMainLooper());
68 public void executeMessage(Message msg) {
69 if (mTvInputSessionImpl == null) {
75 mTvInputSessionImpl.release();
76 mTvInputSessionImpl = null;
77 if (mReceiver != null) {
81 if (mChannel != null) {
87 case DO_SET_SURFACE: {
88 mTvInputSessionImpl.setSurface((Surface) msg.obj);
92 mTvInputSessionImpl.setVolume((Float) msg.obj);
96 mTvInputSessionImpl.tune((Uri) msg.obj);
99 case DO_CREATE_OVERLAY_VIEW: {
100 SomeArgs args = (SomeArgs) msg.obj;
101 mTvInputSessionImpl.createOverlayView((IBinder) args.arg1, (Rect) args.arg2);
105 case DO_RELAYOUT_OVERLAY_VIEW: {
106 mTvInputSessionImpl.relayoutOverlayView((Rect) msg.obj);
109 case DO_REMOVE_OVERLAY_VIEW: {
110 mTvInputSessionImpl.removeOverlayView(true);
114 Log.w(TAG, "Unhandled message code: " + msg.what);
121 public void release() {
122 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_RELEASE));
126 public void setSurface(Surface surface) {
127 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_SURFACE, surface));
131 public final void setVolume(float volume) {
132 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_VOLUME, volume));
136 public void tune(Uri channelUri) {
137 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_TUNE, channelUri));
141 public void createOverlayView(IBinder windowToken, Rect frame) {
142 mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_CREATE_OVERLAY_VIEW, windowToken,
147 public void relayoutOverlayView(Rect frame) {
148 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_RELAYOUT_OVERLAY_VIEW, frame));
152 public void removeOverlayView() {
153 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_REMOVE_OVERLAY_VIEW));
156 private final class TvInputEventReceiver extends InputEventReceiver {
157 public TvInputEventReceiver(InputChannel inputChannel, Looper looper) {
158 super(inputChannel, looper);
162 public void onInputEvent(InputEvent event) {
163 if (mTvInputSessionImpl == null) {
164 // The session has been finished.
165 finishInputEvent(event, false);
169 int handled = mTvInputSessionImpl.dispatchInputEvent(event, this);
170 if (handled != TvInputManager.Session.DISPATCH_IN_PROGRESS) {
171 finishInputEvent(event, handled == TvInputManager.Session.DISPATCH_HANDLED);