Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame^] | 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.tv; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.net.Uri; |
| 21 | import android.os.Message; |
| 22 | import android.util.Log; |
| 23 | import android.view.Surface; |
| 24 | |
| 25 | import com.android.internal.os.HandlerCaller; |
| 26 | |
| 27 | /** |
| 28 | * Implements the internal ITvInputSession interface to convert incoming calls on to it back to |
| 29 | * calls on the public TvInputSession interface, scheduling them on the main thread of the process. |
| 30 | * |
| 31 | * @hide |
| 32 | */ |
| 33 | public class ITvInputSessionWrapper extends ITvInputSession.Stub implements HandlerCaller.Callback { |
| 34 | private static final String TAG = "TvInputSessionWrapper"; |
| 35 | |
| 36 | private static final int DO_RELEASE = 1; |
| 37 | private static final int DO_SET_SURFACE = 2; |
| 38 | private static final int DO_SET_VOLUME = 3; |
| 39 | private static final int DO_TUNE = 4; |
| 40 | |
| 41 | private TvInputSession mTvInputSession; |
| 42 | private final HandlerCaller mCaller; |
| 43 | |
| 44 | public ITvInputSessionWrapper(Context context, TvInputSession session) { |
| 45 | mCaller = new HandlerCaller(context, null, this, true /* asyncHandler */); |
| 46 | mTvInputSession = session; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void executeMessage(Message msg) { |
| 51 | if (mTvInputSession == null) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | switch (msg.what) { |
| 56 | case DO_RELEASE: { |
| 57 | mTvInputSession.release(); |
| 58 | mTvInputSession = null; |
| 59 | return; |
| 60 | } |
| 61 | case DO_SET_SURFACE: { |
| 62 | mTvInputSession.setSurface((Surface) msg.obj); |
| 63 | return; |
| 64 | } |
| 65 | case DO_SET_VOLUME: { |
| 66 | mTvInputSession.setVolume((Float) msg.obj); |
| 67 | return; |
| 68 | } |
| 69 | case DO_TUNE: { |
| 70 | mTvInputSession.tune((Uri) msg.obj); |
| 71 | return; |
| 72 | } |
| 73 | default: { |
| 74 | Log.w(TAG, "Unhandled message code: " + msg.what); |
| 75 | return; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void release() { |
| 82 | mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_RELEASE)); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void setSurface(Surface surface) { |
| 87 | mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_SURFACE, surface)); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public final void setVolume(float volume) { |
| 92 | mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_VOLUME, volume)); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void tune(Uri channelUri) { |
| 97 | mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_TUNE, channelUri)); |
| 98 | } |
| 99 | } |