[Android] Medialibrary: Remove dependency to 'Tools' module

Geoffrey Métais git at videolan.org
Fri Dec 7 14:11:09 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Dec  3 11:26:39 2018 +0100| [e84124e02c6f6138e4e87f4264f9027e0c45ba3a] | committer: Geoffrey Métais

Medialibrary: Remove dependency to 'Tools' module

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

 medialibrary/build.gradle                          |  1 -
 .../org/videolan/medialibrary/Medialibrary.java    | 23 +++++++-
 .../src/org/videolan/medialibrary/SingleEvent.java | 68 ++++++++++++++++++++++
 .../java/videolan/org/commontools/LiveEvent.kt     | 20 +++++++
 4 files changed, 109 insertions(+), 3 deletions(-)

diff --git a/medialibrary/build.gradle b/medialibrary/build.gradle
index d9d87a9fe..bbd9e64f8 100644
--- a/medialibrary/build.gradle
+++ b/medialibrary/build.gradle
@@ -99,7 +99,6 @@ dependencies {
     api "androidx.core:core:$rootProject.ext.androidxVersion"
     api "androidx.fragment:fragment:$rootProject.ext.androidxVersion"
     testImplementation "junit:junit:$rootProject.ext.junitVersion"
-    implementation project(':tools')
 }
 
 apply from: '../publish.gradle'
diff --git a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
index 174157fef..d556aa6e3 100644
--- a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
@@ -1,3 +1,23 @@
+/*****************************************************************************
+ * Medialibrary.java
+ *****************************************************************************
+ * Copyright © 2017-2018 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
 package org.videolan.medialibrary;
 
 import android.Manifest;
@@ -33,7 +53,6 @@ import androidx.core.content.ContextCompat;
 import androidx.lifecycle.LiveData;
 import androidx.lifecycle.MutableLiveData;
 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
-import videolan.org.commontools.LiveEvent;
 
 @SuppressWarnings("JniMissingFunction")
 public class Medialibrary {
@@ -686,7 +705,7 @@ public class Medialibrary {
         }
     }
 
-    public static LiveData<MediaWrapper> lastThumb = new LiveEvent<>();
+    public static LiveData<MediaWrapper> lastThumb = new SingleEvent<>();
     @SuppressWarnings({"unused", "unchecked"})
     void onMediaThumbnailReady(MediaWrapper media, boolean success) {
         if (success) ((MutableLiveData)lastThumb).postValue(media);
diff --git a/medialibrary/src/org/videolan/medialibrary/SingleEvent.java b/medialibrary/src/org/videolan/medialibrary/SingleEvent.java
new file mode 100644
index 000000000..daf67c9e0
--- /dev/null
+++ b/medialibrary/src/org/videolan/medialibrary/SingleEvent.java
@@ -0,0 +1,68 @@
+/*****************************************************************************
+ * SingleEvent.java
+ *****************************************************************************
+ * Copyright © 2018 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+package org.videolan.medialibrary;
+
+import android.util.Log;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import androidx.annotation.NonNull;
+import androidx.lifecycle.LifecycleOwner;
+import androidx.lifecycle.MutableLiveData;
+import androidx.lifecycle.Observer;
+
+public class SingleEvent<T> extends MutableLiveData<T> {
+
+    private static final String TAG = "SingleEvent";
+    private final AtomicBoolean mPending = new AtomicBoolean(false);
+
+    @Override
+    public void observe(@NonNull LifecycleOwner owner, @NonNull final Observer observer) {
+        if (hasActiveObservers()) Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
+        // Observe the internal MutableLiveData
+        super.observe(owner, new Observer<T>() {
+            @Override
+            public void onChanged(T t) {
+                if (mPending.compareAndSet(true, false)) observer.onChanged(t);
+            }
+        });
+    }
+
+    @Override
+    public void observeForever(@NonNull final Observer<? super T> observer) {
+        super.observeForever(new Observer<T>() {
+            @Override
+            public void onChanged(T t) {
+                if (mPending.compareAndSet(true, false)) observer.onChanged(t);
+            }
+        });
+    }
+
+    @Override
+    public void setValue(T value) {
+        mPending.set(true);
+        super.setValue(value);
+    }
+
+    public void clear() {
+        super.setValue(null);
+    }
+}
diff --git a/tools/src/main/java/videolan/org/commontools/LiveEvent.kt b/tools/src/main/java/videolan/org/commontools/LiveEvent.kt
index e9daf4931..4a911362b 100644
--- a/tools/src/main/java/videolan/org/commontools/LiveEvent.kt
+++ b/tools/src/main/java/videolan/org/commontools/LiveEvent.kt
@@ -1,3 +1,23 @@
+/*****************************************************************************
+ * LiveEvent.kt
+ *****************************************************************************
+ * Copyright © 2017-2018 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
 package videolan.org.commontools
 
 import android.util.Log



More information about the Android mailing list