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_SELECT_TRACK = 5;
48 private static final int DO_UNSELECT_TRACK = 6;
49 private static final int DO_CREATE_OVERLAY_VIEW = 7;
50 private static final int DO_RELAYOUT_OVERLAY_VIEW = 8;
51 private static final int DO_REMOVE_OVERLAY_VIEW = 9;
53 private final HandlerCaller mCaller;
55 private TvInputService.Session mTvInputSessionImpl;
56 private InputChannel mChannel;
57 private TvInputEventReceiver mReceiver;
59 public ITvInputSessionWrapper(Context context, TvInputService.Session sessionImpl,
60 InputChannel channel) {
61 mCaller = new HandlerCaller(context, null, this, true /* asyncHandler */);
62 mTvInputSessionImpl = sessionImpl;
64 if (channel != null) {
65 mReceiver = new TvInputEventReceiver(channel, context.getMainLooper());
70 public void executeMessage(Message msg) {
71 if (mTvInputSessionImpl == null) {
77 mTvInputSessionImpl.release();
78 mTvInputSessionImpl = null;
79 if (mReceiver != null) {
83 if (mChannel != null) {
89 case DO_SET_SURFACE: {
90 mTvInputSessionImpl.setSurface((Surface) msg.obj);
94 mTvInputSessionImpl.setVolume((Float) msg.obj);
98 mTvInputSessionImpl.tune((Uri) msg.obj);
101 case DO_SELECT_TRACK: {
102 mTvInputSessionImpl.selectTrack((TvTrackInfo) msg.obj);
105 case DO_UNSELECT_TRACK: {
106 mTvInputSessionImpl.unselectTrack((TvTrackInfo) msg.obj);
109 case DO_CREATE_OVERLAY_VIEW: {
110 SomeArgs args = (SomeArgs) msg.obj;
111 mTvInputSessionImpl.createOverlayView((IBinder) args.arg1, (Rect) args.arg2);
115 case DO_RELAYOUT_OVERLAY_VIEW: {
116 mTvInputSessionImpl.relayoutOverlayView((Rect) msg.obj);
119 case DO_REMOVE_OVERLAY_VIEW: {
120 mTvInputSessionImpl.removeOverlayView(true);
124 Log.w(TAG, "Unhandled message code: " + msg.what);
131 public void release() {
132 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_RELEASE));
136 public void setSurface(Surface surface) {
137 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_SURFACE, surface));
141 public final void setVolume(float volume) {
142 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_VOLUME, volume));
146 public void tune(Uri channelUri) {
147 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_TUNE, channelUri));
151 public void selectTrack(TvTrackInfo track) {
152 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SELECT_TRACK, track));
156 public void unselectTrack(TvTrackInfo track) {
157 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_UNSELECT_TRACK, track));
161 public void createOverlayView(IBinder windowToken, Rect frame) {
162 mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_CREATE_OVERLAY_VIEW, windowToken,
167 public void relayoutOverlayView(Rect frame) {
168 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_RELAYOUT_OVERLAY_VIEW, frame));
172 public void removeOverlayView() {
173 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_REMOVE_OVERLAY_VIEW));
176 private final class TvInputEventReceiver extends InputEventReceiver {
177 public TvInputEventReceiver(InputChannel inputChannel, Looper looper) {
178 super(inputChannel, looper);
182 public void onInputEvent(InputEvent event) {
183 if (mTvInputSessionImpl == null) {
184 // The session has been finished.
185 finishInputEvent(event, false);
189 int handled = mTvInputSessionImpl.dispatchInputEvent(event, this);
190 if (handled != TvInputManager.Session.DISPATCH_IN_PROGRESS) {
191 finishInputEvent(event, handled == TvInputManager.Session.DISPATCH_HANDLED);