1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 package com.android.tv.dvr;
17 
18 import android.content.Context;
19 import android.media.PlaybackParams;
20 import android.media.session.PlaybackState;
21 import android.media.tv.TvTrackInfo;
22 import android.media.tv.TvView;
23 import android.net.Uri;
24 import android.support.annotation.Nullable;
25 import com.android.tv.InputSessionManager;
26 import com.android.tv.InputSessionManager.TvViewSession;
27 import com.android.tv.TvSingletons;
28 import com.android.tv.common.compat.TvViewCompat.TvInputCallbackCompat;
29 import com.android.tv.dvr.ui.playback.DvrPlayer;
30 import com.android.tv.ui.AppLayerTvView;
31 import com.android.tv.ui.api.TunableTvViewPlayingApi;
32 import java.util.List;
33 
34 /**
35  * A {@link TvView} wrapper to handle events and TvView session.
36  */
37 public class DvrTvView implements TunableTvViewPlayingApi {
38 
39     private final AppLayerTvView mTvView;
40     private DvrPlayer mDvrPlayer;
41     private String mInputId;
42     private Uri mRecordedProgramUri;
43     private TvInputCallbackCompat mTvInputCallbackCompat;
44     private InputSessionManager mInputSessionManager;
45     private TvViewSession mSession;
46 
DvrTvView(Context context, AppLayerTvView tvView, DvrPlayer player)47     public DvrTvView(Context context, AppLayerTvView tvView, DvrPlayer player) {
48         mTvView = tvView;
49         mDvrPlayer = player;
50         mInputSessionManager = TvSingletons.getSingletons(context).getInputSessionManager();
51     }
52 
53     @Override
isPlaying()54     public boolean isPlaying() {
55         return mDvrPlayer.getPlaybackState() == PlaybackState.STATE_PLAYING;
56     }
57 
58     @Override
setStreamVolume(float volume)59     public void setStreamVolume(float volume) {
60         mTvView.setStreamVolume(volume);
61     }
62 
63     @Override
setTimeShiftListener(TimeShiftListener listener)64     public void setTimeShiftListener(TimeShiftListener listener) {
65         // TimeShiftListener is never called from DvrTvView because TimeShift is always available
66         // and onRecordStartTimeChanged is not called during playback.
67     }
68 
69     @Override
isTimeShiftAvailable()70     public boolean isTimeShiftAvailable() {
71         return true;
72     }
73 
74     @Override
timeShiftPlay()75     public void timeShiftPlay() {
76         if (mInputId != null && mRecordedProgramUri != null) {
77             mTvView.timeShiftPlay(mInputId, mRecordedProgramUri);
78         }
79     }
80 
timeShiftPlay(String inputId, Uri recordedProgramUri)81     public void timeShiftPlay(String inputId, Uri recordedProgramUri) {
82         mInputId = inputId;
83         mRecordedProgramUri = recordedProgramUri;
84         mSession.timeShiftPlay(inputId, recordedProgramUri);
85     }
86 
87     @Override
timeShiftPause()88     public void timeShiftPause() {
89         mTvView.timeShiftPause();
90     }
91 
92     @Override
timeShiftRewind(int speed)93     public void timeShiftRewind(int speed) {
94         PlaybackParams params = new PlaybackParams();
95         params.setSpeed(speed * -1);
96         mTvView.timeShiftSetPlaybackParams(params);
97     }
98 
99     @Override
timeShiftFastForward(int speed)100     public void timeShiftFastForward(int speed) {
101         PlaybackParams params = new PlaybackParams();
102         params.setSpeed(speed);
103         mTvView.timeShiftSetPlaybackParams(params);
104     }
105 
106     @Override
timeShiftSeekTo(long timeMs)107     public void timeShiftSeekTo(long timeMs) {
108         mTvView.timeShiftSeekTo(timeMs);
109     }
110 
111     @Override
timeShiftGetCurrentPositionMs()112     public long timeShiftGetCurrentPositionMs() {
113         return mDvrPlayer.getPlaybackPosition();
114     }
115 
setCaptionEnabled(boolean enabled)116     public void setCaptionEnabled(boolean enabled) {
117         mTvView.setCaptionEnabled(enabled);
118     }
119 
timeShiftResume()120     public void timeShiftResume() {
121         mTvView.timeShiftResume();
122     }
123 
reset()124     public void reset() {
125         mSession.reset();
126     }
127 
getTracks(int type)128     public List<TvTrackInfo> getTracks(int type) {
129         return mTvView.getTracks(type);
130     }
131 
selectTrack(int type, String trackId)132     public void selectTrack(int type, String trackId) {
133         mTvView.selectTrack(type, trackId);
134     }
135 
timeShiftSetPlaybackParams(PlaybackParams params)136     public void timeShiftSetPlaybackParams(PlaybackParams params) {
137         mTvView.timeShiftSetPlaybackParams(params);
138     }
139 
setTimeShiftPositionCallback(@ullable TvView.TimeShiftPositionCallback callback)140     public void setTimeShiftPositionCallback(@Nullable TvView.TimeShiftPositionCallback callback) {
141         mTvView.setTimeShiftPositionCallback(callback);
142     }
143 
setCallback(@ullable TvInputCallbackCompat callback)144     public void setCallback(@Nullable TvInputCallbackCompat callback) {
145         mTvInputCallbackCompat = callback;
146         mTvView.setCallback(callback);
147     }
148 
init()149     public void init() {
150         mSession = mInputSessionManager.createTvViewSession(mTvView, this, mTvInputCallbackCompat);
151     }
152 
release()153     public void release() {
154         mInputSessionManager.releaseTvViewSession(mSession);
155         mInputSessionManager = null;
156         mDvrPlayer = null;
157     }
158 }
159