Commit 441a48ac authored by Sébastien Toque's avatar Sébastien Toque

Save & Restore last position for each video media

parent bde0509e
...@@ -358,6 +358,41 @@ public class DatabaseManager { ...@@ -358,6 +358,41 @@ public class DatabaseManager {
return medias; 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) { public synchronized Media getMedia(Context context, String location) {
Cursor cursor; Cursor cursor;
...@@ -437,6 +472,10 @@ public class DatabaseManager { ...@@ -437,6 +472,10 @@ public class DatabaseManager {
values.put(MEDIA_PICTURE, new byte[1]); values.put(MEDIA_PICTURE, new byte[1]);
} }
break; break;
case MEDIA_TIME:
if (object != null)
values.put(MEDIA_TIME, (Long)object);
break;
default: default:
return; return;
} }
......
...@@ -47,7 +47,6 @@ public class PreferencesActivity extends PreferenceActivity { ...@@ -47,7 +47,6 @@ public class PreferencesActivity extends PreferenceActivity {
public final static String NAME = "VlcSharedPreferences"; public final static String NAME = "VlcSharedPreferences";
public final static String LAST_MEDIA = "LastMedia"; public final static String LAST_MEDIA = "LastMedia";
public final static String LAST_TIME = "LastTime";
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
package org.videolan.vlc.gui.video; package org.videolan.vlc.gui.video;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap;
import org.videolan.vlc.Media; import org.videolan.vlc.Media;
import org.videolan.vlc.R; import org.videolan.vlc.R;
...@@ -46,7 +47,6 @@ public class VideoListAdapter extends ArrayAdapter<Media> ...@@ -46,7 +47,6 @@ public class VideoListAdapter extends ArrayAdapter<Media>
public final static int SORT_BY_LENGTH = 1; public final static int SORT_BY_LENGTH = 1;
private int mSortDirection = 1; private int mSortDirection = 1;
private int mSortBy = SORT_BY_TITLE; private int mSortBy = SORT_BY_TITLE;
private long mLastTime;
private String mLastMRL; private String mLastMRL;
public VideoListAdapter(Context context) { public VideoListAdapter(Context context) {
...@@ -63,9 +63,15 @@ public class VideoListAdapter extends ArrayAdapter<Media> ...@@ -63,9 +63,15 @@ public class VideoListAdapter extends ArrayAdapter<Media>
} }
} }
public void setLastMedia(long lastTime, String lastMRL) { public void setLastMedia(String lastMRL, HashMap<String, Long> times) {
mLastTime = lastTime; mLastMRL = lastMRL;
mLastMRL = lastTime > 0 ? lastMRL : null; // 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) { public void sortBy(int sortby) {
...@@ -148,17 +154,15 @@ public class VideoListAdapter extends ArrayAdapter<Media> ...@@ -148,17 +154,15 @@ public class VideoListAdapter extends ArrayAdapter<Media>
holder.thumbnail.setImageBitmap(thumbnail); holder.thumbnail.setImageBitmap(thumbnail);
} }
if (media.getLocation().equals(mLastMRL)) holder.title.setTextColor(media.getLocation().equals(mLastMRL) ? 0xFFF48B00 /* ORANGE */ : Color.WHITE);
{ long lastTime = media.getTime();
holder.title.setTextColor(0xFFF48B00 /* ORANGE */); if (lastTime > 0) {
holder.subtitle.setText(String.format("%s / %s - %dx%d", holder.subtitle.setText(String.format("%s / %s - %dx%d",
Util.millisToString(mLastTime), Util.millisToString(lastTime),
Util.millisToString(media.getLength()), Util.millisToString(media.getLength()),
media.getWidth(), media.getHeight())); media.getWidth(), media.getHeight()));
} }
else else {
{
holder.title.setTextColor(Color.WHITE);
holder.subtitle.setText(String.format("%s - %dx%d", holder.subtitle.setText(String.format("%s - %dx%d",
Util.millisToString(media.getLength()), Util.millisToString(media.getLength()),
media.getWidth(), media.getHeight())); media.getWidth(), media.getHeight()));
......
...@@ -20,11 +20,13 @@ ...@@ -20,11 +20,13 @@
package org.videolan.vlc.gui.video; package org.videolan.vlc.gui.video;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier; import java.util.concurrent.CyclicBarrier;
import org.videolan.vlc.AudioServiceController; import org.videolan.vlc.AudioServiceController;
import org.videolan.vlc.DatabaseManager;
import org.videolan.vlc.Media; import org.videolan.vlc.Media;
import org.videolan.vlc.MediaLibrary; import org.videolan.vlc.MediaLibrary;
import org.videolan.vlc.R; import org.videolan.vlc.R;
...@@ -105,8 +107,8 @@ public class VideoListFragment extends SherlockListFragment implements ISortable ...@@ -105,8 +107,8 @@ public class VideoListFragment extends SherlockListFragment implements ISortable
//Get & highlight the last media //Get & highlight the last media
SharedPreferences preferences = getActivity().getSharedPreferences(PreferencesActivity.NAME, Context.MODE_PRIVATE); SharedPreferences preferences = getActivity().getSharedPreferences(PreferencesActivity.NAME, Context.MODE_PRIVATE);
String lastPath = preferences.getString(PreferencesActivity.LAST_MEDIA, null); String lastPath = preferences.getString(PreferencesActivity.LAST_MEDIA, null);
long lastTime = preferences.getLong(PreferencesActivity.LAST_TIME, 0); HashMap<String, Long> times = DatabaseManager.getInstance(getActivity()).getVideoTimes(getActivity());
mVideoAdapter.setLastMedia(lastTime, lastPath); mVideoAdapter.setLastMedia(lastPath, times);
mVideoAdapter.notifyDataSetChanged(); mVideoAdapter.notifyDataSetChanged();
mMediaLibrary.addUpdateHandler(mHandler); mMediaLibrary.addUpdateHandler(mHandler);
super.onResume(); super.onResume();
......
...@@ -26,9 +26,11 @@ import java.lang.reflect.Method; ...@@ -26,9 +26,11 @@ import java.lang.reflect.Method;
import java.net.URLDecoder; import java.net.URLDecoder;
import org.videolan.vlc.AudioServiceController; import org.videolan.vlc.AudioServiceController;
import org.videolan.vlc.DatabaseManager;
import org.videolan.vlc.EventManager; import org.videolan.vlc.EventManager;
import org.videolan.vlc.LibVLC; import org.videolan.vlc.LibVLC;
import org.videolan.vlc.LibVlcException; import org.videolan.vlc.LibVlcException;
import org.videolan.vlc.Media;
import org.videolan.vlc.R; import org.videolan.vlc.R;
import org.videolan.vlc.Util; import org.videolan.vlc.Util;
import org.videolan.vlc.WeakHandler; import org.videolan.vlc.WeakHandler;
...@@ -306,8 +308,11 @@ public class VideoPlayerActivity extends Activity { ...@@ -306,8 +308,11 @@ public class VideoPlayerActivity extends Activity {
SharedPreferences preferences = getSharedPreferences(PreferencesActivity.NAME, MODE_PRIVATE); SharedPreferences preferences = getSharedPreferences(PreferencesActivity.NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit(); SharedPreferences.Editor editor = preferences.edit();
editor.putString(PreferencesActivity.LAST_MEDIA, mLocation); editor.putString(PreferencesActivity.LAST_MEDIA, mLocation);
editor.putLong(PreferencesActivity.LAST_TIME, time);
editor.commit(); editor.commit();
DatabaseManager.getInstance(this).updateMedia(
mLocation,
DatabaseManager.mediaColumn.MEDIA_TIME,
time);
} }
super.onPause(); super.onPause();
} }
...@@ -999,10 +1004,7 @@ public class VideoPlayerActivity extends Activity { ...@@ -999,10 +1004,7 @@ public class VideoPlayerActivity extends Activity {
private void load() { private void load() {
mLocation = null; mLocation = null;
String title = null; String title = null;
String lastLocation = null;
long lastTime = 0;
boolean dontParse = false; boolean dontParse = false;
SharedPreferences preferences = getSharedPreferences(PreferencesActivity.NAME, MODE_PRIVATE);
if (getIntent().getAction() != null if (getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_VIEW)) { && getIntent().getAction().equals(Intent.ACTION_VIEW)) {
...@@ -1019,10 +1021,9 @@ public class VideoPlayerActivity extends Activity { ...@@ -1019,10 +1021,9 @@ public class VideoPlayerActivity extends Activity {
mSurface.setKeepScreenOn(true); mSurface.setKeepScreenOn(true);
// restore last position // restore last position
lastLocation = preferences.getString(PreferencesActivity.LAST_MEDIA, null); Media media = DatabaseManager.getInstance(this).getMedia(this, mLocation);
lastTime = preferences.getLong(PreferencesActivity.LAST_TIME, 0); if (media != null && media.getTime() > 0)
if (lastTime > 0 && mLocation.equals(lastLocation)) mLibVLC.setTime(media.getTime());
mLibVLC.setTime(lastTime);
try { try {
title = URLDecoder.decode(mLocation, "UTF-8"); title = URLDecoder.decode(mLocation, "UTF-8");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment