/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.media.tv; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.annotation.SystemService; import android.content.Context; import android.content.Intent; import android.graphics.Rect; import android.media.PlaybackParams; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Looper; import android.os.Message; import android.os.ParcelFileDescriptor; import android.os.RemoteException; import android.text.TextUtils; import android.util.ArrayMap; import android.util.Log; import android.util.Pools.Pool; import android.util.Pools.SimplePool; import android.util.SparseArray; import android.view.InputChannel; import android.view.InputEvent; import android.view.InputEventSender; import android.view.KeyEvent; import android.view.Surface; import android.view.View; import com.android.internal.util.Preconditions; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * Central system API to the overall TV input framework (TIF) architecture, which arbitrates * interaction between applications and the selected TV inputs. * *
There are three primary parties involved in the TV input framework (TIF) architecture: * *
android:targetSdkVersion
.
*/
public static final int RECORDING_ERROR_UNKNOWN = RECORDING_ERROR_START;
/**
* Error for {@link TvInputService.RecordingSession#notifyError(int)} and
* {@link TvRecordingClient.RecordingCallback#onError(int)}: Recording cannot proceed due to
* insufficient storage space.
*/
public static final int RECORDING_ERROR_INSUFFICIENT_SPACE = 1;
/**
* Error for {@link TvInputService.RecordingSession#notifyError(int)} and
* {@link TvRecordingClient.RecordingCallback#onError(int)}: Recording cannot proceed because
* a required recording resource was not able to be allocated.
*/
public static final int RECORDING_ERROR_RESOURCE_BUSY = RECORDING_ERROR_END;
/** @hide */
@Retention(RetentionPolicy.SOURCE)
@IntDef({INPUT_STATE_CONNECTED, INPUT_STATE_CONNECTED_STANDBY, INPUT_STATE_DISCONNECTED})
public @interface InputState {}
/**
* State for {@link #getInputState(String)} and
* {@link TvInputCallback#onInputStateChanged(String, int)}: The input source is connected.
*
* This state indicates that a source device is connected to the input port and is in the * normal operation mode. It is mostly relevant to hardware inputs such as HDMI input. * Non-hardware inputs are considered connected all the time. */ public static final int INPUT_STATE_CONNECTED = 0; /** * State for {@link #getInputState(String)} and * {@link TvInputCallback#onInputStateChanged(String, int)}: The input source is connected but * in standby mode. * *
This state indicates that a source device is connected to the input port but is in standby * or low power mode. It is mostly relevant to hardware inputs such as HDMI input and Component * inputs. */ public static final int INPUT_STATE_CONNECTED_STANDBY = 1; /** * State for {@link #getInputState(String)} and * {@link TvInputCallback#onInputStateChanged(String, int)}: The input source is disconnected. * *
This state indicates that a source device is disconnected from the input port. It is * mostly relevant to hardware inputs such as HDMI input. * */ public static final int INPUT_STATE_DISCONNECTED = 2; /** * Broadcast intent action when the user blocked content ratings change. For use with the * {@link #isRatingBlocked}. */ public static final String ACTION_BLOCKED_RATINGS_CHANGED = "android.media.tv.action.BLOCKED_RATINGS_CHANGED"; /** * Broadcast intent action when the parental controls enabled state changes. For use with the * {@link #isParentalControlsEnabled}. */ public static final String ACTION_PARENTAL_CONTROLS_ENABLED_CHANGED = "android.media.tv.action.PARENTAL_CONTROLS_ENABLED_CHANGED"; /** * Broadcast intent action used to query available content rating systems. * *
The TV input manager service locates available content rating systems by querying * broadcast receivers that are registered for this action. An application can offer additional * content rating systems to the user by declaring a suitable broadcast receiver in its * manifest. * *
Here is an example broadcast receiver declaration that an application might include in its * AndroidManifest.xml to advertise custom content rating systems. The meta-data specifies a * resource that contains a description of each content rating system that is provided by the * application. * *
* {@literal ** ** }* ** *
In the above example, the @xml/tv_content_rating_systems
resource refers to an
* XML resource whose root element is <rating-system-definitions>
that
* contains zero or more <rating-system-definition>
elements. Each
* <rating-system-definition>
element specifies the ratings, sub-ratings and rating
* orders of a particular content rating system.
*
* @see TvContentRating
*/
public static final String ACTION_QUERY_CONTENT_RATING_SYSTEMS =
"android.media.tv.action.QUERY_CONTENT_RATING_SYSTEMS";
/**
* Content rating systems metadata associated with {@link #ACTION_QUERY_CONTENT_RATING_SYSTEMS}.
*
*
Specifies the resource ID of an XML resource that describes the content rating systems
* that are provided by the application.
*/
public static final String META_DATA_CONTENT_RATING_SYSTEMS =
"android.media.tv.metadata.CONTENT_RATING_SYSTEMS";
/**
* Activity action to set up channel sources i.e. TV inputs of type
* {@link TvInputInfo#TYPE_TUNER}. When invoked, the system will display an appropriate UI for
* the user to initiate the individual setup flow provided by
* {@link android.R.attr#setupActivity} of each TV input service.
*/
public static final String ACTION_SETUP_INPUTS = "android.media.tv.action.SETUP_INPUTS";
/**
* Activity action to display the recording schedules. When invoked, the system will display an
* appropriate UI to browse the schedules.
*/
public static final String ACTION_VIEW_RECORDING_SCHEDULES =
"android.media.tv.action.VIEW_RECORDING_SCHEDULES";
private final ITvInputManager mService;
private final Object mLock = new Object();
// @GuardedBy("mLock")
private final List Normally it happens when the user installs a new TV input package that implements
* {@link TvInputService} interface.
*
* @param inputId The ID of the TV input.
*/
public void onInputAdded(String inputId) {
}
/**
* This is called when a TV input is removed from the system.
*
* Normally it happens when the user uninstalls the previously installed TV input
* package.
*
* @param inputId The ID of the TV input.
*/
public void onInputRemoved(String inputId) {
}
/**
* This is called when a TV input is updated on the system.
*
* Normally it happens when a previously installed TV input package is re-installed or
* the media on which a newer version of the package exists becomes available/unavailable.
*
* @param inputId The ID of the TV input.
*/
public void onInputUpdated(String inputId) {
}
/**
* This is called when the information about an existing TV input has been updated.
*
* Because the system automatically creates a The system automatically creates a Attempting to change information about a TV input that the calling package does not own
* does nothing.
*
* @param inputInfo The The state is one of the following:
* The number of sessions that can be created at the same time is limited by the capability
* of the given TV input.
*
* @param inputId The ID of the TV input.
* @param callback A callback used to receive the created session.
* @param handler A {@link Handler} that the session creation will be delivered to.
* @hide
*/
public void createSession(@NonNull String inputId, @NonNull final SessionCallback callback,
@NonNull Handler handler) {
createSessionInternal(inputId, false, callback, handler);
}
/**
* Creates a recording {@link Session} for a given TV input.
*
* The number of sessions that can be created at the same time is limited by the capability
* of the given TV input.
*
* @param inputId The ID of the TV input.
* @param callback A callback used to receive the created session.
* @param handler A {@link Handler} that the session creation will be delivered to.
* @hide
*/
public void createRecordingSession(@NonNull String inputId,
@NonNull final SessionCallback callback, @NonNull Handler handler) {
createSessionInternal(inputId, true, callback, handler);
}
private void createSessionInternal(String inputId, boolean isRecordingSession,
SessionCallback callback, Handler handler) {
Preconditions.checkNotNull(inputId);
Preconditions.checkNotNull(callback);
Preconditions.checkNotNull(handler);
SessionCallbackRecord record = new SessionCallbackRecord(callback, handler);
synchronized (mSessionCallbackRecordMap) {
int seq = mNextSeq++;
mSessionCallbackRecordMap.put(seq, record);
try {
mService.createSession(mClient, inputId, isRecordingSession, seq, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}
/**
* Returns the TvStreamConfig list of the given TV input.
*
* If you are using {@link Hardware} object from {@link
* #acquireTvInputHardware}, you should get the list of available streams
* from {@link HardwareCallback#onStreamConfigChanged} method, not from
* here. This method is designed to be used with {@link #captureFrame} in
* capture scenarios specifically and not suitable for any other use.
*
* @param inputId The ID of the TV input.
* @return List of {@link TvStreamConfig} which is available for capturing
* of the given TV input.
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.CAPTURE_TV_INPUT)
public List A subsequent call to this method on the same {@code deviceId} will release the currently
* acquired Hardware.
*
* @param deviceId The device ID to acquire Hardware for.
* @param callback A callback to receive updates on Hardware.
* @param info The TV input which will use the acquired Hardware.
* @return Hardware on success, {@code null} otherwise.
*
* @hide
* @removed
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.TV_INPUT_HARDWARE)
public Hardware acquireTvInputHardware(int deviceId, final HardwareCallback callback,
TvInputInfo info) {
return acquireTvInputHardware(deviceId, info, callback);
}
/**
* Acquires {@link Hardware} object for the given device ID.
*
* A subsequent call to this method on the same {@code deviceId} will release the currently
* acquired Hardware.
*
* @param deviceId The device ID to acquire Hardware for.
* @param callback A callback to receive updates on Hardware.
* @param info The TV input which will use the acquired Hardware.
* @return Hardware on success, {@code null} otherwise.
*
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.TV_INPUT_HARDWARE)
public Hardware acquireTvInputHardware(int deviceId, TvInputInfo info,
final HardwareCallback callback) {
try {
return new Hardware(
mService.acquireTvInputHardware(deviceId, new ITvInputHardwareCallback.Stub() {
@Override
public void onReleased() {
callback.onReleased();
}
@Override
public void onStreamConfigChanged(TvStreamConfig[] configs) {
callback.onStreamConfigChanged(configs);
}
}, info, mUserId));
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Releases previously acquired hardware object.
*
* @param deviceId The device ID this Hardware was acquired for
* @param hardware Hardware to release.
*
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.TV_INPUT_HARDWARE)
public void releaseTvInputHardware(int deviceId, Hardware hardware) {
try {
mService.releaseTvInputHardware(deviceId, hardware.getInterface(), mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Returns the list of currently available DVB frontend devices on the system.
*
* @return the list of {@link DvbDeviceInfo} objects representing available DVB devices.
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.DVB_DEVICE)
@NonNull
public List Once called, the system will review the request and make the channel browsable based on
* its policy. The first request from a package is guaranteed to be approved.
*
* @param channelUri The URI for the channel to be browsable.
* @hide
*/
public void requestChannelBrowsable(Uri channelUri) {
try {
mService.requestChannelBrowsable(channelUri, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* The Session provides the per-session functionality of TV inputs.
* @hide
*/
public static final class Session {
static final int DISPATCH_IN_PROGRESS = -1;
static final int DISPATCH_NOT_HANDLED = 0;
static final int DISPATCH_HANDLED = 1;
private static final long INPUT_SESSION_NOT_RESPONDING_TIMEOUT = 2500;
private final ITvInputManager mService;
private final int mUserId;
private final int mSeq;
// For scheduling input event handling on the main thread. This also serves as a lock to
// protect pending input events and the input channel.
private final InputEventHandler mHandler = new InputEventHandler(Looper.getMainLooper());
private final Pool Normally, the position is given within range between the start and the current time,
* inclusively.
*
* @param timeMs The time position to seek to, in milliseconds since the epoch.
* @see TvView.TimeShiftPositionCallback#onTimeShiftStartPositionChanged
*/
void timeShiftSeekTo(long timeMs) {
if (mToken == null) {
Log.w(TAG, "The session has been already released");
return;
}
try {
mService.timeShiftSeekTo(mToken, timeMs, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Sets playback rate using {@link android.media.PlaybackParams}.
*
* @param params The playback params.
*/
void timeShiftSetPlaybackParams(PlaybackParams params) {
if (mToken == null) {
Log.w(TAG, "The session has been already released");
return;
}
try {
mService.timeShiftSetPlaybackParams(mToken, params, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Enable/disable position tracking.
*
* @param enable {@code true} to enable tracking, {@code false} otherwise.
*/
void timeShiftEnablePositionTracking(boolean enable) {
if (mToken == null) {
Log.w(TAG, "The session has been already released");
return;
}
try {
mService.timeShiftEnablePositionTracking(mToken, enable, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Starts TV program recording in the current recording session.
*
* @param programUri The URI for the TV program to record as a hint, built by
* {@link TvContract#buildProgramUri(long)}. Can be {@code null}.
*/
void startRecording(@Nullable Uri programUri) {
if (mToken == null) {
Log.w(TAG, "The session has been already released");
return;
}
try {
mService.startRecording(mToken, programUri, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Stops TV program recording in the current recording session.
*/
void stopRecording() {
if (mToken == null) {
Log.w(TAG, "The session has been already released");
return;
}
try {
mService.stopRecording(mToken, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Calls {@link TvInputService.Session#appPrivateCommand(String, Bundle)
* TvInputService.Session.appPrivateCommand()} on the current TvView.
*
* @param action Name of the command to be performed. This must be a scoped name,
* i.e. prefixed with a package name you own, so that different developers will
* not create conflicting commands.
* @param data Any data to include with the command.
*/
public void sendAppPrivateCommand(String action, Bundle data) {
if (mToken == null) {
Log.w(TAG, "The session has been already released");
return;
}
try {
mService.sendAppPrivateCommand(mToken, action, data, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Creates an overlay view. Once the overlay view is created, {@link #relayoutOverlayView}
* should be called whenever the layout of its containing view is changed.
* {@link #removeOverlayView()} should be called to remove the overlay view.
* Since a session can have only one overlay view, this method should be called only once
* or it can be called again after calling {@link #removeOverlayView()}.
*
* @param view A view playing TV.
* @param frame A position of the overlay view.
* @throws IllegalStateException if {@code view} is not attached to a window.
*/
void createOverlayView(@NonNull View view, @NonNull Rect frame) {
Preconditions.checkNotNull(view);
Preconditions.checkNotNull(frame);
if (view.getWindowToken() == null) {
throw new IllegalStateException("view must be attached to a window");
}
if (mToken == null) {
Log.w(TAG, "The session has been already released");
return;
}
try {
mService.createOverlayView(mToken, view.getWindowToken(), frame, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Relayouts the current overlay view.
*
* @param frame A new position of the overlay view.
*/
void relayoutOverlayView(@NonNull Rect frame) {
Preconditions.checkNotNull(frame);
if (mToken == null) {
Log.w(TAG, "The session has been already released");
return;
}
try {
mService.relayoutOverlayView(mToken, frame, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Removes the current overlay view.
*/
void removeOverlayView() {
if (mToken == null) {
Log.w(TAG, "The session has been already released");
return;
}
try {
mService.removeOverlayView(mToken, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Requests to unblock content blocked by parental controls.
*/
void unblockContent(@NonNull TvContentRating unblockedRating) {
Preconditions.checkNotNull(unblockedRating);
if (mToken == null) {
Log.w(TAG, "The session has been already released");
return;
}
try {
mService.unblockContent(mToken, unblockedRating.flattenToString(), mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Dispatches an input event to this session.
*
* @param event An {@link InputEvent} to dispatch. Cannot be {@code null}.
* @param token A token used to identify the input event later in the callback.
* @param callback A callback used to receive the dispatch result. Cannot be {@code null}.
* @param handler A {@link Handler} that the dispatch result will be delivered to. Cannot be
* {@code null}.
* @return Returns {@link #DISPATCH_HANDLED} if the event was handled. Returns
* {@link #DISPATCH_NOT_HANDLED} if the event was not handled. Returns
* {@link #DISPATCH_IN_PROGRESS} if the event is in progress and the callback will
* be invoked later.
* @hide
*/
public int dispatchInputEvent(@NonNull InputEvent event, Object token,
@NonNull FinishedInputEventCallback callback, @NonNull Handler handler) {
Preconditions.checkNotNull(event);
Preconditions.checkNotNull(callback);
Preconditions.checkNotNull(handler);
synchronized (mHandler) {
if (mChannel == null) {
return DISPATCH_NOT_HANDLED;
}
PendingEvent p = obtainPendingEventLocked(event, token, callback, handler);
if (Looper.myLooper() == Looper.getMainLooper()) {
// Already running on the main thread so we can send the event immediately.
return sendInputEventOnMainLooperLocked(p);
}
// Post the event to the main thread.
Message msg = mHandler.obtainMessage(InputEventHandler.MSG_SEND_INPUT_EVENT, p);
msg.setAsynchronous(true);
mHandler.sendMessage(msg);
return DISPATCH_IN_PROGRESS;
}
}
/**
* Callback that is invoked when an input event that was dispatched to this session has been
* finished.
*
* @hide
*/
public interface FinishedInputEventCallback {
/**
* Called when the dispatched input event is finished.
*
* @param token A token passed to {@link #dispatchInputEvent}.
* @param handled {@code true} if the dispatched input event was handled properly.
* {@code false} otherwise.
*/
void onFinishedInputEvent(Object token, boolean handled);
}
// Must be called on the main looper
private void sendInputEventAndReportResultOnMainLooper(PendingEvent p) {
synchronized (mHandler) {
int result = sendInputEventOnMainLooperLocked(p);
if (result == DISPATCH_IN_PROGRESS) {
return;
}
}
invokeFinishedInputEventCallback(p, false);
}
private int sendInputEventOnMainLooperLocked(PendingEvent p) {
if (mChannel != null) {
if (mSender == null) {
mSender = new TvInputEventSender(mChannel, mHandler.getLooper());
}
final InputEvent event = p.mEvent;
final int seq = event.getSequenceNumber();
if (mSender.sendInputEvent(seq, event)) {
mPendingEvents.put(seq, p);
Message msg = mHandler.obtainMessage(InputEventHandler.MSG_TIMEOUT_INPUT_EVENT, p);
msg.setAsynchronous(true);
mHandler.sendMessageDelayed(msg, INPUT_SESSION_NOT_RESPONDING_TIMEOUT);
return DISPATCH_IN_PROGRESS;
}
Log.w(TAG, "Unable to send input event to session: " + mToken + " dropping:"
+ event);
}
return DISPATCH_NOT_HANDLED;
}
void finishedInputEvent(int seq, boolean handled, boolean timeout) {
final PendingEvent p;
synchronized (mHandler) {
int index = mPendingEvents.indexOfKey(seq);
if (index < 0) {
return; // spurious, event already finished or timed out
}
p = mPendingEvents.valueAt(index);
mPendingEvents.removeAt(index);
if (timeout) {
Log.w(TAG, "Timeout waiting for session to handle input event after "
+ INPUT_SESSION_NOT_RESPONDING_TIMEOUT + " ms: " + mToken);
} else {
mHandler.removeMessages(InputEventHandler.MSG_TIMEOUT_INPUT_EVENT, p);
}
}
invokeFinishedInputEventCallback(p, handled);
}
// Assumes the event has already been removed from the queue.
void invokeFinishedInputEventCallback(PendingEvent p, boolean handled) {
p.mHandled = handled;
if (p.mEventHandler.getLooper().isCurrentThread()) {
// Already running on the callback handler thread so we can send the callback
// immediately.
p.run();
} else {
// Post the event to the callback handler thread.
// In this case, the callback will be responsible for recycling the event.
Message msg = Message.obtain(p.mEventHandler, p);
msg.setAsynchronous(true);
msg.sendToTarget();
}
}
private void flushPendingEventsLocked() {
mHandler.removeMessages(InputEventHandler.MSG_FLUSH_INPUT_EVENT);
final int count = mPendingEvents.size();
for (int i = 0; i < count; i++) {
int seq = mPendingEvents.keyAt(i);
Message msg = mHandler.obtainMessage(InputEventHandler.MSG_FLUSH_INPUT_EVENT, seq, 0);
msg.setAsynchronous(true);
msg.sendToTarget();
}
}
private PendingEvent obtainPendingEventLocked(InputEvent event, Object token,
FinishedInputEventCallback callback, Handler handler) {
PendingEvent p = mPendingEventPool.acquire();
if (p == null) {
p = new PendingEvent();
}
p.mEvent = event;
p.mEventToken = token;
p.mCallback = callback;
p.mEventHandler = handler;
return p;
}
private void recyclePendingEventLocked(PendingEvent p) {
p.recycle();
mPendingEventPool.release(p);
}
IBinder getToken() {
return mToken;
}
private void releaseInternal() {
mToken = null;
synchronized (mHandler) {
if (mChannel != null) {
if (mSender != null) {
flushPendingEventsLocked();
mSender.dispose();
mSender = null;
}
mChannel.dispose();
mChannel = null;
}
}
synchronized (mSessionCallbackRecordMap) {
mSessionCallbackRecordMap.delete(mSeq);
}
}
private final class InputEventHandler extends Handler {
public static final int MSG_SEND_INPUT_EVENT = 1;
public static final int MSG_TIMEOUT_INPUT_EVENT = 2;
public static final int MSG_FLUSH_INPUT_EVENT = 3;
InputEventHandler(Looper looper) {
super(looper, null, true);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_SEND_INPUT_EVENT: {
sendInputEventAndReportResultOnMainLooper((PendingEvent) msg.obj);
return;
}
case MSG_TIMEOUT_INPUT_EVENT: {
finishedInputEvent(msg.arg1, false, true);
return;
}
case MSG_FLUSH_INPUT_EVENT: {
finishedInputEvent(msg.arg1, false, false);
return;
}
}
}
}
private final class TvInputEventSender extends InputEventSender {
public TvInputEventSender(InputChannel inputChannel, Looper looper) {
super(inputChannel, looper);
}
@Override
public void onInputEventFinished(int seq, boolean handled) {
finishedInputEvent(seq, handled, false);
}
}
private final class PendingEvent implements Runnable {
public InputEvent mEvent;
public Object mEventToken;
public FinishedInputEventCallback mCallback;
public Handler mEventHandler;
public boolean mHandled;
public void recycle() {
mEvent = null;
mEventToken = null;
mCallback = null;
mEventHandler = null;
mHandled = false;
}
@Override
public void run() {
mCallback.onFinishedInputEvent(mEventToken, mHandled);
synchronized (mEventHandler) {
recyclePendingEventLocked(this);
}
}
}
}
/**
* The Hardware provides the per-hardware functionality of TV hardware.
*
* TV hardware is physical hardware attached to the Android device; for example, HDMI ports,
* Component/Composite ports, etc. Specifically, logical devices such as HDMI CEC logical
* devices don't fall into this category.
*
* @hide
*/
@SystemApi
public final static class Hardware {
private final ITvInputHardware mInterface;
private Hardware(ITvInputHardware hardwareInterface) {
mInterface = hardwareInterface;
}
private ITvInputHardware getInterface() {
return mInterface;
}
public boolean setSurface(Surface surface, TvStreamConfig config) {
try {
return mInterface.setSurface(surface, config);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
public void setStreamVolume(float volume) {
try {
mInterface.setStreamVolume(volume);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
/** @removed */
@SystemApi
public boolean dispatchKeyEventToHdmi(KeyEvent event) {
return false;
}
public void overrideAudioSink(int audioType, String audioAddress, int samplingRate,
int channelMask, int format) {
try {
mInterface.overrideAudioSink(audioType, audioAddress, samplingRate, channelMask,
format);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
}
}
*
*/
public void onVideoUnavailable(Session session, int reason) {
}
/**
* This is called when the current program content turns out to be allowed to watch since
* its content rating is not blocked by parental controls.
*
* @param session A {@link TvInputManager.Session} associated with this callback.
*/
public void onContentAllowed(Session session) {
}
/**
* This is called when the current program content turns out to be not allowed to watch
* since its content rating is blocked by parental controls.
*
* @param session A {@link TvInputManager.Session} associated with this callback.
* @param rating The content ration of the blocked program.
*/
public void onContentBlocked(Session session, TvContentRating rating) {
}
/**
* This is called when {@link TvInputService.Session#layoutSurface} is called to change the
* layout of surface.
*
* @param session A {@link TvInputManager.Session} associated with this callback.
* @param left Left position.
* @param top Top position.
* @param right Right position.
* @param bottom Bottom position.
*/
public void onLayoutSurface(Session session, int left, int top, int right, int bottom) {
}
/**
* This is called when a custom event has been sent from this session.
*
* @param session A {@link TvInputManager.Session} associated with this callback
* @param eventType The type of the event.
* @param eventArgs Optional arguments of the event.
*/
public void onSessionEvent(Session session, String eventType, Bundle eventArgs) {
}
/**
* This is called when the time shift status is changed.
*
* @param session A {@link TvInputManager.Session} associated with this callback.
* @param status The current time shift status. Should be one of the followings.
*
*
*/
public void onTimeShiftStatusChanged(Session session, int status) {
}
/**
* This is called when the start position for time shifting has changed.
*
* @param session A {@link TvInputManager.Session} associated with this callback.
* @param timeMs The start position for time shifting, in milliseconds since the epoch.
*/
public void onTimeShiftStartPositionChanged(Session session, long timeMs) {
}
/**
* This is called when the current position for time shifting is changed.
*
* @param session A {@link TvInputManager.Session} associated with this callback.
* @param timeMs The current position for time shifting, in milliseconds since the epoch.
*/
public void onTimeShiftCurrentPositionChanged(Session session, long timeMs) {
}
// For the recording session only
/**
* This is called when the recording session has been tuned to the given channel and is
* ready to start recording.
*
* @param channelUri The URI of a channel.
*/
void onTuned(Session session, Uri channelUri) {
}
// For the recording session only
/**
* This is called when the current recording session has stopped recording and created a
* new data entry in the {@link TvContract.RecordedPrograms} table that describes the newly
* recorded program.
*
* @param recordedProgramUri The URI for the newly recorded program.
**/
void onRecordingStopped(Session session, Uri recordedProgramUri) {
}
// For the recording session only
/**
* This is called when an issue has occurred. It may be called at any time after the current
* recording session is created until it is released.
*
* @param error The error code.
*/
void onError(Session session, @TvInputManager.RecordingError int error) {
}
}
private static final class SessionCallbackRecord {
private final SessionCallback mSessionCallback;
private final Handler mHandler;
private Session mSession;
SessionCallbackRecord(SessionCallback sessionCallback,
Handler handler) {
mSessionCallback = sessionCallback;
mHandler = handler;
}
void postSessionCreated(final Session session) {
mSession = session;
mHandler.post(new Runnable() {
@Override
public void run() {
mSessionCallback.onSessionCreated(session);
}
});
}
void postSessionReleased() {
mHandler.post(new Runnable() {
@Override
public void run() {
mSessionCallback.onSessionReleased(mSession);
}
});
}
void postChannelRetuned(final Uri channelUri) {
mHandler.post(new Runnable() {
@Override
public void run() {
mSessionCallback.onChannelRetuned(mSession, channelUri);
}
});
}
void postTracksChanged(final List
*
*/
public void onInputStateChanged(String inputId, @InputState int state) {
}
/**
* This is called when a TV input is added to the system.
*
* TvInputInfo
object for each TV
* input based on the information collected from the AndroidManifest.xml
, this
* method is only called back when such information has changed dynamically.
*
* @param inputInfo The TvInputInfo
object that contains new information.
*/
public void onTvInputInfoUpdated(TvInputInfo inputInfo) {
}
}
private static final class TvInputCallbackRecord {
private final TvInputCallback mCallback;
private final Handler mHandler;
public TvInputCallbackRecord(TvInputCallback callback, Handler handler) {
mCallback = callback;
mHandler = handler;
}
public TvInputCallback getCallback() {
return mCallback;
}
public void postInputAdded(final String inputId) {
mHandler.post(new Runnable() {
@Override
public void run() {
mCallback.onInputAdded(inputId);
}
});
}
public void postInputRemoved(final String inputId) {
mHandler.post(new Runnable() {
@Override
public void run() {
mCallback.onInputRemoved(inputId);
}
});
}
public void postInputUpdated(final String inputId) {
mHandler.post(new Runnable() {
@Override
public void run() {
mCallback.onInputUpdated(inputId);
}
});
}
public void postInputStateChanged(final String inputId, final int state) {
mHandler.post(new Runnable() {
@Override
public void run() {
mCallback.onInputStateChanged(inputId, state);
}
});
}
public void postTvInputInfoUpdated(final TvInputInfo inputInfo) {
mHandler.post(new Runnable() {
@Override
public void run() {
mCallback.onTvInputInfoUpdated(inputInfo);
}
});
}
}
/**
* Interface used to receive events from Hardware objects.
*
* @hide
*/
@SystemApi
public abstract static class HardwareCallback {
/**
* This is called when {@link Hardware} is no longer available for the client.
*/
public abstract void onReleased();
/**
* This is called when the underlying {@link TvStreamConfig} has been changed.
*
* @param configs The new {@link TvStreamConfig}s.
*/
public abstract void onStreamConfigChanged(TvStreamConfig[] configs);
}
/**
* @hide
*/
public TvInputManager(ITvInputManager service, int userId) {
mService = service;
mUserId = userId;
mClient = new ITvInputClient.Stub() {
@Override
public void onSessionCreated(String inputId, IBinder token, InputChannel channel,
int seq) {
synchronized (mSessionCallbackRecordMap) {
SessionCallbackRecord record = mSessionCallbackRecordMap.get(seq);
if (record == null) {
Log.e(TAG, "Callback not found for " + token);
return;
}
Session session = null;
if (token != null) {
session = new Session(token, channel, mService, mUserId, seq,
mSessionCallbackRecordMap);
} else {
mSessionCallbackRecordMap.delete(seq);
}
record.postSessionCreated(session);
}
}
@Override
public void onSessionReleased(int seq) {
synchronized (mSessionCallbackRecordMap) {
SessionCallbackRecord record = mSessionCallbackRecordMap.get(seq);
mSessionCallbackRecordMap.delete(seq);
if (record == null) {
Log.e(TAG, "Callback not found for seq:" + seq);
return;
}
record.mSession.releaseInternal();
record.postSessionReleased();
}
}
@Override
public void onChannelRetuned(Uri channelUri, int seq) {
synchronized (mSessionCallbackRecordMap) {
SessionCallbackRecord record = mSessionCallbackRecordMap.get(seq);
if (record == null) {
Log.e(TAG, "Callback not found for seq " + seq);
return;
}
record.postChannelRetuned(channelUri);
}
}
@Override
public void onTracksChanged(ListTvInputInfo
for an existing TV input. A TV input service
* implementation may call this method to pass the application and system an up-to-date
* TvInputInfo
object that describes itself.
*
* TvInputInfo
object for each TV input,
* based on the information collected from the AndroidManifest.xml
, thus it is not
* necessary to call this method unless such information has changed dynamically.
* Use {@link TvInputInfo.Builder} to build a new TvInputInfo
object.
*
* TvInputInfo
object that contains new information.
* @throws IllegalArgumentException if the argument is {@code null}.
* @see TvInputCallback#onTvInputInfoUpdated(TvInputInfo)
*/
public void updateTvInputInfo(@NonNull TvInputInfo inputInfo) {
Preconditions.checkNotNull(inputInfo);
try {
mService.updateTvInputInfo(inputInfo, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Returns the state of a given TV input.
*
*
*
*
* @param inputId The ID of the TV input.
* @throws IllegalArgumentException if the argument is {@code null}.
*/
@InputState
public int getInputState(@NonNull String inputId) {
Preconditions.checkNotNull(inputId);
synchronized (mLock) {
Integer state = mStateMap.get(inputId);
if (state == null) {
Log.w(TAG, "Unrecognized input ID: " + inputId);
return INPUT_STATE_DISCONNECTED;
}
return state;
}
}
/**
* Registers a {@link TvInputCallback}.
*
* @param callback A callback used to monitor status of the TV inputs.
* @param handler A {@link Handler} that the status change will be delivered to.
*/
public void registerCallback(@NonNull TvInputCallback callback, @NonNull Handler handler) {
Preconditions.checkNotNull(callback);
Preconditions.checkNotNull(handler);
synchronized (mLock) {
mCallbackRecords.add(new TvInputCallbackRecord(callback, handler));
}
}
/**
* Unregisters the existing {@link TvInputCallback}.
*
* @param callback The existing callback to remove.
*/
public void unregisterCallback(@NonNull final TvInputCallback callback) {
Preconditions.checkNotNull(callback);
synchronized (mLock) {
for (Iterator