[Android] Save & Restore last position for each video media

Sébastien Toque git at videolan.org
Tue Aug 7 18:49:33 CEST 2012


android | branch: master | Sébastien Toque <xilasz at gmail.com> | Tue Aug  7 18:44:05 2012 +0200| [441a48ace82e31ae9472dfea64a3cdcdce9ca30a] | committer: Sébastien Toque

Save & Restore last position for each video media

> http://git.videolan.org/gitweb.cgi/android.git/?a=commit;h=441a48ace82e31ae9472dfea64a3cdcdce9ca30a
---

 .../src/org/videolan/vlc/DatabaseManager.java      |   39 ++++++++++++++++++++
 .../org/videolan/vlc/gui/PreferencesActivity.java  |    1 -
 .../videolan/vlc/gui/video/VideoListAdapter.java   |   26 +++++++------
 .../videolan/vlc/gui/video/VideoListFragment.java  |    6 ++-
 .../vlc/gui/video/VideoPlayerActivity.java         |   17 +++++----
 5 files changed, 67 insertions(+), 22 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/DatabaseManager.java b/vlc-android/src/org/videolan/vlc/DatabaseManager.java
index 3f2abb3..2ba06b2 100644
--- a/vlc-android/src/org/videolan/vlc/DatabaseManager.java
+++ b/vlc-android/src/org/videolan/vlc/DatabaseManager.java
@@ -358,6 +358,41 @@ public class DatabaseManager {
         return medias;
     }
 
+    public synchronized HashMap<String, Long> getVideoTimes(Context context) {
+
+        Cursor cursor;
+        HashMap<String, Long> times = new HashMap<String, Long>();
+        int chunk_count = 0;
+        int count = 0;
+
+        do {
+            count = 0;
+            cursor = mDb.rawQuery(String.format(Locale.US,
+                    "SELECT %s,%s FROM %s WHERE %s=%d LIMIT %d OFFSET %d",
+                    MEDIA_LOCATION, //0 string
+                    MEDIA_TIME, //1 long
+                    MEDIA_TABLE_NAME,
+                    MEDIA_TYPE,
+                    Media.TYPE_VIDEO,
+                    CHUNK_SIZE,
+                    chunk_count * CHUNK_SIZE), null);
+
+            if (cursor.moveToFirst()) {
+                do {
+                    String location = cursor.getString(0);
+                    long time = cursor.getLong(1);
+                    times.put(location, time);
+                    count++;
+                } while (cursor.moveToNext());
+            }
+
+            cursor.close();
+            chunk_count++;
+        } while (count == CHUNK_SIZE);
+
+        return times;
+    }
+
     public synchronized Media getMedia(Context context, String location) {
 
         Cursor cursor;
@@ -437,6 +472,10 @@ public class DatabaseManager {
                     values.put(MEDIA_PICTURE, new byte[1]);
                 }
                 break;
+            case MEDIA_TIME:
+                if (object != null)
+                    values.put(MEDIA_TIME, (Long)object);
+                break;
             default:
                 return;
         }
diff --git a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
index e3f9266..9e10bd6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
@@ -47,7 +47,6 @@ public class PreferencesActivity extends PreferenceActivity {
 
     public final static String NAME = "VlcSharedPreferences";
     public final static String LAST_MEDIA = "LastMedia";
-    public final static String LAST_TIME = "LastTime";
 
     @SuppressWarnings("deprecation")
     @Override
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
index 90049b1..57f233f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -21,6 +21,7 @@
 package org.videolan.vlc.gui.video;
 
 import java.util.Comparator;
+import java.util.HashMap;
 
 import org.videolan.vlc.Media;
 import org.videolan.vlc.R;
@@ -46,7 +47,6 @@ public class VideoListAdapter extends ArrayAdapter<Media>
     public final static int SORT_BY_LENGTH = 1;
     private int mSortDirection = 1;
     private int mSortBy = SORT_BY_TITLE;
-    private long mLastTime;
     private String mLastMRL;
 
     public VideoListAdapter(Context context) {
@@ -63,9 +63,15 @@ public class VideoListAdapter extends ArrayAdapter<Media>
         }
     }
 
