diff options
Diffstat (limited to 'src/com/android/tv/InputSessionManager.java')
-rw-r--r-- | src/com/android/tv/InputSessionManager.java | 284 |
1 files changed, 147 insertions, 137 deletions
diff --git a/src/com/android/tv/InputSessionManager.java b/src/com/android/tv/InputSessionManager.java index 2978f409..4f298ed6 100644 --- a/src/com/android/tv/InputSessionManager.java +++ b/src/com/android/tv/InputSessionManager.java @@ -36,28 +36,27 @@ import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.ArraySet; import android.util.Log; - -import com.android.tv.data.Channel; +import com.android.tv.data.api.Channel; import com.android.tv.ui.TunableTvView; import com.android.tv.ui.TunableTvView.OnTuneListener; import com.android.tv.util.TvInputManagerHelper; - import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Set; /** - * Manages input sessions. - * Responsible for: + * Manages input sessions. Responsible for: + * * <ul> - * <li>Manage {@link TvView} sessions and recording sessions</li> - * <li>Manage capabilities (conflict)</li> + * <li>Manage {@link TvView} sessions and recording sessions + * <li>Manage capabilities (conflict) * </ul> - * <p> - * As TvView's methods should be called on the main thread and the {@link RecordingSession} should - * look at the state of the {@link TvViewSession} when it calls the framework methods, the framework - * calls in RecordingSession are made on the main thread not to introduce the multi-thread problems. + * + * <p>As TvView's methods should be called on the main thread and the {@link RecordingSession} + * should look at the state of the {@link TvViewSession} when it calls the framework methods, the + * framework calls in RecordingSession are made on the main thread not to introduce the multi-thread + * problems. */ @TargetApi(Build.VERSION_CODES.N) public class InputSessionManager { @@ -77,27 +76,25 @@ public class InputSessionManager { public InputSessionManager(Context context) { mContext = context.getApplicationContext(); - mInputManager = TvApplication.getSingletons(context).getTvInputManagerHelper(); + mInputManager = TvSingletons.getSingletons(context).getTvInputManagerHelper(); } /** * Creates the session for {@link TvView}. - * <p> - * Do not call {@link TvView#setCallback} after the session is created. + * + * <p>Do not call {@link TvView#setCallback} after the session is created. */ @MainThread @NonNull - public TvViewSession createTvViewSession(TvView tvView, TunableTvView tunableTvView, - TvInputCallback callback) { + public TvViewSession createTvViewSession( + TvView tvView, TunableTvView tunableTvView, TvInputCallback callback) { TvViewSession session = new TvViewSession(tvView, tunableTvView, callback); mTvViewSessions.add(session); if (DEBUG) Log.d(TAG, "TvView session created: " + session); return session; } - /** - * Releases the {@link TvView} session. - */ + /** Releases the {@link TvView} session. */ @MainThread public void releaseTvViewSession(TvViewSession session) { mTvViewSessions.remove(session); @@ -105,12 +102,14 @@ public class InputSessionManager { if (DEBUG) Log.d(TAG, "TvView session released: " + session); } - /** - * Creates the session for recording. - */ + /** Creates the session for recording. */ @NonNull - public RecordingSession createRecordingSession(String inputId, String tag, - RecordingCallback callback, Handler handler, long endTimeMs) { + public RecordingSession createRecordingSession( + String inputId, + String tag, + RecordingCallback callback, + Handler handler, + long endTimeMs) { RecordingSession session = new RecordingSession(inputId, tag, callback, handler, endTimeMs); mRecordingSessions.add(session); if (DEBUG) Log.d(TAG, "Recording session created: " + session); @@ -120,9 +119,7 @@ public class InputSessionManager { return session; } - /** - * Releases the recording session. - */ + /** Releases the recording session. */ public void releaseRecordingSession(RecordingSession session) { mRecordingSessions.remove(session); session.release(); @@ -132,17 +129,13 @@ public class InputSessionManager { } } - /** - * Adds the {@link OnTvViewChannelChangeListener}. - */ + /** Adds the {@link OnTvViewChannelChangeListener}. */ @MainThread public void addOnTvViewChannelChangeListener(OnTvViewChannelChangeListener listener) { mOnTvViewChannelChangeListeners.add(listener); } - /** - * Removes the {@link OnTvViewChannelChangeListener}. - */ + /** Removes the {@link OnTvViewChannelChangeListener}. */ @MainThread public void removeOnTvViewChannelChangeListener(OnTvViewChannelChangeListener listener) { mOnTvViewChannelChangeListeners.remove(listener); @@ -176,9 +169,7 @@ public class InputSessionManager { return null; } - /** - * Retruns the earliest end time of recording sessions in progress of the certain TV input. - */ + /** Retruns the earliest end time of recording sessions in progress of the certain TV input. */ @MainThread public Long getEarliestRecordingSessionEndTimeMs(String inputId) { long timeMs = Long.MAX_VALUE; @@ -240,8 +231,8 @@ public class InputSessionManager { /** * The session for {@link TvView}. - * <p> - * The methods which create or release session for the TV input should be called through this + * + * <p>The methods which create or release session for the TV input should be called through this * session. */ @MainThread @@ -261,31 +252,32 @@ public class InputSessionManager { mTvView = tvView; mTunableTvView = tunableTvView; mCallback = callback; - mTvView.setCallback(new DelegateTvInputCallback(mCallback) { - @Override - public void onConnectionFailed(String inputId) { - if (DEBUG) Log.d(TAG, "TvViewSession: connection failed"); - mTuned = false; - mNeedToBeRetuned = false; - super.onConnectionFailed(inputId); - notifyTvViewChannelChange(null); - } + mTvView.setCallback( + new DelegateTvInputCallback(mCallback) { + @Override + public void onConnectionFailed(String inputId) { + if (DEBUG) Log.d(TAG, "TvViewSession: connection failed"); + mTuned = false; + mNeedToBeRetuned = false; + super.onConnectionFailed(inputId); + notifyTvViewChannelChange(null); + } - @Override - public void onDisconnected(String inputId) { - if (DEBUG) Log.d(TAG, "TvViewSession: disconnected"); - mTuned = false; - mNeedToBeRetuned = false; - super.onDisconnected(inputId); - notifyTvViewChannelChange(null); - } - }); + @Override + public void onDisconnected(String inputId) { + if (DEBUG) Log.d(TAG, "TvViewSession: disconnected"); + mTuned = false; + mNeedToBeRetuned = false; + super.onDisconnected(inputId); + notifyTvViewChannelChange(null); + } + }); } /** * Tunes to the channel. - * <p> - * As this is called only for the warming up, there's no need to be retuned. + * + * <p>As this is called only for the warming up, there's no need to be retuned. */ public void tune(String inputId, Uri channelUri) { if (DEBUG) { @@ -299,13 +291,22 @@ public class InputSessionManager { notifyTvViewChannelChange(channelUri); } - /** - * Tunes to the channel. - */ + /** Tunes to the channel. */ public void tune(Channel channel, Bundle params, OnTuneListener listener) { if (DEBUG) { - Log.d(TAG, "tune: {session=" + this + ", channel=" + channel + ", params=" + params - + ", listener=" + listener + ", mTuned=" + mTuned + "}"); + Log.d( + TAG, + "tune: {session=" + + this + + ", channel=" + + channel + + ", params=" + + params + + ", listener=" + + listener + + ", mTuned=" + + mTuned + + "}"); } mChannel = channel; mInputId = channel.getInputId(); @@ -313,8 +314,10 @@ public class InputSessionManager { mParams = params; mOnTuneListener = listener; TvInputInfo input = mInputManager.getTvInputInfo(mInputId); - if (input == null || (input.canRecord() && !isTunedForRecording(mChannelUri) - && getTunedRecordingSessionCount(mInputId) >= input.getTunerCount())) { + if (input == null + || (input.canRecord() + && !isTunedForRecording(mChannelUri) + && getTunedRecordingSessionCount(mInputId) >= input.getTunerCount())) { if (DEBUG) { if (input == null) { Log.d(TAG, "Can't find input for input ID: " + mInputId); @@ -354,9 +357,7 @@ public class InputSessionManager { notifyTvViewChannelChange(null); } - /** - * Resets this TvView. - */ + /** Resets this TvView. */ public void reset() { if (DEBUG) Log.d(TAG, "Reset TvView session"); mTuned = false; @@ -366,8 +367,8 @@ public class InputSessionManager { } void resetByRecording() { - mCallback.onVideoUnavailable(mInputId, - TunableTvView.VIDEO_UNAVAILABLE_REASON_NO_RESOURCE); + mCallback.onVideoUnavailable( + mInputId, TunableTvView.VIDEO_UNAVAILABLE_REASON_NO_RESOURCE); if (mTuned) { if (DEBUG) Log.d(TAG, "Reset TvView session by recording"); mTunableTvView.resetByRecording(); @@ -379,8 +380,8 @@ public class InputSessionManager { /** * The session for recording. - * <p> - * The caller is responsible for releasing the session when the error occurs. + * + * <p>The caller is responsible for releasing the session when the error occurs. */ public class RecordingSession { private final String mInputId; @@ -391,8 +392,12 @@ public class InputSessionManager { private TvRecordingClient mClient; private boolean mTuned; - RecordingSession(String inputId, String tag, RecordingCallback callback, - Handler handler, long endTimeMs) { + RecordingSession( + String inputId, + String tag, + RecordingCallback callback, + Handler handler, + long endTimeMs) { mInputId = inputId; mCallback = callback; mHandler = handler; @@ -402,83 +407,90 @@ public class InputSessionManager { void release() { if (DEBUG) Log.d(TAG, "Release of recording session requested."); - runOnHandler(mMainThreadHandler, new Runnable() { - @Override - public void run() { - if (DEBUG) Log.d(TAG, "Releasing of recording session."); - mTuned = false; - mClient.release(); - mClient = null; - for (TvViewSession session : mTvViewSessions) { - if (DEBUG) { - Log.d(TAG, "Finding TvView sessions for retune: {tuned=" - + session.mTuned + ", inputId=" + session.mInputId - + ", session=" + session + "}"); - } - if (!session.mTuned && Objects.equals(session.mInputId, mInputId)) { - session.retune(); - break; + runOnHandler( + mMainThreadHandler, + new Runnable() { + @Override + public void run() { + if (DEBUG) Log.d(TAG, "Releasing of recording session."); + mTuned = false; + mClient.release(); + mClient = null; + for (TvViewSession session : mTvViewSessions) { + if (DEBUG) { + Log.d( + TAG, + "Finding TvView sessions for retune: {tuned=" + + session.mTuned + + ", inputId=" + + session.mInputId + + ", session=" + + session + + "}"); + } + if (!session.mTuned && Objects.equals(session.mInputId, mInputId)) { + session.retune(); + break; + } + } } - } - } - }); + }); } - /** - * Tunes to the channel for recording. - */ + /** Tunes to the channel for recording. */ public void tune(String inputId, Uri channelUri) { - runOnHandler(mMainThreadHandler, new Runnable() { - @Override - public void run() { - int tunedRecordingSessionCount = getTunedRecordingSessionCount(inputId); - TvInputInfo input = mInputManager.getTvInputInfo(inputId); - if (input == null || !input.canRecord() - || input.getTunerCount() <= tunedRecordingSessionCount) { - runOnHandler(mHandler, new Runnable() { - @Override - public void run() { - mCallback.onConnectionFailed(inputId); + runOnHandler( + mMainThreadHandler, + new Runnable() { + @Override + public void run() { + int tunedRecordingSessionCount = getTunedRecordingSessionCount(inputId); + TvInputInfo input = mInputManager.getTvInputInfo(inputId); + if (input == null + || !input.canRecord() + || input.getTunerCount() <= tunedRecordingSessionCount) { + runOnHandler( + mHandler, + new Runnable() { + @Override + public void run() { + mCallback.onConnectionFailed(inputId); + } + }); + return; } - }); - return; - } - mTuned = true; - int tunedTuneSessionCount = getTunedTvViewSessionCount(inputId); - if (!isTunedForTvView(channelUri) && tunedTuneSessionCount > 0 - && tunedRecordingSessionCount + tunedTuneSessionCount - >= input.getTunerCount()) { - for (TvViewSession session : mTvViewSessions) { - if (session.mTuned && Objects.equals(session.mInputId, inputId) - && !isTunedForRecording(session.mChannelUri)) { - session.resetByRecording(); - break; + mTuned = true; + int tunedTuneSessionCount = getTunedTvViewSessionCount(inputId); + if (!isTunedForTvView(channelUri) + && tunedTuneSessionCount > 0 + && tunedRecordingSessionCount + tunedTuneSessionCount + >= input.getTunerCount()) { + for (TvViewSession session : mTvViewSessions) { + if (session.mTuned + && Objects.equals(session.mInputId, inputId) + && !isTunedForRecording(session.mChannelUri)) { + session.resetByRecording(); + break; + } + } } + mChannelUri = channelUri; + mClient.tune(inputId, channelUri); } - } - mChannelUri = channelUri; - mClient.tune(inputId, channelUri); - } - }); + }); } - /** - * Starts recording. - */ + /** Starts recording. */ public void startRecording(Uri programHintUri) { mClient.startRecording(programHintUri); } - /** - * Stops recording. - */ + /** Stops recording. */ public void stopRecording() { mClient.stopRecording(); } - /** - * Sets recording session's ending time. - */ + /** Sets recording session's ending time. */ public void setEndTimeMs(long endTimeMs) { mEndTimeMs = endTimeMs; } @@ -555,9 +567,7 @@ public class InputSessionManager { } } - /** - * Called when the {@link TvView} channel is changed. - */ + /** Called when the {@link TvView} channel is changed. */ public interface OnTvViewChannelChangeListener { void onTvViewChannelChange(@Nullable Uri channelUri); } |