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 {
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;
}
......
......@@ -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
......
......@@ -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()));
......
......@@ -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();
......
......@@ -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");
......
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