Skip to content

Commit

Permalink
correct some code and add more doc
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Liu <airk908@gmail.com>
  • Loading branch information
lkv1988 committed Apr 7, 2015
1 parent b1219de commit 47676b6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
Expand Up @@ -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);
}
21 changes: 18 additions & 3 deletions sobitmap/src/main/java/com/github/airk/tool/sobitmap/Options.java
Expand Up @@ -51,7 +51,7 @@ public final class Options {
*/
final Bitmap.CompressFormat format;

public static enum QualityLevel {
public enum QualityLevel {
HIGH {
@Override
int getStep() {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down
Expand Up @@ -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;
}
Expand Down
37 changes: 21 additions & 16 deletions sobitmap/src/main/java/com/github/airk/tool/sobitmap/SoBitmap.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}
Expand All @@ -148,7 +153,7 @@ private SoBitmap(Context context, boolean useExternalCache) {
for (Class<? extends Hunter> cls : HUNTERS) {
try {
hunterSet.add(cls.newInstance());
} catch (InstantiationException | IllegalAccessException ignore) {
} catch (Exception ignore) {
}
}
if (useExternalCache) {
Expand All @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -326,7 +331,7 @@ public void shutdown() {
Log.d(TAG, "SoBitmap Shutdown!");
}
executor.shutdownNow();
INSTANCE = null;
sInstance = null;
}

}

0 comments on commit 47676b6

Please sign in to comment.