Skip to content

Commit

Permalink
fix recycler bitmap in floatingWidget , java.lang.nullpointerexceptio…
Browse files Browse the repository at this point in the history
…n fix, move playlist methods in PlaylistHelper class......
  • Loading branch information
Rajneesh Singh committed Jun 20, 2017
1 parent 7bd8170 commit 26a0649
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 285 deletions.
34 changes: 28 additions & 6 deletions app/src/main/java/com/cleveroad/audiowidget/PlayPauseButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ public void onAnimationCancel(Animator animation) {
});
}

public static Bitmap drawableToBitmap(@NonNull Drawable drawable) {
Bitmap bitmap;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if (bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}

if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.RGB_565); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.RGB_565);
}

Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int size = MeasureSpec.makeMeasureSpec((int) (radius * 4), MeasureSpec.EXACTLY);
Expand Down Expand Up @@ -271,9 +292,11 @@ public void onDraw(@NonNull Canvas canvas) {
canvas.drawCircle(cx, cy, radius, buttonPaint);
albumCover.setBounds((int) (cx - radius), (int) (cy - radius), (int) (cx + radius), (int) (cy + radius));
albumCover.draw(canvas);
Boolean isNeedToFillAlbumCover = isNeedToFillAlbumCoverMap.get(albumCover.hashCode());
if (isNeedToFillAlbumCover != null && isNeedToFillAlbumCover) {
canvas.drawCircle(cx, cy, radius, albumPlaceholderPaint);
if (albumCover.hashCode() > 0) {
Boolean isNeedToFillAlbumCover = isNeedToFillAlbumCoverMap.get(albumCover.hashCode());
if (isNeedToFillAlbumCover != null && isNeedToFillAlbumCover) {
canvas.drawCircle(cx, cy, radius, albumPlaceholderPaint);
}
}
}

Expand Down Expand Up @@ -366,8 +389,8 @@ public void albumCover(Drawable newAlbumCover) {
if (albumCover.hashCode() == 0) {
return;
}
if (albumCover instanceof BitmapDrawable && !isNeedToFillAlbumCoverMap.containsKey(albumCover.hashCode())) {
Bitmap bitmap = ((BitmapDrawable) albumCover).getBitmap();
if (!isNeedToFillAlbumCoverMap.containsKey(albumCover.hashCode())) {
Bitmap bitmap = drawableToBitmap(albumCover);
if (bitmap != null && !bitmap.isRecycled()) {
if (lastPaletteAsyncTask != null && !lastPaletteAsyncTask.isCancelled()) {
lastPaletteAsyncTask.cancel(true);
Expand All @@ -382,7 +405,6 @@ public void albumCover(Drawable newAlbumCover) {
});
}
}
postInvalidate();
}

private static final class BoundsCheckerImpl extends AudioWidget.BoundsCheckerWithOffset {
Expand Down
80 changes: 42 additions & 38 deletions app/src/main/java/com/rks/musicx/base/BasePlayingFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
Expand Down Expand Up @@ -40,6 +41,7 @@
import com.rks.musicx.database.CommonDatabase;
import com.rks.musicx.misc.utils.Extras;
import com.rks.musicx.misc.utils.Helper;
import com.rks.musicx.misc.utils.PlaylistHelper;
import com.rks.musicx.services.MusicXService;
import com.rks.musicx.ui.activities.EqualizerActivity;
import com.rks.musicx.ui.activities.PlayingActivity;
Expand Down Expand Up @@ -79,6 +81,7 @@

public abstract class BasePlayingFragment extends Fragment implements ImageChooserListener {

private static Handler handler;
public boolean isalbumArtChanged;
private MusicXService musicXService;
private Intent mServiceIntent;
Expand All @@ -90,7 +93,8 @@ public abstract class BasePlayingFragment extends Fragment implements ImageChoos
private Helper helper;
private int size;
private CommonDatabase commonDatabase;
private static Handler handler;
private Drawable shuffleOff, shuffleOn, repeatAll, repeatOne, noRepeat;


/**
* Service Connection
Expand Down Expand Up @@ -140,6 +144,10 @@ public void onReceive(Context context, Intent intent) {
}
};

public static Handler getHandler() {
return handler;
}

protected abstract void reload();

protected abstract void playbackConfig();
Expand Down Expand Up @@ -168,8 +176,6 @@ public void onReceive(Context context, Intent intent) {

protected abstract void updateProgress();



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(setLayout(), container, false);
Expand All @@ -179,6 +185,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
size = getResources().getDimensionPixelSize(R.dimen.cover_size);
commonDatabase = new CommonDatabase(getContext(), Queue_TableName, true);
handler = new Handler();
shuffleOff = ContextCompat.getDrawable(getContext(), R.drawable.shuf_off);
shuffleOn = ContextCompat.getDrawable(getContext(), R.drawable.shuf_on);
repeatOne = ContextCompat.getDrawable(getContext(), R.drawable.rep_one);
repeatAll = ContextCompat.getDrawable(getContext(), R.drawable.rep_all);
noRepeat = ContextCompat.getDrawable(getContext(), R.drawable.rep_no);
return rootView;
}

Expand Down Expand Up @@ -271,7 +282,6 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
Extras.getInstance().getThemevalue(getActivity());
}


@Override
public void onImageChosen(ChosenImage chosenImage) {
chosenImages = chosenImage;
Expand All @@ -288,7 +298,6 @@ public void run() {
});
}


@Override
public void onError(String s) {
Log.d("BaseFragment", s);
Expand Down Expand Up @@ -348,9 +357,9 @@ public void pickupArtwork() {
public void updateShuffleButton() {
boolean shuffle = musicXService.isShuffleEnabled();
if (shuffle) {
shuffleButton().setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.shuf_on));
shuffleButton().setImageDrawable(shuffleOn);
} else {
shuffleButton().setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.shuf_off));
shuffleButton().setImageDrawable(shuffleOff);
}
}

Expand All @@ -360,11 +369,11 @@ public void updateShuffleButton() {
public void updateRepeatButton() {
int mode = musicXService.getRepeatMode();
if (mode == getMusicXService().getNoRepeat()) {
repeatButton().setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.rep_no));
repeatButton().setImageDrawable(noRepeat);
} else if (mode == getMusicXService().getRepeatCurrent()) {
repeatButton().setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.rep_all));
repeatButton().setImageDrawable(repeatAll);
} else if (mode == getMusicXService().getRepeatAll()) {
repeatButton().setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.rep_one));
repeatButton().setImageDrawable(repeatOne);
}
}

Expand Down Expand Up @@ -405,7 +414,7 @@ public boolean onMenuItemClick(MenuItem item) {
isalbumArtChanged = false;
break;
case R.id.action_playlist:
helper.PlaylistChooser(BasePlayingFragment.this, getContext(), musicXService.getsongId());
PlaylistHelper.PlaylistChooser(BasePlayingFragment.this, getContext(), musicXService.getsongId());
break;
case R.id.action_lyrics:
helper.searchLyrics(getContext(), musicXService.getsongTitle(), musicXService.getsongArtistName(), musicXService.getsongData(), lyricsView());
Expand Down Expand Up @@ -441,7 +450,6 @@ public boolean onMenuItemClick(MenuItem item) {
popupMenu.show();
}


/**
* for next update
*/
Expand All @@ -465,21 +473,22 @@ public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
if(editText.getText() != null){
CommonDatabase commonDatabase = new CommonDatabase(getContext(), editText.getText().toString(), true);
commonDatabase.add(musicXService.getPlayList());
commonDatabase.close();
queueName.add(editText.getText().toString());
Extras.getInstance().saveQueueName(queueName);
Toast.makeText(getContext(), "Saved Queue", Toast.LENGTH_SHORT).show();
editText.getText().clear();
}
if (editText.getText() != null) {
CommonDatabase commonDatabase = new CommonDatabase(getContext(), editText.getText().toString(), true);
commonDatabase.add(musicXService.getPlayList());
commonDatabase.close();
queueName.add(editText.getText().toString());
Extras.getInstance().saveQueueName(queueName);
Toast.makeText(getContext(), "Saved Queue", Toast.LENGTH_SHORT).show();
editText.getText().clear();
}
}
})
.customView(view1, false)
.build()
.show();
}

