2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-09-02 15:15:09 +00:00

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
This commit is contained in:
Albert Vaca
2014-12-13 18:43:05 -08:00
parent 8e23ff5cf6
commit 1a7697009d
4 changed files with 42 additions and 31 deletions

View File

@@ -91,7 +91,8 @@
<LinearLayout <LinearLayout
android:orientation="horizontal" android:orientation="horizontal"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:id="@+id/progress_slider">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"

View File

@@ -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 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) { return mBody.optInt(key,-1); }
public int getInt(String key, int defaultValue) { return mBody.optInt(key,defaultValue); } 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 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) { return mBody.optBoolean(key,false); }
public boolean getBoolean(String key, boolean defaultValue) { return mBody.optBoolean(key,defaultValue); } public boolean getBoolean(String key, boolean defaultValue) { return mBody.optBoolean(key,defaultValue); }

View File

@@ -55,6 +55,22 @@ public class MprisActivity extends ActionBarActivity {
private Runnable positionSeekUpdateRunnable; private Runnable positionSeekUpdateRunnable;
private boolean positionSeekUpdateScheduled = false; 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() { protected void connectToPlugin() {
final String deviceId = getIntent().getStringExtra("deviceId"); final String deviceId = getIntent().getStringExtra("deviceId");
@@ -79,25 +95,21 @@ public class MprisActivity extends ActionBarActivity {
String s = mpris.getCurrentSong(); String s = mpris.getCurrentSong();
((TextView) findViewById(R.id.now_playing_textview)).setText(s); ((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 + ":"; SeekBar positionSeek = (SeekBar)findViewById(R.id.positionSeek);
int seconds = (mpris.getLength() % 60000000) / 1000000; positionSeek.setMax((int)(mpris.getLength()));
// needed to show length properly (eg 4:05 instead of 4:5) positionSeek.setProgress((int)(mpris.getPosition()));
if(seconds < 10) text = text + "0";
text = text + seconds;
SeekBar positionSeek = (SeekBar)findViewById(R.id.positionSeek); findViewById(R.id.progress_slider).setVisibility(View.VISIBLE);
positionSeek.setMax(mpris.getLength()); } else {
positionSeek.setProgress(mpris.getPosition()); findViewById(R.id.progress_slider).setVisibility(View.GONE);
}
int volume = mpris.getVolume(); int volume = mpris.getVolume();
((TextView) findViewById(R.id.time_textview)).setText(text);
((SeekBar) findViewById(R.id.volume_seek)).setProgress(volume); ((SeekBar) findViewById(R.id.volume_seek)).setProgress(volume);
boolean isPlaying = mpris.isPlaying(); boolean isPlaying = mpris.isPlaying();
if (isPlaying) { if (isPlaying) {
((ImageButton) findViewById(R.id.play_button)).setImageResource(android.R.drawable.ic_media_pause); ((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.rew_button).setVisibility(View.GONE);
findViewById(R.id.ff_button).setVisibility(View.GONE); findViewById(R.id.ff_button).setVisibility(View.GONE);
findViewById(R.id.positionSeek).setVisibility(View.INVISIBLE); findViewById(R.id.positionSeek).setVisibility(View.INVISIBLE);
findViewById(R.id.progress_slider).setVisibility(View.GONE);
} else { } else {
findViewById(R.id.volume_layout).setVisibility(View.VISIBLE); findViewById(R.id.volume_layout).setVisibility(View.VISIBLE);
findViewById(R.id.rew_button).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); Device device = service.getDevice(deviceId);
MprisPlugin mpris = (MprisPlugin) device.getPlugin("plugin_mpris"); MprisPlugin mpris = (MprisPlugin) device.getPlugin("plugin_mpris");
if (mpris == null) return; if (mpris == null) return;
positionSeek.setProgress(mpris.getPosition()); positionSeek.setProgress((int)(mpris.getPosition()));
if(!mpris.isPlaying()) return; if(!mpris.isPlaying()) return;
positionSeekUpdateHandler.postDelayed(thisRunnable, 1000); positionSeekUpdateHandler.postDelayed(thisRunnable, 1000);
positionSeekUpdateScheduled = true; positionSeekUpdateScheduled = true;
@@ -412,11 +425,7 @@ public class MprisActivity extends ActionBarActivity {
((SeekBar)findViewById(R.id.positionSeek)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { ((SeekBar)findViewById(R.id.positionSeek)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean byUser) { public void onProgressChanged(SeekBar seekBar, int progress, boolean byUser) {
String text = progress / 60000000 + ":"; ((TextView)findViewById(R.id.progress_textview)).setText(milisToProgress(progress));
int seconds = (progress % 60000000) / 1000000;
if(seconds < 10) text = text + "0";
text = text + seconds;
((TextView)findViewById(R.id.progress_textview)).setText(text);
} }
@Override @Override

View File

@@ -42,8 +42,8 @@ public class MprisPlugin extends Plugin {
private String currentSong = ""; private String currentSong = "";
private int volume = 50; private int volume = 50;
private int length = 0; private long length = -1;
private int lastPosition; private long lastPosition;
private long lastPositionTime; private long lastPositionTime;
private Handler playerStatusUpdated = null; private Handler playerStatusUpdated = null;
@@ -134,9 +134,9 @@ public class MprisPlugin extends Plugin {
if (np.getString("player").equals(player)) { if (np.getString("player").equals(player)) {
currentSong = np.getString("nowPlaying", currentSong); currentSong = np.getString("nowPlaying", currentSong);
volume = np.getInt("volume", volume); volume = np.getInt("volume", volume);
length = np.getInt("length", length); length = np.getLong("length", length);
if(np.has("pos")){ if(np.has("pos")){
lastPosition = np.getInt("pos", lastPosition); lastPosition = np.getLong("pos", lastPosition);
lastPositionTime = System.currentTimeMillis(); lastPositionTime = System.currentTimeMillis();
} }
playing = np.getBoolean("isPlaying", playing); playing = np.getBoolean("isPlaying", playing);
@@ -227,18 +227,18 @@ public class MprisPlugin extends Plugin {
return volume; return volume;
} }
public int getLength(){ return length; } public long getLength(){ return length; }
public boolean isPlaying() { public boolean isPlaying() {
return playing; return playing;
} }
public int getPosition(){ public long getPosition(){
if(playing) {
if(playing) return lastPosition + (System.currentTimeMillis() - lastPositionTime);
return lastPosition + (int)(System.currentTimeMillis() - lastPositionTime)*1000; } else {
else
return lastPosition; return lastPosition;
}
} }
private void requestPlayerList() { private void requestPlayerList() {
@@ -247,7 +247,6 @@ public class MprisPlugin extends Plugin {
device.sendPackage(np); device.sendPackage(np);
} }
private void requestPlayerStatus() { private void requestPlayerStatus() {
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MPRIS); NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MPRIS);
np.set("player",player); np.set("player",player);