Fixed an annoying bug in the MusicControllerActivity where the recents card of the app is placed at the wrong position when switching between the songs. Also some code clean ups.

This commit is contained in:
Karim Abou Zeid 2015-06-11 16:28:04 +02:00
commit b07e602651
12 changed files with 182 additions and 57 deletions

View file

@ -78,7 +78,7 @@ public class MusicControllerActivity extends AbsFabActivity {
private Toolbar toolbar;
private int lastFooterColor = -1;
private int lastTextColor = -2;
private boolean killThreads = false;
private Thread progressViewsUpdateThread;
private final boolean opaqueStatusBar = PreferenceUtils.getInstance(this).opaqueStatusbarNowPlaying();
private final boolean opaqueToolBar = opaqueStatusBar && PreferenceUtils.getInstance(this).opaqueToolbarNowPlaying();
@ -169,7 +169,7 @@ public class MusicControllerActivity extends AbsFabActivity {
songArtist.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallerTitleBox ? getResources().getDimensionPixelSize(R.dimen.title_box_caption_text_size_small) : getResources().getDimensionPixelSize(R.dimen.title_box_caption_text_size_large));
}
private void setUpPlaybackControllerCard(){
private void setUpPlaybackControllerCard() {
playbackControllerCard.setVisibility(showPlaybackControllerCard ? View.VISIBLE : View.GONE);
mediaControllerContainer.setBackgroundColor(showPlaybackControllerCard ? Color.TRANSPARENT : Util.resolveColor(this, R.attr.music_controller_container_color));
}
@ -309,7 +309,7 @@ public class MusicControllerActivity extends AbsFabActivity {
protected void onResume() {
super.onResume();
updateControllerState();
startMusicControllerStateUpdateThread();
startProgressViewsUpdateThread();
updateCurrentSong();
}
@ -429,36 +429,37 @@ public class MusicControllerActivity extends AbsFabActivity {
}
}
private void startMusicControllerStateUpdateThread() {
killThreads = false;
new Thread(new Runnable() {
private void startProgressViewsUpdateThread() {
if (progressViewsUpdateThread != null) progressViewsUpdateThread.interrupt();
progressViewsUpdateThread = new Thread(new Runnable() {
int totalMillis = 0;
int progressMillis = 0;
@Override
public void run() {
int currentPosition = 0;
int total = 0;
while (!killThreads) {
try {
total = MusicPlayerRemote.getSongDurationMillis();
currentPosition = MusicPlayerRemote.getSongProgressMillis();
Thread.sleep(1);
} catch (InterruptedException e) {
return;
} catch (Exception e2) {
e2.printStackTrace();
try {
while (!Thread.currentThread().isInterrupted()) {
totalMillis = MusicPlayerRemote.getSongDurationMillis();
progressMillis = MusicPlayerRemote.getSongProgressMillis();
runOnUiThread(updateProgressViews);
Thread.sleep(100);
}
final int finalTotal = total;
final int finalCurrentPosition = currentPosition;
runOnUiThread(new Runnable() {
@Override
public void run() {
progressSlider.setMax(finalTotal);
progressSlider.setProgress(finalCurrentPosition);
currentSongProgress.setText(MusicUtil.getReadableDurationString(finalCurrentPosition));
}
});
} catch (InterruptedException ignored) {
}
}
}).start();
private Runnable updateProgressViews = new Runnable() {
@Override
public void run() {
progressSlider.setMax(totalMillis);
progressSlider.setProgress(progressMillis);
currentSongProgress.setText(MusicUtil.getReadableDurationString(progressMillis));
}
};
});
progressViewsUpdateThread.start();
}
protected void updateControllerState() {
@ -486,7 +487,7 @@ public class MusicControllerActivity extends AbsFabActivity {
@Override
protected void onPause() {
super.onPause();
killThreads = true;
if(progressViewsUpdateThread != null) progressViewsUpdateThread.interrupt();
}
@Override

View file

@ -1,7 +1,6 @@
package com.kabouzeid.gramophone.ui.activities.base;
import android.app.ActivityManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
@ -22,6 +21,8 @@ public abstract class ThemeBaseActivity extends AppCompatActivity implements Kab
private int colorPrimaryDarker;
private int colorAccent;
private ActivityManager.TaskDescription taskDescription;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(PreferenceUtils.getInstance(this).getGeneralTheme());
@ -49,11 +50,13 @@ public abstract class ThemeBaseActivity extends AppCompatActivity implements Kab
protected void notifyTaskColorChange(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Sets color of entry in the system recents page
ActivityManager.TaskDescription td = new ActivityManager.TaskDescription(
getString(R.string.app_name),
BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher),
color);
setTaskDescription(td);
if (taskDescription == null || taskDescription.getPrimaryColor() != color) {
taskDescription = new ActivityManager.TaskDescription(
null,
null,
color);
setTaskDescription(taskDescription);
}
}
}