Skip to content

Commit

Permalink
Merge pull request #49 from luigi-agosti/master
Browse files Browse the repository at this point in the history
improving tests issue #25,and fix issue #47
  • Loading branch information
luigi-agosti committed Jun 27, 2012
2 parents 63b7bf5 + 0355992 commit 3056ba6
Show file tree
Hide file tree
Showing 21 changed files with 636 additions and 237 deletions.
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
<version>1.4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit-addons</groupId>
<artifactId>junit-addons</artifactId>
<version>1.4</version>
</dependency>
</dependencies>

<build>
Expand Down
14 changes: 9 additions & 5 deletions core/src/main/java/com/novoda/imageloader/core/ImageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ public ImageManager(Context context, LoaderSettings settings) {
cacheManager = new SoftMapCache();
}
loaderContext.setCache(cacheManager);
if (settings.isUseAsyncTasks()) {
this.loader = new ConcurrentLoader(loaderContext);
} else {
this.loader = new SimpleLoader(loaderContext);
}
setLoader(settings);
verifyPermissions(context);
}

Expand All @@ -83,6 +79,14 @@ public CacheManager getCacheManager() {
public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}

protected void setLoader(LoaderSettings settings) {
if (settings.isUseAsyncTasks()) {
this.loader = new ConcurrentLoader(loaderContext);
} else {
this.loader = new SimpleLoader(loaderContext);
}
}

