Skip to content

Commit

Permalink
* attempt to use OK HTTP client
Browse files Browse the repository at this point in the history
* fallback to native Android HTTP client
  • Loading branch information
ArthurHub committed Feb 17, 2015
1 parent bcd452d commit 15c1004
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion fastimageloader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ android {
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "0.9"
versionName "0.8"
}

lintOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.theartofdev.fastimageloader.impl.LoaderHandler;
import com.theartofdev.fastimageloader.impl.MemoryPoolImpl;
import com.theartofdev.fastimageloader.impl.NativeHttpClient;
import com.theartofdev.fastimageloader.impl.OkHttpClient;
import com.theartofdev.fastimageloader.impl.util.FILLogger;
import com.theartofdev.fastimageloader.impl.util.FILUtils;
import com.theartofdev.fastimageloader.target.TargetHelper;
Expand Down Expand Up @@ -115,6 +116,7 @@ public static FastImageLoader init(Application application) {
FILUtils.notNull(application, "context");

if (INST.mLoaderHandler == null) {
FILLogger.debug("Init fast image loader...");
INST.mApplication = application;
return INST;
} else {
Expand Down Expand Up @@ -225,6 +227,8 @@ public static ImageLoadSpecBuilder buildSpec(String key) {
if (INST.mSpecs.containsKey(key)) {
throw new IllegalArgumentException("Spec with the same key already exists");
}

FILLogger.debug("Create image load spec... [{}]", key);
return new ImageLoadSpecBuilder(key, INST.mApplication, INST.mDefaultImageServiceAdapter);
}

Expand Down Expand Up @@ -254,6 +258,8 @@ public static void prefetchImage(String uri, String specKey) {
if (spec == null) {
throw new IllegalArgumentException("Invalid spec key, no spec defined for the given key: " + specKey);
}

FILLogger.debug("Prefetch image... [{}] [{}]", uri, spec);
INST.mLoaderHandler.prefetchImage(uri, spec);
}
}
Expand Down Expand Up @@ -283,6 +289,8 @@ public static void loadImage(Target target, String altSpecKey) {
if (altSpecKey != null && altSpec == null) {
throw new IllegalArgumentException("Invalid alternative spec key, no spec defined for the given key: " + altSpecKey);
}

FILLogger.debug("Load image... [{}] [{}] [{}]", target, spec, altSpecKey);
INST.mLoaderHandler.loadImage(target, spec, altSpec);
}

Expand All @@ -295,13 +303,16 @@ public static void loadImage(Target target, String altSpecKey) {
*/
public static void clearDiskCache() {
INST.finishInit();

FILLogger.debug("Clear image cache...");
INST.mLoaderHandler.clearDiskCache();
}

/**
* Add the given image load spec to the defined specs.
*/
static void addSpec(ImageLoadSpec spec) {
FILLogger.debug("Image load spec created... [{}]", spec);
INST.mSpecs.put(spec.getKey(), spec);
}

Expand All @@ -316,32 +327,60 @@ private void finishInit() {
if (INST.mLoaderHandler == null) {
if (INST.mApplication != null) {
if (INST.mDefaultImageServiceAdapter == null) {
FILLogger.debug("Use default identity image service adapter...");
INST.mDefaultImageServiceAdapter = new IdentityAdapter();
}
if (mMemoryPool == null) {
FILLogger.debug("Use default memory pool...");
mMemoryPool = new MemoryPoolImpl();
}
if (mDecoder == null) {
FILLogger.debug("Use default decoder...");
mDecoder = new DecoderImpl();
}
if (mDiskCache == null) {
if (mCacheFolder == null) {
FILLogger.debug("Use default cache folder...");
mCacheFolder = new File(FILUtils.pathCombine(mApplication.getCacheDir().getPath(), "ImageCache"));
}
FILLogger.debug("Use default disk cache... [{}]", mCacheFolder);
mDiskCache = new DiskCacheImpl(mApplication, mCacheFolder, mMemoryPool, mDecoder);
}
if (mDownloader == null) {
if (INST.mHttpClient == null) {
INST.mHttpClient = new NativeHttpClient();
//INST.mHttpClient = new OkHttpClient();
}
initHttpClient();

FILLogger.debug("Use default downloader...");
mDownloader = new DownloaderImpl(mApplication, mHttpClient, mMemoryPool, mDecoder);
}

FILLogger.debug("Create load handler... [{}] [{}] [{}]", mMemoryPool, mDiskCache, mDownloader);
INST.mLoaderHandler = new LoaderHandler(mApplication, mMemoryPool, mDiskCache, mDownloader);
} else {
throw new IllegalStateException("Fast Image Loader is NOT initialized, call init(...)");
}
}
}

/**
* Init HTTP client to be used by the downloader.<br>
* 1. If one given externally use it.<br>
* 2. Try using OK HTTP client, won't work if there is no OK HTTP dependency<br>
* 3. Use native Android URL connection.<br>
*/
private void initHttpClient() {
if (INST.mHttpClient == null) {
try {
FILLogger.debug("Try create OK HTTP client...");
INST.mHttpClient = new OkHttpClient();
} catch (Throwable e) {
if (e.getClass().isAssignableFrom(NoClassDefFoundError.class)) {
FILLogger.debug("OK HTTP dependency no found, use native Android URL Connection");
} else {
FILLogger.warn("Failed to init OK HTTP client, use native Android URL Connection", e);
}
INST.mHttpClient = new NativeHttpClient();
}
}
}
//endregion
}

0 comments on commit 15c1004

Please sign in to comment.