Added base classes for glide to work with Phonograph.

This commit is contained in:
Karim Abou Zeid 2015-12-31 01:58:51 +01:00
commit f35fc7cb27
11 changed files with 245 additions and 45 deletions

View file

@ -132,4 +132,5 @@ dependencies {
compile 'com.anjlab.android.iab.v3:library:1.0.+'
compile 'de.psdev.licensesdialog:licensesdialog:1.8.0'
compile 'com.github.kabouzeid:AppIntro:3.3.0k'
compile 'com.github.bumptech.glide:glide:3.6.1'
}

View file

@ -22,9 +22,9 @@
-keepattributes Signature
-keepattributes Exceptions
# LeakCanary
-keep class org.eclipse.mat.** { *; }
-keep class com.squareup.leakcanary.** { *; }
# Glide
-keepnames class com.kabouzeid.gramophone.glide.PhonographGlideModule
-keep public class * implements com.bumptech.glide.module.GlideModule
# ButterKnife
-keep class butterknife.** { *; }

View file

@ -1,8 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.kabouzeid.gramophone"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.kabouzeid.gramophone">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
@ -110,6 +109,10 @@
android:name="com.crashlytics.ApiKey"
android:value="b23725bd3d266aa65c5a3dd1816b2f801524a189" />
<meta-data
android:name="com.kabouzeid.gramophone.glide.PhonographGlideModule"
android:value="GlideModule" />
<activity
android:name=".ui.activities.tageditor.SongTagEditorActivity"
android:windowSoftInputMode="adjustResize" />

View file

@ -1,9 +1,7 @@
package com.kabouzeid.gramophone.adapter;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
@ -11,17 +9,15 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.glide.BitmapPaletteTranscoder;
import com.kabouzeid.gramophone.glide.BitmapPaletteWrapper;
import com.kabouzeid.gramophone.glide.PhonographPaletteTarget;
import com.kabouzeid.gramophone.misc.CustomFragmentStatePagerAdapter;
import com.kabouzeid.gramophone.model.Song;
import com.kabouzeid.gramophone.util.ColorUtil;
import com.kabouzeid.gramophone.util.MusicUtil;
import com.kabouzeid.gramophone.util.PreferenceUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.process.BitmapProcessor;
import java.util.ArrayList;
@ -131,39 +127,51 @@ public class AlbumCoverPagerAdapter extends CustomFragmentStatePagerAdapter {
}
private void loadAlbumCover() {
ImageLoader.getInstance().displayImage(
MusicUtil.getSongImageLoaderString(song),
albumCover,
new DisplayImageOptions.Builder()
.cacheInMemory(true)
.showImageOnFail(R.drawable.default_album_art)
.postProcessor(new BitmapProcessor() {
@Override
public Bitmap process(Bitmap bitmap) {
// don't use set color here, as this is not running on the ui-thread
color = ColorUtil.generateColor(getActivity(), bitmap);
return bitmap;
}
})
.build(),
new SimpleImageLoadingListener() {
Glide.with(this)
.loadFromMediaStore(MusicUtil.getAlbumArtUri(song.albumId))
.asBitmap()
.transcode(new BitmapPaletteTranscoder(getActivity()), BitmapPaletteWrapper.class)
.error(R.drawable.default_album_art)
.into(new PhonographPaletteTarget(albumCover) {
@Override
public void onLoadingFailed(String imageUri, View view, @Nullable FailReason failReason) {
if (getActivity() != null) {
setColor(ColorUtil.resolveColor(getActivity(), R.attr.default_bar_color));
}
}
@Override
public void onLoadingComplete(String imageUri, View view, @Nullable Bitmap loadedImage) {
if (loadedImage == null) {
onLoadingFailed(imageUri, view, null);
return;
}
public void onColorReady(int color) {
setColor(color);
}
}
);
});
// ImageLoader.getInstance().displayImage(
// MusicUtil.getSongImageLoaderString(song),
// albumCover,
// new DisplayImageOptions.Builder()
// .cacheInMemory(true)
// .showImageOnFail(R.drawable.default_album_art)
// .postProcessor(new BitmapProcessor() {
// @Override
// public Bitmap process(Bitmap bitmap) {
// // don't use set color here, as this is not running on the ui-thread
// color = ColorUtil.generateColor(getActivity(), bitmap);
// return bitmap;
// }
// })
// .build(),
// new SimpleImageLoadingListener() {
// @Override
// public void onLoadingFailed(String imageUri, View view, @Nullable FailReason failReason) {
// if (getActivity() != null) {
// setColor(ColorUtil.resolveColor(getActivity(), R.attr.default_bar_color));
// }
// }
//
// @Override
// public void onLoadingComplete(String imageUri, View view, @Nullable Bitmap loadedImage) {
// if (loadedImage == null) {
// onLoadingFailed(imageUri, view, null);
// return;
// }
// setColor(color);
// }
// }
// );
}
@Override

View file

@ -0,0 +1,36 @@
package com.kabouzeid.gramophone.glide;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.util.Util;
/**
* @author Karim Abou Zeid (kabouzeid)
*/
public class BitmapPaletteResource implements Resource<BitmapPaletteWrapper> {
private final BitmapPaletteWrapper bitmapPaletteWrapper;
private final BitmapPool bitmapPool;
public BitmapPaletteResource(BitmapPaletteWrapper bitmapPaletteWrapper, BitmapPool bitmapPool) {
this.bitmapPaletteWrapper = bitmapPaletteWrapper;
this.bitmapPool = bitmapPool;
}
@Override
public BitmapPaletteWrapper get() {
return bitmapPaletteWrapper;
}
@Override
public int getSize() {
return Util.getBitmapByteSize(bitmapPaletteWrapper.getBitmap());
}
@Override
public void recycle() {
if (!bitmapPool.put(bitmapPaletteWrapper.getBitmap())) {
bitmapPaletteWrapper.getBitmap().recycle();
}
}
}

View file

@ -0,0 +1,16 @@
package com.kabouzeid.gramophone.glide;
import android.widget.ImageView;
import com.bumptech.glide.request.target.ImageViewTarget;
public class BitmapPaletteTarget extends ImageViewTarget<BitmapPaletteWrapper> {
public BitmapPaletteTarget(ImageView view) {
super(view);
}
@Override
protected void setResource(BitmapPaletteWrapper bitmapPaletteWrapper) {
view.setImageBitmap(bitmapPaletteWrapper.getBitmap());
}
}

View file

@ -0,0 +1,34 @@
package com.kabouzeid.gramophone.glide;
import android.content.Context;
import android.graphics.Bitmap;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.kabouzeid.gramophone.util.ColorUtil;
public class BitmapPaletteTranscoder implements ResourceTranscoder<Bitmap, BitmapPaletteWrapper> {
private final BitmapPool bitmapPool;
public BitmapPaletteTranscoder(Context context) {
this(Glide.get(context).getBitmapPool());
}
public BitmapPaletteTranscoder(BitmapPool bitmapPool) {
this.bitmapPool = bitmapPool;
}
@Override
public Resource<BitmapPaletteWrapper> transcode(Resource<Bitmap> bitmapResource) {
Bitmap bitmap = bitmapResource.get();
BitmapPaletteWrapper bitmapPaletteWrapper = new BitmapPaletteWrapper(bitmap, ColorUtil.generatePalette(bitmap));
return new BitmapPaletteResource(bitmapPaletteWrapper, bitmapPool);
}
@Override
public String getId() {
return "";
}
}

View file

@ -0,0 +1,22 @@
package com.kabouzeid.gramophone.glide;
import android.graphics.Bitmap;
import android.support.v7.graphics.Palette;
public class BitmapPaletteWrapper {
private final Bitmap mBitmap;
private final Palette mPalette;
public BitmapPaletteWrapper(Bitmap bitmap, Palette palette) {
mBitmap = bitmap;
mPalette = palette;
}
public Bitmap getBitmap() {
return mBitmap;
}
public Palette getPalette() {
return mPalette;
}
}

View file

@ -0,0 +1,22 @@
package com.kabouzeid.gramophone.glide;
import android.content.Context;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.module.GlideModule;
/**
* @author Karim Abou Zeid (kabouzeid)
*/
public class PhonographGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
}
@Override
public void registerComponents(Context context, Glide glide) {
}
}