private void verifyPermissions(Context context) {
verifyPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.os.Build;

import com.novoda.imageloader.core.cache.CacheManager;
import com.novoda.imageloader.core.file.util.AndroidFileContext;
import com.novoda.imageloader.core.file.util.FileUtil;

/**
Expand Down Expand Up @@ -127,6 +128,10 @@ public boolean isUseAsyncTasks() {
public void setUseAsyncTasks(boolean useAsyncTasks) {
this.useAsyncTasks = useAsyncTasks;
}

public boolean isCleanOnSetup() {
return true;
}

/**
* Builder for the LoaderSettings.
Expand Down Expand Up @@ -175,7 +180,8 @@ public SettingsBuilder withCacheDir(File file) {
}

public LoaderSettings build(Context context) {
settings.setCacheDir(new FileUtil().prepareCacheDirectory(context));
File dir = new FileUtil().prepareCacheDirectory(new AndroidFileContext(context));
settings.setCacheDir(dir);
settings.setSdkVersion(Build.VERSION.SDK);
return settings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.novoda.imageloader.core.LoaderSettings;
import com.novoda.imageloader.core.file.util.FileUtil;
import com.novoda.imageloader.core.network.UrlUtil;
import com.novoda.imageloader.core.util.Log;

/**
* This is a basic implementation for the file manager.
Expand All @@ -31,11 +32,13 @@
*/
public class BasicFileManager implements FileManager {

private LoaderSettings settings;
private LoaderSettings loaderSettings;

public BasicFileManager(LoaderSettings settings) {
this.settings = settings;
cleanOldFiles();
this.loaderSettings = settings;
if(settings.isCleanOnSetup()) {
cleanOldFiles();
}
}

/**
Expand All @@ -52,7 +55,7 @@ public void clean() {
*/
@Override
public void cleanOldFiles() {
deleteOldFiles(settings.getExpirationPeriod());
deleteOldFiles(loaderSettings.getExpirationPeriod());
}

@Override
Expand All @@ -70,33 +73,33 @@ public void saveBitmap(String fileName, Bitmap b, int width, int height) {
FileOutputStream out = new FileOutputStream(fileName + "-" + width + "x" + height);
b.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
Log.warning("" + e.getMessage());
}
}

@Override
public File getFile(String url) {
url = processUrl(url);
String filename = String.valueOf(url.hashCode());
return new File(settings.getCacheDir(), filename);
return new File(loaderSettings.getCacheDir(), filename);
}

@Override
public File getFile(String url, int width, int height) {
url = processUrl(url);
String filename = String.valueOf(url.hashCode()) + "-" + width + "x" + height;
return new File(settings.getCacheDir(), filename);
return new File(loaderSettings.getCacheDir(), filename);
}

private String processUrl(String url) {
if (settings.isQueryIncludedInHash()) {
if (loaderSettings.isQueryIncludedInHash()) {
return url;
}
return new UrlUtil().removeQuery(url);
}

private void deleteOldFiles(final long expirationPeriod) {
final String cacheDir = settings.getCacheDir().getAbsolutePath();
final String cacheDir = loaderSettings.getCacheDir().getAbsolutePath();
Thread cleaner = new Thread(new Runnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright 2012 Novoda Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.novoda.imageloader.core.file.util;

import java.io.File;

import android.content.Context;

/**
* Internal class to abstract the dependency to specific implementation
* of android functionalities.
* This class is for internal usage of the imageLoader.
*/
public class AndroidFileContext {

private Context context;

public AndroidFileContext(Context context) {
this.context = context;
}

protected boolean isMounted() {
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
return true;
}
return false;
}

protected String getPackageName() {
return context.getPackageName();
}

protected File getExternalStorageDirectory() {
return android.os.Environment.getExternalStorageDirectory();
}

protected File preparePhoneCacheDir() {
return context.getCacheDir();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,46 @@
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.util.Log;

import com.novoda.imageloader.core.exception.ImageCopyException;
import com.novoda.imageloader.core.util.Log;

/**
* This class is internal to the imageLoader.
* If you want to used it make sure to prepare for changes to the interface.
*/
public class FileUtil {

private static final String ANDROID_ROOT = "/Android/data/";
private static final String NOMEDIA_FILE_NAME = ".nomedia";
private static final String DEFAULT_IMAGE_FOLDER_NAME = "/cache/images";
private static final String TAG = "ImageLoader";

private static final int BUFFER_SIZE = 60 * 1024;
private static final byte[] BUFFER = new byte[BUFFER_SIZE];

public void closeSilently(Closeable c) {
try {
if (c != null) {
c.close();
}
} catch (Exception e) {
Log.e(TAG, "Problem closing stream " + e.getMessage());
Log.warning("Problem closing stream " + e.getMessage());
}
}

public void copyStream(InputStream is, OutputStream os) {
try {
byte[] buffer = new byte[BUFFER_SIZE];
while (true) {
synchronized (BUFFER) {
int amountRead = is.read(BUFFER);
if (amountRead == -1) {
break;
}
os.write(BUFFER, 0, amountRead);
int amountRead = is.read(buffer);
if (amountRead == -1) {
break;
}
os.write(buffer, 0, amountRead);
}
} catch (Exception ex) {
Log.e(TAG, "Exception : ", ex);
} catch (Exception e) {
Log.warning("Exception : " + e.getMessage());
}
}


public boolean deleteFileCache(String cacheDirFullPath) {
return reduceFileCache(cacheDirFullPath, -1);
}
Expand Down Expand Up @@ -101,42 +100,31 @@ public void copy(File src, File dst) throws ImageCopyException {
}
}

public File prepareCacheDirectory(Context context) {
public File prepareCacheDirectory(AndroidFileContext fileContext) {
File dir = null;
if (!isMounted()) {
dir = preparePhoneCacheDir(context);
if (fileContext.isMounted()) {
dir = prepareExternalCacheDir(fileContext);
} else {
dir = prepareExternalCacheDir(context);
dir = fileContext.preparePhoneCacheDir();
}
addNomediaFile(dir);
return dir;
}

public boolean isMounted() {
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
return true;
}
return false;
}

private File prepareExternalCacheDir(Context context) {
String relativepath = ANDROID_ROOT + context.getPackageName() + DEFAULT_IMAGE_FOLDER_NAME;
File file = new File(android.os.Environment.getExternalStorageDirectory(), relativepath);
private File prepareExternalCacheDir(AndroidFileContext fileContext) {
String relativepath = ANDROID_ROOT + fileContext.getPackageName() + DEFAULT_IMAGE_FOLDER_NAME;
File file = new File(fileContext.getExternalStorageDirectory(), relativepath);
if (!file.isDirectory()) {
file.mkdirs();
}
return file;
}

private File preparePhoneCacheDir(Context context) {
return context.getCacheDir();
}

private void addNomediaFile(File dir) {
try {
new File(dir, NOMEDIA_FILE_NAME).createNewFile();
} catch (Exception e) {
// Don't care if doesn't save the file
Log.warning("Problem creating .nomedia file : " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@

public class BitmapDisplayer implements Runnable {
private Bitmap bitmap;
private ImageWrapper imageView;
private ImageWrapper imageWrapper;

public BitmapDisplayer(Bitmap b, ImageWrapper i) {
bitmap = b;
imageView = i;
public BitmapDisplayer(Bitmap bitmap, ImageWrapper imageWrapper) {
this.bitmap = bitmap;
this.imageWrapper = imageWrapper;
}

public void runOnUiThread() {
imageView.runOnUiThread(this);
imageWrapper.runOnUiThread(this);
}

@Override
public void run() {
if (bitmap == null) {
return;
}
if(imageView.isUrlChanged()) {
if(imageWrapper.isUrlChanged()) {
return;
}
imageView.setBitmap(bitmap);
imageWrapper.setBitmap(bitmap);
}
}
23 changes: 23 additions & 0 deletions core/src/main/java/com/novoda/imageloader/core/util/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.novoda.imageloader.core.util;

public class Log {

private static final String TAG = "ImageLoader";

public static final void info(String message) {
try {
android.util.Log.i(TAG, message);
} catch(Exception e) {
System.out.println(TAG + " " + message);
}
}

public static final void warning(String message) {
try {
android.util.Log.w(TAG, message);
} catch(Exception e) {
System.out.println(TAG + " " + message);
}
}

}
Loading

0 comments on commit 3056ba6

Please sign in to comment.