[Android] Restore playlists from old mediadatabase

Geoffrey Métais git at videolan.org
Wed Dec 6 14:33:33 CET 2017


vlc-android | branch: 2.5.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Dec  6 14:04:39 2017 +0100| [dc38520bf5d39d3a4171532f15b6fe60a3992a29] | committer: Geoffrey Métais

Restore playlists from old mediadatabase

Restoration is done at the end of medialibrary parsing.
3 max attempts to get it, then we drop the case.

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

 .../src/org/videolan/vlc/MediaParsingService.java  |  3 ++
 vlc-android/src/org/videolan/vlc/util/Util.java    | 43 ++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.java b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
index e0dfb8013..81733f0a5 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.java
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
@@ -27,6 +27,7 @@ import org.videolan.vlc.gui.helpers.NotificationHelper;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.FileUtils;
 import org.videolan.vlc.util.Strings;
+import org.videolan.vlc.util.Util;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -341,6 +342,8 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
         mParsing = percent;
         if (mParsing != 100)
             showNotification();
+        else
+            Util.recoverPLaylists();
     }
 
     @Override
diff --git a/vlc-android/src/org/videolan/vlc/util/Util.java b/vlc-android/src/org/videolan/vlc/util/Util.java
index 11ce0cb13..66f19130c 100644
--- a/vlc-android/src/org/videolan/vlc/util/Util.java
+++ b/vlc-android/src/org/videolan/vlc/util/Util.java
@@ -22,16 +22,22 @@ package org.videolan.vlc.util;
 
 import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
+import android.support.v7.preference.PreferenceManager;
 import android.text.TextUtils;
 
 import org.videolan.libvlc.util.AndroidUtil;
+import org.videolan.medialibrary.Medialibrary;
 import org.videolan.medialibrary.Tools;
 import org.videolan.medialibrary.media.MediaLibraryItem;
+import org.videolan.medialibrary.media.MediaWrapper;
+import org.videolan.medialibrary.media.Playlist;
 import org.videolan.vlc.VLCApplication;
+import org.videolan.vlc.media.MediaDatabase;
 
 import java.io.BufferedReader;
 import java.io.Closeable;
@@ -41,6 +47,7 @@ import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.LinkedList;
 import java.util.List;
 
 public class Util {
@@ -182,4 +189,40 @@ public class Util {
         else
             ctx.startForegroundService(intent);
     }
+
+    public static void recoverPLaylists() {
+        final Context ctx = VLCApplication.getAppContext();
+        if (ctx == null) return;
+        final SharedPreferences prefs =  PreferenceManager.getDefaultSharedPreferences(ctx);
+        final int retryCount = prefs.getInt("playlist_migration_retry_count", 0);
+        try {
+            if (retryCount < 3) {
+                final MediaDatabase database = MediaDatabase.getInstance();
+                final Medialibrary medialibrary = Medialibrary.getInstance();
+                final String[] playlists = database.getPlaylists();
+                if (!isArrayEmpty(playlists)) {
+                    for (String pl : playlists) {
+                        final String[] locations = database.playlistGetItems(pl);
+                        if (!isArrayEmpty(locations)) {
+                            final LinkedList<Long> ids = new LinkedList<>();
+                            for (String mrl : locations) {
+                                MediaWrapper media = medialibrary.getMedia(mrl);
+                                if (media == null) media = medialibrary.addMedia(mrl);
+                                if (media != null) ids.add(media.getId());
+                            }
+                            if (!ids.isEmpty()) {
+                                final boolean playlistExists = !isArrayEmpty(medialibrary.searchPlaylist(pl));
+                                final Playlist playlist = medialibrary.createPlaylist(playlistExists ? pl+"_recovered" : pl);
+                                playlist.append(ids);
+                            }
+                        }
+                        database.playlistDelete(pl);
+                    }
+                }
+                prefs.edit().putInt("playlist_migration_retry_count", 3).apply();
+            }
+        } catch (Throwable ignored) {
+            prefs.edit().putInt("playlist_migration_retry_count", retryCount+1).apply();
+        }
+    }
 }



More information about the Android mailing list