Skip to content

Commit

Permalink
Converts millis to mm:ss using DateTime from org.joda.time
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 committed Mar 23, 2018
1 parent 58a97fd commit 04a9316
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,4 @@ public static DatePickerDetails getDatePickerDetails(String appearance) {

return new DatePickerDetails(datePickerType, datePickerMode);
}

/**
* Converts milliseconds time to Hours:Minutes:Seconds format
*/
public static String millisToTimer(long milliseconds) {
StringBuilder sb = new StringBuilder();

// Convert total duration into time
int hours = (int) (milliseconds / (1000 * 60 * 60));
int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);

// Add hours if there
if (hours > 0) {
sb.append(hours).append(':');
}

// Add minutes
sb.append(minutes < 10 ? '0' : "").append(minutes).append(':');

// Add seconds
sb.append(seconds < 10 ? '0' : "").append(seconds);

return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
import android.widget.TextView;

import org.javarosa.form.api.FormEntryPrompt;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.odk.collect.android.R;
import org.odk.collect.android.activities.FormEntryActivity;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.utilities.DateTimeUtils;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -80,6 +81,15 @@ public void run() {
}
};

/**
* Converts {@param millis} to mm:ss format
*
* @return formatted time as string
*/
private static String getTime(long millis) {
return new DateTime(millis, DateTimeZone.UTC).toString("mm:ss");
}

void init(Context context, MediaPlayer mediaPlayer, FormEntryPrompt formEntryPrompt) {
this.context = context;
this.mediaPlayer = mediaPlayer;
Expand Down Expand Up @@ -118,11 +128,8 @@ void playClicked() {
}

private void updateTimer() {
String totalDuration = DateTimeUtils.millisToTimer(mediaPlayer.getDuration());
String currentDuration = DateTimeUtils.millisToTimer(mediaPlayer.getCurrentPosition());

totalDurationLabel.setText(totalDuration);
currentDurationLabel.setText(currentDuration);
totalDurationLabel.setText(getTime(mediaPlayer.getDuration()));
currentDurationLabel.setText(getTime(mediaPlayer.getCurrentPosition()));

seekBar.setMax(mediaPlayer.getDuration());
seekBar.setProgress(mediaPlayer.getCurrentPosition());
Expand Down

0 comments on commit 04a9316

Please sign in to comment.