-    public void setLastMedia(long lastTime, String lastMRL) {
-        mLastTime = lastTime;
-        mLastMRL = lastTime > 0 ? lastMRL : null;
+    public void setLastMedia(String lastMRL, HashMap<String, Long> times) {
+        mLastMRL = lastMRL;
+        // update times
+        for (int i = 0; i < getCount(); ++i) {
+            Media media = getItem(i);
+            Long time = times.get(media.getLocation());
+            if (time != null)
+                media.setTime(time);
+        }
     }
 
     public void sortBy(int sortby) {
@@ -148,17 +154,15 @@ public class VideoListAdapter extends ArrayAdapter<Media>
             holder.thumbnail.setImageBitmap(thumbnail);
         }
 
-        if (media.getLocation().equals(mLastMRL))
-        {
-            holder.title.setTextColor(0xFFF48B00 /* ORANGE */);
+        holder.title.setTextColor(media.getLocation().equals(mLastMRL) ? 0xFFF48B00 /* ORANGE */ : Color.WHITE);
+        long lastTime = media.getTime();
+        if (lastTime > 0) {
             holder.subtitle.setText(String.format("%s / %s - %dx%d",
-                    Util.millisToString(mLastTime),
+                    Util.millisToString(lastTime),
                     Util.millisToString(media.getLength()),
                     media.getWidth(), media.getHeight()));
         }
-        else
-        {
-            holder.title.setTextColor(Color.WHITE);
+        else {
             holder.subtitle.setText(String.format("%s - %dx%d",
                     Util.millisToString(media.getLength()),
                     media.getWidth(), media.getHeight()));
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListFragment.java
index b660fb6..d8b473f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListFragment.java
@@ -20,11 +20,13 @@
 
 package org.videolan.vlc.gui.video;
 
+import java.util.HashMap;
 import java.util.List;
 import java.util.concurrent.BrokenBarrierException;
 import java.util.concurrent.CyclicBarrier;
 
 import org.videolan.vlc.AudioServiceController;
+import org.videolan.vlc.DatabaseManager;
 import org.videolan.vlc.Media;
 import org.videolan.vlc.MediaLibrary;
 import org.videolan.vlc.R;
@@ -105,8 +107,8 @@ public class VideoListFragment extends SherlockListFragment implements ISortable
         //Get & highlight the last media
         SharedPreferences preferences = getActivity().getSharedPreferences(PreferencesActivity.NAME, Context.MODE_PRIVATE);
         String lastPath = preferences.getString(PreferencesActivity.LAST_MEDIA, null);
-        long lastTime = preferences.getLong(PreferencesActivity.LAST_TIME, 0);
-        mVideoAdapter.setLastMedia(lastTime, lastPath);
+        HashMap<String, Long> times = DatabaseManager.getInstance(getActivity()).getVideoTimes(getActivity());
+        mVideoAdapter.setLastMedia(lastPath, times);
         mVideoAdapter.notifyDataSetChanged();
         mMediaLibrary.addUpdateHandler(mHandler);
         super.onResume();
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 0d4da85..22d3372 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -26,9 +26,11 @@ import java.lang.reflect.Method;
 import java.net.URLDecoder;
 
 import org.videolan.vlc.AudioServiceController;
+import org.videolan.vlc.DatabaseManager;
 import org.videolan.vlc.EventManager;
 import org.videolan.vlc.LibVLC;
 import org.videolan.vlc.LibVlcException;
+import org.videolan.vlc.Media;
 import org.videolan.vlc.R;
 import org.videolan.vlc.Util;
 import org.videolan.vlc.WeakHandler;
@@ -306,8 +308,11 @@ public class VideoPlayerActivity extends Activity {
             SharedPreferences preferences = getSharedPreferences(PreferencesActivity.NAME, MODE_PRIVATE);
             SharedPreferences.Editor editor = preferences.edit();
             editor.putString(PreferencesActivity.LAST_MEDIA, mLocation);
-            editor.putLong(PreferencesActivity.LAST_TIME, time);
             editor.commit();
+            DatabaseManager.getInstance(this).updateMedia(
+                    mLocation,
+                    DatabaseManager.mediaColumn.MEDIA_TIME,
+                    time);
         }
         super.onPause();
     }
@@ -999,10 +1004,7 @@ public class VideoPlayerActivity extends Activity {
     private void load() {
         mLocation = null;
         String title = null;
-        String lastLocation = null;
-        long lastTime = 0;
         boolean dontParse = false;
-        SharedPreferences preferences = getSharedPreferences(PreferencesActivity.NAME, MODE_PRIVATE);
 
         if (getIntent().getAction() != null
                 && getIntent().getAction().equals(Intent.ACTION_VIEW)) {
@@ -1019,10 +1021,9 @@ public class VideoPlayerActivity extends Activity {
             mSurface.setKeepScreenOn(true);
 
             // restore last position
-            lastLocation = preferences.getString(PreferencesActivity.LAST_MEDIA, null);
-            lastTime = preferences.getLong(PreferencesActivity.LAST_TIME, 0);
-            if (lastTime > 0 && mLocation.equals(lastLocation))
-                mLibVLC.setTime(lastTime);
+            Media media = DatabaseManager.getInstance(this).getMedia(this, mLocation);
+            if (media != null && media.getTime() > 0)
+                mLibVLC.setTime(media.getTime());
 
             try {
                 title = URLDecoder.decode(mLocation, "UTF-8");



More information about the Android mailing list