Skip to content

Commit a8a37f8

Browse files
author
Adam Koch
committed
3 small updates:
Update inSampleSize calculation - while this didn't affect iosched the existing formula would calculate the wrong value in some cases. Unpause the image loader when the activity/fragment is paused - the image loader is paused while flinging the social stream fragment, however if the activity is destroyed while the image loader is paused this can leave the background thread in a hung state. Change available app memory calculation from memory class to available runtime memory - used to set the image memory cache size.
1 parent e795b1a commit a8a37f8

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

android/src/com/google/android/apps/iosched/ui/SocialStreamFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
165165
@Override
166166
public void onPause() {
167167
super.onPause();
168+
mImageFetcher.setPauseWork(false);
168169
mImageFetcher.flushCache();
169170
}
170171

android/src/com/google/android/apps/iosched/util/ImageCache.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,13 @@ private void init(ImageCacheParams cacheParams) {
129129
LOGD(TAG, "Memory cache created (size = " + mCacheParams.memCacheSize + ")");
130130
mMemoryCache = new LruCache<String, Bitmap>(mCacheParams.memCacheSize) {
131131
/**
132-
* Measure item size in bytes rather than units which is more practical
132+
* Measure item size in kilobytes rather than units which is more practical
133133
* for a bitmap cache
134134
*/
135135
@Override
136136
protected int sizeOf(String key, Bitmap bitmap) {
137-
return getBitmapSize(bitmap);
137+
final int bitmapSize = getBitmapSize(bitmap) / 1024;
138+
return bitmapSize == 0 ? 1 : bitmapSize;
138139
}
139140
};
140141
}
@@ -362,40 +363,41 @@ public static class ImageCacheParams {
362363
public boolean initDiskCacheOnCreate = DEFAULT_INIT_DISK_CACHE_ON_CREATE;
363364

364365
public ImageCacheParams(Context context) {
365-
init(context, getDiskCacheDir(context, DEFAULT_DISK_CACHE_DIR));
366+
init(getDiskCacheDir(context, DEFAULT_DISK_CACHE_DIR));
366367
}
367368

368369
public ImageCacheParams(Context context, String uniqueName) {
369-
init(context, getDiskCacheDir(context, uniqueName));
370+
init(getDiskCacheDir(context, uniqueName));
370371
}
371372

372-
public ImageCacheParams(Context context, File diskCacheDir) {
373-
init(context, diskCacheDir);
373+
public ImageCacheParams(File diskCacheDir) {
374+
init(diskCacheDir);
374375
}
375376

376-
private void init(Context context, File diskCacheDir) {
377-
setMemCacheSizePercent(context, DEFAULT_MEM_CACHE_PERCENT);
377+
private void init(File diskCacheDir) {
378+
setMemCacheSizePercent(DEFAULT_MEM_CACHE_PERCENT);
378379
this.diskCacheDir = diskCacheDir;
379380
}
380381

381382
/**
382-
* Sets the memory cache size based on a percentage of the device memory class.
383-
* Eg. setting percent to 0.2 would set the memory cache to one fifth of the device memory
384-
* class. Throws {@link IllegalArgumentException} if percent is < 0.05 or > .8.
383+
* Sets the memory cache size based on a percentage of the max available VM memory.
384+
* Eg. setting percent to 0.2 would set the memory cache to one fifth of the avilable
385+
* memory. Throws {@link IllegalArgumentException} if percent is < 0.05 or > .8.
386+
* memCacheSize is stored in kilobytes instead of bytes as this will eventually be passed
387+
* to construct a LruCache which takes an int in its constructor.
385388
*
386389
* This value should be chosen carefully based on a number of factors
387390
* Refer to the corresponding Android Training class for more discussion:
388391
* http://developer.android.com/training/displaying-bitmaps/
389392
*
390-
* @param context Context to use to fetch memory class
391393
* @param percent Percent of memory class to use to size memory cache
392394
*/
393-
public void setMemCacheSizePercent(Context context, float percent) {
395+
public void setMemCacheSizePercent(float percent) {
394396
if (percent < 0.05f || percent > 0.8f) {
395397
throw new IllegalArgumentException("setMemCacheSizePercent - percent must be "
396398
+ "between 0.05 and 0.8 (inclusive)");
397399
}
398-
memCacheSize = Math.round(percent * getMemoryClass(context) * 1024 * 1024);
400+
memCacheSize = Math.round(percent * Runtime.getRuntime().maxMemory() / 1024);
399401
}
400402

401403
private static int getMemoryClass(Context context) {

android/src/com/google/android/apps/iosched/util/ImageFetcher.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,14 @@ public static int calculateInSampleSize(BitmapFactory.Options options,
456456
int inSampleSize = 1;
457457

458458
if (height > reqHeight || width > reqWidth) {
459-
if (width > height) {
460-
inSampleSize = Math.round((float) height / (float) reqHeight);
461-
} else {
462-
inSampleSize = Math.round((float) width / (float) reqWidth);
463-
}
459+
// Calculate ratios of height and width to requested height and width
460+
final int heightRatio = Math.round((float) height / (float) reqHeight);
461+
final int widthRatio = Math.round((float) width / (float) reqWidth);
462+
463+
// Choose the smallest ratio as inSampleSize value, this will guarantee
464+
// a final image with both dimensions larger than or equal to the
465+
// requested height and width.
466+
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
464467

465468
// This offers some additional logic in case the image has a strange
466469
// aspect ratio. For example, a panorama may have a much larger

android/src/com/google/android/apps/iosched/util/ImageWorker.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public void setImageFadeIn(boolean fadeIn) {
229229
*/
230230
public void setExitTasksEarly(boolean exitTasksEarly) {
231231
mExitTasksEarly = exitTasksEarly;
232+
setPauseWork(false);
232233
}
233234

234235
/**
@@ -439,6 +440,18 @@ private void setImageBitmap(ImageView imageView, Bitmap bitmap) {
439440
}
440441
}
441442

443+
/**
444+
* Pause any ongoing background work. This can be used as a temporary
445+
* measure to improve performance. For example background work could
446+
* be paused when a ListView or GridView is being scrolled using a
447+
* {@link android.widget.AbsListView.OnScrollListener} to keep
448+
* scrolling smooth.
449+
* <p>
450+
* If work is paused, be sure setPauseWork(false) is called again
451+
* before your fragment or activity is destroyed (for example during
452+
* {@link android.app.Activity#onPause()}), or there is a risk the
453+
* background thread will never finish.
454+
*/
442455
public void setPauseWork(boolean pauseWork) {
443456
synchronized (mPauseWorkLock) {
444457
mPauseWork = pauseWork;

0 commit comments

Comments
 (0)