[Android] Only save last media & position on exit and if not at the beginning

Sébastien Toque git at videolan.org
Wed Jul 4 07:32:58 CEST 2012


android | branch: master | Sébastien Toque <xilasz at gmail.com> | Wed Jul  4 07:32:38 2012 +0200| [7d5d28c19d512509b74e51ab8bdb94167d247370] | committer: Sébastien Toque

Only save last media & position on exit and if not at the beginning

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

 .../videolan/vlc/gui/video/VideoListFragment.java  |    4 +--
 .../vlc/gui/video/VideoPlayerActivity.java         |   29 ++++++++++----------
 2 files changed, 17 insertions(+), 16 deletions(-)

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 11c58ea..099bd21 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListFragment.java
@@ -105,8 +105,8 @@ public class VideoListFragment extends SherlockListFragment implements ISortable
     public void onResume() {
         //Get & highlight the last media
         SharedPreferences preferences = getActivity().getSharedPreferences(PreferencesActivity.NAME, Context.MODE_PRIVATE);
-        String lastPath = preferences.getString("LastMedia", null);
-        long lastTime = preferences.getLong("LastTime", 0);
+        String lastPath = preferences.getString(PreferencesActivity.LAST_MEDIA, null);
+        long lastTime = preferences.getLong(PreferencesActivity.LAST_TIME, 0);
         mVideoAdapter.setLastMedia(lastTime, lastPath);
         mVideoAdapter.notifyDataSetChanged();
         mMediaLibrary.addUpdateHandler(mHandler);
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 4fec8d2..ee8251b 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -81,6 +81,7 @@ public class VideoPlayerActivity extends Activity {
     private SurfaceView mSurface;
     private SurfaceHolder mSurfaceHolder;
     private LibVLC mLibVLC;
+    private String mLocation;
 
     private static final int SURFACE_BEST_FIT = 0;
     private static final int SURFACE_FIT_HORIZONTAL = 1;
@@ -212,10 +213,13 @@ public class VideoPlayerActivity extends Activity {
         mSurface.setKeepScreenOn(false);
 
         // Save position
-        SharedPreferences preferences = getSharedPreferences(PreferencesActivity.NAME, MODE_PRIVATE);
-        SharedPreferences.Editor editor = preferences.edit();
-        editor.putLong(PreferencesActivity.LAST_TIME, time);
-        editor.commit();
+        if (time >= 0) {
+            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();
+        }
         super.onPause();
     }
 
@@ -827,7 +831,7 @@ public class VideoPlayerActivity extends Activity {
      *
      */
     private void load() {
-        String location = null;
+        mLocation = null;
         String title = null;
         String lastLocation = null;
         long lastTime = 0;
@@ -836,27 +840,24 @@ public class VideoPlayerActivity extends Activity {
         if (getIntent().getAction() != null
                 && getIntent().getAction().equals(Intent.ACTION_VIEW)) {
             /* Started from external application */
-            location = getIntent().getDataString();
+            mLocation = getIntent().getDataString();
         } else if(getIntent().getExtras() != null) {
             /* Started from VideoListActivity */
-            location = getIntent().getExtras().getString("itemLocation");
+            mLocation = getIntent().getExtras().getString("itemLocation");
         }
 
-        if (location != null && location.length() > 0) {
-            mLibVLC.readMedia(location, false);
+        if (mLocation != null && mLocation.length() > 0) {
+            mLibVLC.readMedia(mLocation, false);
             mSurface.setKeepScreenOn(true);
 
             // Save media for next time, and restore position if it's the same one as before
             lastLocation = preferences.getString(PreferencesActivity.LAST_MEDIA, null);
             lastTime = preferences.getLong(PreferencesActivity.LAST_TIME, 0);
-            SharedPreferences.Editor editor = preferences.edit();
-            editor.putString(PreferencesActivity.LAST_MEDIA, location);
-            editor.commit();
-            if (lastTime > 0 && location.equals(lastLocation))
+            if (lastTime > 0 && mLocation.equals(lastLocation))
                 mLibVLC.setTime(lastTime);
 
             try {
-                title = URLDecoder.decode(location, "UTF-8");
+                title = URLDecoder.decode(mLocation, "UTF-8");
             } catch (UnsupportedEncodingException e) {
             }
             if (title.startsWith("file:")) {



More information about the Android mailing list