diff --git a/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Callback.java b/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Callback.java index 69d1745..5fd8e19 100644 --- a/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Callback.java +++ b/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Callback.java @@ -25,7 +25,7 @@ * Hunter's callback */ public interface Callback { - public void onHunted(Bitmap bitmap, BitmapFactory.Options options); + void onHunted(Bitmap bitmap, BitmapFactory.Options options); - public void onException(HuntException e); + void onException(HuntException e); } diff --git a/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Options.java b/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Options.java index 6b4eee6..1ebbbe2 100644 --- a/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Options.java +++ b/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Options.java @@ -51,7 +51,7 @@ public final class Options { */ final Bitmap.CompressFormat format; - public static enum QualityLevel { + public enum QualityLevel { HIGH { @Override int getStep() { @@ -97,6 +97,15 @@ float getMemoryFactor() { abstract float getMemoryFactor(); } + /** + * Use detail parameters construct display option + * + * @param maxInput Max input in memory + * @param maxOutput Max output in memory + * @param maxSize The max display size in pixel + * @param qualityStep quality down step + * @param format JPG\PNG\WEBP + */ public Options(long maxInput, long maxOutput, int maxSize, int qualityStep, Bitmap.CompressFormat format) { this.maxInput = maxInput; this.maxOutput = maxOutput; @@ -108,6 +117,13 @@ public Options(long maxInput, long maxOutput, int maxSize, int qualityStep, Bitm level = null; } + /** + * Use level to construct display option, don't care about the detail inside. + * + * @param maxSize The max display size in pixel + * @param format JPG\PNG\WEBP + * @param level {@link com.github.airk.tool.sobitmap.Options.QualityLevel} + */ public Options(int maxSize, Bitmap.CompressFormat format, QualityLevel level) { this.maxSize = maxSize; this.format = format; @@ -128,9 +144,8 @@ public boolean equals(Object o) { if (maxSize != options.maxSize) return false; if (qualityStep != options.qualityStep) return false; if (format != options.format) return false; - if (level != options.level) return false; + return level == options.level; - return true; } @Override diff --git a/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Request.java b/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Request.java index 7b7073d..8490806 100644 --- a/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Request.java +++ b/sobitmap/src/main/java/com/github/airk/tool/sobitmap/Request.java @@ -51,7 +51,7 @@ final class Request implements Callback, Runnable { Request(Context context, String tag, Uri source, Options options, Callback callback, Hunter target, Handler handler, File dir) { this.context = context; if (tag == null) { - this.tag = "sobitmap_request_" + Integer.toHexString(this.hashCode()); + this.tag = "sobitmap:request:" + Integer.toHexString(this.hashCode()); } else { this.tag = tag; } diff --git a/sobitmap/src/main/java/com/github/airk/tool/sobitmap/SoBitmap.java b/sobitmap/src/main/java/com/github/airk/tool/sobitmap/SoBitmap.java index 98dd36b..3efbb75 100644 --- a/sobitmap/src/main/java/com/github/airk/tool/sobitmap/SoBitmap.java +++ b/sobitmap/src/main/java/com/github/airk/tool/sobitmap/SoBitmap.java @@ -49,7 +49,7 @@ public final class SoBitmap { */ static boolean LOG = Log.isLoggable(TAG, Log.VERBOSE); - private volatile static SoBitmap INSTANCE; + private volatile static SoBitmap sInstance; private Context context; private Options defaultOps; private File cacheDir; @@ -94,31 +94,31 @@ public boolean handleMessage(Message msg) { * @return SoBitmap instance */ public static SoBitmap getInstance(Context context) { - if (INSTANCE == null) { + if (sInstance == null) { synchronized (SoBitmap.class) { - if (INSTANCE == null) { - INSTANCE = new SoBitmap(context); + if (sInstance == null) { + sInstance = new SoBitmap(context); } } } - return INSTANCE; + return sInstance; } /** * User can set SoBitmap's singleton instance to follow what he want by using this method. The Builder{@link com.github.airk.tool.sobitmap.SoBitmap.Builder} * can give users more opportunity to custom SoBitmap but not invoke the SoBitmap's constructor immediately, so we can keep SoBitmap's singleton mode safe. - * ps: the method must be invoked before {@link #getInstance(android.content.Context)}, otherwise there will be a IllegalArgumentException. + * ps: the method must be invoked before {@link #getInstance(android.content.Context)}, otherwise there will be a IllegalStateException. * * @param context Context * @param builder {@link com.github.airk.tool.sobitmap.SoBitmap.Builder} * @return SoBitmap instance */ public static SoBitmap setInstanceByBuilder(Context context, Builder builder) { - if (INSTANCE != null) { - throw new IllegalArgumentException("Singleton instance has been created, please call this method before getInstance(Context)"); + if (sInstance != null) { + throw new IllegalStateException("Singleton instance has been created, please call this method before getInstance(Context)"); } - INSTANCE = new SoBitmap(context, builder.useExternalCache); - return INSTANCE; + sInstance = new SoBitmap(context, builder.useExternalCache); + return sInstance; } /** @@ -127,6 +127,11 @@ public static SoBitmap setInstanceByBuilder(Context context, Builder builder) { public static class Builder { boolean useExternalCache = true; + /** + * Shall SoBitmap use external storage for cache, default is true. + * + * @param useExternalCache true for use + */ public void setUseExternalCache(boolean useExternalCache) { this.useExternalCache = useExternalCache; } @@ -148,7 +153,7 @@ private SoBitmap(Context context, boolean useExternalCache) { for (Class cls : HUNTERS) { try { hunterSet.add(cls.newInstance()); - } catch (InstantiationException | IllegalAccessException ignore) { + } catch (Exception ignore) { } } if (useExternalCache) { @@ -170,8 +175,8 @@ private SoBitmap(Context context, boolean useExternalCache) { * @return SoBitmap single instance */ public SoBitmap setDefaultOption(Options option) { - INSTANCE.defaultOps = option; - return INSTANCE; + sInstance.defaultOps = option; + return sInstance; } /** @@ -182,7 +187,7 @@ public SoBitmap setDefaultOption(Options option) { * @return true if hunt in process successful, false otherwise */ public boolean hunt(Uri uri, Callback callback) { - return hunt(null, uri, INSTANCE.defaultOps, callback); + return hunt(null, uri, sInstance.defaultOps, callback); } /** @@ -194,7 +199,7 @@ public boolean hunt(Uri uri, Callback callback) { * @return true if hunt in process successful, false otherwise */ public boolean hunt(String tag, Uri uri, Callback callback) { - return hunt(tag, uri, INSTANCE.defaultOps, callback); + return hunt(tag, uri, sInstance.defaultOps, callback); } /** @@ -326,7 +331,7 @@ public void shutdown() { Log.d(TAG, "SoBitmap Shutdown!"); } executor.shutdownNow(); - INSTANCE = null; + sInstance = null; } }