[Android] Remember the last movie and its position

Sébastien Toque git at videolan.org
Mon Mar 5 23:10:57 CET 2012


android | branch: master | Sébastien Toque <xilasz at gmail.com> | Mon Mar  5 23:10:49 2012 +0100| [7cb36ca18720cb78a8d03fc4be0faa5d6da2e4ae] | committer: Sébastien Toque

Remember the last movie and its position

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

 .../org/videolan/vlc/gui/PreferencesActivity.java  |    4 +++
 .../videolan/vlc/gui/video/VideoListActivity.java  |   14 ++++++++++
 .../videolan/vlc/gui/video/VideoListAdapter.java   |    7 +++++
 .../vlc/gui/video/VideoPlayerActivity.java         |   26 +++++++++++++++++++-
 4 files changed, 50 insertions(+), 1 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
index 56b68b1..aa2e4bc 100644
--- a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
@@ -38,6 +38,10 @@ public class PreferencesActivity extends PreferenceActivity {
 
     public final static String TAG = "VLC/PreferencesActivity";
 
+    public final static String NAME = "VlcSharedPreferences";
+    public final static String LAST_MEDIA = "LastMedia";
+    public final static String LAST_TIME = "LastTime";
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListActivity.java
index 2beacaa..c038d18 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListActivity.java
@@ -29,11 +29,14 @@ import org.videolan.vlc.Media;
 import org.videolan.vlc.MediaLibrary;
 import org.videolan.vlc.R;
 import org.videolan.vlc.ThumbnailerManager;
+import org.videolan.vlc.gui.PreferencesActivity;
 import org.videolan.vlc.gui.SearchActivity;
 import org.videolan.vlc.interfaces.ISortable;
 
 import android.app.ListActivity;
+import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
@@ -73,6 +76,17 @@ public class VideoListActivity extends ListActivity implements ISortable {
     }
 
     @Override
+    protected void onResume() {
+        //Get & highlight the last media
+        SharedPreferences preferences = getSharedPreferences(PreferencesActivity.NAME, Context.MODE_PRIVATE);
+        String lastPath = preferences.getString("LastMedia", null);
+        long lastTime = preferences.getLong("LastTime", 0);
+        mVideoAdapter.setLastMedia(lastTime > 0 ? lastPath : null);
+        mVideoAdapter.notifyDataSetChanged();
+        super.onResume();
+    }
+
+    @Override
     protected void onDestroy() {
         mMediaLibrary.removeUpdateHandler(mHandler);
         mThumbnailerManager.clearJobs();
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 b573dc6..b230b42 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -30,6 +30,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.graphics.Color;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
@@ -45,6 +46,7 @@ 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 String mLastPath;
 
     public VideoListAdapter(Context context, int textViewResourceId) {
         super(context, textViewResourceId);
@@ -60,6 +62,10 @@ public class VideoListAdapter extends ArrayAdapter<Media>
         }
     }
 
+    public void setLastMedia(String lastPath) {
+        mLastPath = lastPath;
+    }
+
     public void sortBy(int sortby) {
         switch (sortby) {
             case SORT_BY_TITLE:
@@ -138,6 +144,7 @@ public class VideoListAdapter extends ArrayAdapter<Media>
             holder.thumbnail.setImageBitmap(thumbnail);
         }
 
+        holder.title.setTextColor(media.getPath().equals(mLastPath) ? Color.RED : Color.WHITE);
         holder.more.setTag(media);
         holder.more.setOnClickListener(moreClickListener);
 
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 db84b59..26db89f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -28,6 +28,7 @@ import org.videolan.vlc.LibVLC;
 import org.videolan.vlc.LibVlcException;
 import org.videolan.vlc.R;
 import org.videolan.vlc.Util;
+import org.videolan.vlc.gui.PreferencesActivity;
 
 import android.app.Activity;
 import android.app.AlertDialog;
@@ -37,6 +38,7 @@ import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.SharedPreferences;
 import android.content.pm.ActivityInfo;
 import android.content.res.Configuration;
 import android.graphics.PixelFormat;
@@ -195,10 +197,19 @@ public class VideoPlayerActivity extends Activity {
 
     @Override
     protected void onPause() {
-        if (mLibVLC.isPlaying())
+        long time = 0;
+        if (mLibVLC.isPlaying()) {
+            time = mLibVLC.getTime() - 5000;
             mLibVLC.pause();
+        }
         if (mWakeLock.isHeld())
             mWakeLock.release();
+
+        // Save position
+        SharedPreferences preferences = getSharedPreferences(PreferencesActivity.NAME, MODE_PRIVATE);
+        SharedPreferences.Editor editor = preferences.edit();
+        editor.putLong(PreferencesActivity.LAST_TIME, time);
+        editor.commit();
         super.onPause();
     }
 
@@ -777,6 +788,9 @@ public class VideoPlayerActivity extends Activity {
     private void load() {
         String path = null;
         String title = null;
+        String lastPath = null;
+        long lastTime = 0;
+        SharedPreferences preferences = getSharedPreferences(PreferencesActivity.NAME, MODE_PRIVATE);
 
         if (getIntent().getAction() != null
                 && getIntent().getAction().equals(Intent.ACTION_VIEW)) {
@@ -786,11 +800,21 @@ public class VideoPlayerActivity extends Activity {
             /* Started from VideoListActivity */
             path = getIntent().getExtras().getString("filePath");
         }
+
         if (path != null && path.length() > 0) {
             mLibVLC.readMedia(path);
             if (!mWakeLock.isHeld())
                 mWakeLock.acquire();
 
+            // Save media for next time, and restore position if it's the same one as before
+            lastPath = preferences.getString(PreferencesActivity.LAST_MEDIA, null);
+            lastTime = preferences.getLong(PreferencesActivity.LAST_TIME, 0);
+            SharedPreferences.Editor editor = preferences.edit();
+            editor.putString(PreferencesActivity.LAST_MEDIA, path);
+            editor.commit();
+            if (lastTime > 0 && path.equals(lastPath))
+                mLibVLC.setTime(lastTime);
+
             title = new File(path).getName();
             int dotIndex = title.lastIndexOf('.');
             if (dotIndex != -1)



More information about the Android mailing list