3535import com .google .common .util .concurrent .ListenableFuture ;
3636import com .google .common .util .concurrent .MoreExecutors ;
3737import com .xapps .media .xmusic .R ;
38+ import com .xapps .media .xmusic .activity .controller .ActivityMediaController ;
3839import com .xapps .media .xmusic .common .PlaybackControlListener ;
3940import com .xapps .media .xmusic .common .SongLoadListener ;
4041import com .xapps .media .xmusic .data .*;
@@ -62,15 +63,15 @@ public class MainActivity extends BaseActivity implements ServiceCallback, Playb
6263 ExecutorService executor = Executors .newSingleThreadExecutor ();
6364 private ArrayList <HashMap <String , Object >> songsMap = new ArrayList <>();
6465 private Context context = this ;
65- private Handler handler , backgroundHandler ;
66- private boolean isRestoring , wasAdjusted , seekbarFree , isBnvHidden , isColorAnimated , isAnimated , isBsInvisible , isOledTheme , isResuming , bound , isBlurOn , isCallbackValid = false ;
66+ public Handler handler , backgroundHandler ;
67+ public boolean isRestoring , wasAdjusted , seekbarFree , isBnvHidden , isColorAnimated , isAnimated , isBsInvisible , isOledTheme , isResuming , bound , isBlurOn , isCallbackValid = false ;
6768 private ListenableFuture <MediaController > controllerFuture ;
6869 private SessionToken sessionToken ;
6970 private PlayerService service ;
7071 private MainActivityViewModel viewmodel ;
7172 public BottomSheetBehavior bottomSheetBehavior , innerBottomSheetBehavior ;
7273 private HandlerThread handlerThread = new HandlerThread ("BackgroundThread" );
73- private SquigglyProgress progressDrawable ;
74+ public SquigglyProgress progressDrawable ;
7475 private int statusBarHeight , navBarHeight , bsbHeight , bottomSheetColor , tmpColor , playerSurface ;
7576 public int bnvHeight ;
7677 private long lastClick ;
@@ -80,6 +81,8 @@ public class MainActivity extends BaseActivity implements ServiceCallback, Playb
8081 private CustomTarget <Drawable > coverTarget ;
8182 private ValueAnimator colorAnimator ;
8283 private Map <String , Integer > effectiveOldColors = new HashMap <>();
84+
85+ private ActivityMediaController mController ;
8386
8487 @ Override
8588 protected void onSaveInstanceState (@ NonNull Bundle outState ) {
@@ -158,21 +161,13 @@ public void onPause() {
158161 public void onStart () {
159162 super .onStart ();
160163 if (sessionToken == null ) sessionToken = new SessionToken (context , new ComponentName (context , PlayerService .class ));
161- if (controllerFuture == null && mediaController == null ) {
162- controllerFuture = new MediaController .Builder (this , sessionToken ).buildAsync ();
163- controllerFuture .addListener (() -> {
164- try {
165- if (!XUtils .isDarkMode (this )) setDarkStatusBar (getWindow (), true );
166- mediaController = controllerFuture .get ();
167- progressDrawable .setAnimate (mediaController .isPlaying ());
168- setupControllerListener ();
169- } catch (Exception e ) {
170- showInfoDialog ("Error" , 0 , e .toString (), "OK" );
171- } finally {
172- restoreStateIfPossible ();
173- }
174- }, MoreExecutors .directExecutor ());
175- }
164+ mController = new ActivityMediaController (this , sessionToken );
165+ mController .initialize (controller -> {
166+ mediaController = controller ;
167+ if (!XUtils .isDarkMode (this )) setDarkStatusBar (getWindow (), true );
168+ progressDrawable .setAnimate (mediaController .isPlaying ());
169+ setupControllerListener ();
170+ }, e -> showInfoDialog ("Error" , 0 , e .toString (), "OK" ), this ::restoreStateIfPossible );
176171
177172 }
178173
@@ -182,79 +177,7 @@ public void updateAdapters(int position, boolean isPlaying) {
182177 }
183178
184179 public void setupControllerListener () {
185- mediaController .addListener (new Player .Listener () {
186- @ Override
187- public void onMediaItemTransition (@ Nullable MediaItem mediaItem , int reason ) {
188- if (mediaController .getPlaybackState () == Player .STATE_BUFFERING || mediaController .getPlaybackState () == Player .STATE_READY ) {
189- String songPath = RuntimeData .songs .get (mediaController .getCurrentMediaItemIndex ()).path ;
190- loadLyrics (songPath );
191- }
192- if (mediaItem != null ) {
193- int position = mediaController .getCurrentMediaItemIndex ();
194- updateAdapters (position , mediaController .isPlaying ());
195- progressDrawable .setAnimate (true );
196- if (!binding .toggleView .isAnimating ()) binding .toggleView .startAnimation ();
197- PlayerService .currentPosition = position ;
198- seekbarFree = false ;
199- binding .currentDurationText .setText (XUtils .millisecondsToDuration (0 ));
200- binding .songSeekbar .setProgress (0 , true );
201- binding .musicProgress .setProgressCompat (0 , true );
202- if (bottomSheetBehavior .getState () == BottomSheetBehavior .STATE_HIDDEN ) bottomSheetBehavior .setState (BottomSheetBehavior .STATE_COLLAPSED );
203- syncPlayerUI (position );
204- saveUIState ();
205- backgroundHandler .postDelayed (() -> {
206- seekbarFree = true ;
207- }, 150 );
208- if (bottomSheetBehavior .getState () == BottomSheetBehavior .STATE_EXPANDED ) {
209- binding .miniPlayerBottomSheet .setProgress (1f );
210- } else {
211- binding .miniPlayerBottomSheet .setProgress (0f );
212- }
213- if (RuntimeData .songs .size () == 1 ) {
214- binding .previousButton .setActive (false );
215- binding .nextButton .setActive (false );
216- } else {
217- if (position == 0 && !(mediaController .getShuffleModeEnabled () || mediaController .getRepeatMode () == Player .REPEAT_MODE_ALL )) {
218- binding .previousButton .setActive (false );
219- binding .nextButton .setActive (true );
220- } else if (position == RuntimeData .songs .size () - 1 && !(mediaController .getShuffleModeEnabled () || mediaController .getRepeatMode () == Player .REPEAT_MODE_ALL )) {
221- binding .previousButton .setActive (true );
222- binding .nextButton .setActive (false );
223- } else {
224- binding .previousButton .setActive (true );
225- binding .nextButton .setActive (true );
226- }
227- }
228- }
229-
230- }
231-
232- @ Override
233- public void onPositionDiscontinuity (Player .PositionInfo positionInfo , Player .PositionInfo positionInfo2 , int i ) {
234- updateProgress (mediaController .getCurrentPosition ());
235- }
236-
237- @ Override
238- public void onPlayWhenReadyChanged (boolean playWhenReady , int reason ) {
239- if (reason == Player .PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST || reason == Player .PLAY_WHEN_READY_CHANGE_REASON_END_OF_MEDIA_ITEM ) {
240- if (playWhenReady ) {
241- binding .toggleView .startAnimation ();
242- progressDrawable .setAnimate (true );
243- } else {
244- binding .toggleView .stopAnimation ();
245- progressDrawable .setAnimate (false );
246- }
247- }
248- }
249-
250- @ Override
251- public void onPlaybackStateChanged (int playbackState ) {
252- if (playbackState == Player .STATE_ENDED ) {
253- binding .toggleView .stopAnimation ();
254- progressDrawable .setAnimate (false );
255- }
256- }
257- });
180+ mController .setupListener (this );
258181 }
259182
260183 @ Override
@@ -263,15 +186,13 @@ public void onDestroy() {
263186 super .onDestroy ();
264187 ServiceCallback .Hub .set (null );
265188 mediaController .release ();
189+ mediaController = null ;
266190 }
267191
268192 private void initialize () {
269193 viewmodel = new ViewModelProvider (this ).get (MainActivityViewModel .class );
270194 handler = new Handler (Looper .getMainLooper ());
271195 bottomSheetBehavior = BottomSheetBehavior .from (binding .miniPlayerBottomSheet );
272- binding .bottomNavigation .post (() -> {
273-
274- });
275196 ViewKt .doOnLayout (binding .bottomNavigation , v -> {
276197 binding .bottomNavigation .setSelectedItemId (viewmodel .loadBNVPosition ());
277198 bnvHeight = binding .bottomNavigation .getHeight () - XUtils .getNavigationBarHeight (context );
@@ -340,7 +261,7 @@ private static boolean samePlaylistByPath(MediaController controller, List<Media
340261 return true ;
341262 }
342263
343- private void updateProgress (long position ) {
264+ public void updateProgress (long position ) {
344265 binding .musicProgress .setProgressCompat ((int ) position , true );
345266 binding .songSeekbar .setProgress ((int ) position , false );
346267 binding .currentDurationText .setText (XUtils .millisecondsToDuration (position ));
@@ -350,6 +271,7 @@ private void updateProgress(long position) {
350271
351272 private void updateCoverPager (int index ) {
352273 if (RuntimeData .songs .isEmpty ()) return ;
274+ if (isDestroyed () || isFinishing ()) return ;
353275
354276 Uri cover = RuntimeData .songs .get (index ).getArtworkUri ();
355277 if (coverTarget != null ) {
@@ -406,7 +328,7 @@ public void updateSongsQueue(ArrayList<Song> s) {
406328 bindService (new Intent (this , PlayerService .class ), connection , BIND_AUTO_CREATE );
407329 }
408330
409- private void saveUIState () {
331+ public void saveUIState () {
410332 viewmodel .markDataAsSaved (true );
411333 viewmodel .setBNVAsHidden (isBnvHidden );
412334 viewmodel .saveBNVPosition (binding .bottomNavigation .getSelectedItemId ());
@@ -427,6 +349,13 @@ private void restoreStateIfPossible() {
427349 updateColors ();
428350 }
429351 if (mediaController .getMediaItemCount () > 0 ) {
352+ if (mediaController .isPlaying ()) {
353+ binding .toggleView .forcePauseState ();
354+ binding .toggleView .forcePlayState ();
355+ } else {
356+ binding .toggleView .forcePauseState ();
357+ binding .toggleView .forcePlayState ();
358+ }
430359 updateAdapters (index , mediaController .isPlaying ());
431360 updateProgress (mediaController .getCurrentPosition ());
432361 updateColors ();
@@ -450,6 +379,11 @@ private void restoreStateIfPossible() {
450379 } else if (PlayerService .isAnythingPlaying ()) {
451380 ColorPaletteUtils .darkColors = PlayerService .darkColors ;
452381 ColorPaletteUtils .lightColors = PlayerService .lightColors ;
382+ if (!PlayerService .isPlaying ) {
383+ binding .toggleView .forcePauseState ();
384+ } else {
385+ binding .toggleView .forcePlayState ();
386+ }
453387 updateColors ();
454388 String songPath = RuntimeData .songs .get (mediaController .getCurrentMediaItemIndex ()).path ;
455389 loadLyrics (songPath );
@@ -627,6 +561,7 @@ public void onStateChanged(@NonNull View bottomSheet, int newState) {
627561 ColorPaletteUtils .lightColors = null ;
628562 ColorPaletteUtils .darkColors = null ;
629563 PlayerService .currentPosition = -1 ;
564+ mediaController .pause ();
630565 mediaController .stop ();
631566 mediaController .clearMediaItems ();
632567 PlayerService .areMediaItemsEmpty = true ;
@@ -884,7 +819,7 @@ public void onComplete(ArrayList<Song> songs) {
884819 });
885820 }
886821
887- private void syncPlayerUI (int position ) {
822+ public void syncPlayerUI (int position ) {
888823 updateMaxValue (position );
889824 updateCoverPager (position );
890825
@@ -1269,7 +1204,7 @@ public void onSeekRequested(long ms) {
12691204 }
12701205 }
12711206
1272- private void loadLyrics (String path ) {
1207+ public void loadLyrics (String path ) {
12731208 LyricsExtractor .extract (path , lyrics -> {
12741209 if (lyrics != null && !lyrics .isEmpty ()) {
12751210 LyricsParser .parse (lyrics , result -> {
0 commit comments