blob: 5ad1bce3cd66814c449a57eed8e32ac0ce97f07b [file] [log] [blame]
Jae Seo39570912014-02-20 18:23:25 -08001/*
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
Jae Seod5cc4a22014-05-30 16:57:43 -070017package android.media.tv;
Jae Seo39570912014-02-20 18:23:25 -080018
19import android.content.Context;
Youngsang Cho9a22f0f2014-04-09 22:51:54 +090020import android.graphics.Rect;
Jae Seo3d04b762015-05-12 20:05:00 -070021import android.media.PlaybackParams;
Jae Seo39570912014-02-20 18:23:25 -080022import android.net.Uri;
Jae Seoa759b112014-07-18 22:16:08 -070023import android.os.Bundle;
Youngsang Cho9a22f0f2014-04-09 22:51:54 +090024import android.os.IBinder;
Jae Seo6a6059a2014-04-17 21:35:29 -070025import android.os.Looper;
Jae Seo39570912014-02-20 18:23:25 -080026import android.os.Message;
27import android.util.Log;
Jae Seo6a6059a2014-04-17 21:35:29 -070028import android.view.InputChannel;
29import android.view.InputEvent;
30import android.view.InputEventReceiver;
Jae Seo39570912014-02-20 18:23:25 -080031import android.view.Surface;
32
33import com.android.internal.os.HandlerCaller;
Youngsang Cho9a22f0f2014-04-09 22:51:54 +090034import com.android.internal.os.SomeArgs;
Jae Seo39570912014-02-20 18:23:25 -080035
36/**
37 * Implements the internal ITvInputSession interface to convert incoming calls on to it back to
38 * calls on the public TvInputSession interface, scheduling them on the main thread of the process.
39 *
40 * @hide
41 */
42public class ITvInputSessionWrapper extends ITvInputSession.Stub implements HandlerCaller.Callback {
43 private static final String TAG = "TvInputSessionWrapper";
44
Dongwon Kang21add3e2015-04-27 17:43:52 +090045 private static final int EXECUTE_MESSAGE_TIMEOUT_SHORT_MILLIS = 50;
46 private static final int EXECUTE_MESSAGE_TUNE_TIMEOUT_MILLIS = 2000;
47 private static final int EXECUTE_MESSAGE_TIMEOUT_LONG_MILLIS = 5 * 1000;
Youngsang Cho9df2c1b2014-09-03 22:27:56 +090048
Jae Seo39570912014-02-20 18:23:25 -080049 private static final int DO_RELEASE = 1;
Ji-Hwan Lee15c56aa2014-08-18 22:01:55 +090050 private static final int DO_SET_MAIN = 2;
Ji-Hwan Lee4c526972014-07-22 04:46:30 +090051 private static final int DO_SET_SURFACE = 3;
52 private static final int DO_DISPATCH_SURFACE_CHANGED = 4;
53 private static final int DO_SET_STREAM_VOLUME = 5;
54 private static final int DO_TUNE = 6;
55 private static final int DO_SET_CAPTION_ENABLED = 7;
56 private static final int DO_SELECT_TRACK = 8;
Jae Seo10d285a2014-07-31 22:46:47 +090057 private static final int DO_APP_PRIVATE_COMMAND = 9;
58 private static final int DO_CREATE_OVERLAY_VIEW = 10;
59 private static final int DO_RELAYOUT_OVERLAY_VIEW = 11;
60 private static final int DO_REMOVE_OVERLAY_VIEW = 12;
Jae Seoa9033832015-03-11 19:29:46 -070061 private static final int DO_UNBLOCK_CONTENT = 13;
Dongwon Kang6f0240c2015-03-31 17:56:36 -070062 private static final int DO_TIME_SHIFT_PAUSE = 14;
63 private static final int DO_TIME_SHIFT_RESUME = 15;
64 private static final int DO_TIME_SHIFT_SEEK_TO = 16;
Jae Seo4b34cc72015-05-15 17:29:39 -070065 private static final int DO_TIME_SHIFT_SET_PLAYBACK_PARAMS = 17;
Jae Seo465f0d62015-04-06 18:40:46 -070066 private static final int DO_TIME_SHIFT_ENABLE_POSITION_TRACKING = 18;
Jae Seo39570912014-02-20 18:23:25 -080067
Jae Seo39570912014-02-20 18:23:25 -080068 private final HandlerCaller mCaller;
69
Jae Seo782f7342014-06-03 12:51:21 -070070 private TvInputService.Session mTvInputSessionImpl;
Jae Seo6a6059a2014-04-17 21:35:29 -070071 private InputChannel mChannel;
72 private TvInputEventReceiver mReceiver;
73
Jae Seo782f7342014-06-03 12:51:21 -070074 public ITvInputSessionWrapper(Context context, TvInputService.Session sessionImpl,
Jae Seo6a6059a2014-04-17 21:35:29 -070075 InputChannel channel) {
Jae Seo39570912014-02-20 18:23:25 -080076 mCaller = new HandlerCaller(context, null, this, true /* asyncHandler */);
Jae Seo6a6059a2014-04-17 21:35:29 -070077 mTvInputSessionImpl = sessionImpl;
78 mChannel = channel;
79 if (channel != null) {
80 mReceiver = new TvInputEventReceiver(channel, context.getMainLooper());
81 }
Jae Seo39570912014-02-20 18:23:25 -080082 }
83
84 @Override
85 public void executeMessage(Message msg) {
Jae Seo6a6059a2014-04-17 21:35:29 -070086 if (mTvInputSessionImpl == null) {
Jae Seo39570912014-02-20 18:23:25 -080087 return;
88 }
89
Youngsang Cho9df2c1b2014-09-03 22:27:56 +090090 long startTime = System.currentTimeMillis();
Jae Seo39570912014-02-20 18:23:25 -080091 switch (msg.what) {
92 case DO_RELEASE: {
Jae Seo6a6059a2014-04-17 21:35:29 -070093 mTvInputSessionImpl.release();
94 mTvInputSessionImpl = null;
95 if (mReceiver != null) {
96 mReceiver.dispose();
97 mReceiver = null;
98 }
99 if (mChannel != null) {
100 mChannel.dispose();
101 mChannel = null;
102 }
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900103 break;
Jae Seo39570912014-02-20 18:23:25 -0800104 }
Ji-Hwan Lee15c56aa2014-08-18 22:01:55 +0900105 case DO_SET_MAIN: {
106 mTvInputSessionImpl.setMain((Boolean) msg.obj);
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900107 break;
Ji-Hwan Lee4c526972014-07-22 04:46:30 +0900108 }
Jae Seo39570912014-02-20 18:23:25 -0800109 case DO_SET_SURFACE: {
Jae Seo6a6059a2014-04-17 21:35:29 -0700110 mTvInputSessionImpl.setSurface((Surface) msg.obj);
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900111 break;
Jae Seo39570912014-02-20 18:23:25 -0800112 }
Youngsang Choe821d712014-07-16 14:22:19 -0700113 case DO_DISPATCH_SURFACE_CHANGED: {
114 SomeArgs args = (SomeArgs) msg.obj;
115 mTvInputSessionImpl.dispatchSurfaceChanged(args.argi1, args.argi2, args.argi3);
116 args.recycle();
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900117 break;
Youngsang Choe821d712014-07-16 14:22:19 -0700118 }
Dongwon Kang4b662d12014-07-17 19:24:56 +0900119 case DO_SET_STREAM_VOLUME: {
120 mTvInputSessionImpl.setStreamVolume((Float) msg.obj);
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900121 break;
Jae Seo39570912014-02-20 18:23:25 -0800122 }
123 case DO_TUNE: {
Sungsoo Lim1a6b25e2014-07-09 10:40:43 +0900124 SomeArgs args = (SomeArgs) msg.obj;
125 mTvInputSessionImpl.tune((Uri) args.arg1, (Bundle) args.arg2);
126 args.recycle();
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900127 break;
Jae Seo39570912014-02-20 18:23:25 -0800128 }
Jae Seo2c1c31c2014-07-10 14:57:01 -0700129 case DO_SET_CAPTION_ENABLED: {
130 mTvInputSessionImpl.setCaptionEnabled((Boolean) msg.obj);
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900131 break;
Jae Seo2c1c31c2014-07-10 14:57:01 -0700132 }
Dongwon Kang1f213912014-07-02 18:35:08 +0900133 case DO_SELECT_TRACK: {
Jae Seo10d285a2014-07-31 22:46:47 +0900134 SomeArgs args = (SomeArgs) msg.obj;
135 mTvInputSessionImpl.selectTrack((Integer) args.arg1, (String) args.arg2);
136 args.recycle();
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900137 break;
Dongwon Kang1f213912014-07-02 18:35:08 +0900138 }
Jae Seoa759b112014-07-18 22:16:08 -0700139 case DO_APP_PRIVATE_COMMAND: {
140 SomeArgs args = (SomeArgs) msg.obj;
141 mTvInputSessionImpl.appPrivateCommand((String) args.arg1, (Bundle) args.arg2);
142 args.recycle();
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900143 break;
Jae Seoa759b112014-07-18 22:16:08 -0700144 }
Youngsang Cho9a22f0f2014-04-09 22:51:54 +0900145 case DO_CREATE_OVERLAY_VIEW: {
146 SomeArgs args = (SomeArgs) msg.obj;
Jae Seo6a6059a2014-04-17 21:35:29 -0700147 mTvInputSessionImpl.createOverlayView((IBinder) args.arg1, (Rect) args.arg2);
Youngsang Cho9a22f0f2014-04-09 22:51:54 +0900148 args.recycle();
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900149 break;
Youngsang Cho9a22f0f2014-04-09 22:51:54 +0900150 }
151 case DO_RELAYOUT_OVERLAY_VIEW: {
Jae Seo6a6059a2014-04-17 21:35:29 -0700152 mTvInputSessionImpl.relayoutOverlayView((Rect) msg.obj);
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900153 break;
Youngsang Cho9a22f0f2014-04-09 22:51:54 +0900154 }
155 case DO_REMOVE_OVERLAY_VIEW: {
Jae Seo6a6059a2014-04-17 21:35:29 -0700156 mTvInputSessionImpl.removeOverlayView(true);
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900157 break;
Youngsang Cho9a22f0f2014-04-09 22:51:54 +0900158 }
Jae Seoa9033832015-03-11 19:29:46 -0700159 case DO_UNBLOCK_CONTENT: {
Jae Seo91a801d2014-07-24 13:49:03 -0700160 mTvInputSessionImpl.unblockContent((String) msg.obj);
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900161 break;
Jaewan Kim903d6b72014-07-16 11:28:56 +0900162 }
Dongwon Kang6f0240c2015-03-31 17:56:36 -0700163 case DO_TIME_SHIFT_PAUSE: {
164 mTvInputSessionImpl.timeShiftPause();
165 break;
166 }
167 case DO_TIME_SHIFT_RESUME: {
168 mTvInputSessionImpl.timeShiftResume();
169 break;
170 }
171 case DO_TIME_SHIFT_SEEK_TO: {
172 mTvInputSessionImpl.timeShiftSeekTo((Long) msg.obj);
173 break;
174 }
Jae Seo4b34cc72015-05-15 17:29:39 -0700175 case DO_TIME_SHIFT_SET_PLAYBACK_PARAMS: {
Dongwon Kangcf4f3d52015-05-20 13:57:34 +0900176 mTvInputSessionImpl.timeShiftSetPlaybackParams((PlaybackParams) msg.obj);
Dongwon Kang6f0240c2015-03-31 17:56:36 -0700177 break;
178 }
Jae Seo465f0d62015-04-06 18:40:46 -0700179 case DO_TIME_SHIFT_ENABLE_POSITION_TRACKING: {
180 mTvInputSessionImpl.timeShiftEnablePositionTracking((Boolean) msg.obj);
Dongwon Kang6f0240c2015-03-31 17:56:36 -0700181 break;
182 }
Jae Seo39570912014-02-20 18:23:25 -0800183 default: {
184 Log.w(TAG, "Unhandled message code: " + msg.what);
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900185 break;
Jae Seo39570912014-02-20 18:23:25 -0800186 }
187 }
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900188 long duration = System.currentTimeMillis() - startTime;
Dongwon Kang21add3e2015-04-27 17:43:52 +0900189 if (duration > EXECUTE_MESSAGE_TIMEOUT_SHORT_MILLIS) {
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900190 Log.w(TAG, "Handling message (" + msg.what + ") took too long time (duration="
191 + duration + "ms)");
Dongwon Kang21add3e2015-04-27 17:43:52 +0900192 if (msg.what == DO_TUNE && duration > EXECUTE_MESSAGE_TUNE_TIMEOUT_MILLIS) {
Dongwon Kangc9181722014-11-18 14:29:50 +0900193 throw new RuntimeException("Too much time to handle tune request. (" + duration
Dongwon Kang21add3e2015-04-27 17:43:52 +0900194 + "ms > " + EXECUTE_MESSAGE_TUNE_TIMEOUT_MILLIS + "ms) "
Dongwon Kangc9181722014-11-18 14:29:50 +0900195 + "Consider handling the tune request in a separate thread.");
196 }
Dongwon Kang21add3e2015-04-27 17:43:52 +0900197 if (duration > EXECUTE_MESSAGE_TIMEOUT_LONG_MILLIS) {
198 throw new RuntimeException("Too much time to handle a request. (type=" + msg.what +
199 ", " + duration + "ms > " + EXECUTE_MESSAGE_TIMEOUT_LONG_MILLIS + "ms).");
200 }
Youngsang Cho9df2c1b2014-09-03 22:27:56 +0900201 }
Jae Seo39570912014-02-20 18:23:25 -0800202 }
203
204 @Override
205 public void release() {
Dongwon Kangce34c6d2014-10-27 17:55:27 +0900206 mTvInputSessionImpl.scheduleOverlayViewCleanup();
Jae Seo39570912014-02-20 18:23:25 -0800207 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_RELEASE));
208 }
209
210 @Override
Ji-Hwan Lee15c56aa2014-08-18 22:01:55 +0900211 public void setMain(boolean isMain) {
212 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_MAIN, isMain));
Ji-Hwan Lee4c526972014-07-22 04:46:30 +0900213 }
214
215 @Override
Jae Seo39570912014-02-20 18:23:25 -0800216 public void setSurface(Surface surface) {
217 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_SURFACE, surface));
218 }
219
220 @Override
Youngsang Choe821d712014-07-16 14:22:19 -0700221 public void dispatchSurfaceChanged(int format, int width, int height) {
222 mCaller.executeOrSendMessage(mCaller.obtainMessageIIII(DO_DISPATCH_SURFACE_CHANGED,
223 format, width, height, 0));
224 }
225
226 @Override
Jae Seo39570912014-02-20 18:23:25 -0800227 public final void setVolume(float volume) {
Dongwon Kang4b662d12014-07-17 19:24:56 +0900228 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_STREAM_VOLUME, volume));
Jae Seo39570912014-02-20 18:23:25 -0800229 }
230
231 @Override
Sungsoo Lim1a6b25e2014-07-09 10:40:43 +0900232 public void tune(Uri channelUri, Bundle params) {
Dongwon Kangc9181722014-11-18 14:29:50 +0900233 // Clear the pending tune requests.
234 mCaller.removeMessages(DO_TUNE);
Sungsoo Lim1a6b25e2014-07-09 10:40:43 +0900235 mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_TUNE, channelUri, params));
Jae Seo39570912014-02-20 18:23:25 -0800236 }
Youngsang Cho9a22f0f2014-04-09 22:51:54 +0900237
238 @Override
Jae Seo2c1c31c2014-07-10 14:57:01 -0700239 public void setCaptionEnabled(boolean enabled) {
240 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_CAPTION_ENABLED, enabled));
241 }
242
243 @Override
Jae Seo10d285a2014-07-31 22:46:47 +0900244 public void selectTrack(int type, String trackId) {
245 mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_SELECT_TRACK, type, trackId));
Dongwon Kang1f213912014-07-02 18:35:08 +0900246 }
247
248 @Override
Jae Seoa759b112014-07-18 22:16:08 -0700249 public void appPrivateCommand(String action, Bundle data) {
250 mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_APP_PRIVATE_COMMAND, action,
251 data));
252 }
253
254 @Override
Youngsang Cho9a22f0f2014-04-09 22:51:54 +0900255 public void createOverlayView(IBinder windowToken, Rect frame) {
256 mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_CREATE_OVERLAY_VIEW, windowToken,
257 frame));
258 }
259
260 @Override
261 public void relayoutOverlayView(Rect frame) {
262 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_RELAYOUT_OVERLAY_VIEW, frame));
263 }
264
265 @Override
266 public void removeOverlayView() {
267 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_REMOVE_OVERLAY_VIEW));
268 }
Jae Seo6a6059a2014-04-17 21:35:29 -0700269
Jaewan Kim903d6b72014-07-16 11:28:56 +0900270 @Override
Jae Seoa9033832015-03-11 19:29:46 -0700271 public void unblockContent(String unblockedRating) {
Sungsoo Lim9bf671f2014-07-19 12:59:51 +0900272 mCaller.executeOrSendMessage(mCaller.obtainMessageO(
Jae Seoa9033832015-03-11 19:29:46 -0700273 DO_UNBLOCK_CONTENT, unblockedRating));
Jaewan Kim903d6b72014-07-16 11:28:56 +0900274 }
275
Dongwon Kang6f0240c2015-03-31 17:56:36 -0700276 @Override
277 public void timeShiftPause() {
278 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_TIME_SHIFT_PAUSE));
279 }
280
281 @Override
282 public void timeShiftResume() {
283 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_TIME_SHIFT_RESUME));
284 }
285
286 @Override
287 public void timeShiftSeekTo(long timeMs) {
Jae Seo093d9942015-06-05 16:13:03 -0700288 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_TIME_SHIFT_SEEK_TO, timeMs));
Dongwon Kang6f0240c2015-03-31 17:56:36 -0700289 }
290
291 @Override
Jae Seo4b34cc72015-05-15 17:29:39 -0700292 public void timeShiftSetPlaybackParams(PlaybackParams params) {
293 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_TIME_SHIFT_SET_PLAYBACK_PARAMS,
294 params));
Dongwon Kang6f0240c2015-03-31 17:56:36 -0700295 }
296
297 @Override
Jae Seo465f0d62015-04-06 18:40:46 -0700298 public void timeShiftEnablePositionTracking(boolean enable) {
299 mCaller.executeOrSendMessage(mCaller.obtainMessageO(
Jae Seo093d9942015-06-05 16:13:03 -0700300 DO_TIME_SHIFT_ENABLE_POSITION_TRACKING, enable));
Dongwon Kang6f0240c2015-03-31 17:56:36 -0700301 }
302
Jae Seo6a6059a2014-04-17 21:35:29 -0700303 private final class TvInputEventReceiver extends InputEventReceiver {
304 public TvInputEventReceiver(InputChannel inputChannel, Looper looper) {
305 super(inputChannel, looper);
306 }
307
308 @Override
309 public void onInputEvent(InputEvent event) {
310 if (mTvInputSessionImpl == null) {
311 // The session has been finished.
312 finishInputEvent(event, false);
313 return;
314 }
315
316 int handled = mTvInputSessionImpl.dispatchInputEvent(event, this);
Jae Seo782f7342014-06-03 12:51:21 -0700317 if (handled != TvInputManager.Session.DISPATCH_IN_PROGRESS) {
318 finishInputEvent(event, handled == TvInputManager.Session.DISPATCH_HANDLED);
Jae Seo6a6059a2014-04-17 21:35:29 -0700319 }
320 }
321 }
Jae Seo39570912014-02-20 18:23:25 -0800322}