TvInputService.java revision 25c9c5edab42d6c9e9e0469ab04fb7ff87704d1c
13957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo/*
23957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo * Copyright (C) 2014 The Android Open Source Project
33957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo *
43957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo * Licensed under the Apache License, Version 2.0 (the "License");
53957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo * you may not use this file except in compliance with the License.
63957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo * You may obtain a copy of the License at
73957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo *
83957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo *      http://www.apache.org/licenses/LICENSE-2.0
93957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo *
103957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo * Unless required by applicable law or agreed to in writing, software
113957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo * distributed under the License is distributed on an "AS IS" BASIS,
123957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo * See the License for the specific language governing permissions and
143957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo * limitations under the License.
153957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo */
163957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
17d5cc4a281e7ce29d1e8687ff3394b57a3a549260Jae Seopackage android.media.tv;
183957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
19a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seoimport android.annotation.MainThread;
20c8b7356434f665c494504661a943323c0bbe702eJae Seoimport android.annotation.NonNull;
214bf607b00c14c031e991ac9dc0ad49b9249c9162Dongwon Kangimport android.annotation.Nullable;
222c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seoimport android.annotation.SuppressLint;
23187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kimimport android.annotation.SystemApi;
24345af96677d5fbfc00f8f38a46ab7d57c1ff4cbbJae Seoimport android.app.ActivityManager;
253957091ba8f08c02b5e781098cb955a5f697a1ffJae Seoimport android.app.Service;
269a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Choimport android.content.Context;
273957091ba8f08c02b5e781098cb955a5f697a1ffJae Seoimport android.content.Intent;
289a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Choimport android.graphics.PixelFormat;
299a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Choimport android.graphics.Rect;
3061f4fbd2e8436a1ecd478c2a1f516d064a24d43bJungshik Jangimport android.hardware.hdmi.HdmiDeviceInfo;
313d04b7693428dd887c93e2ac7f4e90fd0cc87859Jae Seoimport android.media.PlaybackParams;
323957091ba8f08c02b5e781098cb955a5f697a1ffJae Seoimport android.net.Uri;
33ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kangimport android.os.AsyncTask;
34832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Choimport android.os.Bundle;
353957091ba8f08c02b5e781098cb955a5f697a1ffJae Seoimport android.os.Handler;
363957091ba8f08c02b5e781098cb955a5f697a1ffJae Seoimport android.os.IBinder;
373957091ba8f08c02b5e781098cb955a5f697a1ffJae Seoimport android.os.Message;
38ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kangimport android.os.Process;
393957091ba8f08c02b5e781098cb955a5f697a1ffJae Seoimport android.os.RemoteCallbackList;
403957091ba8f08c02b5e781098cb955a5f697a1ffJae Seoimport android.os.RemoteException;
41bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kangimport android.text.TextUtils;
423957091ba8f08c02b5e781098cb955a5f697a1ffJae Seoimport android.util.Log;
439a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Choimport android.view.Gravity;
446a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seoimport android.view.InputChannel;
456a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seoimport android.view.InputDevice;
466a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seoimport android.view.InputEvent;
476a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seoimport android.view.InputEventReceiver;
486a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seoimport android.view.KeyEvent;
496a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seoimport android.view.MotionEvent;
503957091ba8f08c02b5e781098cb955a5f697a1ffJae Seoimport android.view.Surface;
519a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Choimport android.view.View;
529a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Choimport android.view.WindowManager;
532c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seoimport android.view.accessibility.CaptioningManager;
54ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kangimport android.widget.FrameLayout;
553957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
566a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seoimport com.android.internal.os.SomeArgs;
57de08be8f79ea40f3dffae9edff4227704a5c0a3aJae Seoimport com.android.internal.util.Preconditions;
583957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
593eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kangimport java.util.ArrayList;
6010d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seoimport java.util.HashSet;
611f213914c45c23c653f721690da2ce0718e63139Dongwon Kangimport java.util.List;
6210d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seoimport java.util.Set;
631f213914c45c23c653f721690da2ce0718e63139Dongwon Kang
643957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo/**
65782f7345471072b630e58c7abd3579b0015273b1Jae Seo * The TvInputService class represents a TV input or source such as HDMI or built-in tuner which
66782f7345471072b630e58c7abd3579b0015273b1Jae Seo * provides pass-through video or broadcast TV programs.
670610e12733875a267f59d87a2a68aebbf486066eDongwon Kang *
680610e12733875a267f59d87a2a68aebbf486066eDongwon Kang * <p>Applications will not normally use this service themselves, instead relying on the standard
69782f7345471072b630e58c7abd3579b0015273b1Jae Seo * interaction provided by {@link TvView}. Those implementing TV input services should normally do
70782f7345471072b630e58c7abd3579b0015273b1Jae Seo * so by deriving from this class and providing their own session implementation based on
71782f7345471072b630e58c7abd3579b0015273b1Jae Seo * {@link TvInputService.Session}. All TV input services must require that clients hold the
72782f7345471072b630e58c7abd3579b0015273b1Jae Seo * {@link android.Manifest.permission#BIND_TV_INPUT} in order to interact with the service; if this
73782f7345471072b630e58c7abd3579b0015273b1Jae Seo * permission is not specified in the manifest, the system will refuse to bind to that TV input
74782f7345471072b630e58c7abd3579b0015273b1Jae Seo * service.
753957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo */
763957091ba8f08c02b5e781098cb955a5f697a1ffJae Seopublic abstract class TvInputService extends Service {
77ee2ec05ed7c0d3cb9115f4ddd7c3613269c4a57bJae Seo    private static final boolean DEBUG = false;
783957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo    private static final String TAG = "TvInputService";
793957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
803957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo    /**
813957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo     * This is the interface name that a service implementing a TV input should say that it support
823957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo     * -- that is, this is the action it uses for its intent filter. To be supported, the service
833957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo     * must also require the {@link android.Manifest.permission#BIND_TV_INPUT} permission so that
843957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo     * other applications cannot abuse it.
853957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo     */
86d5cc4a281e7ce29d1e8687ff3394b57a3a549260Jae Seo    public static final String SERVICE_INTERFACE = "android.media.tv.TvInputService";
873957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
88e7bb7d6bb2257c24076f5a4b9f536f90a6637f58Chulwoo Lee    /**
89e7bb7d6bb2257c24076f5a4b9f536f90a6637f58Chulwoo Lee     * Name under which a TvInputService component publishes information about itself.
90e7bb7d6bb2257c24076f5a4b9f536f90a6637f58Chulwoo Lee     * This meta-data must reference an XML resource containing an
91e7bb7d6bb2257c24076f5a4b9f536f90a6637f58Chulwoo Lee     * <code>&lt;{@link android.R.styleable#TvInputService tv-input}&gt;</code>
92e7bb7d6bb2257c24076f5a4b9f536f90a6637f58Chulwoo Lee     * tag.
93e7bb7d6bb2257c24076f5a4b9f536f90a6637f58Chulwoo Lee     */
94e7bb7d6bb2257c24076f5a4b9f536f90a6637f58Chulwoo Lee    public static final String SERVICE_META_DATA = "android.media.tv.input";
95e7bb7d6bb2257c24076f5a4b9f536f90a6637f58Chulwoo Lee
9666b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang    /**
9766b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang     * Handler instance to handle request from TV Input Manager Service. Should be run in the main
9866b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang     * looper to be synchronously run with {@code Session.mHandler}.
9966b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang     */
100546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo    private final Handler mServiceHandler = new ServiceHandler();
1013957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo    private final RemoteCallbackList<ITvInputServiceCallback> mCallbacks =
102093d994965bef197fb676731fc50f6f6f630b8feJae Seo            new RemoteCallbackList<>();
1033957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
104911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang    private TvInputManager mTvInputManager;
105911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang
1063957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo    @Override
1073957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo    public final IBinder onBind(Intent intent) {
1083957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        return new ITvInputService.Stub() {
1093957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            @Override
1103957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            public void registerCallback(ITvInputServiceCallback cb) {
1113957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                if (cb != null) {
1123957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                    mCallbacks.register(cb);
1133957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                }
1143957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            }
1153957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
1163957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            @Override
1173957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            public void unregisterCallback(ITvInputServiceCallback cb) {
1183957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                if (cb != null) {
1193957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                    mCallbacks.unregister(cb);
1203957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                }
1213957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            }
1223957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
1233957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            @Override
124187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            public void createSession(InputChannel channel, ITvInputSessionCallback cb,
125187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                    String inputId) {
1266a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                if (channel == null) {
1276a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    Log.w(TAG, "Creating session without input channel");
1286a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                }
1296a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                if (cb == null) {
1306a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    return;
1313957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                }
1326a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                SomeArgs args = SomeArgs.obtain();
1336a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                args.arg1 = channel;
1346a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                args.arg2 = cb;
135187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                args.arg3 = inputId;
13666b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang                mServiceHandler.obtainMessage(ServiceHandler.DO_CREATE_SESSION, args).sendToTarget();
1373957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            }
138187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim
139187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            @Override
140a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            public void createRecordingSession(ITvInputSessionCallback cb, String inputId) {
141a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                if (cb == null) {
142a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    return;
143a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                }
144a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                SomeArgs args = SomeArgs.obtain();
145a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                args.arg1 = cb;
146a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                args.arg2 = inputId;
147a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                mServiceHandler.obtainMessage(ServiceHandler.DO_CREATE_RECORDING_SESSION, args)
148a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        .sendToTarget();
149a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            }
150a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
151a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            @Override
152187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            public void notifyHardwareAdded(TvInputHardwareInfo hardwareInfo) {
1531abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                mServiceHandler.obtainMessage(ServiceHandler.DO_ADD_HARDWARE_INPUT,
154187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                        hardwareInfo).sendToTarget();
155187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            }
156187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim
157187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            @Override
1584f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee            public void notifyHardwareRemoved(TvInputHardwareInfo hardwareInfo) {
1591abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                mServiceHandler.obtainMessage(ServiceHandler.DO_REMOVE_HARDWARE_INPUT,
1604f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                        hardwareInfo).sendToTarget();
1614f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee            }
1624f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee
1634f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee            @Override
164546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo            public void notifyHdmiDeviceAdded(HdmiDeviceInfo deviceInfo) {
1651abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                mServiceHandler.obtainMessage(ServiceHandler.DO_ADD_HDMI_INPUT,
166546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo                        deviceInfo).sendToTarget();
1674f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee            }
1684f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee
1694f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee            @Override
170546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo            public void notifyHdmiDeviceRemoved(HdmiDeviceInfo deviceInfo) {
1711abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                mServiceHandler.obtainMessage(ServiceHandler.DO_REMOVE_HDMI_INPUT,
172546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo                        deviceInfo).sendToTarget();
173187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            }
1743957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        };
1753957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo    }
1763957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
1773957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo    /**
178782f7345471072b630e58c7abd3579b0015273b1Jae Seo     * Returns a concrete implementation of {@link Session}.
1790610e12733875a267f59d87a2a68aebbf486066eDongwon Kang     *
1800610e12733875a267f59d87a2a68aebbf486066eDongwon Kang     * <p>May return {@code null} if this TV input service fails to create a session for some
1810610e12733875a267f59d87a2a68aebbf486066eDongwon Kang     * reason. If TV input represents an external device connected to a hardware TV input,
182bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang     * {@link HardwareSession} should be returned.
1830610e12733875a267f59d87a2a68aebbf486066eDongwon Kang     *
184187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim     * @param inputId The ID of the TV input associated with the session.
185187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim     */
1864bf607b00c14c031e991ac9dc0ad49b9249c9162Dongwon Kang    @Nullable
1871032f034ff4f0506872e4899b5a232057abe724bWonsik Kim    public abstract Session onCreateSession(String inputId);
188187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim
189187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim    /**
190a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     * Returns a concrete implementation of {@link RecordingSession}.
191a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     *
192a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     * <p>May return {@code null} if this TV input service fails to create a recording session for
193a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     * some reason.
194a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     *
195a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     * @param inputId The ID of the TV input associated with the recording session.
196a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     */
197a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo    @Nullable
198533b34568280e61d885c3ea438fdb585ea2e4ba0Jae Seo    public RecordingSession onCreateRecordingSession(String inputId) {
199533b34568280e61d885c3ea438fdb585ea2e4ba0Jae Seo        return null;
200533b34568280e61d885c3ea438fdb585ea2e4ba0Jae Seo    }
201a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
202a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo    /**
2034f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     * Returns a new {@link TvInputInfo} object if this service is responsible for
2044f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     * {@code hardwareInfo}; otherwise, return {@code null}. Override to modify default behavior of
2054f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     * ignoring all hardware input.
206187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim     *
2074f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     * @param hardwareInfo {@link TvInputHardwareInfo} object just added.
208187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim     * @hide
209187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim     */
2104bf607b00c14c031e991ac9dc0ad49b9249c9162Dongwon Kang    @Nullable
211187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim    @SystemApi
212187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim    public TvInputInfo onHardwareAdded(TvInputHardwareInfo hardwareInfo) {
213187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim        return null;
214187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim    }
215187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim
216187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim    /**
217187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim     * Returns the input ID for {@code deviceId} if it is handled by this service;
2184f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     * otherwise, return {@code null}. Override to modify default behavior of ignoring all hardware
2194f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     * input.
220187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim     *
2214f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     * @param hardwareInfo {@link TvInputHardwareInfo} object just removed.
222187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim     * @hide
223187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim     */
2244bf607b00c14c031e991ac9dc0ad49b9249c9162Dongwon Kang    @Nullable
225187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim    @SystemApi
2264f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee    public String onHardwareRemoved(TvInputHardwareInfo hardwareInfo) {
2274f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee        return null;
2284f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee    }
2294f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee
2304f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee    /**
2314f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     * Returns a new {@link TvInputInfo} object if this service is responsible for
232546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo     * {@code deviceInfo}; otherwise, return {@code null}. Override to modify default behavior of
233546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo     * ignoring all HDMI logical input device.
2344f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     *
235546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo     * @param deviceInfo {@link HdmiDeviceInfo} object just added.
2364f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     * @hide
2374f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     */
2384bf607b00c14c031e991ac9dc0ad49b9249c9162Dongwon Kang    @Nullable
2394f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee    @SystemApi
240546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo    public TvInputInfo onHdmiDeviceAdded(HdmiDeviceInfo deviceInfo) {
2414f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee        return null;
2424f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee    }
2434f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee
2444f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee    /**
2458960d1b1552729e3dfd33deee951ac75933ad8e5Jinsuk Kim     * Returns the input ID for {@code deviceInfo} if it is handled by this service; otherwise,
246546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo     * return {@code null}. Override to modify default behavior of ignoring all HDMI logical input
247546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo     * device.
2484f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     *
249546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo     * @param deviceInfo {@link HdmiDeviceInfo} object just removed.
2504f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     * @hide
2514f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee     */
2524bf607b00c14c031e991ac9dc0ad49b9249c9162Dongwon Kang    @Nullable
2534f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee    @SystemApi
254546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo    public String onHdmiDeviceRemoved(HdmiDeviceInfo deviceInfo) {
255187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim        return null;
256187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim    }
257187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim
258a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
259a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo    /**
260a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     * Sets the TvInputInfo for this TV input.
261a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     *
262a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     * <p>The system service automatically creates the TvInputInfo for each TV input based on
263a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     * information collected from the AndroidManifest.xml, thus it is not necessary to call this
264a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     * method unless the TV input has additional information to pass such as ability to record and
265c2a89510ddda390d6d53ff24dd20d257fcd2379eJae Seo     * tuner count. Attempting to change information about a TV input that the calling package does
266c2a89510ddda390d6d53ff24dd20d257fcd2379eJae Seo     * not own does nothing.
267a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     *
268c2a89510ddda390d6d53ff24dd20d257fcd2379eJae Seo     * @param context The application context.
269a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     * @param inputInfo The TvInputInfo object that contains that new information.
270a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     */
271c2a89510ddda390d6d53ff24dd20d257fcd2379eJae Seo    public static final void setTvInputInfo(Context context, TvInputInfo inputInfo) {
272c2a89510ddda390d6d53ff24dd20d257fcd2379eJae Seo        TvInputManager manager = (TvInputManager) context.getSystemService(
273c2a89510ddda390d6d53ff24dd20d257fcd2379eJae Seo                Context.TV_INPUT_SERVICE);
274c2a89510ddda390d6d53ff24dd20d257fcd2379eJae Seo        manager.setTvInputInfo(inputInfo);
275a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo    }
276a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
277911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang    private boolean isPassthroughInput(String inputId) {
278911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang        if (mTvInputManager == null) {
279911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang            mTvInputManager = (TvInputManager) getSystemService(Context.TV_INPUT_SERVICE);
280911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang        }
281911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang        TvInputInfo info = mTvInputManager.getTvInputInfo(inputId);
2826e4cbfd2e5ffb739269e5e4affc2b6894bc4090eJae Seo        return info != null && info.isPassthroughInput();
283911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang    }
284911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang
285187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim    /**
286b8a64416e5e7cf39fd899fa600a940b0ef3c15fdJae Seo     * Base class for derived classes to implement to provide a TV input session.
2873957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo     */
28866b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang    public abstract static class Session implements KeyEvent.Callback {
2896f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        private static final int DETACH_OVERLAY_VIEW_TIMEOUT_MS = 5000;
2906f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        private static final int POSITION_UPDATE_INTERVAL_MS = 1000;
2916f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
2926a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        private final KeyEvent.DispatcherState mDispatcherState = new KeyEvent.DispatcherState();
2939a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        private final WindowManager mWindowManager;
29466b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang        final Handler mHandler;
2959a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        private WindowManager.LayoutParams mWindowParams;
296f836206818ce338db83a3c23c486fb8cab29cb6dYoungsang Cho        private Surface mSurface;
297c6a1e5d16c9e1fab5e597f308c3886e512791289Jae Seo        private final Context mContext;
298ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang        private FrameLayout mOverlayViewContainer;
2999a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        private View mOverlayView;
300ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang        private OverlayViewCleanUpTask mOverlayViewCleanUpTask;
3019a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        private boolean mOverlayViewEnabled;
3029a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        private IBinder mWindowToken;
3039a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        private Rect mOverlayFrame;
304465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo        private long mStartPositionMs;
3056f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        private long mCurrentPositionMs;
306465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo        private final TimeShiftPositionTrackingRunnable
307465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                mTimeShiftPositionTrackingRunnable = new TimeShiftPositionTrackingRunnable();
3083eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang
309c6a1e5d16c9e1fab5e597f308c3886e512791289Jae Seo        private final Object mLock = new Object();
3103eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang        // @GuardedBy("mLock")
311832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho        private ITvInputSessionCallback mSessionCallback;
3123eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang        // @GuardedBy("mLock")
313c6a1e5d16c9e1fab5e597f308c3886e512791289Jae Seo        private final List<Runnable> mPendingActions = new ArrayList<>();
3149a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho
31566b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang        /**
31666b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang         * Creates a new Session.
31766b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang         *
31866b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang         * @param context The context of the application
31966b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang         */
32066b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang        public Session(Context context) {
321ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            mContext = context;
32266b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang            mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
32366b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang            mHandler = new Handler(context.getMainLooper());
3246f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            mCurrentPositionMs = TvInputManager.TIME_SHIFT_INVALID_TIME;
3259a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        }
3269a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho
3276a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        /**
328ba2a1c3b92631a65d6dd1916a9d28368e1f5f589Dongwon Kang         * Enables or disables the overlay view.
329ba2a1c3b92631a65d6dd1916a9d28368e1f5f589Dongwon Kang         *
330ba2a1c3b92631a65d6dd1916a9d28368e1f5f589Dongwon Kang         * <p>By default, the overlay view is disabled. Must be called explicitly after the
331ba2a1c3b92631a65d6dd1916a9d28368e1f5f589Dongwon Kang         * session is created to enable the overlay view.
332ba2a1c3b92631a65d6dd1916a9d28368e1f5f589Dongwon Kang         *
333ba2a1c3b92631a65d6dd1916a9d28368e1f5f589Dongwon Kang         * <p>The TV input service can disable its overlay view when the size of the overlay view is
334ba2a1c3b92631a65d6dd1916a9d28368e1f5f589Dongwon Kang         * insufficient to display the whole information, such as when used in Picture-in-picture.
335ba2a1c3b92631a65d6dd1916a9d28368e1f5f589Dongwon Kang         * Override {@link #onOverlayViewSizeChanged} to get the size of the overlay view, which
336ba2a1c3b92631a65d6dd1916a9d28368e1f5f589Dongwon Kang         * then can be used to determine whether to enable/disable the overlay view.
3376a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *
3386a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param enable {@code true} if you want to enable the overlay view. {@code false}
3396a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *            otherwise.
3406a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         */
3419a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        public void setOverlayViewEnabled(final boolean enable) {
3429a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mHandler.post(new Runnable() {
3439a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                @Override
3449a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                public void run() {
3459a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                    if (enable == mOverlayViewEnabled) {
3469a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                        return;
3479a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                    }
3489a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                    mOverlayViewEnabled = enable;
3499a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                    if (enable) {
3509a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                        if (mWindowToken != null) {
3519a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                            createOverlayView(mWindowToken, mOverlayFrame);
3529a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                        }
3539a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                    } else {
3549a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                        removeOverlayView(false);
3559a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                    }
3569a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                }
3579a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            });
3589a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        }
3599a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho
3603957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        /**
361832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho         * Dispatches an event to the application using this session.
362832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho         *
363832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho         * @param eventType The type of the event.
364832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho         * @param eventArgs Optional arguments of the event.
365832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho         * @hide
366832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho         */
3673eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang        @SystemApi
368de08be8f79ea40f3dffae9edff4227704a5c0a3aJae Seo        public void notifySessionEvent(@NonNull final String eventType, final Bundle eventArgs) {
369de08be8f79ea40f3dffae9edff4227704a5c0a3aJae Seo            Preconditions.checkNotNull(eventType);
370a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
371832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho                @Override
372832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho                public void run() {
373832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho                    try {
37491a801d42f3acc35404da51ba26605093922503aJae Seo                        if (DEBUG) Log.d(TAG, "notifySessionEvent(" + eventType + ")");
375711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        if (mSessionCallback != null) {
376711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                            mSessionCallback.onSessionEvent(eventType, eventArgs);
377711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        }
378832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho                    } catch (RemoteException e) {
379465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in sending event (event=" + eventType + ")", e);
380832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho                    }
381832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho                }
382832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho            });
383832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho        }
384832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho
385832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho        /**
3864640d3d81a182e8f3ce50199bff1d97519d27938Jae Seo         * Informs the application that the current channel is re-tuned for some reason and the
3874640d3d81a182e8f3ce50199bff1d97519d27938Jae Seo         * session now displays the content from a new channel. This is used to handle special cases
3884640d3d81a182e8f3ce50199bff1d97519d27938Jae Seo         * such as when the current channel becomes unavailable, it is necessary to send the user to
3894640d3d81a182e8f3ce50199bff1d97519d27938Jae Seo         * a certain channel or the user changes channel in some other way (e.g. by using a
3904640d3d81a182e8f3ce50199bff1d97519d27938Jae Seo         * dedicated remote).
391a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang         *
3924640d3d81a182e8f3ce50199bff1d97519d27938Jae Seo         * @param channelUri The URI of the new channel.
393a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang         */
39491a801d42f3acc35404da51ba26605093922503aJae Seo        public void notifyChannelRetuned(final Uri channelUri) {
395a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
396a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
397a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang                @Override
398a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang                public void run() {
399a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang                    try {
40091a801d42f3acc35404da51ba26605093922503aJae Seo                        if (DEBUG) Log.d(TAG, "notifyChannelRetuned");
401711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        if (mSessionCallback != null) {
402711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                            mSessionCallback.onChannelRetuned(channelUri);
403711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        }
404a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang                    } catch (RemoteException e) {
405465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in notifyChannelRetuned", e);
406a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang                    }
407a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang                }
408a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang            });
409a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang        }
410a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang
411a3be12a236aef0d9c4ff1274075f1e7899d29153Dongwon Kang        /**
4128ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * Sends the list of all audio/video/subtitle tracks. The is used by the framework to
4138ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * maintain the track information for a given session, which in turn is used by
4148ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * {@link TvView#getTracks} for the application to retrieve metadata for a given track type.
4158ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * The TV input service must call this method as soon as the track information becomes
4168ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * available or is updated. Note that in a case where a part of the information for a
4178ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * certain track is updated, it is not necessary to create a new {@link TvTrackInfo} object
4188ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * with a different track ID.
419b93ccca6139a7ee2dba5c110e5f8213a2bd231e5Dongwon Kang         *
4201f213914c45c23c653f721690da2ce0718e63139Dongwon Kang         * @param tracks A list which includes track information.
42110d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         * @throws IllegalArgumentException if {@code tracks} contains redundant tracks.
42210d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         */
42310d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo        public void notifyTracksChanged(final List<TvTrackInfo> tracks) {
424093d994965bef197fb676731fc50f6f6f630b8feJae Seo            Set<String> trackIdSet = new HashSet<>();
42510d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo            for (TvTrackInfo track : tracks) {
42610d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo                String trackId = track.getId();
42710d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo                if (trackIdSet.contains(trackId)) {
42810d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo                    throw new IllegalArgumentException("redundant track ID: " + trackId);
42910d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo                }
43010d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo                trackIdSet.add(trackId);
43110d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo            }
43210d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo            trackIdSet.clear();
43310d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo
43410d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo            // TODO: Validate the track list.
4355a3ef42422ea542fce1d8d1d5b0fbf61d4a570a6Terry Heo            final List<TvTrackInfo> tracksCopy = new ArrayList<>(tracks);
436a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
437a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
438b93ccca6139a7ee2dba5c110e5f8213a2bd231e5Dongwon Kang                @Override
439b93ccca6139a7ee2dba5c110e5f8213a2bd231e5Dongwon Kang                public void run() {
440b93ccca6139a7ee2dba5c110e5f8213a2bd231e5Dongwon Kang                    try {
44110d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo                        if (DEBUG) Log.d(TAG, "notifyTracksChanged");
442711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        if (mSessionCallback != null) {
4435a3ef42422ea542fce1d8d1d5b0fbf61d4a570a6Terry Heo                            mSessionCallback.onTracksChanged(tracksCopy);
444711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        }
445b93ccca6139a7ee2dba5c110e5f8213a2bd231e5Dongwon Kang                    } catch (RemoteException e) {
446465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in notifyTracksChanged", e);
447b93ccca6139a7ee2dba5c110e5f8213a2bd231e5Dongwon Kang                    }
448b93ccca6139a7ee2dba5c110e5f8213a2bd231e5Dongwon Kang                }
449b93ccca6139a7ee2dba5c110e5f8213a2bd231e5Dongwon Kang            });
450b93ccca6139a7ee2dba5c110e5f8213a2bd231e5Dongwon Kang        }
451b93ccca6139a7ee2dba5c110e5f8213a2bd231e5Dongwon Kang
452b93ccca6139a7ee2dba5c110e5f8213a2bd231e5Dongwon Kang        /**
4538ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * Sends the type and ID of a selected track. This is used to inform the application that a
4548ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * specific track is selected. The TV input service must call this method as soon as a track
4558ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * is selected either by default or in response to a call to {@link #onSelectTrack}. The
4568ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * selected track ID for a given type is maintained in the framework until the next call to
4578ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * this method even after the entire track list is updated (but is reset when the session is
4588ddb01ec7b1360e9fccf341b5f1989182160fceeJae Seo         * tuned to a new channel), so care must be taken not to result in an obsolete track ID.
459d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo         *
46010d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         * @param type The type of the selected track. The type can be
46110d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         *            {@link TvTrackInfo#TYPE_AUDIO}, {@link TvTrackInfo#TYPE_VIDEO} or
46210d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         *            {@link TvTrackInfo#TYPE_SUBTITLE}.
46310d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         * @param trackId The ID of the selected track.
46410d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         * @see #onSelectTrack
465d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo         */
46610d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo        public void notifyTrackSelected(final int type, final String trackId) {
467a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
468a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
469d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo                @Override
470d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo                public void run() {
471d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo                    try {
47210d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo                        if (DEBUG) Log.d(TAG, "notifyTrackSelected");
473711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        if (mSessionCallback != null) {
474711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                            mSessionCallback.onTrackSelected(type, trackId);
475711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        }
476d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo                    } catch (RemoteException e) {
477465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in notifyTrackSelected", e);
478d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo                    }
479d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo                }
480d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo            });
481d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo        }
482d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo
483d5ce9759524740cfb02638fd1d7b44315957b422Jae Seo        /**
4841bccd280f59b86d00c04dcc9d827d76b373c600aNick Chalko         * Informs the application that the video is now available for watching. Video is blocked
4851bccd280f59b86d00c04dcc9d827d76b373c600aNick Chalko         * until this method is called.
4861bccd280f59b86d00c04dcc9d827d76b373c600aNick Chalko         *
4871bccd280f59b86d00c04dcc9d827d76b373c600aNick Chalko         * <p>The TV input service must call this method as soon as the content rendered onto its
4881bccd280f59b86d00c04dcc9d827d76b373c600aNick Chalko         * surface is ready for viewing. This method must be called each time {@link #onTune(Uri)}
4891bccd280f59b86d00c04dcc9d827d76b373c600aNick Chalko         * is called.
490d254611778be6bcaf091f1b081caf839dedf79acJae Seo         *
491d254611778be6bcaf091f1b081caf839dedf79acJae Seo         * @see #notifyVideoUnavailable
4929b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang         */
49391a801d42f3acc35404da51ba26605093922503aJae Seo        public void notifyVideoAvailable() {
494a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
495a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
4969b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                @Override
4979b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                public void run() {
4989b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                    try {
49991a801d42f3acc35404da51ba26605093922503aJae Seo                        if (DEBUG) Log.d(TAG, "notifyVideoAvailable");
500711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        if (mSessionCallback != null) {
501711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                            mSessionCallback.onVideoAvailable();
502711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        }
5039b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                    } catch (RemoteException e) {
504465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in notifyVideoAvailable", e);
5059b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                    }
5069b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                }
5079b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang            });
5089b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang        }
5099b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang
5109b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang        /**
511d254611778be6bcaf091f1b081caf839dedf79acJae Seo         * Informs the application that the video became unavailable for some reason. This is
512d254611778be6bcaf091f1b081caf839dedf79acJae Seo         * primarily used to signal the application to block the screen not to show any intermittent
513d254611778be6bcaf091f1b081caf839dedf79acJae Seo         * video artifacts.
514bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo         *
515d254611778be6bcaf091f1b081caf839dedf79acJae Seo         * @param reason The reason why the video became unavailable:
516d254611778be6bcaf091f1b081caf839dedf79acJae Seo         *            <ul>
517d254611778be6bcaf091f1b081caf839dedf79acJae Seo         *            <li>{@link TvInputManager#VIDEO_UNAVAILABLE_REASON_UNKNOWN}
518d254611778be6bcaf091f1b081caf839dedf79acJae Seo         *            <li>{@link TvInputManager#VIDEO_UNAVAILABLE_REASON_TUNING}
519d254611778be6bcaf091f1b081caf839dedf79acJae Seo         *            <li>{@link TvInputManager#VIDEO_UNAVAILABLE_REASON_WEAK_SIGNAL}
520d254611778be6bcaf091f1b081caf839dedf79acJae Seo         *            <li>{@link TvInputManager#VIDEO_UNAVAILABLE_REASON_BUFFERING}
521ff1f29e1b112e68d16908b1a89225315089f8e50Dongwon Kang         *            <li>{@link TvInputManager#VIDEO_UNAVAILABLE_REASON_AUDIO_ONLY}
522d254611778be6bcaf091f1b081caf839dedf79acJae Seo         *            </ul>
523d254611778be6bcaf091f1b081caf839dedf79acJae Seo         * @see #notifyVideoAvailable
524bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo         */
52591a801d42f3acc35404da51ba26605093922503aJae Seo        public void notifyVideoUnavailable(final int reason) {
526bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo            if (reason < TvInputManager.VIDEO_UNAVAILABLE_REASON_START
527bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo                    || reason > TvInputManager.VIDEO_UNAVAILABLE_REASON_END) {
5288a151a0a2143cd962764f74a75cf4e587f6759d1Jae Seo                Log.e(TAG, "notifyVideoUnavailable - unknown reason: " + reason);
529bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo            }
530a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
531a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
532bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo                @Override
533bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo                public void run() {
534bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo                    try {
53591a801d42f3acc35404da51ba26605093922503aJae Seo                        if (DEBUG) Log.d(TAG, "notifyVideoUnavailable");
536711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        if (mSessionCallback != null) {
537711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                            mSessionCallback.onVideoUnavailable(reason);
538711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        }
539bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo                    } catch (RemoteException e) {
540465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in notifyVideoUnavailable", e);
541bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo                    }
542bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo                }
543bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo            });
544bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo        }
545bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo
546bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo        /**
547bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo         * Informs the application that the user is allowed to watch the current program content.
5480610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
5490610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>Each TV input service is required to query the system whether the user is allowed to
550783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * watch the current program before showing it to the user if the parental controls is
551783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * enabled (i.e. {@link TvInputManager#isParentalControlsEnabled
552783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * TvInputManager.isParentalControlsEnabled()} returns {@code true}). Whether the TV input
553783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * service should block the content or not is determined by invoking
554783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * {@link TvInputManager#isRatingBlocked TvInputManager.isRatingBlocked(TvContentRating)}
555783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * with the content rating for the current program. Then the {@link TvInputManager} makes a
556783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * judgment based on the user blocked ratings stored in the secure settings and returns the
557783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * result. If the rating in question turns out to be allowed by the user, the TV input
558783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * service must call this method to notify the application that is permitted to show the
559783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * content.
5600610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
5610610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>Each TV input service also needs to continuously listen to any changes made to the
562783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * parental controls settings by registering a broadcast receiver to receive
563783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * {@link TvInputManager#ACTION_BLOCKED_RATINGS_CHANGED} and
564783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * {@link TvInputManager#ACTION_PARENTAL_CONTROLS_ENABLED_CHANGED} and immediately
565783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * reevaluate the current program with the new parental controls settings.
5666057102dbb746593a7d59cf377c969b62e38c664Jae Seo         *
56791a801d42f3acc35404da51ba26605093922503aJae Seo         * @see #notifyContentBlocked
568783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * @see TvInputManager
5696057102dbb746593a7d59cf377c969b62e38c664Jae Seo         */
57091a801d42f3acc35404da51ba26605093922503aJae Seo        public void notifyContentAllowed() {
571a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
572a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
5736057102dbb746593a7d59cf377c969b62e38c664Jae Seo                @Override
5746057102dbb746593a7d59cf377c969b62e38c664Jae Seo                public void run() {
5756057102dbb746593a7d59cf377c969b62e38c664Jae Seo                    try {
57691a801d42f3acc35404da51ba26605093922503aJae Seo                        if (DEBUG) Log.d(TAG, "notifyContentAllowed");
577711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        if (mSessionCallback != null) {
578711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                            mSessionCallback.onContentAllowed();
579711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        }
5806057102dbb746593a7d59cf377c969b62e38c664Jae Seo                    } catch (RemoteException e) {
581465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in notifyContentAllowed", e);
5826057102dbb746593a7d59cf377c969b62e38c664Jae Seo                    }
5836057102dbb746593a7d59cf377c969b62e38c664Jae Seo                }
5846057102dbb746593a7d59cf377c969b62e38c664Jae Seo            });
5856057102dbb746593a7d59cf377c969b62e38c664Jae Seo        }
5866057102dbb746593a7d59cf377c969b62e38c664Jae Seo
5876057102dbb746593a7d59cf377c969b62e38c664Jae Seo        /**
588bbcd206a798c8c2845200daf7a2d4cb7b29056f3Jae Seo         * Informs the application that the current program content is blocked by parent controls.
5890610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
5900610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>Each TV input service is required to query the system whether the user is allowed to
591783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * watch the current program before showing it to the user if the parental controls is
592783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * enabled (i.e. {@link TvInputManager#isParentalControlsEnabled
593783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * TvInputManager.isParentalControlsEnabled()} returns {@code true}). Whether the TV input
594783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * service should block the content or not is determined by invoking
595783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * {@link TvInputManager#isRatingBlocked TvInputManager.isRatingBlocked(TvContentRating)}
596936c040ec445afad98ec16fc634ae6573eceefbbJae Seo         * with the content rating for the current program or {@link TvContentRating#UNRATED} in
597936c040ec445afad98ec16fc634ae6573eceefbbJae Seo         * case the rating information is missing. Then the {@link TvInputManager} makes a judgment
598936c040ec445afad98ec16fc634ae6573eceefbbJae Seo         * based on the user blocked ratings stored in the secure settings and returns the result.
599936c040ec445afad98ec16fc634ae6573eceefbbJae Seo         * If the rating in question turns out to be blocked, the TV input service must immediately
600936c040ec445afad98ec16fc634ae6573eceefbbJae Seo         * block the content and call this method with the content rating of the current program to
601936c040ec445afad98ec16fc634ae6573eceefbbJae Seo         * prompt the PIN verification screen.
6020610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
6030610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>Each TV input service also needs to continuously listen to any changes made to the
604783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * parental controls settings by registering a broadcast receiver to receive
605783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * {@link TvInputManager#ACTION_BLOCKED_RATINGS_CHANGED} and
606783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * {@link TvInputManager#ACTION_PARENTAL_CONTROLS_ENABLED_CHANGED} and immediately
607783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * reevaluate the current program with the new parental controls settings.
6089b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang         *
609936c040ec445afad98ec16fc634ae6573eceefbbJae Seo         * @param rating The content rating for the current TV program. Can be
610936c040ec445afad98ec16fc634ae6573eceefbbJae Seo         *            {@link TvContentRating#UNRATED}.
61191a801d42f3acc35404da51ba26605093922503aJae Seo         * @see #notifyContentAllowed
612783645e99f909ffc7a2d5d2fca9324cc0e9b7362Jae Seo         * @see TvInputManager
6139b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang         */
614c8b7356434f665c494504661a943323c0bbe702eJae Seo        public void notifyContentBlocked(@NonNull final TvContentRating rating) {
615de08be8f79ea40f3dffae9edff4227704a5c0a3aJae Seo            Preconditions.checkNotNull(rating);
616a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
617a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
6189b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                @Override
6199b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                public void run() {
6209b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                    try {
62191a801d42f3acc35404da51ba26605093922503aJae Seo                        if (DEBUG) Log.d(TAG, "notifyContentBlocked");
622711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        if (mSessionCallback != null) {
623711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                            mSessionCallback.onContentBlocked(rating.flattenToString());
624711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        }
6259b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                    } catch (RemoteException e) {
626465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in notifyContentBlocked", e);
6279b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                    }
6289b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang                }
6299b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang            });
6309b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang        }
6319b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang
6329b08edff236fc68d836eccfaa1a5f028dc390cecDongwon Kang        /**
633465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * Informs the application that the time shift status is changed.
6340610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
6350610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>Prior to calling this method, the application assumes the status
636465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * {@link TvInputManager#TIME_SHIFT_STATUS_UNKNOWN}. Right after the session is created, it
637465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * is important to invoke the method with the status
638465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * {@link TvInputManager#TIME_SHIFT_STATUS_AVAILABLE} if the implementation does support
639465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * time shifting, or {@link TvInputManager#TIME_SHIFT_STATUS_UNSUPPORTED} otherwise. Failure
640465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * to notifying the current status change immediately might result in an undesirable
641465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * behavior in the application such as hiding the play controls.
6420610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
6430610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>If the status {@link TvInputManager#TIME_SHIFT_STATUS_AVAILABLE} is reported, the
644465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * application assumes it can pause/resume playback, seek to a specified time position and
645465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * set playback rate and audio mode. The implementation should override
646465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * {@link #onTimeShiftPause}, {@link #onTimeShiftResume}, {@link #onTimeShiftSeekTo},
647465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * {@link #onTimeShiftGetStartPosition}, {@link #onTimeShiftGetCurrentPosition} and
6483d04b7693428dd887c93e2ac7f4e90fd0cc87859Jae Seo         * {@link #onTimeShiftSetPlaybackParams}.
6496f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         *
650465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * @param status The current time shift status. Should be one of the followings.
6516f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         * <ul>
652465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * <li>{@link TvInputManager#TIME_SHIFT_STATUS_UNSUPPORTED}
6536f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         * <li>{@link TvInputManager#TIME_SHIFT_STATUS_UNAVAILABLE}
654465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * <li>{@link TvInputManager#TIME_SHIFT_STATUS_AVAILABLE}
6556f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         * </ul>
6566f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         */
6576f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        public void notifyTimeShiftStatusChanged(final int status) {
658a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
659a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
6606f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                @Override
6616f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                public void run() {
6626f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                    try {
6636f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                        if (DEBUG) Log.d(TAG, "notifyTimeShiftStatusChanged");
6646f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                        if (mSessionCallback != null) {
6656f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                            mSessionCallback.onTimeShiftStatusChanged(status);
6666f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                        }
6676f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                    } catch (RemoteException e) {
668465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in notifyTimeShiftStatusChanged", e);
6696f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                    }
6706f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                }
6716f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            });
6726f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
6736f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
674465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo        private void notifyTimeShiftStartPositionChanged(final long timeMs) {
675a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
676a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
6776f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                @Override
6786f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                public void run() {
6796f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                    try {
6806f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                        if (DEBUG) Log.d(TAG, "notifyTimeShiftStartPositionChanged");
6816f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                        if (mSessionCallback != null) {
6826f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                            mSessionCallback.onTimeShiftStartPositionChanged(timeMs);
6836f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                        }
6846f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                    } catch (RemoteException e) {
685465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in notifyTimeShiftStartPositionChanged", e);
6866f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                    }
6876f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                }
6886f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            });
6896f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
6906f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
6916f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        private void notifyTimeShiftCurrentPositionChanged(final long timeMs) {
692a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
693a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
6946f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                @Override
6956f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                public void run() {
6966f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                    try {
6976f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                        if (DEBUG) Log.d(TAG, "notifyTimeShiftCurrentPositionChanged");
6986f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                        if (mSessionCallback != null) {
6996f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                            mSessionCallback.onTimeShiftCurrentPositionChanged(timeMs);
7006f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                        }
7016f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                    } catch (RemoteException e) {
702465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in notifyTimeShiftCurrentPositionChanged", e);
7036f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                    }
7046f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                }
7056f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            });
7066f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
7076f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
7086f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        /**
709606c8a396558e9714159db4969340af170677172Jae Seo         * Assigns a size and position to the surface passed in {@link #onSetSurface}. The position
710606c8a396558e9714159db4969340af170677172Jae Seo         * is relative to the overlay view that sits on top of this surface.
711ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho         *
712ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho         * @param left Left position in pixels, relative to the overlay view.
713ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho         * @param top Top position in pixels, relative to the overlay view.
714ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho         * @param right Right position in pixels, relative to the overlay view.
71579d2b4c810f9359640a0aaa80e610508481cd413Dongwon Kang         * @param bottom Bottom position in pixels, relative to the overlay view.
7165b1caaf7d8408bf0ce78d8d7a36f4649dda17797Jae Seo         * @see #onOverlayViewSizeChanged
717ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho         */
71879d2b4c810f9359640a0aaa80e610508481cd413Dongwon Kang        public void layoutSurface(final int left, final int top, final int right,
71979d2b4c810f9359640a0aaa80e610508481cd413Dongwon Kang                final int bottom) {
72079d2b4c810f9359640a0aaa80e610508481cd413Dongwon Kang            if (left > right || top > bottom) {
721ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                throw new IllegalArgumentException("Invalid parameter");
722ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho            }
723a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
724a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
725ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                @Override
726ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                public void run() {
727ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                    try {
728ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                        if (DEBUG) Log.d(TAG, "layoutSurface (l=" + left + ", t=" + top + ", r="
72979d2b4c810f9359640a0aaa80e610508481cd413Dongwon Kang                                + right + ", b=" + bottom + ",)");
730711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        if (mSessionCallback != null) {
731711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                            mSessionCallback.onLayoutSurface(left, top, right, bottom);
732711914421896ad15ab3c944c3adc838ac67cf2a6Jae Seo                        }
733ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                    } catch (RemoteException e) {
734465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.w(TAG, "error in layoutSurface", e);
735ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                    }
736ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                }
737ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho            });
738ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho        }
739ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho
740ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho        /**
7413957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         * Called when the session is released.
7423957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         */
7433957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        public abstract void onRelease();
7443957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
7453957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        /**
74615c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * Sets the current session as the main session. The main session is a session whose
74715c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * corresponding TV input determines the HDMI-CEC active source device.
7480610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
7490610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>TV input service that manages HDMI-CEC logical device should implement {@link
75015c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * #onSetMain} to (1) select the corresponding HDMI logical device as the source device
75115c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * when {@code isMain} is {@code true}, and to (2) select the internal device (= TV itself)
75215c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * as the source device when {@code isMain} is {@code false} and the session is still main.
75315c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * Also, if a surface is passed to a non-main session and active source is changed to
75415c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * initiate the surface, the active source should be returned to the main session.
7550610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
7560610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>{@link TvView} guarantees that, when tuning involves a session transition, {@code
75715c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * onSetMain(true)} for new session is called first, {@code onSetMain(false)} for old
75815c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * session is called afterwards. This allows {@code onSetMain(false)} to be no-op when TV
75915c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * input service knows that the next main session corresponds to another HDMI logical
76015c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * device. Practically, this implies that one TV input service should handle all HDMI port
76115c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * and HDMI-CEC logical devices for smooth active source transition.
7624c52697dbed682a19dacc78b0c08931ea8dbc6b5Ji-Hwan Lee         *
76315c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * @param isMain If true, session should become main.
76415c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * @see TvView#setMain
7654c52697dbed682a19dacc78b0c08931ea8dbc6b5Ji-Hwan Lee         * @hide
7664c52697dbed682a19dacc78b0c08931ea8dbc6b5Ji-Hwan Lee         */
7674c52697dbed682a19dacc78b0c08931ea8dbc6b5Ji-Hwan Lee        @SystemApi
76815c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee        public void onSetMain(boolean isMain) {
7694c52697dbed682a19dacc78b0c08931ea8dbc6b5Ji-Hwan Lee        }
7704c52697dbed682a19dacc78b0c08931ea8dbc6b5Ji-Hwan Lee
771c6a1e5d16c9e1fab5e597f308c3886e512791289Jae Seo        /**
772606c8a396558e9714159db4969340af170677172Jae Seo         * Called when the application sets the surface.
7730610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
774606c8a396558e9714159db4969340af170677172Jae Seo         * <p>The TV input service should render video onto the given surface. When called with
775606c8a396558e9714159db4969340af170677172Jae Seo         * {@code null}, the input service should immediately release any references to the
776606c8a396558e9714159db4969340af170677172Jae Seo         * currently set surface and stop using it.
777c6a1e5d16c9e1fab5e597f308c3886e512791289Jae Seo         *
778606c8a396558e9714159db4969340af170677172Jae Seo         * @param surface The surface to be used for video rendering. Can be {@code null}.
779606c8a396558e9714159db4969340af170677172Jae Seo         * @return {@code true} if the surface was set successfully, {@code false} otherwise.
780c6a1e5d16c9e1fab5e597f308c3886e512791289Jae Seo         */
7814bf607b00c14c031e991ac9dc0ad49b9249c9162Dongwon Kang        public abstract boolean onSetSurface(@Nullable Surface surface);
7823957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
7833957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        /**
784606c8a396558e9714159db4969340af170677172Jae Seo         * Called after any structural changes (format or size) have been made to the surface passed
785606c8a396558e9714159db4969340af170677172Jae Seo         * in {@link #onSetSurface}. This method is always called at least once, after
786606c8a396558e9714159db4969340af170677172Jae Seo         * {@link #onSetSurface} is called with non-null surface.
787e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho         *
788606c8a396558e9714159db4969340af170677172Jae Seo         * @param format The new PixelFormat of the surface.
789606c8a396558e9714159db4969340af170677172Jae Seo         * @param width The new width of the surface.
790606c8a396558e9714159db4969340af170677172Jae Seo         * @param height The new height of the surface.
791e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho         */
792e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho        public void onSurfaceChanged(int format, int width, int height) {
793e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho        }
794e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho
795e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho        /**
796606c8a396558e9714159db4969340af170677172Jae Seo         * Called when the size of the overlay view is changed by the application.
797606c8a396558e9714159db4969340af170677172Jae Seo         *
798606c8a396558e9714159db4969340af170677172Jae Seo         * <p>This is always called at least once when the session is created regardless of whether
799606c8a396558e9714159db4969340af170677172Jae Seo         * the overlay view is enabled or not. The overlay view size is the same as the containing
800606c8a396558e9714159db4969340af170677172Jae Seo         * {@link TvView}. Note that the size of the underlying surface can be different if the
801606c8a396558e9714159db4969340af170677172Jae Seo         * surface was changed by calling {@link #layoutSurface}.
802ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho         *
803ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho         * @param width The width of the overlay view.
804ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho         * @param height The height of the overlay view.
805ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho         */
806ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho        public void onOverlayViewSizeChanged(int width, int height) {
807ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho        }
808ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho
809ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho        /**
8101da8fb39499e8f5a962f7307fefdfd2ab6b79224Jae Seo         * Sets the relative stream volume of the current TV input session.
8113957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         *
8121da8fb39499e8f5a962f7307fefdfd2ab6b79224Jae Seo         * <p>The implementation should honor this request in order to handle audio focus changes or
8131da8fb39499e8f5a962f7307fefdfd2ab6b79224Jae Seo         * mute the current session when multiple sessions, possibly from different inputs are
8141da8fb39499e8f5a962f7307fefdfd2ab6b79224Jae Seo         * active. If the method has not yet been called, the implementation should assume the
8151da8fb39499e8f5a962f7307fefdfd2ab6b79224Jae Seo         * default value of {@code 1.0f}.
8161da8fb39499e8f5a962f7307fefdfd2ab6b79224Jae Seo         *
8171da8fb39499e8f5a962f7307fefdfd2ab6b79224Jae Seo         * @param volume A volume value between {@code 0.0f} to {@code 1.0f}.
8183957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         */
819782f7345471072b630e58c7abd3579b0015273b1Jae Seo        public abstract void onSetStreamVolume(float volume);
8203957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
8213957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        /**
8221bccd280f59b86d00c04dcc9d827d76b373c600aNick Chalko         * Tunes to a given channel.
8231bccd280f59b86d00c04dcc9d827d76b373c600aNick Chalko         *
8241bccd280f59b86d00c04dcc9d827d76b373c600aNick Chalko         * <p>No video will be displayed until {@link #notifyVideoAvailable()} is called.
8251bccd280f59b86d00c04dcc9d827d76b373c600aNick Chalko         * Also, {@link #notifyVideoUnavailable(int)} should be called when the TV input cannot
8261bccd280f59b86d00c04dcc9d827d76b373c600aNick Chalko         * continue playing the given channel.
8273957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         *
8283957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         * @param channelUri The URI of the channel.
829c6a1e5d16c9e1fab5e597f308c3886e512791289Jae Seo         * @return {@code true} if the tuning was successful, {@code false} otherwise.
8303957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         */
8313957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        public abstract boolean onTune(Uri channelUri);
8323957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
8333957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        /**
8341a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim         * Calls {@link #onTune(Uri)}. Override this method in order to handle {@code params}.
8351a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim         *
8361a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim         * @param channelUri The URI of the channel.
8371a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim         * @param params The extra parameters from other applications.
838c6a1e5d16c9e1fab5e597f308c3886e512791289Jae Seo         * @return {@code true} if the tuning was successful, {@code false} otherwise.
8391a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim         * @hide
8401a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim         */
8411a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim        @SystemApi
8421a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim        public boolean onTune(Uri channelUri, Bundle params) {
8431a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim            return onTune(channelUri);
8441a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim        }
8451a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim
8461a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim        /**
8472c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo         * Enables or disables the caption.
8480610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
8490610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>The locale for the user's preferred captioning language can be obtained by calling
8502c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo         * {@link CaptioningManager#getLocale CaptioningManager.getLocale()}.
8512c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo         *
8522c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo         * @param enabled {@code true} to enable, {@code false} to disable.
8532c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo         * @see CaptioningManager
8542c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo         */
8552c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo        public abstract void onSetCaptionEnabled(boolean enabled);
8562c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo
8572c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo        /**
8589bf671f8ee72b156f16fcf05a3d1c6e093ecba67Sungsoo Lim         * Requests to unblock the content according to the given rating.
8590610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
8600610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>The implementation should unblock the content.
861903d6b72cd572665309633e925485464d08bb25aJaewan Kim         * TV input service has responsibility to decide when/how the unblock expires
862903d6b72cd572665309633e925485464d08bb25aJaewan Kim         * while it can keep previously unblocked ratings in order not to ask a user
863903d6b72cd572665309633e925485464d08bb25aJaewan Kim         * to unblock whenever a content rating is changed.
864903d6b72cd572665309633e925485464d08bb25aJaewan Kim         * Therefore an unblocked rating can be valid for a channel, a program,
865903d6b72cd572665309633e925485464d08bb25aJaewan Kim         * or certain amount of time depending on the implementation.
866903d6b72cd572665309633e925485464d08bb25aJaewan Kim         *
867903d6b72cd572665309633e925485464d08bb25aJaewan Kim         * @param unblockedRating An unblocked content rating
868903d6b72cd572665309633e925485464d08bb25aJaewan Kim         */
86991a801d42f3acc35404da51ba26605093922503aJae Seo        public void onUnblockContent(TvContentRating unblockedRating) {
8709bf671f8ee72b156f16fcf05a3d1c6e093ecba67Sungsoo Lim        }
871903d6b72cd572665309633e925485464d08bb25aJaewan Kim
872903d6b72cd572665309633e925485464d08bb25aJaewan Kim        /**
873c6a1e5d16c9e1fab5e597f308c3886e512791289Jae Seo         * Selects a given track.
8740610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
8750610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>If this is done successfully, the implementation should call
8760610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * {@link #notifyTrackSelected} to help applications maintain the up-to-date list of the
8770610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * selected tracks.
8782c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo         *
87910d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         * @param trackId The ID of the track to select. {@code null} means to unselect the current
88010d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         *            track for a given type.
88110d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         * @param type The type of the track to select. The type can be
88210d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         *            {@link TvTrackInfo#TYPE_AUDIO}, {@link TvTrackInfo#TYPE_VIDEO} or
88310d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         *            {@link TvTrackInfo#TYPE_SUBTITLE}.
884c6a1e5d16c9e1fab5e597f308c3886e512791289Jae Seo         * @return {@code true} if the track selection was successful, {@code false} otherwise.
88510d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         * @see #notifyTrackSelected
88610d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo         */
8874bf607b00c14c031e991ac9dc0ad49b9249c9162Dongwon Kang        public boolean onSelectTrack(int type, @Nullable String trackId) {
8881f213914c45c23c653f721690da2ce0718e63139Dongwon Kang            return false;
8891f213914c45c23c653f721690da2ce0718e63139Dongwon Kang        }
8901f213914c45c23c653f721690da2ce0718e63139Dongwon Kang
8911f213914c45c23c653f721690da2ce0718e63139Dongwon Kang        /**
892a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         * Processes a private command sent from the application to the TV input. This can be used
893a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         * to provide domain-specific features that are only known between certain TV inputs and
894a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         * their clients.
895a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         *
896a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         * @param action Name of the command to be performed. This <em>must</em> be a scoped name,
897a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         *            i.e. prefixed with a package name you own, so that different developers will
898a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         *            not create conflicting commands.
899a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         * @param data Any data to include with the command.
900a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         * @hide
901a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         */
902a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo        @SystemApi
903c8b7356434f665c494504661a943323c0bbe702eJae Seo        public void onAppPrivateCommand(@NonNull String action, Bundle data) {
904a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo        }
905a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo
906a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo        /**
907465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * Called when the application requests to create an overlay view. Each session
9089a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         * implementation can override this method and return its own view.
9099a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         *
9109a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         * @return a view attached to the overlay window
9119a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         */
9129a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        public View onCreateOverlayView() {
9139a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            return null;
9149a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        }
9159a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho
9169a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        /**
917a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * Called when the application requests to play a given recorded TV program.
918a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
919a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @param recordedProgramUri The URI of a recorded TV program.
920a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftResume()
921a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPause()
922a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSeekTo(long)
923a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSetPlaybackParams(PlaybackParams)
924a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetStartPosition()
925a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetCurrentPosition()
926a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
927a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        public void onTimeShiftPlay(Uri recordedProgramUri) {
928a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
929a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
930a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
931465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * Called when the application requests to pause playback.
9326f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         *
933a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPlay(Uri)
934a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftResume()
935a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSeekTo(long)
936a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSetPlaybackParams(PlaybackParams)
937a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetStartPosition()
938a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetCurrentPosition()
9396f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         */
9406f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        public void onTimeShiftPause() {
9416f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
9426f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
9436f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        /**
944465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * Called when the application requests to resume playback.
9456f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         *
946a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPlay(Uri)
947a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPause()
948a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSeekTo(long)
949a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSetPlaybackParams(PlaybackParams)
950a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetStartPosition()
951a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetCurrentPosition()
9526f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         */
9536f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        public void onTimeShiftResume() {
9546f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
9556f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
9566f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        /**
957465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * Called when the application requests to seek to a specified time position. Normally, the
958465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * position is given within range between the start and the current time, inclusively. The
959465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * implementation is expected to seek to the nearest time position if the given position is
960465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * not in the range.
9616f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         *
962465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * @param timeMs The time position to seek to, in milliseconds since the epoch.
963a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPlay(Uri)
964a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftResume()
965a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPause()
966a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSetPlaybackParams(PlaybackParams)
967a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetStartPosition()
968a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetCurrentPosition()
9696f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         */
9706f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        public void onTimeShiftSeekTo(long timeMs) {
9716f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
9726f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
9736f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        /**
9743d04b7693428dd887c93e2ac7f4e90fd0cc87859Jae Seo         * Called when the application sets playback parameters containing the speed and audio mode.
9750610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
9763d04b7693428dd887c93e2ac7f4e90fd0cc87859Jae Seo         * <p>Once the playback parameters are set, the implementation should honor the current
9773d04b7693428dd887c93e2ac7f4e90fd0cc87859Jae Seo         * settings until the next tune request. Pause/resume/seek request does not reset the
9783d04b7693428dd887c93e2ac7f4e90fd0cc87859Jae Seo         * parameters previously set.
9796f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         *
9803d04b7693428dd887c93e2ac7f4e90fd0cc87859Jae Seo         * @param params The playback params.
981a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPlay(Uri)
982a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftResume()
983a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPause()
984a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSeekTo(long)
985a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetStartPosition()
986a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetCurrentPosition()
9876f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         */
9883d04b7693428dd887c93e2ac7f4e90fd0cc87859Jae Seo        public void onTimeShiftSetPlaybackParams(PlaybackParams params) {
9896f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
9906f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
9916f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        /**
992465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * Returns the start playback position for time shifting, in milliseconds since the epoch.
993465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * Returns {@link TvInputManager#TIME_SHIFT_INVALID_TIME} if the position is unknown at the
994465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * moment.
9950610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
9960610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>The start playback position of the time shifted program should be adjusted when the
997465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * implementation cannot retain the whole recorded program due to some reason (e.g.
998465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * limitation on storage space). It is the earliest possible time position that the user can
999465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * seek to, thus failure to notifying its change immediately might result in bad experience
1000465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * where the application allows the user to seek to an invalid time position.
1001465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         *
1002a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPlay(Uri)
1003a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftResume()
1004a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPause()
1005a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSeekTo(long)
1006a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSetPlaybackParams(PlaybackParams)
1007a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetCurrentPosition()
1008465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         */
1009465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo        public long onTimeShiftGetStartPosition() {
1010465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo            return TvInputManager.TIME_SHIFT_INVALID_TIME;
1011465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo        }
1012465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo
1013465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo        /**
1014465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * Returns the current playback position for time shifting, in milliseconds since the epoch.
1015465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * Returns {@link TvInputManager#TIME_SHIFT_INVALID_TIME} if the position is unknown at the
1016465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * moment.
10176f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         *
1018bb3e2674b5ad095d9c1e5e6069cd6b8ee1c1c11eDongwon Kang         * <p>Note that the current playback position should be equal to or greater than the start
1019bb3e2674b5ad095d9c1e5e6069cd6b8ee1c1c11eDongwon Kang         * playback position reported by {@link #onTimeShiftGetStartPosition}. Failure to notifying
1020bb3e2674b5ad095d9c1e5e6069cd6b8ee1c1c11eDongwon Kang         * the correct current position might lead to bad user experience.
1021bb3e2674b5ad095d9c1e5e6069cd6b8ee1c1c11eDongwon Kang         *
1022a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPlay(Uri)
1023a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftResume()
1024a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftPause()
1025a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSeekTo(long)
1026a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftSetPlaybackParams(PlaybackParams)
1027a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @see #onTimeShiftGetStartPosition()
10286f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         */
10296f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        public long onTimeShiftGetCurrentPosition() {
10306f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            return TvInputManager.TIME_SHIFT_INVALID_TIME;
10316f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
10326f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
10336f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        /**
10346a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * Default implementation of {@link android.view.KeyEvent.Callback#onKeyDown(int, KeyEvent)
10356a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * KeyEvent.Callback.onKeyDown()}: always returns false (doesn't handle the event).
10360610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
10370610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>Override this to intercept key down events before they are processed by the
10380610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * application. If you return true, the application will not process the event itself. If
10390610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * you return false, the normal application processing will occur as if the TV input had not
10400610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * seen the event at all.
10416a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *
10426a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param keyCode The value in event.getKeyCode().
10436a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param event Description of the key event.
10446a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @return If you handled the event, return {@code true}. If you want to allow the event to
10456a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *         be handled by the next receiver, return {@code false}.
10466a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         */
10476a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        @Override
10486a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        public boolean onKeyDown(int keyCode, KeyEvent event) {
10496a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            return false;
10506a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        }
10516a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo
10526a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        /**
10536a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * Default implementation of
10546a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * {@link android.view.KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
10556a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle the event).
10560610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
10570610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>Override this to intercept key long press events before they are processed by the
10586a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * application. If you return true, the application will not process the event itself. If
10596a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * you return false, the normal application processing will occur as if the TV input had not
10606a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * seen the event at all.
10616a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *
10626a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param keyCode The value in event.getKeyCode().
10636a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param event Description of the key event.
10646a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @return If you handled the event, return {@code true}. If you want to allow the event to
10656a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *         be handled by the next receiver, return {@code false}.
10666a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         */
10676a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        @Override
10686a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        public boolean onKeyLongPress(int keyCode, KeyEvent event) {
10696a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            return false;
10706a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        }
10716a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo
10726a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        /**
10736a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * Default implementation of
10746a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * {@link android.view.KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
10756a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle the event).
10760610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
10770610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>Override this to intercept special key multiple events before they are processed by
10780610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * the application. If you return true, the application will not itself process the event.
10790610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * If you return false, the normal application processing will occur as if the TV input had
10800610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * not seen the event at all.
10816a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *
10826a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param keyCode The value in event.getKeyCode().
10836a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param count The number of times the action was made.
10846a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param event Description of the key event.
10856a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @return If you handled the event, return {@code true}. If you want to allow the event to
10866a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *         be handled by the next receiver, return {@code false}.
10876a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         */
10886a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        @Override
10896a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) {
10906a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            return false;
10916a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        }
10926a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo
10936a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        /**
10946a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * Default implementation of {@link android.view.KeyEvent.Callback#onKeyUp(int, KeyEvent)
10956a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * KeyEvent.Callback.onKeyUp()}: always returns false (doesn't handle the event).
10960610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
10970610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>Override this to intercept key up events before they are processed by the application.
10980610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * If you return true, the application will not itself process the event. If you return false,
10996a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * the normal application processing will occur as if the TV input had not seen the event at
11006a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * all.
11016a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *
11026a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param keyCode The value in event.getKeyCode().
11036a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param event Description of the key event.
11046a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @return If you handled the event, return {@code true}. If you want to allow the event to
11056a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *         be handled by the next receiver, return {@code false}.
11066a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         */
11076a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        @Override
11086a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        public boolean onKeyUp(int keyCode, KeyEvent event) {
11096a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            return false;
11106a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        }
11116a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo
11126a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        /**
11136a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * Implement this method to handle touch screen motion events on the current input session.
11146a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *
11156a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param event The motion event being received.
11166a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @return If you handled the event, return {@code true}. If you want to allow the event to
11176a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *         be handled by the next receiver, return {@code false}.
11186a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @see View#onTouchEvent
11196a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         */
11206a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        public boolean onTouchEvent(MotionEvent event) {
11216a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            return false;
11226a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        }
11236a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo
11246a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        /**
11256a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * Implement this method to handle trackball events on the current input session.
11266a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *
11276a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param event The motion event being received.
11286a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @return If you handled the event, return {@code true}. If you want to allow the event to
11296a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *         be handled by the next receiver, return {@code false}.
11306a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @see View#onTrackballEvent
11316a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         */
11326a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        public boolean onTrackballEvent(MotionEvent event) {
11336a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            return false;
11346a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        }
11356a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo
11366a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        /**
11376a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * Implement this method to handle generic motion events on the current input session.
11386a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *
11396a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @param event The motion event being received.
11406a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @return If you handled the event, return {@code true}. If you want to allow the event to
11416a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         *         be handled by the next receiver, return {@code false}.
11426a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * @see View#onGenericMotionEvent
11436a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         */
11446a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        public boolean onGenericMotionEvent(MotionEvent event) {
11456a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            return false;
11466a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        }
11476a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo
11486a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        /**
11493957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         * This method is called when the application would like to stop using the current input
11503957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         * session.
11513957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         */
1152674e96216d6a60f0d87d3a6a0d62f358a101532bYoungsang Cho        void release() {
1153674e96216d6a60f0d87d3a6a0d62f358a101532bYoungsang Cho            onRelease();
1154f836206818ce338db83a3c23c486fb8cab29cb6dYoungsang Cho            if (mSurface != null) {
1155f836206818ce338db83a3c23c486fb8cab29cb6dYoungsang Cho                mSurface.release();
1156f836206818ce338db83a3c23c486fb8cab29cb6dYoungsang Cho                mSurface = null;
1157f836206818ce338db83a3c23c486fb8cab29cb6dYoungsang Cho            }
11583eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang            synchronized(mLock) {
11593eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                mSessionCallback = null;
11603eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                mPendingActions.clear();
11613eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang            }
1162ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            // Removes the overlay view lastly so that any hanging on the main thread can be handled
1163ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            // in {@link #scheduleOverlayViewCleanup}.
1164ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            removeOverlayView(true);
1165465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo            mHandler.removeCallbacks(mTimeShiftPositionTrackingRunnable);
11663957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        }
11673957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
11683957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        /**
116915c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee         * Calls {@link #onSetMain}.
11704c52697dbed682a19dacc78b0c08931ea8dbc6b5Ji-Hwan Lee         */
117115c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee        void setMain(boolean isMain) {
117215c56aac985bc8d75f38fb4ecb92dda12d2ca06cJi-Hwan Lee            onSetMain(isMain);
11734c52697dbed682a19dacc78b0c08931ea8dbc6b5Ji-Hwan Lee        }
11744c52697dbed682a19dacc78b0c08931ea8dbc6b5Ji-Hwan Lee
11754c52697dbed682a19dacc78b0c08931ea8dbc6b5Ji-Hwan Lee        /**
11766a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * Calls {@link #onSetSurface}.
11773957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         */
1178674e96216d6a60f0d87d3a6a0d62f358a101532bYoungsang Cho        void setSurface(Surface surface) {
1179674e96216d6a60f0d87d3a6a0d62f358a101532bYoungsang Cho            onSetSurface(surface);
1180f836206818ce338db83a3c23c486fb8cab29cb6dYoungsang Cho            if (mSurface != null) {
1181f836206818ce338db83a3c23c486fb8cab29cb6dYoungsang Cho                mSurface.release();
1182f836206818ce338db83a3c23c486fb8cab29cb6dYoungsang Cho            }
1183f836206818ce338db83a3c23c486fb8cab29cb6dYoungsang Cho            mSurface = surface;
11843957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            // TODO: Handle failure.
11853957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        }
11863957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
11873957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        /**
1188e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho         * Calls {@link #onSurfaceChanged}.
1189e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho         */
1190e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho        void dispatchSurfaceChanged(int format, int width, int height) {
1191e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho            if (DEBUG) {
1192e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho                Log.d(TAG, "dispatchSurfaceChanged(format=" + format + ", width=" + width
1193e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho                        + ", height=" + height + ")");
1194e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho            }
1195e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho            onSurfaceChanged(format, width, height);
1196e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho        }
1197e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho
1198e821d711db1799dc51661a3ed6188f3cd942bae7Youngsang Cho        /**
1199782f7345471072b630e58c7abd3579b0015273b1Jae Seo         * Calls {@link #onSetStreamVolume}.
12003957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         */
12014b662d1b98e312792235c6718afd7c406270d1f1Dongwon Kang        void setStreamVolume(float volume) {
1202782f7345471072b630e58c7abd3579b0015273b1Jae Seo            onSetStreamVolume(volume);
12033957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        }
12043957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
12053957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        /**
12066a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * Calls {@link #onTune}.
12073957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo         */
12081a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim        void tune(Uri channelUri, Bundle params) {
12096f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            mCurrentPositionMs = TvInputManager.TIME_SHIFT_INVALID_TIME;
12101a6b25eabcc1fb66e6e8d76f91fd413e18b793a9Sungsoo Lim            onTune(channelUri, params);
12113957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            // TODO: Handle failure.
12123957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        }
12139a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho
12149a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        /**
12152c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo         * Calls {@link #onSetCaptionEnabled}.
12162c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo         */
12172c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo        void setCaptionEnabled(boolean enabled) {
12182c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo            onSetCaptionEnabled(enabled);
12192c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo        }
12202c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo
12212c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo        /**
12221f213914c45c23c653f721690da2ce0718e63139Dongwon Kang         * Calls {@link #onSelectTrack}.
12231f213914c45c23c653f721690da2ce0718e63139Dongwon Kang         */
122410d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo        void selectTrack(int type, String trackId) {
122510d285ac06b3d3060c7d90d3dc196d4ac8367467Jae Seo            onSelectTrack(type, trackId);
12261f213914c45c23c653f721690da2ce0718e63139Dongwon Kang        }
12271f213914c45c23c653f721690da2ce0718e63139Dongwon Kang
12281f213914c45c23c653f721690da2ce0718e63139Dongwon Kang        /**
122991a801d42f3acc35404da51ba26605093922503aJae Seo         * Calls {@link #onUnblockContent}.
1230903d6b72cd572665309633e925485464d08bb25aJaewan Kim         */
123191a801d42f3acc35404da51ba26605093922503aJae Seo        void unblockContent(String unblockedRating) {
123291a801d42f3acc35404da51ba26605093922503aJae Seo            onUnblockContent(TvContentRating.unflattenFromString(unblockedRating));
1233903d6b72cd572665309633e925485464d08bb25aJaewan Kim            // TODO: Handle failure.
1234903d6b72cd572665309633e925485464d08bb25aJaewan Kim        }
1235903d6b72cd572665309633e925485464d08bb25aJaewan Kim
1236903d6b72cd572665309633e925485464d08bb25aJaewan Kim        /**
1237a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         * Calls {@link #onAppPrivateCommand}.
1238a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo         */
1239a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo        void appPrivateCommand(String action, Bundle data) {
1240a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo            onAppPrivateCommand(action, data);
1241a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo        }
1242a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo
1243a759b111a1c9cb00284038f8a1554bf29709b952Jae Seo        /**
12446a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * Creates an overlay view. This calls {@link #onCreateOverlayView} to get a view to attach
12456a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * to the overlay window.
12469a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         *
1247465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * @param windowToken A window token of the application.
12489a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         * @param frame A position of the overlay view.
12499a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         */
12509a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        void createOverlayView(IBinder windowToken, Rect frame) {
1251ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            if (mOverlayViewContainer != null) {
1252ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                removeOverlayView(false);
12539a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            }
1254832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho            if (DEBUG) Log.d(TAG, "create overlay view(" + frame + ")");
12559a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mWindowToken = windowToken;
12569a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mOverlayFrame = frame;
1257ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho            onOverlayViewSizeChanged(frame.right - frame.left, frame.bottom - frame.top);
12589a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            if (!mOverlayViewEnabled) {
12599a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                return;
12609a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            }
12619a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mOverlayView = onCreateOverlayView();
12629a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            if (mOverlayView == null) {
12639a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                return;
12649a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            }
1265ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            if (mOverlayViewCleanUpTask != null) {
1266ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                mOverlayViewCleanUpTask.cancel(true);
1267ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                mOverlayViewCleanUpTask = null;
1268ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            }
1269ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            // Creates a container view to check hanging on the overlay view detaching.
1270ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            // Adding/removing the overlay view to/from the container make the view attach/detach
1271ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            // logic run on the main thread.
1272ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            mOverlayViewContainer = new FrameLayout(mContext);
1273ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            mOverlayViewContainer.addView(mOverlayView);
12749a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            // TvView's window type is TYPE_APPLICATION_MEDIA and we want to create
12759a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            // an overlay window above the media window but below the application window.
12769a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            int type = WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
12779a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            // We make the overlay view non-focusable and non-touchable so that
12789a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            // the application that owns the window token can decide whether to consume or
12799a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            // dispatch the input events.
1280345af96677d5fbfc00f8f38a46ab7d57c1ff4cbbJae Seo            int flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1281345af96677d5fbfc00f8f38a46ab7d57c1ff4cbbJae Seo                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
1282345af96677d5fbfc00f8f38a46ab7d57c1ff4cbbJae Seo                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
1283345af96677d5fbfc00f8f38a46ab7d57c1ff4cbbJae Seo            if (ActivityManager.isHighEndGfx()) {
1284345af96677d5fbfc00f8f38a46ab7d57c1ff4cbbJae Seo                flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
1285345af96677d5fbfc00f8f38a46ab7d57c1ff4cbbJae Seo            }
12869a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mWindowParams = new WindowManager.LayoutParams(
12879a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                    frame.right - frame.left, frame.bottom - frame.top,
1288345af96677d5fbfc00f8f38a46ab7d57c1ff4cbbJae Seo                    frame.left, frame.top, type, flags, PixelFormat.TRANSPARENT);
12899a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mWindowParams.privateFlags |=
12909a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                    WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
12919a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mWindowParams.gravity = Gravity.START | Gravity.TOP;
12929a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mWindowParams.token = windowToken;
1293ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            mWindowManager.addView(mOverlayViewContainer, mWindowParams);
12949a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        }
12959a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho
12969a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        /**
12979a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         * Relayouts the current overlay view.
12989a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         *
12999a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         * @param frame A new position of the overlay view.
13009a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         */
13019a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        void relayoutOverlayView(Rect frame) {
1302832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho            if (DEBUG) Log.d(TAG, "relayoutOverlayView(" + frame + ")");
1303ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho            if (mOverlayFrame == null || mOverlayFrame.width() != frame.width()
1304ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                    || mOverlayFrame.height() != frame.height()) {
1305ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                // Note: relayoutOverlayView is called whenever TvView's layout is changed
1306ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                // regardless of setOverlayViewEnabled.
1307ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho                onOverlayViewSizeChanged(frame.right - frame.left, frame.bottom - frame.top);
1308ff04ae757a5542d2d5633e75b7adacc4fce1ce7eYoungsang Cho            }
13099a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mOverlayFrame = frame;
1310ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            if (!mOverlayViewEnabled || mOverlayViewContainer == null) {
13119a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                return;
13129a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            }
13139a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mWindowParams.x = frame.left;
13149a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mWindowParams.y = frame.top;
13159a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mWindowParams.width = frame.right - frame.left;
13169a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            mWindowParams.height = frame.bottom - frame.top;
1317ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            mWindowManager.updateViewLayout(mOverlayViewContainer, mWindowParams);
13189a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        }
13199a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho
13209a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        /**
13219a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         * Removes the current overlay view.
13229a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho         */
13239a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        void removeOverlayView(boolean clearWindowToken) {
1324ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            if (DEBUG) Log.d(TAG, "removeOverlayView(" + mOverlayViewContainer + ")");
13259a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            if (clearWindowToken) {
13269a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                mWindowToken = null;
13279a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                mOverlayFrame = null;
13289a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            }
1329ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            if (mOverlayViewContainer != null) {
1330ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                // Removes the overlay view from the view hierarchy in advance so that it can be
1331ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                // cleaned up in the {@link OverlayViewCleanUpTask} if the remove process is
1332ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                // hanging.
1333ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                mOverlayViewContainer.removeView(mOverlayView);
13349a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                mOverlayView = null;
1335ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                mWindowManager.removeView(mOverlayViewContainer);
1336ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                mOverlayViewContainer = null;
13379a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho                mWindowParams = null;
13389a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho            }
13399a22f0f0a631849d9c622c642d3ab0395f77584bYoungsang Cho        }
13406a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo
13416a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        /**
1342a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * Calls {@link #onTimeShiftPlay(Uri)}.
1343a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1344a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        void timeShiftPlay(Uri recordedProgramUri) {
1345a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            mCurrentPositionMs = 0;
1346a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            onTimeShiftPlay(recordedProgramUri);
1347a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1348a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1349a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
13506f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         * Calls {@link #onTimeShiftPause}.
13516f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         */
13526f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        void timeShiftPause() {
13536f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            onTimeShiftPause();
13546f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
13556f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
13566f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        /**
13576f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         * Calls {@link #onTimeShiftResume}.
13586f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         */
13596f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        void timeShiftResume() {
13606f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            onTimeShiftResume();
13616f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
13626f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
13636f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        /**
13646f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         * Calls {@link #onTimeShiftSeekTo}.
13656f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         */
13666f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        void timeShiftSeekTo(long timeMs) {
13676f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            onTimeShiftSeekTo(timeMs);
13686f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
13696f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
13706f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        /**
13713d04b7693428dd887c93e2ac7f4e90fd0cc87859Jae Seo         * Calls {@link #onTimeShiftSetPlaybackParams}.
13726f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         */
13733d04b7693428dd887c93e2ac7f4e90fd0cc87859Jae Seo        void timeShiftSetPlaybackParams(PlaybackParams params) {
13743d04b7693428dd887c93e2ac7f4e90fd0cc87859Jae Seo            onTimeShiftSetPlaybackParams(params);
13756f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
13766f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
13776f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        /**
1378465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * Enable/disable position tracking.
1379465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         *
1380465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo         * @param enable {@code true} to enable tracking, {@code false} otherwise.
13816f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang         */
1382465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo        void timeShiftEnablePositionTracking(boolean enable) {
1383465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo            if (enable) {
1384465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                mHandler.post(mTimeShiftPositionTrackingRunnable);
13856f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            } else {
1386465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                mHandler.removeCallbacks(mTimeShiftPositionTrackingRunnable);
1387465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                mStartPositionMs = TvInputManager.TIME_SHIFT_INVALID_TIME;
13886f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                mCurrentPositionMs = TvInputManager.TIME_SHIFT_INVALID_TIME;
13896f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            }
13906f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
13916f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
13926f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        /**
1393ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang         * Schedules a task which checks whether the overlay view is detached and kills the process
1394ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang         * if it is not. Note that this method is expected to be called in a non-main thread.
1395ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang         */
1396ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang        void scheduleOverlayViewCleanup() {
1397ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            View overlayViewParent = mOverlayViewContainer;
1398ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            if (overlayViewParent != null) {
1399ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                mOverlayViewCleanUpTask = new OverlayViewCleanUpTask();
1400ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                mOverlayViewCleanUpTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
1401ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                        overlayViewParent);
1402ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            }
1403ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang        }
1404ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang
1405ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang        /**
14066a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         * Takes care of dispatching incoming input events and tells whether the event was handled.
14076a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo         */
14086a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        int dispatchInputEvent(InputEvent event, InputEventReceiver receiver) {
14096a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            if (DEBUG) Log.d(TAG, "dispatchInputEvent(" + event + ")");
141044fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            boolean isNavigationKey = false;
141153aa5744c9a233e54b8f8b8389a86f99abcc4244Youngsang Cho            boolean skipDispatchToOverlayView = false;
14126a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            if (event instanceof KeyEvent) {
141344fbbca35459ee756a531661eba53ec33419790aYoungsang Cho                KeyEvent keyEvent = (KeyEvent) event;
141444fbbca35459ee756a531661eba53ec33419790aYoungsang Cho                if (keyEvent.dispatch(this, mDispatcherState, this)) {
1415782f7345471072b630e58c7abd3579b0015273b1Jae Seo                    return TvInputManager.Session.DISPATCH_HANDLED;
14166a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                }
141753aa5744c9a233e54b8f8b8389a86f99abcc4244Youngsang Cho                isNavigationKey = isNavigationKey(keyEvent.getKeyCode());
141853aa5744c9a233e54b8f8b8389a86f99abcc4244Youngsang Cho                // When media keys and KEYCODE_MEDIA_AUDIO_TRACK are dispatched to ViewRootImpl,
1419465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                // ViewRootImpl always consumes the keys. In this case, the application loses
142053aa5744c9a233e54b8f8b8389a86f99abcc4244Youngsang Cho                // a chance to handle media keys. Therefore, media keys are not dispatched to
142153aa5744c9a233e54b8f8b8389a86f99abcc4244Youngsang Cho                // ViewRootImpl.
142253aa5744c9a233e54b8f8b8389a86f99abcc4244Youngsang Cho                skipDispatchToOverlayView = KeyEvent.isMediaKey(keyEvent.getKeyCode())
142353aa5744c9a233e54b8f8b8389a86f99abcc4244Youngsang Cho                        || keyEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK;
14246a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            } else if (event instanceof MotionEvent) {
14256a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                MotionEvent motionEvent = (MotionEvent) event;
14266a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                final int source = motionEvent.getSource();
14276a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                if (motionEvent.isTouchEvent()) {
14286a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    if (onTouchEvent(motionEvent)) {
1429782f7345471072b630e58c7abd3579b0015273b1Jae Seo                        return TvInputManager.Session.DISPATCH_HANDLED;
14306a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    }
14316a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
14326a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    if (onTrackballEvent(motionEvent)) {
1433782f7345471072b630e58c7abd3579b0015273b1Jae Seo                        return TvInputManager.Session.DISPATCH_HANDLED;
14346a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    }
14356a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                } else {
14366a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    if (onGenericMotionEvent(motionEvent)) {
1437782f7345471072b630e58c7abd3579b0015273b1Jae Seo                        return TvInputManager.Session.DISPATCH_HANDLED;
14386a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    }
14396a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                }
14406a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            }
144153aa5744c9a233e54b8f8b8389a86f99abcc4244Youngsang Cho            if (mOverlayViewContainer == null || !mOverlayViewContainer.isAttachedToWindow()
144253aa5744c9a233e54b8f8b8389a86f99abcc4244Youngsang Cho                    || skipDispatchToOverlayView) {
1443782f7345471072b630e58c7abd3579b0015273b1Jae Seo                return TvInputManager.Session.DISPATCH_NOT_HANDLED;
14446a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            }
1445ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            if (!mOverlayViewContainer.hasWindowFocus()) {
1446ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                mOverlayViewContainer.getViewRootImpl().windowFocusChanged(true, true);
14476a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo            }
1448ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            if (isNavigationKey && mOverlayViewContainer.hasFocusable()) {
144944fbbca35459ee756a531661eba53ec33419790aYoungsang Cho                // If mOverlayView has focusable views, navigation key events should be always
145044fbbca35459ee756a531661eba53ec33419790aYoungsang Cho                // handled. If not, it can make the application UI navigation messed up.
145144fbbca35459ee756a531661eba53ec33419790aYoungsang Cho                // For example, in the case that the left-most view is focused, a left key event
145244fbbca35459ee756a531661eba53ec33419790aYoungsang Cho                // will not be handled in ViewRootImpl. Then, the left key event will be handled in
145344fbbca35459ee756a531661eba53ec33419790aYoungsang Cho                // the application during the UI navigation of the TV input.
1454ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                mOverlayViewContainer.getViewRootImpl().dispatchInputEvent(event);
145544fbbca35459ee756a531661eba53ec33419790aYoungsang Cho                return TvInputManager.Session.DISPATCH_HANDLED;
145644fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            } else {
1457ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                mOverlayViewContainer.getViewRootImpl().dispatchInputEvent(event, receiver);
145844fbbca35459ee756a531661eba53ec33419790aYoungsang Cho                return TvInputManager.Session.DISPATCH_IN_PROGRESS;
145944fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            }
14606a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo        }
1461832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho
14623eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang        private void initialize(ITvInputSessionCallback callback) {
14633eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang            synchronized(mLock) {
14643eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                mSessionCallback = callback;
14653eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                for (Runnable runnable : mPendingActions) {
14663eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    runnable.run();
14673eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                }
14683eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                mPendingActions.clear();
14693eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang            }
1470832860fb9f6b3a7188a6af2d5d67806593595800Youngsang Cho        }
14719044be13dc0b625f10c459574abdd22603f86d50Youngsang Cho
1472a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        private void executeOrPostRunnableOnMainThread(Runnable action) {
14733eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang            synchronized(mLock) {
14743eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                if (mSessionCallback == null) {
14753eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    // The session is not initialized yet.
14763eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    mPendingActions.add(action);
14773eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                } else {
14783eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    if (mHandler.getLooper().isCurrentThread()) {
14793eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                        action.run();
14803eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    } else {
14813eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                        // Posts the runnable if this is not called from the main thread
14823eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                        mHandler.post(action);
14833eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    }
14843eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                }
14859044be13dc0b625f10c459574abdd22603f86d50Youngsang Cho            }
14869044be13dc0b625f10c459574abdd22603f86d50Youngsang Cho        }
1487ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang
1488465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo        private final class TimeShiftPositionTrackingRunnable implements Runnable {
14896f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            @Override
14906f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            public void run() {
1491465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                long startPositionMs = onTimeShiftGetStartPosition();
1492465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                if (mStartPositionMs != startPositionMs) {
1493465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                    mStartPositionMs = startPositionMs;
1494465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                    notifyTimeShiftStartPositionChanged(startPositionMs);
1495465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                }
1496465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                long currentPositionMs = onTimeShiftGetCurrentPosition();
1497bb3e2674b5ad095d9c1e5e6069cd6b8ee1c1c11eDongwon Kang                if (currentPositionMs < mStartPositionMs) {
1498bb3e2674b5ad095d9c1e5e6069cd6b8ee1c1c11eDongwon Kang                    Log.w(TAG, "Current position (" + currentPositionMs + ") cannot be earlier than"
1499bb3e2674b5ad095d9c1e5e6069cd6b8ee1c1c11eDongwon Kang                            + " start position (" + mStartPositionMs + "). Reset to the start "
1500bb3e2674b5ad095d9c1e5e6069cd6b8ee1c1c11eDongwon Kang                            + "position.");
1501bb3e2674b5ad095d9c1e5e6069cd6b8ee1c1c11eDongwon Kang                    currentPositionMs = mStartPositionMs;
1502bb3e2674b5ad095d9c1e5e6069cd6b8ee1c1c11eDongwon Kang                }
1503465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                if (mCurrentPositionMs != currentPositionMs) {
1504465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                    mCurrentPositionMs = currentPositionMs;
1505465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                    notifyTimeShiftCurrentPositionChanged(currentPositionMs);
15066f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                }
1507465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                mHandler.removeCallbacks(mTimeShiftPositionTrackingRunnable);
1508465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                mHandler.postDelayed(mTimeShiftPositionTrackingRunnable,
15096f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                        POSITION_UPDATE_INTERVAL_MS);
15106f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang            }
15116f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang        }
15126f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang
1513ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang        private final class OverlayViewCleanUpTask extends AsyncTask<View, Void, Void> {
1514ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            @Override
1515ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            protected Void doInBackground(View... views) {
1516ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                View overlayViewParent = views[0];
1517ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                try {
15186f0240cf63fe62b0af2c7d5112f9881d1e167bfcDongwon Kang                    Thread.sleep(DETACH_OVERLAY_VIEW_TIMEOUT_MS);
1519ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                } catch (InterruptedException e) {
1520ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                    return null;
1521ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                }
1522ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                if (isCancelled()) {
1523ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                    return null;
1524ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                }
1525ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                if (overlayViewParent.isAttachedToWindow()) {
1526ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                    Log.e(TAG, "Time out on releasing overlay view. Killing "
1527ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                            + overlayViewParent.getContext().getPackageName());
1528ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                    Process.killProcess(Process.myPid());
1529ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                }
1530ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang                return null;
1531ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang            }
1532ce34c6d308629c214ab9b7963755eb60cac03c9dDongwon Kang        }
15333957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo    }
15343957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo
1535bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang    /**
1536a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     * Base class for derived classes to implement to provide a TV input recording session.
1537a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo     */
1538a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo    public abstract static class RecordingSession {
1539a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        final Handler mHandler;
1540a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1541a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        private final Object mLock = new Object();
1542a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        // @GuardedBy("mLock")
1543a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        private ITvInputSessionCallback mSessionCallback;
1544a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        // @GuardedBy("mLock")
1545a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        private final List<Runnable> mPendingActions = new ArrayList<>();
1546a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1547a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1548e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * Creates a new RecordingSession.
1549a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
1550a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @param context The context of the application
1551a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1552a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        public RecordingSession(Context context) {
1553a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            mHandler = new Handler(context.getMainLooper());
1554a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1555a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1556a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1557e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * Informs the application that this recording session has been tuned to the given channel
1558e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * and is ready to start recording.
1559e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         *
1560e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * <p>Upon receiving a call to {@link #onTune(Uri)}, the session is expected to tune to the
1561e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * passed channel and call this method to indicate that it is now available for immediate
1562e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * recording. When {@link #onStartRecording(Uri)} is called, recording must start with
1563e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * minimal delay.
1564a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1565e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo        public void notifyTuned() {
1566a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
1567a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
1568a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @Override
1569a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                public void run() {
1570a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    try {
1571e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo                        if (DEBUG) Log.d(TAG, "notifyTuned");
1572a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        if (mSessionCallback != null) {
1573e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo                            mSessionCallback.onTuned();
1574a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        }
1575a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    } catch (RemoteException e) {
1576e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo                        Log.w(TAG, "error in notifyTuned", e);
1577a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    }
1578a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                }
1579a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            });
1580a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1581a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1582a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1583e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * Informs the application that this recording session has stopped recording and created a
1584e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * new data entry in the {@link TvContract.RecordedPrograms} table that describes the newly
1585e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * recorded program.
1586a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
158725c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         * <p>The recording session must call this method in response to {@link #onStopRecording()}.
158825c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         * The session may call it even before receiving a call to {@link #onStopRecording()} if a
158925c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         * partially recorded program is available when there is an error.
159025c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         *
1591e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * @param recordedProgramUri The URI of the newly recorded program.
1592a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1593a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        public void notifyRecordingStopped(final Uri recordedProgramUri) {
1594a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
1595a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
1596a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @Override
1597a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                public void run() {
1598a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    try {
1599a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        if (DEBUG) Log.d(TAG, "notifyRecordingStopped");
1600a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        if (mSessionCallback != null) {
1601a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                            mSessionCallback.onRecordingStopped(recordedProgramUri);
1602a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        }
1603a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    } catch (RemoteException e) {
1604a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        Log.w(TAG, "error in notifyRecordingStopped", e);
1605a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    }
1606a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                }
1607a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            });
1608a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1609a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1610a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
161125c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         * Informs the application that there is an error and this recording session is no longer
161225c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         * able to start or continue recording. It may be called at any time after the recording
161325c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         * session is created until {@link #onRelease()} is called.
161425c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         *
161525c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         * <p>The application may release the current session upon receiving the error code through
161625c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         * {@link TvRecordingClient.RecordingCallback#onError(int)}. The session may call
161725c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         * {@link #notifyRecordingStopped(Uri)} if a partially recorded but still playable program
161825c9c5edab42d6c9e9e0469ab04fb7ff87704d1cJae Seo         * is available, before calling this method.
1619a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
1620a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @param error The error code. Should be one of the followings.
1621a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * <ul>
1622a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * <li>{@link TvInputManager#RECORDING_ERROR_UNKNOWN}
1623a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * <li>{@link TvInputManager#RECORDING_ERROR_INSUFFICIENT_SPACE}
1624a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * <li>{@link TvInputManager#RECORDING_ERROR_RESOURCE_BUSY}
1625a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * </ul>
1626a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1627a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        public void notifyError(@TvInputManager.RecordingError final int error) {
1628a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
1629a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
1630a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @Override
1631a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                public void run() {
1632a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    try {
1633a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        if (DEBUG) Log.d(TAG, "notifyError");
1634a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        if (mSessionCallback != null) {
1635a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                            mSessionCallback.onError(error);
1636a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        }
1637a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    } catch (RemoteException e) {
1638a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        Log.w(TAG, "error in notifyError", e);
1639a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    }
1640a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                }
1641a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            });
1642a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1643a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1644a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1645a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * Dispatches an event to the application using this recording session.
1646a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
1647a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @param eventType The type of the event.
1648a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @param eventArgs Optional arguments of the event.
1649a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @hide
1650a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1651a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        @SystemApi
1652a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        public void notifySessionEvent(@NonNull final String eventType, final Bundle eventArgs) {
1653a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            Preconditions.checkNotNull(eventType);
1654a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            executeOrPostRunnableOnMainThread(new Runnable() {
1655a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @MainThread
1656a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                @Override
1657a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                public void run() {
1658a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    try {
1659a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        if (DEBUG) Log.d(TAG, "notifySessionEvent(" + eventType + ")");
1660a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        if (mSessionCallback != null) {
1661a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                            mSessionCallback.onSessionEvent(eventType, eventArgs);
1662a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        }
1663a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    } catch (RemoteException e) {
1664a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        Log.w(TAG, "error in sending event (event=" + eventType + ")", e);
1665a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    }
1666a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                }
1667a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            });
1668a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1669a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1670a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1671e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * Called when the application requests to tune to a given channel for TV program recording.
1672a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
1673e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * <p>The application may call this method before starting or after stopping recording, but
1674e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * not during recording.
1675e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         *
1676e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * <p>The session must call {@link #notifyTuned()} if the tune request was fulfilled, or
1677e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * {@link #notifyError(int)} otherwise.
1678e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         *
1679e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * @param channelUri The URI of a channel.
1680a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1681e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo        public abstract void onTune(Uri channelUri);
1682a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1683a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1684e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * Called when the application requests to tune to a given channel for TV program recording.
1685a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
1686e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * <p>The application may call this method before starting or after stopping recording, but
1687e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * not during recording.
1688e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         *
1689e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * <p>The session must call {@link #notifyTuned()} if the tune request was fulfilled, or
1690e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * {@link #notifyError(int)} otherwise.
1691e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         *
1692e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * @param channelUri The URI of a channel.
1693a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @param params Extra parameters.
1694a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @hide
1695a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1696a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        @SystemApi
1697e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo        public void onTune(Uri channelUri, Bundle params) {
1698e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo            onTune(channelUri);
1699a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1700a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1701a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1702e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * Called when the application requests to start TV program recording. Recording must start
1703e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * immediately when this method is called.
1704a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
17054eee6a73e476cd2d82a69f3a535628901047f140Jae Seo         * <p>The application may supply the URI for a TV program as a hint for filling in program
17064eee6a73e476cd2d82a69f3a535628901047f140Jae Seo         * specific data fields in the {@link android.media.tv.TvContract.RecordedPrograms} table.
17074eee6a73e476cd2d82a69f3a535628901047f140Jae Seo         * A non-null {@code programHint} implies the started recording should be of that specific
17084eee6a73e476cd2d82a69f3a535628901047f140Jae Seo         * program, whereas null {@code programHint} does not impose such a requirement and the
17094eee6a73e476cd2d82a69f3a535628901047f140Jae Seo         * recording can span across multiple TV programs. In either case, the application must call
1710cd502cf7150f0fb426f7f808c7d407ce89705239Jae Seo         * {@link TvRecordingClient#stopRecording()} to stop the recording.
17114eee6a73e476cd2d82a69f3a535628901047f140Jae Seo         *
1712e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * <p>The session must call {@link #notifyError(int)} if the start request cannot be
1713e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * fulfilled.
17144eee6a73e476cd2d82a69f3a535628901047f140Jae Seo         *
17154eee6a73e476cd2d82a69f3a535628901047f140Jae Seo         * @param programHint The URI for the TV program to record as a hint, built by
17164eee6a73e476cd2d82a69f3a535628901047f140Jae Seo         *            {@link TvContract#buildProgramUri(long)}. Can be {@code null}.
1717a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
17184eee6a73e476cd2d82a69f3a535628901047f140Jae Seo        public abstract void onStartRecording(@Nullable Uri programHint);
1719a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1720a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1721e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * Called when the application requests to stop TV program recording. Recording must stop
1722e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * immediately when this method is called.
1723e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         *
1724e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * <p>The session must create a new data entry in the
1725e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * {@link android.media.tv.TvContract.RecordedPrograms} table that describes the newly
1726e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * recorded program and call {@link #notifyRecordingStopped(Uri)} with the URI to that
1727e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * entry.
1728e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * If the stop request cannot be fulfilled, the session must call {@link #notifyError(int)}.
1729a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
1730a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1731a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        public abstract void onStopRecording();
1732a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1733e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo
1734e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo        /**
1735e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * Called when the application requests to release all the resources held by this recording
1736e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * session.
1737e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         */
1738e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo        public abstract void onRelease();
1739e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo
1740a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1741a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * Processes a private command sent from the application to the TV input. This can be used
1742a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * to provide domain-specific features that are only known between certain TV inputs and
1743a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * their clients.
1744a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
1745a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @param action Name of the command to be performed. This <em>must</em> be a scoped name,
1746a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *            i.e. prefixed with a package name you own, so that different developers will
1747a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *            not create conflicting commands.
1748a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @param data Any data to include with the command.
1749a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * @hide
1750a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1751a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        @SystemApi
1752a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        public void onAppPrivateCommand(@NonNull String action, Bundle data) {
1753a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1754a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1755a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1756e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * Calls {@link #onTune(Uri, Bundle)}.
1757a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
1758a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1759e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo        void tune(Uri channelUri, Bundle params) {
1760e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo            onTune(channelUri, params);
1761a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1762a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1763a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1764e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo         * Calls {@link #onRelease()}.
1765a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
1766a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1767e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo        void release() {
1768e3c11e842937f50f54c9d82363f33338dc9e261bJae Seo            onRelease();
1769a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1770a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1771a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
17724eee6a73e476cd2d82a69f3a535628901047f140Jae Seo         * Calls {@link #onStartRecording(Uri)}.
1773a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
1774a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
17754eee6a73e476cd2d82a69f3a535628901047f140Jae Seo        void startRecording(@Nullable  Uri programHint) {
17764eee6a73e476cd2d82a69f3a535628901047f140Jae Seo            onStartRecording(programHint);
1777a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1778a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1779a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1780a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * Calls {@link #onStopRecording()}.
1781a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         *
1782a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1783a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        void stopRecording() {
1784a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            onStopRecording();
1785a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1786a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1787a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        /**
1788a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         * Calls {@link #onAppPrivateCommand(String, Bundle)}.
1789a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo         */
1790a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        void appPrivateCommand(String action, Bundle data) {
1791a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            onAppPrivateCommand(action, data);
1792a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1793a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1794a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        private void initialize(ITvInputSessionCallback callback) {
1795a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            synchronized(mLock) {
1796a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                mSessionCallback = callback;
1797a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                for (Runnable runnable : mPendingActions) {
1798a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    runnable.run();
1799a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                }
1800a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                mPendingActions.clear();
1801a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            }
1802a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1803a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1804a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        private void executeOrPostRunnableOnMainThread(Runnable action) {
1805a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            synchronized(mLock) {
1806a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                if (mSessionCallback == null) {
1807a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    // The session is not initialized yet.
1808a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    mPendingActions.add(action);
1809a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                } else {
1810a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    if (mHandler.getLooper().isCurrentThread()) {
1811a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        action.run();
1812a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    } else {
1813a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        // Posts the runnable if this is not called from the main thread
1814a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        mHandler.post(action);
1815a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    }
1816a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                }
1817a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo            }
1818a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        }
1819a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo    }
1820a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo
1821a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo    /**
1822bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang     * Base class for a TV input session which represents an external device connected to a
182349c1b69a568ce58c940e5a2b0641a2c50798cbe2Dongwon Kang     * hardware TV input.
18240610e12733875a267f59d87a2a68aebbf486066eDongwon Kang     *
18250610e12733875a267f59d87a2a68aebbf486066eDongwon Kang     * <p>This class is for an input which provides channels for the external set-top box to the
182649c1b69a568ce58c940e5a2b0641a2c50798cbe2Dongwon Kang     * application. Once a TV input returns an implementation of this class on
182749c1b69a568ce58c940e5a2b0641a2c50798cbe2Dongwon Kang     * {@link #onCreateSession(String)}, the framework will create a separate session for
182849c1b69a568ce58c940e5a2b0641a2c50798cbe2Dongwon Kang     * a hardware TV Input (e.g. HDMI 1) and forward the application's surface to the session so
182949c1b69a568ce58c940e5a2b0641a2c50798cbe2Dongwon Kang     * that the user can see the screen of the hardware TV Input when she tunes to a channel from
183049c1b69a568ce58c940e5a2b0641a2c50798cbe2Dongwon Kang     * this TV input. The implementation of this class is expected to change the channel of the
183149c1b69a568ce58c940e5a2b0641a2c50798cbe2Dongwon Kang     * external set-top box via a proprietary protocol when {@link HardwareSession#onTune(Uri)} is
183249c1b69a568ce58c940e5a2b0641a2c50798cbe2Dongwon Kang     * requested by the application.
18330610e12733875a267f59d87a2a68aebbf486066eDongwon Kang     *
18340610e12733875a267f59d87a2a68aebbf486066eDongwon Kang     * <p>Note that this class is not for inputs for internal hardware like built-in tuner and HDMI
18350610e12733875a267f59d87a2a68aebbf486066eDongwon Kang     * 1.
18360610e12733875a267f59d87a2a68aebbf486066eDongwon Kang     *
1837bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang     * @see #onCreateSession(String)
1838bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang     */
183966b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang    public abstract static class HardwareSession extends Session {
184066b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang
184166b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang        /**
184266b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang         * Creates a new HardwareSession.
184366b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang         *
184466b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang         * @param context The context of the application
184566b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang         */
184666b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang        public HardwareSession(Context context) {
184766b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang            super(context);
184866b9e91801ba8867514fbcf0ef8f03a2f4fb8798Dongwon Kang        }
1849bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang
1850bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        private TvInputManager.Session mHardwareSession;
1851bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        private ITvInputSession mProxySession;
1852bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        private ITvInputSessionCallback mProxySessionCallback;
1853c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang        private Handler mServiceHandler;
1854bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang
1855bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        /**
1856bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * Returns the hardware TV input ID the external device is connected to.
18570610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         *
18580610e12733875a267f59d87a2a68aebbf486066eDongwon Kang         * <p>TV input is expected to provide {@link android.R.attr#setupActivity} so that
1859bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * the application can launch it before using this TV input. The setup activity may let
1860bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * the user select the hardware TV input to which the external device is connected. The ID
1861bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * of the selected one should be stored in the TV input so that it can be returned here.
1862bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         */
1863bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        public abstract String getHardwareInputId();
1864bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang
1865bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        private final TvInputManager.SessionCallback mHardwareSessionCallback =
1866bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                new TvInputManager.SessionCallback() {
1867bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang            @Override
1868bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang            public void onSessionCreated(TvInputManager.Session session) {
1869bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                mHardwareSession = session;
1870bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                SomeArgs args = SomeArgs.obtain();
1871bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                if (session != null) {
18723eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    args.arg1 = HardwareSession.this;
18733eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    args.arg2 = mProxySession;
18743eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    args.arg3 = mProxySessionCallback;
18753eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    args.arg4 = session.getToken();
1876731843e424a0d7091eb42d579aab6a66a74b92caDongwon Kang                    session.tune(TvContract.buildChannelUriForPassthroughInput(
1877731843e424a0d7091eb42d579aab6a66a74b92caDongwon Kang                            getHardwareInputId()));
1878bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                } else {
1879bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                    args.arg1 = null;
18803eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    args.arg2 = null;
18813eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    args.arg3 = mProxySessionCallback;
18823eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    args.arg4 = null;
1883bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                    onRelease();
1884bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                }
1885c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                mServiceHandler.obtainMessage(ServiceHandler.DO_NOTIFY_SESSION_CREATED, args)
1886bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                        .sendToTarget();
1887bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang            }
1888bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang
1889bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang            @Override
1890bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang            public void onVideoAvailable(final TvInputManager.Session session) {
1891bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                if (mHardwareSession == session) {
1892bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                    onHardwareVideoAvailable();
1893bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                }
1894bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang            }
1895bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang
1896bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang            @Override
1897bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang            public void onVideoUnavailable(final TvInputManager.Session session,
1898bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                    final int reason) {
1899bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                if (mHardwareSession == session) {
1900bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                    onHardwareVideoUnavailable(reason);
1901bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                }
1902bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang            }
1903bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        };
1904bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang
1905bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        /**
1906bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * This method will not be called in {@link HardwareSession}. Framework will
1907bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * forward the application's surface to the hardware TV input.
1908bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         */
1909bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        @Override
1910bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        public final boolean onSetSurface(Surface surface) {
1911bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang            Log.e(TAG, "onSetSurface() should not be called in HardwareProxySession.");
1912bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang            return false;
1913bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        }
1914bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang
1915bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        /**
1916bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * Called when the underlying hardware TV input session calls
1917bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * {@link TvInputService.Session#notifyVideoAvailable()}.
1918bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         */
1919bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        public void onHardwareVideoAvailable() { }
1920bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang
1921bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        /**
1922bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * Called when the underlying hardware TV input session calls
1923bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * {@link TvInputService.Session#notifyVideoUnavailable(int)}.
1924bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         *
1925bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * @param reason The reason that the hardware TV input stopped the playback:
1926bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * <ul>
1927bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * <li>{@link TvInputManager#VIDEO_UNAVAILABLE_REASON_UNKNOWN}
1928bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * <li>{@link TvInputManager#VIDEO_UNAVAILABLE_REASON_TUNING}
1929bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * <li>{@link TvInputManager#VIDEO_UNAVAILABLE_REASON_WEAK_SIGNAL}
1930bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * <li>{@link TvInputManager#VIDEO_UNAVAILABLE_REASON_BUFFERING}
1931ff1f29e1b112e68d16908b1a89225315089f8e50Dongwon Kang         * <li>{@link TvInputManager#VIDEO_UNAVAILABLE_REASON_AUDIO_ONLY}
1932bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         * </ul>
1933bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang         */
1934bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        public void onHardwareVideoUnavailable(int reason) { }
1935bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang    }
1936bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang
193744fbbca35459ee756a531661eba53ec33419790aYoungsang Cho    /** @hide */
193844fbbca35459ee756a531661eba53ec33419790aYoungsang Cho    public static boolean isNavigationKey(int keyCode) {
193944fbbca35459ee756a531661eba53ec33419790aYoungsang Cho        switch (keyCode) {
194044fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_DPAD_LEFT:
194144fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_DPAD_RIGHT:
194244fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_DPAD_UP:
194344fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_DPAD_DOWN:
194444fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_DPAD_CENTER:
194544fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_PAGE_UP:
194644fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_PAGE_DOWN:
194744fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_MOVE_HOME:
194844fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_MOVE_END:
194944fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_TAB:
195044fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_SPACE:
195144fbbca35459ee756a531661eba53ec33419790aYoungsang Cho            case KeyEvent.KEYCODE_ENTER:
195244fbbca35459ee756a531661eba53ec33419790aYoungsang Cho                return true;
195344fbbca35459ee756a531661eba53ec33419790aYoungsang Cho        }
195444fbbca35459ee756a531661eba53ec33419790aYoungsang Cho        return false;
195544fbbca35459ee756a531661eba53ec33419790aYoungsang Cho    }
195644fbbca35459ee756a531661eba53ec33419790aYoungsang Cho
19572c1c31c7ae9bd972b974a5cc2d8b0942746af612Jae Seo    @SuppressLint("HandlerLeak")
19583957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo    private final class ServiceHandler extends Handler {
19593957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        private static final int DO_CREATE_SESSION = 1;
1960bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang        private static final int DO_NOTIFY_SESSION_CREATED = 2;
1961a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo        private static final int DO_CREATE_RECORDING_SESSION = 3;
19621abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo        private static final int DO_ADD_HARDWARE_INPUT = 4;
19631abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo        private static final int DO_REMOVE_HARDWARE_INPUT = 5;
19641abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo        private static final int DO_ADD_HDMI_INPUT = 6;
19651abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo        private static final int DO_REMOVE_HDMI_INPUT = 7;
19664f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee
19671abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo        private void broadcastAddHardwareInput(int deviceId, TvInputInfo inputInfo) {
19684f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee            int n = mCallbacks.beginBroadcast();
19694f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee            for (int i = 0; i < n; ++i) {
19704f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                try {
19711abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                    mCallbacks.getBroadcastItem(i).addHardwareInput(deviceId, inputInfo);
19724f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                } catch (RemoteException e) {
19731abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                    Log.e(TAG, "error in broadcastAddHardwareInput", e);
19744f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                }
19754f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee            }
19764f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee            mCallbacks.finishBroadcast();
19774f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee        }
1978187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim
19791abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo        private void broadcastAddHdmiInput(int id, TvInputInfo inputInfo) {
1980187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            int n = mCallbacks.beginBroadcast();
1981187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            for (int i = 0; i < n; ++i) {
1982187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                try {
19831abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                    mCallbacks.getBroadcastItem(i).addHdmiInput(id, inputInfo);
1984187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                } catch (RemoteException e) {
19851abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                    Log.e(TAG, "error in broadcastAddHdmiInput", e);
1986187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                }
1987187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            }
1988187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            mCallbacks.finishBroadcast();
1989187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim        }
1990187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim
19911abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo        private void broadcastRemoveHardwareInput(String inputId) {
1992187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            int n = mCallbacks.beginBroadcast();
1993187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim            for (int i = 0; i < n; ++i) {
1994187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                try {
19951abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                    mCallbacks.getBroadcastItem(i).removeHardwareInput(inputId);
1996187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                } catch (RemoteException e) {
19971abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                    Log.e(TAG, "error in broadcastRemoveHardwareInput", e);
199879124a717c09f12c74d587d3977bf33ca37e6420Terry Heo                }
199979124a717c09f12c74d587d3977bf33ca37e6420Terry Heo            }
200079124a717c09f12c74d587d3977bf33ca37e6420Terry Heo            mCallbacks.finishBroadcast();
200179124a717c09f12c74d587d3977bf33ca37e6420Terry Heo        }
200279124a717c09f12c74d587d3977bf33ca37e6420Terry Heo
20033957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        @Override
20043957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        public final void handleMessage(Message msg) {
20053957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            switch (msg.what) {
20063957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                case DO_CREATE_SESSION: {
20076a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    SomeArgs args = (SomeArgs) msg.obj;
20086a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    InputChannel channel = (InputChannel) args.arg1;
20096a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    ITvInputSessionCallback cb = (ITvInputSessionCallback) args.arg2;
2010187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                    String inputId = (String) args.arg3;
2011bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                    args.recycle();
2012c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                    Session sessionImpl = onCreateSession(inputId);
2013bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                    if (sessionImpl == null) {
2014bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                        try {
20153957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                            // Failed to create a session.
2016bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                            cb.onSessionCreated(null, null);
2017bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                        } catch (RemoteException e) {
2018465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                            Log.e(TAG, "error in onSessionCreated", e);
2019bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                        }
2020c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                        return;
2021c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                    }
2022c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                    ITvInputSession stub = new ITvInputSessionWrapper(TvInputService.this,
2023c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                            sessionImpl, channel);
2024c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                    if (sessionImpl instanceof HardwareSession) {
2025c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                        HardwareSession proxySession =
2026c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                                ((HardwareSession) sessionImpl);
20276e4cbfd2e5ffb739269e5e4affc2b6894bc4090eJae Seo                        String hardwareInputId = proxySession.getHardwareInputId();
20286e4cbfd2e5ffb739269e5e4affc2b6894bc4090eJae Seo                        if (TextUtils.isEmpty(hardwareInputId) ||
20296e4cbfd2e5ffb739269e5e4affc2b6894bc4090eJae Seo                                !isPassthroughInput(hardwareInputId)) {
20306e4cbfd2e5ffb739269e5e4affc2b6894bc4090eJae Seo                            if (TextUtils.isEmpty(hardwareInputId)) {
2031911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang                                Log.w(TAG, "Hardware input id is not setup yet.");
2032911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang                            } else {
20336e4cbfd2e5ffb739269e5e4affc2b6894bc4090eJae Seo                                Log.w(TAG, "Invalid hardware input id : " + hardwareInputId);
2034911d0827ca67d61c141ab91e75816da94b3f414eDongwon Kang                            }
2035c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                            sessionImpl.onRelease();
2036c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                            try {
2037c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                                cb.onSessionCreated(null, null);
2038c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                            } catch (RemoteException e) {
2039465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                                Log.e(TAG, "error in onSessionCreated", e);
2040bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                            }
2041c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                            return;
20423957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                        }
2043c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                        proxySession.mProxySession = stub;
2044c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                        proxySession.mProxySessionCallback = cb;
2045c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                        proxySession.mServiceHandler = mServiceHandler;
2046c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                        TvInputManager manager = (TvInputManager) getSystemService(
2047c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                                Context.TV_INPUT_SERVICE);
20486e4cbfd2e5ffb739269e5e4affc2b6894bc4090eJae Seo                        manager.createSession(hardwareInputId,
2049c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                                proxySession.mHardwareSessionCallback, mServiceHandler);
2050c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                    } else {
2051c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                        SomeArgs someArgs = SomeArgs.obtain();
20523eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                        someArgs.arg1 = sessionImpl;
20533eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                        someArgs.arg2 = stub;
20543eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                        someArgs.arg3 = cb;
20553eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                        someArgs.arg4 = null;
2056c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                        mServiceHandler.obtainMessage(ServiceHandler.DO_NOTIFY_SESSION_CREATED,
2057c566ca54b10fca266216555c3b6fb56a5ac63343Dongwon Kang                                someArgs).sendToTarget();
2058bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                    }
2059bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                    return;
2060bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                }
2061bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                case DO_NOTIFY_SESSION_CREATED: {
2062bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                    SomeArgs args = (SomeArgs) msg.obj;
20633eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    Session sessionImpl = (Session) args.arg1;
20643eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    ITvInputSession stub = (ITvInputSession) args.arg2;
20653eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    ITvInputSessionCallback cb = (ITvInputSessionCallback) args.arg3;
20663eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    IBinder hardwareSessionToken = (IBinder) args.arg4;
2067bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                    try {
2068bd2fa2c02d916a9b6c62f8fd8701d779c00bd68dDongwon Kang                        cb.onSessionCreated(stub, hardwareSessionToken);
20693957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                    } catch (RemoteException e) {
2070465f0d6aa36f2f1db88603aa487bcba9f5af068dJae Seo                        Log.e(TAG, "error in onSessionCreated", e);
20713957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                    }
20723eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    if (sessionImpl != null) {
20733eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                        sessionImpl.initialize(cb);
20743eefa59e37291abc72edd1c30b5469a21993dbbbDongwon Kang                    }
20756a6059a29edf31e65541b3d8927a46f5846fb0a2Jae Seo                    args.recycle();
20763957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                    return;
20773957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                }
2078a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                case DO_CREATE_RECORDING_SESSION: {
2079a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    SomeArgs args = (SomeArgs) msg.obj;
2080a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    ITvInputSessionCallback cb = (ITvInputSessionCallback) args.arg1;
2081a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    String inputId = (String) args.arg2;
2082a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    args.recycle();
2083a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    RecordingSession recordingSessionImpl = onCreateRecordingSession(inputId);
2084a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    if (recordingSessionImpl == null) {
2085a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        try {
2086a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                            // Failed to create a recording session.
2087a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                            cb.onSessionCreated(null, null);
2088a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        } catch (RemoteException e) {
2089a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                            Log.e(TAG, "error in onSessionCreated", e);
2090a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        }
2091a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        return;
2092a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    }
2093a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    ITvInputSession stub = new ITvInputSessionWrapper(TvInputService.this,
2094a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                            recordingSessionImpl);
2095a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    try {
2096a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        cb.onSessionCreated(stub, null);
2097a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    } catch (RemoteException e) {
2098a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                        Log.e(TAG, "error in onSessionCreated", e);
2099a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    }
2100a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    recordingSessionImpl.initialize(cb);
2101a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                    return;
2102a826d0172aae5e91d633ffe606059a2355fbf7e5Jae Seo                }
21031abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                case DO_ADD_HARDWARE_INPUT: {
2104187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                    TvInputHardwareInfo hardwareInfo = (TvInputHardwareInfo) msg.obj;
2105187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                    TvInputInfo inputInfo = onHardwareAdded(hardwareInfo);
2106187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                    if (inputInfo != null) {
21071abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                        broadcastAddHardwareInput(hardwareInfo.getDeviceId(), inputInfo);
21084f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                    }
21094f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                    return;
21104f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                }
21111abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                case DO_REMOVE_HARDWARE_INPUT: {
21124f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                    TvInputHardwareInfo hardwareInfo = (TvInputHardwareInfo) msg.obj;
21134f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                    String inputId = onHardwareRemoved(hardwareInfo);
21144f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                    if (inputId != null) {
21151abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                        broadcastRemoveHardwareInput(inputId);
21164f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                    }
21174f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                    return;
21184f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                }
21191abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                case DO_ADD_HDMI_INPUT: {
2120546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo                    HdmiDeviceInfo deviceInfo = (HdmiDeviceInfo) msg.obj;
2121546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo                    TvInputInfo inputInfo = onHdmiDeviceAdded(deviceInfo);
21224f9f57cede3de2e2aa3045e04b485b176ab22dbdJi-Hwan Lee                    if (inputInfo != null) {
21231abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                        broadcastAddHdmiInput(deviceInfo.getId(), inputInfo);
2124187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                    }
2125187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                    return;
2126187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                }
21271abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                case DO_REMOVE_HDMI_INPUT: {
2128546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo                    HdmiDeviceInfo deviceInfo = (HdmiDeviceInfo) msg.obj;
2129546c635ad9a26421fbdf54efa765b5ab0a63c191Jae Seo                    String inputId = onHdmiDeviceRemoved(deviceInfo);
2130187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                    if (inputId != null) {
21311abbbcd33efb8e5897c0fad5b6dd3af9a6b49b0dJae Seo                        broadcastRemoveHardwareInput(inputId);
2132187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                    }
2133187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                    return;
2134187423c0bc4b27479bc8c23bd86969429094b296Wonsik Kim                }
21353957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                default: {
21363957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                    Log.w(TAG, "Unhandled message code: " + msg.what);
21373957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                    return;
21383957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo                }
21393957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo            }
21403957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo        }
21413957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo    }
21423957091ba8f08c02b5e781098cb955a5f697a1ffJae Seo}
2143