2
2
3
3
import android .app .Dialog ;
4
4
import android .os .Bundle ;
5
+ import android .os .Handler ;
5
6
import android .view .Menu ;
6
7
import android .view .View ;
7
8
import android .widget .SeekBar ;
8
9
10
+ import com .atomjack .shared .Intent ;
9
11
import com .atomjack .shared .SendToDataLayerThread ;
10
12
import com .atomjack .shared .WearConstants ;
11
13
import com .atomjack .vcfp .interfaces .AfterTransientTokenRequest ;
19
21
import com .atomjack .vcfp .model .PlexMedia ;
20
22
import com .google .android .libraries .cast .companionlibrary .cast .VideoCastManager ;
21
23
22
- import java .util .Arrays ;
24
+ import java .util .ArrayList ;
23
25
import java .util .List ;
24
26
25
27
public class CastActivity extends PlayerActivity {
@@ -28,54 +30,151 @@ public class CastActivity extends PlayerActivity {
28
30
29
31
private PlayerState currentState = PlayerState .STOPPED ;
30
32
31
- private List <PlexMedia > nowPlayingAlbum ;
33
+ private List <PlexMedia > nowPlayingAlbum = new ArrayList <>() ;
32
34
33
35
private Dialog infoDialog ;
34
36
35
- @ Override
37
+ final Handler handler = new Handler ();
38
+
39
+ private boolean uiShowing = false ;
40
+
41
+ private void start (final boolean setView ) {
42
+ mClient = getIntent ().getParcelableExtra (Intent .EXTRA_CLIENT );
43
+ Logger .d ("[CastActivity] set mClient: %s" , mClient );
44
+
45
+ if (getIntent ().getBooleanExtra (WearConstants .FROM_WEAR , false )) {
46
+ new SendToDataLayerThread (WearConstants .FINISH , this ).start ();
47
+ }
48
+
49
+ if (getIntent ().getAction () != null && getIntent ().getAction ().equals (Intent .CAST_MEDIA )) {
50
+ Logger .d ("[CastActivity] checking subscribed: %s" , castPlayerManager .isSubscribed ());
51
+ if (castPlayerManager .isSubscribed ()) {
52
+ nowPlayingMedia = castPlayerManager .getNowPlayingMedia ();
53
+ nowPlayingAlbum = castPlayerManager .getNowPlayingAlbum ();
54
+ if (!castPlayerManager .getCurrentState ().equals (PlayerState .STOPPED )) {
55
+ // Media is playing, so show ui
56
+ showNowPlaying (setView );
57
+ } else {
58
+ handler .postDelayed (new Runnable () {
59
+ @ Override
60
+ public void run () {
61
+ Logger .d ("Checking for playback state: %s" , castPlayerManager .getCurrentState ());
62
+ if (!castPlayerManager .getCurrentState ().equals (PlayerState .STOPPED )) {
63
+ showNowPlaying (setView );
64
+ } else {
65
+ handler .postDelayed (this , 1000 );
66
+ }
67
+ }
68
+ }, 1000 );
69
+ }
70
+ } else {
71
+ if (getIntent ().getParcelableExtra (Intent .EXTRA_MEDIA ) != null ) {
72
+ nowPlayingMedia = getIntent ().getParcelableExtra (Intent .EXTRA_MEDIA );
73
+ nowPlayingAlbum = getIntent ().getParcelableExtra (Intent .EXTRA_ALBUM );
74
+ showNowPlaying (setView );
75
+ }
76
+ castPlayerManager .subscribe (mClient , new Runnable () {
77
+ @ Override
78
+ public void run () {
79
+ // TODO: this
80
+ nowPlayingMedia = castPlayerManager .getNowPlayingMedia ();
81
+ nowPlayingAlbum = castPlayerManager .getNowPlayingAlbum ();
82
+ }
83
+ });
84
+ }
85
+ } else {
86
+ // We're coming here expecting to already be connected and have media playing.
87
+ if (!castPlayerManager .isSubscribed () || castPlayerManager .getCurrentState ().equals (PlayerState .STOPPED ))
88
+ finish ();
89
+ else {
90
+ nowPlayingMedia = castPlayerManager .getNowPlayingMedia ();
91
+ nowPlayingAlbum = castPlayerManager .getNowPlayingAlbum ();
92
+ showNowPlaying (setView );
93
+ }
94
+ }
95
+ }
96
+
97
+ @ Override
36
98
protected void onCreate (Bundle savedInstanceState ) {
37
99
super .onCreate (savedInstanceState );
38
100
39
- mClient = getIntent ().getParcelableExtra (com .atomjack .shared .Intent .EXTRA_CLIENT );
40
- Logger .d ("[CastActivity] set mClient: %s" , mClient );
41
- nowPlayingMedia = getIntent ().getParcelableExtra (com .atomjack .shared .Intent .EXTRA_MEDIA );
42
- nowPlayingAlbum = getIntent ().getParcelableArrayListExtra (com .atomjack .shared .Intent .EXTRA_ALBUM );
101
+ start (true );
102
+
103
+ /*
104
+
105
+ boolean mediaChange = false;
106
+ PlexMedia newMedia = getIntent().getParcelableExtra(Intent.EXTRA_MEDIA);
107
+ if(castPlayerManager.isSubscribed()) {
108
+ if(newMedia != null && castPlayerManager.getNowPlayingMedia() != null && !newMedia.key.equals(castPlayerManager.getNowPlayingMedia().key))
109
+ mediaChange = true;
110
+ }
111
+
112
+
113
+ nowPlayingMedia = newMedia;
114
+ nowPlayingAlbum = getIntent().getParcelableArrayListExtra(Intent.EXTRA_ALBUM);
43
115
resumePlayback = getIntent().getBooleanExtra("resume", false);
44
116
castManager = castPlayerManager.getCastManager();
45
117
46
118
if(getIntent().getBooleanExtra(WearConstants.FROM_WEAR, false)) {
47
119
new SendToDataLayerThread(WearConstants.FINISH, this).start();
48
120
}
49
121
// If just playing a single track, put the media into an array
50
- if (nowPlayingAlbum == null )
51
- nowPlayingAlbum = Arrays .asList (nowPlayingMedia );
122
+ if(nowPlayingAlbum == null) {
123
+ nowPlayingAlbum = new ArrayList<>();
124
+ nowPlayingAlbum.add(nowPlayingMedia);
125
+ }
52
126
53
127
Logger.d("[CastActivity] starting up, action: %s, current state: %s", getIntent().getAction(), castPlayerManager.getCurrentState());
54
128
Logger.d("client: %s", mClient);
55
- if (getIntent ().getAction () != null && getIntent ().getAction ().equals (com . atomjack . shared . Intent .CAST_MEDIA )) {
129
+ if(getIntent().getAction() != null && getIntent().getAction().equals(Intent.CAST_MEDIA)) {
56
130
131
+ /*
57
132
Logger.d("Casting %s (%s)", nowPlayingMedia.title, nowPlayingMedia.viewOffset);
58
133
59
- showNowPlaying ();
60
- if (castPlayerManager .isSubscribed ()) {
61
- init ();
134
+ // TODO: only show now playing if stopped?
135
+ // if(castPlayerManager.getCurrentState().equals(PlayerState.STOPPED))
136
+
137
+ if(mediaChange) {
138
+ Logger.d("[CastActivity] MEDIA CHANGED!");
139
+
140
+ init(true); // tell the chromecast to load the new media. The cast player activity will receive a notification of the new media and will update accordingly
62
141
} else {
63
- showInfoDialog (getResources ().getString (R .string .connecting ));
64
- castPlayerManager .subscribe (mClient );
142
+
143
+ showNowPlaying(castPlayerManager.getCurrentState().equals(PlayerState.STOPPED) || !mediaChange ? true : false);
144
+ if (castPlayerManager.isSubscribed()) {
145
+ init();
146
+ } else {
147
+ showInfoDialog(getResources().getString(R.string.connecting));
148
+ castPlayerManager.subscribe(mClient);
149
+ }
65
150
}
66
151
} else {
67
152
Logger.d("[CastActivity] No action found.");
68
153
if(castPlayerManager.getCurrentState().equals(PlayerState.STOPPED))
69
154
finish();
70
155
else {
71
- showNowPlaying ();
156
+ showNowPlaying(true );
72
157
}
73
158
}
159
+ */
74
160
}
75
161
162
+ @ Override
163
+ protected void onNewIntent (android .content .Intent intent ) {
164
+ super .onNewIntent (intent );
165
+ // If the currently playing media is of a different type than the one being received, make sure to set the view
166
+ PlexMedia newMedia = intent .getParcelableExtra (Intent .EXTRA_MEDIA );
167
+ start (newMedia != null && nowPlayingMedia != null && !newMedia .getType ().equals (nowPlayingMedia .getType ()));
168
+ }
169
+
76
170
@ Override
77
171
public void showNowPlaying () {
78
- super .showNowPlaying ();
172
+ showNowPlaying (true );
173
+ }
174
+
175
+ @ Override
176
+ public void showNowPlaying (boolean setView ) {
177
+ super .showNowPlaying (setView );
79
178
setupUI ();
80
179
}
81
180
@@ -91,17 +190,22 @@ private void setupUI() {
91
190
seekBar = (SeekBar )findViewById (R .id .seekBar );
92
191
seekBar .setOnSeekBarChangeListener (this );
93
192
seekBar .setMax (nowPlayingMedia .duration );
94
- seekBar .setProgress (getOffset (nowPlayingMedia )* 1000 );
193
+ seekBar .setProgress (getOffset (nowPlayingMedia ) * 1000 );
95
194
96
195
Logger .d ("setupUI, setting time display to %d" , getOffset (nowPlayingMedia ));
97
196
setCurrentTimeDisplay (getOffset (nowPlayingMedia ));
98
197
durationDisplay .setText (VoiceControlForPlexApplication .secondsToTimecode (nowPlayingMedia .duration / 1000 ));
198
+ uiShowing = true ;
99
199
}
100
200
101
201
private void init () {
202
+ init (false );
203
+ }
204
+
205
+ private void init (boolean forceLoad ) {
102
206
currentState = castPlayerManager .getCurrentState ();
103
207
Logger .d ("castPlayerManager.getCurrentState(): %s" , castPlayerManager .getCurrentState ());
104
- if (castPlayerManager .getCurrentState () != PlayerState .STOPPED )
208
+ if (castPlayerManager .getCurrentState () != PlayerState .STOPPED && ! forceLoad )
105
209
return ;
106
210
if (VoiceControlForPlexApplication .getInstance ().prefs .getString (Preferences .PLEX_USERNAME ) != null ) {
107
211
nowPlayingMedia .server .requestTransientAccessToken (new AfterTransientTokenRequest () {
@@ -271,16 +375,19 @@ public boolean onCreateOptionsMenu(Menu _menu) {
271
375
public void onCastPlayerStateChanged (PlayerState state ) {
272
376
super .onCastPlayerStateChanged (state );
273
377
Logger .d ("[CastActivity] onCastPlayerStateChanged: %s" , state );
378
+
274
379
if (isSeeking ) {
275
380
isSeeking = false ;
276
381
}
277
- hideInfoDialog ();
278
382
if (state == PlayerState .STOPPED ) {
279
383
Logger .d ("[CastActivity] media player is idle, finishing" );
280
384
VoiceControlForPlexApplication .getInstance ().cancelNotification ();
281
385
finish ();
282
- } else
386
+ } else if ( uiShowing ) {
283
387
setState (state );
388
+ }
389
+ if (uiShowing )
390
+ hideInfoDialog ();
284
391
}
285
392
286
393
@ Override
0 commit comments