/**
* Queue Menu
*
Expand All @@ -505,7 +514,7 @@ public void qeueMenu(QueueAdapter queueAdapter, View view, int position) {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_playlist:
helper.PlaylistChooser(BasePlayingFragment.this, getContext(), queue.getId());
PlaylistHelper.PlaylistChooser(BasePlayingFragment.this, getContext(), queue.getId());
break;
case R.id.action_edit_tags:
Extras.getInstance().saveMetaData(queue);
Expand All @@ -528,6 +537,17 @@ public boolean onMenuItemClick(MenuItem item) {
popupMenu.show();
}

public void seekbarProgress() {
ProgressRunnable progressRunnable = new ProgressRunnable(BasePlayingFragment.this);
handler.post(progressRunnable);
}

public void removeCallback() {
ProgressRunnable progressRunnable = new ProgressRunnable(BasePlayingFragment.this);
handler.removeCallbacks(progressRunnable);
handler.removeCallbacksAndMessages(null);
}

private static class ProgressRunnable implements Runnable {

private final WeakReference<BasePlayingFragment> baseLoaderWeakReference;
Expand All @@ -546,21 +566,5 @@ public void run () {
}
}

public void seekbarProgress(){
ProgressRunnable progressRunnable = new ProgressRunnable(BasePlayingFragment.this);
handler.post(progressRunnable);
}

public void removeCallback(){
ProgressRunnable progressRunnable = new ProgressRunnable(BasePlayingFragment.this);
handler.removeCallbacks(progressRunnable);
handler.removeCallbacksAndMessages(null);
}


public static Handler getHandler() {
return handler;
}


}
Loading

0 comments on commit 26a0649

Please sign in to comment.