OSDN Git Service

am 9d8a30d9: am a7f02a5c: am ef2e7667: Merge "[ActivityManager] Avoid keeping restart...
[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_CREATE_OVERLAY_VIEW = 5;
48     private static final int DO_RELAYOUT_OVERLAY_VIEW = 6;
49     private static final int DO_REMOVE_OVERLAY_VIEW = 7;
50
51     private final HandlerCaller mCaller;
52
53     private TvInputService.Session mTvInputSessionImpl;
54     private InputChannel mChannel;
55     private TvInputEventReceiver mReceiver;
56
57     public ITvInputSessionWrapper(Context context, TvInputService.Session sessionImpl,
58             InputChannel channel) {
59         mCaller = new HandlerCaller(context, null, this, true /* asyncHandler */);
60         mTvInputSessionImpl = sessionImpl;
61         mChannel = channel;
62         if (channel != null) {
63             mReceiver = new TvInputEventReceiver(channel, context.getMainLooper());
64         }
65     }
66
67     @Override
68     public void executeMessage(Message msg) {
69         if (mTvInputSessionImpl == null) {
70             return;
71         }
72
73         switch (msg.what) {
74             case DO_RELEASE: {
75                 mTvInputSessionImpl.release();
76                 mTvInputSessionImpl = null;
77                 if (mReceiver != null) {
78                     mReceiver.dispose();
79                     mReceiver = null;
80                 }
81                 if (mChannel != null) {
82                     mChannel.dispose();
83                     mChannel = null;
84                 }
85                 return;
86             }
87             case DO_SET_SURFACE: {
88                 mTvInputSessionImpl.setSurface((Surface) msg.obj);
89                 return;
90             }
91             case DO_SET_VOLUME: {
92                 mTvInputSessionImpl.setVolume((Float) msg.obj);
93                 return;
94             }
95             case DO_TUNE: {
96                 mTvInputSessionImpl.tune((Uri) msg.obj);
97                 return;
98             }
99             case DO_CREATE_OVERLAY_VIEW: {
100                 SomeArgs args = (SomeArgs) msg.obj;
101                 mTvInputSessionImpl.createOverlayView((IBinder) args.arg1, (Rect) args.arg2);
102                 args.recycle();
103                 return;
104             }
105             case DO_RELAYOUT_OVERLAY_VIEW: {
106                 mTvInputSessionImpl.relayoutOverlayView((Rect) msg.obj);
107                 return;
108             }
109             case DO_REMOVE_OVERLAY_VIEW: {
110                 mTvInputSessionImpl.removeOverlayView(true);
111                 return;
112             }
113             default: {
114                 Log.w(TAG, "Unhandled message code: " + msg.what);
115                 return;
116             }
117         }
118     }
119
120     @Override
121     public void release() {
122         mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_RELEASE));
123     }
124
125     @Override
126     public void setSurface(Surface surface) {
127         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_SURFACE, surface));
128     }
129
130     @Override
131     public final void setVolume(float volume) {
132         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_VOLUME, volume));
133     }
134
135     @Override
136     public void tune(Uri channelUri) {
137         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_TUNE, channelUri));
138     }
139
140     @Override
141     public void createOverlayView(IBinder windowToken, Rect frame) {
142         mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_CREATE_OVERLAY_VIEW, windowToken,
143                 frame));
144     }
145
146     @Override
147     public void relayoutOverlayView(Rect frame) {
148         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_RELAYOUT_OVERLAY_VIEW, frame));
149     }
150
151     @Override
152     public void removeOverlayView() {
153         mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_REMOVE_OVERLAY_VIEW));
154     }
155
156     private final class TvInputEventReceiver extends InputEventReceiver {
157         public TvInputEventReceiver(InputChannel inputChannel, Looper looper) {
158             super(inputChannel, looper);
159         }
160
161         @Override
162         public void onInputEvent(InputEvent event) {
163             if (mTvInputSessionImpl == null) {
164                 // The session has been finished.
165                 finishInputEvent(event, false);
166                 return;
167             }
168
169             int handled = mTvInputSessionImpl.dispatchInputEvent(event, this);
170             if (handled != TvInputManager.Session.DISPATCH_IN_PROGRESS) {
171                 finishInputEvent(event, handled == TvInputManager.Session.DISPATCH_HANDLED);
172             }
173         }
174     }
175 }