[Android] Disconnect preferences from PlaybackService

Geoffrey Métais git at videolan.org
Mon Jan 14 11:46:27 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jan 14 11:45:58 2019 +0100| [226dd128946132b7c27168a597f3df205b06bd25] | committer: Geoffrey Métais

Disconnect preferences from PlaybackService

> https://code.videolan.org/videolan/vlc-android/commit/226dd128946132b7c27168a597f3df205b06bd25
---

 .../src/org/videolan/vlc/PlaybackService.kt        | 11 ++---
 .../vlc/gui/preferences/PreferencesActivity.java   | 48 +++++++---------------
 .../gui/tv/preferences/PreferencesActivity.java    | 39 ++++--------------
 3 files changed, 28 insertions(+), 70 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 5f24f0d2d..5e125af88 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -46,10 +46,6 @@ import androidx.core.app.ServiceCompat
 import androidx.core.content.ContextCompat
 import androidx.lifecycle.LifecycleOwner
 import androidx.lifecycle.MutableLiveData
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.ObsoleteCoroutinesApi
 import androidx.lifecycle.Observer
 import androidx.lifecycle.ServiceLifecycleDispatcher
 import androidx.media.MediaBrowserServiceCompat
@@ -75,6 +71,7 @@ import org.videolan.vlc.util.*
 import org.videolan.vlc.widget.VLCAppWidgetProvider
 import org.videolan.vlc.widget.VLCAppWidgetProviderBlack
 import org.videolan.vlc.widget.VLCAppWidgetProviderWhite
+import videolan.org.commontools.LiveEvent
 import java.util.*
 
 private const val TAG = "VLC/PlaybackService"
@@ -502,6 +499,8 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
 
         keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
         renderer.observe(this, Observer { setRenderer(it) })
