2020-12-09 19:31:35 +01:00
|
|
|
package com.cappielloantonio.play.helper;
|
|
|
|
|
|
|
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.os.Message;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
2021-04-27 10:44:55 +02:00
|
|
|
import com.cappielloantonio.play.service.MusicPlayerRemote;
|
|
|
|
|
|
2020-12-09 19:31:35 +01:00
|
|
|
public class MusicProgressViewUpdateHelper extends Handler {
|
|
|
|
|
private static final int CMD_REFRESH_PROGRESS_VIEWS = 1;
|
|
|
|
|
|
|
|
|
|
private static final int MIN_INTERVAL = 20;
|
|
|
|
|
private static final int UPDATE_INTERVAL_PLAYING = 1000;
|
|
|
|
|
private static final int UPDATE_INTERVAL_PAUSED = 500;
|
|
|
|
|
|
2021-08-14 17:52:46 +02:00
|
|
|
private final Callback callback;
|
|
|
|
|
private final int intervalPlaying;
|
|
|
|
|
private final int intervalPaused;
|
2020-12-09 19:31:35 +01:00
|
|
|
|
2021-04-27 11:01:02 +02:00
|
|
|
public MusicProgressViewUpdateHelper(Callback callback) {
|
|
|
|
|
this.callback = callback;
|
|
|
|
|
this.intervalPlaying = UPDATE_INTERVAL_PLAYING;
|
|
|
|
|
this.intervalPaused = UPDATE_INTERVAL_PAUSED;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 19:31:35 +01:00
|
|
|
public void start() {
|
|
|
|
|
queueNextRefresh(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void stop() {
|
|
|
|
|
removeMessages(CMD_REFRESH_PROGRESS_VIEWS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void handleMessage(@NonNull Message msg) {
|
|
|
|
|
super.handleMessage(msg);
|
|
|
|
|
if (msg.what == CMD_REFRESH_PROGRESS_VIEWS) {
|
|
|
|
|
queueNextRefresh(refreshProgressViews());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int refreshProgressViews() {
|
|
|
|
|
final int progressMillis = MusicPlayerRemote.getSongProgressMillis();
|
|
|
|
|
final int totalMillis = MusicPlayerRemote.getSongDurationMillis();
|
|
|
|
|
|
|
|
|
|
callback.onUpdateProgressViews(progressMillis, totalMillis);
|
|
|
|
|
|
|
|
|
|
if (!MusicPlayerRemote.isPlaying()) {
|
|
|
|
|
return intervalPaused;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final int remainingMillis = intervalPlaying - progressMillis % intervalPlaying;
|
|
|
|
|
|
|
|
|
|
return Math.max(MIN_INTERVAL, remainingMillis);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void queueNextRefresh(final long delay) {
|
|
|
|
|
final Message message = obtainMessage(CMD_REFRESH_PROGRESS_VIEWS);
|
|
|
|
|
removeMessages(CMD_REFRESH_PROGRESS_VIEWS);
|
|
|
|
|
sendMessageDelayed(message, delay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface Callback {
|
|
|
|
|
void onUpdateProgressViews(int progress, int total);
|
|
|
|
|
}
|
|
|
|
|
}
|