[Android] Disconnect details view from playback service

Geoffrey Métais git at videolan.org
Wed Jan 16 09:53:12 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Jan 16 09:47:05 2019 +0100| [1779dd48424397954b0cd638dc5c52807590560d] | committer: Geoffrey Métais

Disconnect details view from playback service

> https://code.videolan.org/videolan/vlc-android/commit/1779dd48424397954b0cd638dc5c52807590560d
---

 .../vlc/gui/tv/MediaItemDetailsFragment.java       | 46 ++++++++--------------
 .../videolan/vlc/widget/VLCAppWidgetProvider.java  | 13 +++---
 2 files changed, 24 insertions(+), 35 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetailsFragment.java b/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetailsFragment.java
index 6bd9c7684..0357a30f0 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetailsFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetailsFragment.java
@@ -22,6 +22,7 @@ package org.videolan.vlc.gui.tv;
 
 import android.annotation.TargetApi;
 import android.app.Activity;
+import android.content.Context;
 import android.content.Intent;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
@@ -33,9 +34,7 @@ import android.widget.Toast;
 
 import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.medialibrary.media.MediaWrapper;
-import org.videolan.vlc.PlaybackService;
 import org.videolan.vlc.R;
-import org.videolan.vlc.gui.PlaybackServiceFragment;
 import org.videolan.vlc.gui.helpers.AudioUtil;
 import org.videolan.vlc.gui.helpers.UiTools;
 import org.videolan.vlc.gui.tv.audioplayer.AudioPlayerActivity;
@@ -60,8 +59,10 @@ import androidx.leanback.widget.ListRow;
 import androidx.leanback.widget.ListRowPresenter;
 import androidx.leanback.widget.OnActionClickedListener;
 
