OSDN Git Service

06ee4b57ebec1e4cf7a0f2b9fb18df8026cc6d21
[android-x86/frameworks-base.git] / media / java / android / media / tv / ITvInputSessionWrapper.java
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
17 package android.media.tv;
18
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;
30
31 import com.android.internal.os.HandlerCaller;
32 import com.android.internal.os.SomeArgs;
33
34 /**
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.
37  *
38  * @hide
39  */
40 public class ITvInputSessionWrapper extends ITvInputSession.Stub implements HandlerCaller.Callback {
41     private static final String TAG = "TvInputSessionWrapper";
42
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;
52
53     private final HandlerCaller mCaller;
54
55     private TvInputService.Session mTvInputSessionImpl;
56     private InputChannel mChannel;
57     private TvInputEventReceiver mReceiver;
58
59     public ITvInputSessionWrapper(Context context, TvInputService.Session sessionImpl,
60             InputChannel channel) {
61         mCaller = new HandlerCaller(context, null, this, true /* asyncHandler */);
62         mTvInputSessionImpl = sessionImpl;
63         mChannel = channel;
64         if (channel != null) {
65             mReceiver = new TvInputEventReceiver(channel, context.getMainLooper());
66         }
67     }
68
69     @Override
70     public void executeMessage(Message msg) {
71         if (mTvInputSessionImpl == null) {
72             return;
73         }
74
75         switch (msg.what) {
76             case DO_RELEASE: {
77                 mTvInputSessionImpl.release();
78                 mTvInputSessionImpl = null;
79                 if (mReceiver != null) {
80                     mReceiver.dispose();
81                     mReceiver = null;
82                 }
83                 if (mChannel != null) {
84                     mChannel.dispose();
85                     mChannel = null;
86                 }
87                 return;
88             }
89             case DO_SET_SURFACE: {
90                 mTvInputSessionImpl.setSurface((Surface) msg.obj);
91                 return;
92             }
93             case DO_SET_VOLUME: {
94                 mTvInputSessionImpl.setVolume((Float) msg.obj);
95                 return;
96             }
97             case DO_TUNE: {
98                 mTvInputSessionImpl.tune((Uri) msg.obj);
99                 return;
100             }
101             case DO_SELECT_TRACK: {
102                 mTvInputSessionImpl.selectTrack((TvTrackInfo) msg.obj);
103                 return;
104             }
105             case DO_UNSELECT_TRACK: {
106                 mTvInputSessionImpl.unselectTrack((TvTrackInfo) msg.obj);
107                 return;
108             }
109             case DO_CREATE_OVERLAY_VIEW: {
110                 SomeArgs args = (SomeArgs) msg.obj;
111                 mTvInputSessionImpl.createOverlayView((IBinder) args.arg1, (Rect) args.arg2);
112                 args.recycle();
113                 return;
114             }
115             case DO_RELAYOUT_OVERLAY_VIEW: {
116                 mTvInputSessionImpl.relayoutOverlayView((Rect) msg.obj);
117                 return;
118             }
119             case DO_REMOVE_OVERLAY_VIEW: {
120                 mTvInputSessionImpl.removeOverlayView(true);
121                 return;
122             }
123             default: {
124                 Log.w(TAG, "Unhandled message code: " + msg.what);
125                 return;
126             }
127         }
128     }
129
130     @Override
131     public void release() {
132         mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_RELEASE));
133     }
134
135     @Override
136     public void setSurface(Surface surface) {
137         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_SURFACE, surface));
138     }
139
140     @Override
141     public final void setVolume(float volume) {
142         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_VOLUME, volume));
143     }
144
145     @Override
146     public void tune(Uri channelUri) {
147         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_TUNE, channelUri));
148     }
149
150     @Override
151     public void selectTrack(TvTrackInfo track) {
152         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SELECT_TRACK, track));
153     }
154
155     @Override
156     public void unselectTrack(TvTrackInfo track) {
157         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_UNSELECT_TRACK, track));
158     }
159
160     @Override
161     public void createOverlayView(IBinder windowToken, Rect frame) {
162         mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_CREATE_OVERLAY_VIEW, windowToken,
163                 frame));
164     }
165
166     @Override
167     public void relayoutOverlayView(Rect frame) {
168         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_RELAYOUT_OVERLAY_VIEW, frame));
169     }
170
171     @Override
172     public void removeOverlayView() {
173         mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_REMOVE_OVERLAY_VIEW));
174     }
175
176     private final class TvInputEventReceiver extends InputEventReceiver {
177         public TvInputEventReceiver(InputChannel inputChannel, Looper looper) {
178             super(inputChannel, looper);
179         }
180
181         @Override
182         public void onInputEvent(InputEvent event) {
183             if (mTvInputSessionImpl == null) {
184                 // The session has been finished.
185                 finishInputEvent(event, false);
186                 return;
187             }
188
189             int handled = mTvInputSessionImpl.dispatchInputEvent(event, this);
190             if (handled != TvInputManager.Session.DISPATCH_IN_PROGRESS) {
191                 finishInputEvent(event, handled == TvInputManager.Session.DISPATCH_HANDLED);
192             }
193         }
194     }
195 }