Commit 1a769700 authored by Albert Vaca Cintora's avatar Albert Vaca Cintora

Better support for seeking long media:

-Displaying duration in hours if longer than 60 minutes.
-Using miliseconds instead of nanoseconds for time.
-Using longs instead of ints to store time.

CCMAIL: zelitomas@gmail.com
parent 8e23ff5c
......@@ -91,7 +91,8 @@
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:id="@+id/progress_slider">
<TextView
android:layout_width="wrap_content"
......
......@@ -89,6 +89,8 @@ public class NetworkPackage {
public void set(String key, String value) { if (value == null) return; try { mBody.put(key,value); } catch(Exception e) { } }
public int getInt(String key) { return mBody.optInt(key,-1); }
public int getInt(String key, int defaultValue) { return mBody.optInt(key,defaultValue); }
public long getLong(String key) { return mBody.optLong(key,-1); }
public long getLong(String key,long defaultValue) { return mBody.optLong(key,defaultValue); }
public void set(String key, int value) { try { mBody.put(key,value); } catch(Exception e) { } }
public boolean getBoolean(String key) { return mBody.optBoolean(key,false); }
public boolean getBoolean(String key, boolean defaultValue) { return mBody.optBoolean(key,defaultValue); }
......
......@@ -55,6 +55,22 @@ public class MprisActivity extends ActionBarActivity {
private Runnable positionSeekUpdateRunnable;
private boolean positionSeekUpdateScheduled = false;
private static String milisToProgress(long milis) {
int length = (int)(milis / 1000); //From milis to seconds
StringBuilder text = new StringBuilder();
int minutes = length / 60;
if (minutes > 60) {
int hours = minutes / 60;
minutes = minutes % 60;
text.append(hours).append(':');
if (minutes < 10) text.append('0');
}
text.append(minutes).append(':');
int seconds = (length % 60);
if(seconds < 10) text.append('0'); // needed to show length properly (eg 4:05 instead of 4:5)
text.append(seconds);
return text.toString();
}
protected void connectToPlugin() {
final String deviceId = getIntent().getStringExtra("deviceId");
......@@ -79,25 +95,21 @@ public class MprisActivity extends ActionBarActivity {
String s = mpris.getCurrentSong();
((TextView) findViewById(R.id.now_playing_textview)).setText(s);
if (mpris.getLength() > -1 && mpris.getPosition() > -1 && !"Spotify".equals(mpris.getPlayer())) {
((TextView) findViewById(R.id.time_textview)).setText(milisToProgress(mpris.getLength()));
String text = mpris.getLength() / 60000000 + ":";
int seconds = (mpris.getLength() % 60000000) / 1000000;
// needed to show length properly (eg 4:05 instead of 4:5)
if(seconds < 10) text = text + "0";
text = text + seconds;
SeekBar positionSeek = (SeekBar)findViewById(R.id.positionSeek);
positionSeek.setMax((int)(mpris.getLength()));
positionSeek.setProgress((int)(mpris.getPosition()));
SeekBar positionSeek = (SeekBar)findViewById(R.id.positionSeek);
positionSeek.setMax(mpris.getLength());
positionSeek.setProgress(mpris.getPosition());
findViewById(R.id.progress_slider).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.progress_slider).setVisibility(View.GONE);
}
int volume = mpris.getVolume();
((TextView) findViewById(R.id.time_textview)).setText(text);
((SeekBar) findViewById(R.id.volume_seek)).setProgress(volume);
boolean isPlaying = mpris.isPlaying();
if (isPlaying) {
((ImageButton) findViewById(R.id.play_button)).setImageResource(android.R.drawable.ic_media_pause);
......@@ -145,6 +157,7 @@ public class MprisActivity extends ActionBarActivity {
findViewById(R.id.rew_button).setVisibility(View.GONE);
findViewById(R.id.ff_button).setVisibility(View.GONE);
findViewById(R.id.positionSeek).setVisibility(View.INVISIBLE);
findViewById(R.id.progress_slider).setVisibility(View.GONE);
} else {
findViewById(R.id.volume_layout).setVisibility(View.VISIBLE);
findViewById(R.id.rew_button).setVisibility(View.VISIBLE);
......@@ -397,7 +410,7 @@ public class MprisActivity extends ActionBarActivity {
Device device = service.getDevice(deviceId);
MprisPlugin mpris = (MprisPlugin) device.getPlugin("plugin_mpris");
if (mpris == null) return;
positionSeek.setProgress(mpris.getPosition());
positionSeek.setProgress((int)(mpris.getPosition()));
if(!mpris.isPlaying()) return;
positionSeekUpdateHandler.postDelayed(thisRunnable, 1000);
positionSeekUpdateScheduled = true;
......@@ -412,11 +425,7 @@ public class MprisActivity extends ActionBarActivity {
((SeekBar)findViewById(R.id.positionSeek)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean byUser) {
String text = progress / 60000000 + ":";
int seconds = (progress % 60000000) / 1000000;
if(seconds < 10) text = text + "0";
text = text + seconds;
((TextView)findViewById(R.id.progress_textview)).setText(text);
((TextView)findViewById(R.id.progress_textview)).setText(milisToProgress(progress));
}
@Override
......
......@@ -42,8 +42,8 @@ public class MprisPlugin extends Plugin {
private String currentSong = "";
private int volume = 50;
private int length = 0;
private int lastPosition;
private long length = -1;
private long lastPosition;
private long lastPositionTime;
private Handler playerStatusUpdated = null;
......@@ -134,9 +134,9 @@ public class MprisPlugin extends Plugin {
if (np.getString("player").equals(player)) {
currentSong = np.getString("nowPlaying", currentSong);
volume = np.getInt("volume", volume);
length = np.getInt("length", length);
length = np.getLong("length", length);
if(np.has("pos")){
lastPosition = np.getInt("pos", lastPosition);
lastPosition = np.getLong("pos", lastPosition);
lastPositionTime = System.currentTimeMillis();
}
playing = np.getBoolean("isPlaying", playing);
......@@ -227,18 +227,18 @@ public class MprisPlugin extends Plugin {
return volume;
}
public int getLength(){ return length; }
public long getLength(){ return length; }
public boolean isPlaying() {
return playing;
}
public int getPosition(){
if(playing)
return lastPosition + (int)(System.currentTimeMillis() - lastPositionTime)*1000;
else
public long getPosition(){
if(playing) {
return lastPosition + (System.currentTimeMillis() - lastPositionTime);
} else {
return lastPosition;
}
}
private void requestPlayerList() {
......@@ -247,7 +247,6 @@ public class MprisPlugin extends Plugin {
device.sendPackage(np);
}
private void requestPlayerStatus() {
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MPRIS);
np.set("player",player);
......
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