+import static org.videolan.vlc.util.Constants.ACTION_REMOTE_STOP;
+
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
-public class MediaItemDetailsFragment extends DetailsSupportFragment implements PlaybackService.Client.Callback {
+public class MediaItemDetailsFragment extends DetailsSupportFragment {
     private static final String TAG = "MediaItemDetailsFragment";
     private static final int ID_PLAY = 1;
     private static final int ID_LISTEN = 2;
@@ -77,7 +78,7 @@ public class MediaItemDetailsFragment extends DetailsSupportFragment implements
     private MediaItemDetails mMedia;
     private MediaWrapper mMediaWrapper;
     private BrowserFavRepository mBrowserFavRepository;
-    private PlaybackService mService;
+    private boolean mMediaStarted;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -85,6 +86,7 @@ public class MediaItemDetailsFragment extends DetailsSupportFragment implements
         mBackgroundManager = BackgroundManager.getInstance(requireActivity());
         mBackgroundManager.setAutoReleaseOnStop(false);
         mBrowserFavRepository = BrowserFavRepository.Companion.getInstance(requireContext());
+        mMediaStarted = false;
         buildDetails();
     }
 
@@ -98,18 +100,12 @@ public class MediaItemDetailsFragment extends DetailsSupportFragment implements
     public void onPause() {
         mBackgroundManager.release();
         super.onPause();
-        if (mService != null && mService.isPlaying()) mService.stop();
-    }
-
-    @Override
-    public void onStop() {
-        super.onStop();
-        PlaybackServiceFragment.unregisterPlaybackService(this, this);
-    }
-
-    @Override
-    public void onDestroy() {
-        super.onDestroy();
+        if (mMediaStarted) {
+            final Context ctx = getContext();
+            if (ctx != null) {
+                ctx.sendBroadcast(new Intent(ACTION_REMOTE_STOP));
+            }
+        }
     }
 
     private void buildDetails() {
@@ -141,9 +137,11 @@ public class MediaItemDetailsFragment extends DetailsSupportFragment implements
             public void onActionClicked(Action action) {
                 switch ((int)action.getId()){
                     case ID_LISTEN:
-                        PlaybackServiceFragment.registerPlaybackService(MediaItemDetailsFragment.this, MediaItemDetailsFragment.this);
+                        MediaUtils.INSTANCE.openMedia(activity, mMediaWrapper);
+                        mMediaStarted = true;
                         break;
                     case ID_PLAY:
+                        mMediaStarted = false;
                         TvUtil.INSTANCE.playMedia(activity, media);
                         activity.finish();
                         break;
@@ -189,6 +187,7 @@ public class MediaItemDetailsFragment extends DetailsSupportFragment implements
                         }
                         break;
                     case ID_PLAY_FROM_START:
+                        mMediaStarted = false;
                         VideoPlayerActivity.start(getActivity(), media.getUri(), true);
                         activity.finish();
                         break;
@@ -205,7 +204,7 @@ public class MediaItemDetailsFragment extends DetailsSupportFragment implements
                 final Bitmap cover = media.getType() == MediaWrapper.TYPE_AUDIO || media.getType() == MediaWrapper.TYPE_VIDEO
                 ? AudioUtil.readCoverBitmap(mMedia.getArtworkUrl(), 512) : null;
                 final Bitmap blurred = cover != null ? UiTools.blurBitmap(cover) : null;
-                final Boolean browserFavExists = mBrowserFavRepository.browserFavExists((Uri.parse(mMedia.getLocation())));
+                final boolean browserFavExists = mBrowserFavRepository.browserFavExists((Uri.parse(mMedia.getLocation())));
                 final boolean isDir = media.getType() == MediaWrapper.TYPE_DIR;
                 final boolean canSave = isDir && FileUtils.canSave(media);
                 WorkersKt.runOnMainThread(new Runnable() {
@@ -256,15 +255,4 @@ public class MediaItemDetailsFragment extends DetailsSupportFragment implements
             }
         });
     }
-
-    @Override
-    public void onConnected(PlaybackService service) {
-        mService = service;
-        mService.load(mMediaWrapper);
-    }
-
-    @Override
-    public void onDisconnected() {
-        mService = null;
-    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java b/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java
index 510f271a8..02a7ba7bc 100644
--- a/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java
+++ b/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java
@@ -43,12 +43,13 @@ import org.videolan.vlc.util.WorkersKt;
 
 import java.util.Locale;
 
+import static org.videolan.vlc.util.Constants.ACTION_REMOTE_BACKWARD;
+import static org.videolan.vlc.util.Constants.ACTION_REMOTE_FORWARD;
+import static org.videolan.vlc.util.Constants.ACTION_REMOTE_PLAYPAUSE;
+import static org.videolan.vlc.util.Constants.ACTION_REMOTE_STOP;
+
 abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
     public static final String TAG = "VLC/VLCAppWidgetProvider";
-    public static final String ACTION_REMOTE_BACKWARD = Strings.buildPkgString("remote.Backward");
-    public static final String ACTION_REMOTE_PLAYPAUSE = Strings.buildPkgString("remote.PlayPause");
-    public static final String ACTION_REMOTE_STOP = Strings.buildPkgString("remote.Stop");
-    public static final String ACTION_REMOTE_FORWARD = Strings.buildPkgString("remote.Forward");
     public static final String ACTION_WIDGET_PREFIX = Strings.buildPkgString("widget.");
     public static final String ACTION_WIDGET_INIT = ACTION_WIDGET_PREFIX+"INIT";
     public static final String ACTION_WIDGET_UPDATE = ACTION_WIDGET_PREFIX+"UPDATE";
@@ -107,8 +108,8 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
         }
 
         if (ACTION_WIDGET_UPDATE.equals(action)) {
-            String title = intent.getStringExtra("title");
-            String artist = intent.getStringExtra("artist");
+            final String title = intent.getStringExtra("title");
+            final String artist = intent.getStringExtra("artist");
             final boolean isplaying = intent.getBooleanExtra("isplaying", false);
 
             views.setTextViewText(R.id.songName, title);



More information about the Android mailing list