-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de0b842
commit 42252f2
Showing
3 changed files
with
1,071 additions
and
1 deletion.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
...ava/bitmap/trainning/google/com/displaybitmapsefficiently/util/core/DiskLruCacheUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package bitmap.trainning.google.com.displaybitmapsefficiently.util.core; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageInfo; | ||
import android.content.pm.PackageManager; | ||
import android.os.AsyncTask; | ||
import android.os.Environment; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import bitmap.trainning.google.com.displaybitmapsefficiently.util.extra.DiskLruCache; | ||
|
||
import static android.os.Environment.isExternalStorageRemovable; | ||
|
||
/** | ||
*DiskLruCacheUtil for bitmap ,save it in disk. | ||
* Note :design single instance | ||
*/ | ||
public class DiskLruCacheUtil { | ||
private static DiskLruCacheUtil instance = null; | ||
private DiskLruCache mDiskLruCache; | ||
private static final Class<DiskLruCacheUtil> mDiskCacheLock = DiskLruCacheUtil.class; | ||
private boolean mDiskCacheStarting = true; | ||
private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB | ||
private static final String DISK_CACHE_SUBDIR = "thumbnails"; | ||
private Context mContext; | ||
|
||
private DiskLruCacheUtil(Context context){ | ||
mContext = context; | ||
// Initialize disk cache on background thread | ||
File cacheDir = getDiskCacheDir(context, DISK_CACHE_SUBDIR); | ||
new InitDiskCacheTask().execute(cacheDir); | ||
} | ||
|
||
public static DiskLruCacheUtil getInstance(Context context){ | ||
if(instance == null){ | ||
synchronized (mDiskCacheLock) | ||
{ | ||
if(instance == null){ | ||
instance = new DiskLruCacheUtil(context); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
class InitDiskCacheTask extends AsyncTask<File, Void, Void> { | ||
@Override | ||
protected Void doInBackground(File... params) { | ||
synchronized (mDiskCacheLock) { | ||
File cacheDir = params[0]; | ||
try { | ||
mDiskLruCache = DiskLruCache.open(cacheDir, getAppVersion(mContext),1,DISK_CACHE_SIZE); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
mDiskCacheStarting = false; // Finished initialization | ||
mDiskCacheLock.notifyAll(); // Wake any waiting threads | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
/* public void addBitmapToCache(String key, Bitmap bitmap) { | ||
// Also add to disk cache | ||
synchronized (mDiskCacheLock) { | ||
try { | ||
if (mDiskLruCache != null && mDiskLruCache.get(key) == null) { | ||
mDiskLruCache.put(key, bitmap); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
public Bitmap getBitmapFromDiskCache(String key) { | ||
synchronized (mDiskCacheLock) { | ||
// Wait while disk cache is started from background thread | ||
while (mDiskCacheStarting) { | ||
try { | ||
mDiskCacheLock.wait(); | ||
} catch (InterruptedException e) {} | ||
} | ||
if (mDiskLruCache != null) { | ||
return mDiskLruCache.get(key); | ||
} | ||
} | ||
return null; | ||
}*/ | ||
|
||
// Creates a unique subdirectory of the designated app cache directory. Tries to use external | ||
// but if not mounted, falls back on internal storage. | ||
public static File getDiskCacheDir(Context context, String uniqueName) { | ||
// Check if media is mounted or storage is built-in, if so, try and use external cache dir | ||
// otherwise use internal cache dir | ||
final String cachePath = | ||
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || | ||
!isExternalStorageRemovable() ? context.getExternalCacheDir().getPath() : | ||
context.getCacheDir().getPath(); | ||
|
||
return new File(cachePath + File.separator + uniqueName); | ||
} | ||
|
||
private int getAppVersion(Context context) { | ||
try { | ||
PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); | ||
return info.versionCode; | ||
} catch (PackageManager.NameNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.