+        restartPlayer.observe(this, Observer { restartMediaPlayer() })
+        headSetDetection.observe(this, Observer { detectHeadset(it) })
     }
 
     private fun updateHasWidget() {
@@ -1331,7 +1330,9 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
 
     companion object {
 
-        var renderer = MutableLiveData<RendererItem>()
+        val renderer = MutableLiveData<RendererItem>()
+        val restartPlayer = LiveEvent<Boolean>()
+        val headSetDetection = LiveEvent<Boolean>()
 
         private const val SHOW_TOAST = 1
         private const val END_MEDIASESSION = 2
diff --git a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.java b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.java
index f254449d5..1991cd81d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.java
@@ -23,18 +23,21 @@ package org.videolan.vlc.gui.preferences;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
-import com.google.android.material.appbar.AppBarLayout;
-import androidx.core.view.ViewCompat;
-import androidx.appcompat.app.AppCompatActivity;
-import androidx.appcompat.widget.Toolbar;
 import android.view.MenuItem;
 
+import com.google.android.material.appbar.AppBarLayout;
+
 import org.videolan.vlc.PlaybackService;
 import org.videolan.vlc.R;
 import org.videolan.vlc.util.Settings;
 
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.appcompat.widget.Toolbar;
+import androidx.core.view.ViewCompat;
+import videolan.org.commontools.LiveEvent;
+
 @SuppressWarnings("deprecation")
-public class PreferencesActivity extends AppCompatActivity implements PlaybackService.Client.Callback {
+public class PreferencesActivity extends AppCompatActivity {
 
     public final static String TAG = "VLC/PreferencesActivity";
 
@@ -56,8 +59,6 @@ public class PreferencesActivity extends AppCompatActivity implements PlaybackSe
     public final static int RESULT_UPDATE_SEEN_MEDIA = RESULT_FIRST_USER + 4;
     public final static int RESULT_UPDATE_ARTISTS = RESULT_FIRST_USER + 5;
 
-    private PlaybackService.Client mClient = new PlaybackService.Client(this, this);
-    private PlaybackService mService;
     private AppBarLayout mAppBarLayout;
 
     @Override
@@ -87,18 +88,6 @@ public class PreferencesActivity extends AppCompatActivity implements PlaybackSe
         mAppBarLayout.setExpanded(true);
     }
 
-    @Override
-    protected void onStart() {
-        super.onStart();
-        mClient.connect();
-    }
-
-    @Override
-    protected void onStop() {
-        super.onStop();
-        mClient.disconnect();
-    }
-
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         if (item.getItemId() == android.R.id.home) {
@@ -117,18 +106,9 @@ public class PreferencesActivity extends AppCompatActivity implements PlaybackSe
         }
     }
 
-    @Override
-    public void onConnected(PlaybackService service) {
-        mService = service;
-    }
-
-    @Override
-    public void onDisconnected() {
-        mService = null;
-    }
-
-    public void restartMediaPlayer(){
-        if (mService != null) mService.restartMediaPlayer();
+    public void restartMediaPlayer() {
+        final LiveEvent<Boolean> le = PlaybackService.Companion.getRestartPlayer();
+        if (le.hasObservers()) le.setValue(true);
     }
 
     public void exitAndRescan(){
@@ -150,8 +130,8 @@ public class PreferencesActivity extends AppCompatActivity implements PlaybackSe
         setResult(RESULT_UPDATE_ARTISTS);
     }
 
-    public void detectHeadset(boolean detect){
-        if (mService != null)
-            mService.detectHeadset(detect);
+    public void detectHeadset(boolean detect) {
+        final LiveEvent<Boolean> le = PlaybackService.Companion.getHeadSetDetection();
+        if (le.hasObservers()) le.setValue(detect);
     }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesActivity.java b/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesActivity.java
index 7e17a85b8..7fe833a3d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesActivity.java
@@ -35,10 +35,11 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.util.Settings;
 
 import androidx.fragment.app.FragmentActivity;
+import videolan.org.commontools.LiveEvent;
 
 @SuppressWarnings("deprecation")
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
-public class PreferencesActivity extends FragmentActivity implements PlaybackService.Client.Callback {
+public class PreferencesActivity extends FragmentActivity {
 
     public final static String TAG = "VLC/PreferencesActivity";
 
@@ -46,9 +47,6 @@ public class PreferencesActivity extends FragmentActivity implements PlaybackSer
     public final static int RESULT_RESTART = RESULT_FIRST_USER + 2;
     public final static int RESULT_RESTART_APP = RESULT_FIRST_USER + 3;
 
-    private PlaybackService.Client mClient = new PlaybackService.Client(this, this);
-    private PlaybackService mService;
-
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -56,18 +54,6 @@ public class PreferencesActivity extends FragmentActivity implements PlaybackSer
         setContentView(R.layout.tv_preferences_activity);
     }
 
-    @Override
-    protected void onStart() {
-        super.onStart();
-        mClient.connect();
-    }
-
-    @Override
-    protected void onStop() {
-        super.onStop();
-        mClient.disconnect();
-    }
-
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         if (item.getItemId() == android.R.id.home) {
@@ -85,19 +71,9 @@ public class PreferencesActivity extends FragmentActivity implements PlaybackSer
         }
     }
 
-    @Override
-    public void onConnected(PlaybackService service) {
-        mService = service;
-    }
-
-    @Override
-    public void onDisconnected() {
-        mService = null;
-    }
-
-    public void restartMediaPlayer(){
-        if (mService != null)
-            mService.restartMediaPlayer();
+    public void restartMediaPlayer() {
+        final LiveEvent<Boolean> le = PlaybackService.Companion.getRestartPlayer();
+        if (le.hasObservers()) le.setValue(true);
     }
 
     public void setRestart(){
@@ -115,7 +91,8 @@ public class PreferencesActivity extends FragmentActivity implements PlaybackSer
         startActivity(intent);
     }
 
-    public void detectHeadset(boolean detect){
-        if (mService != null) mService.detectHeadset(detect);
+    public void detectHeadset(boolean detect) {
+        final LiveEvent<Boolean> le = PlaybackService.Companion.getHeadSetDetection();
+        if (le.hasObservers()) le.setValue(detect);
     }
 }



More information about the Android mailing list