View file

@ -0,0 +1,36 @@
package com.kabouzeid.gramophone.glide;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.util.ColorUtil;
import hugo.weaving.DebugLog;
public abstract class PhonographPaletteTarget extends BitmapPaletteTarget {
public PhonographPaletteTarget(ImageView view) {
super(view);
}
@DebugLog
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
onColorReady(getDefaultBarColor());
}
@DebugLog
@Override
public void onResourceReady(BitmapPaletteWrapper resource, GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
super.onResourceReady(resource, glideAnimation);
onColorReady(ColorUtil.getColor(resource.getPalette(), getDefaultBarColor()));
}
private int getDefaultBarColor() {
return ColorUtil.resolveColor(getView().getContext(), R.attr.default_bar_color);
}
public abstract void onColorReady(int color);
}

View file

@ -20,6 +20,7 @@ import com.kabouzeid.gramophone.R;
public class ColorUtil {
public static final int PALETTE_BITMAP_SIZE = 100;
@Deprecated
@ColorInt
public static int generateColor(Context context, Bitmap bitmap) {
return getColor(context, generatePalette(bitmap));
@ -31,6 +32,7 @@ public class ColorUtil {
.generate();
}
@Deprecated
@ColorInt
public static int getColor(Context context, @Nullable Palette palette) {
if (palette != null) {
@ -51,6 +53,26 @@ public class ColorUtil {
return ColorUtil.resolveColor(context, R.attr.default_bar_color);
}
@ColorInt
public static int getColor(@Nullable Palette palette, int fallback) {
if (palette != null) {
if (palette.getVibrantSwatch() != null) {
return palette.getVibrantSwatch().getRgb();
} else if (palette.getMutedSwatch() != null) {
return palette.getMutedSwatch().getRgb();
} else if (palette.getDarkVibrantSwatch() != null) {
return palette.getDarkVibrantSwatch().getRgb();
} else if (palette.getDarkMutedSwatch() != null) {
return palette.getDarkMutedSwatch().getRgb();
} else if (palette.getLightVibrantSwatch() != null) {
return palette.getLightVibrantSwatch().getRgb();
} else if (palette.getLightMutedSwatch() != null) {
return palette.getLightMutedSwatch().getRgb();
}
}
return fallback;
}
@ColorInt
public static int resolveColor(@NonNull Context context, @AttrRes int colorAttr) {
TypedArray a = context.obtainStyledAttributes(new int[]{colorAttr});