am 8180be14: Bitmapfun Sample: Minor updates/fixes.
* commit '8180be1421028c4f4d4d837c4f9a59205701d1fe':
Bitmapfun Sample: Minor updates/fixes.
diff --git a/samples/training/bitmapfun/BitmapFun/build.gradle b/samples/training/bitmapfun/BitmapFun/build.gradle
index 28bb0d8..c978231 100644
--- a/samples/training/bitmapfun/BitmapFun/build.gradle
+++ b/samples/training/bitmapfun/BitmapFun/build.gradle
@@ -14,7 +14,7 @@
android {
compileSdkVersion 19
- buildToolsVersion "18.1.1"
+ buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 7
@@ -23,5 +23,5 @@
}
dependencies {
- compile 'com.android.support:support-v4:18.0.0'
+ compile 'com.android.support:support-v4:19.0.+'
}
diff --git a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/ui/ImageDetailActivity.java b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/ui/ImageDetailActivity.java
index 5fb0776..05d7b03 100644
--- a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/ui/ImageDetailActivity.java
+++ b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/ui/ImageDetailActivity.java
@@ -18,6 +18,7 @@
import android.annotation.TargetApi;
import android.app.ActionBar;
+import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
@@ -48,7 +49,7 @@
private ImageFetcher mImageFetcher;
private ViewPager mPager;
- @TargetApi(11)
+ @TargetApi(VERSION_CODES.HONEYCOMB)
@Override
public void onCreate(Bundle savedInstanceState) {
if (BuildConfig.DEBUG) {
@@ -199,7 +200,7 @@
* Set on the ImageView in the ViewPager children fragments, to enable/disable low profile mode
* when the ImageView is touched.
*/
- @TargetApi(11)
+ @TargetApi(VERSION_CODES.HONEYCOMB)
@Override
public void onClick(View v) {
final int vis = mPager.getSystemUiVisibility();
diff --git a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/ui/ImageGridFragment.java b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/ui/ImageGridFragment.java
index a968326..ba4581a 100644
--- a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/ui/ImageGridFragment.java
+++ b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/ui/ImageGridFragment.java
@@ -20,6 +20,7 @@
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
+import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
@@ -134,6 +135,13 @@
if (BuildConfig.DEBUG) {
Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
}
+ if (Utils.hasJellyBean()) {
+ mGridView.getViewTreeObserver()
+ .removeOnGlobalLayoutListener(this);
+ } else {
+ mGridView.getViewTreeObserver()
+ .removeGlobalOnLayoutListener(this);
+ }
}
}
}
@@ -163,7 +171,7 @@
mImageFetcher.closeCache();
}
- @TargetApi(16)
+ @TargetApi(VERSION_CODES.JELLY_BEAN)
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
final Intent i = new Intent(getActivity(), ImageDetailActivity.class);
@@ -226,6 +234,11 @@
@Override
public int getCount() {
+ // If columns have yet to be determined, return no items
+ if (getNumColumns() == 0) {
+ return 0;
+ }
+
// Size + number of columns for top empty row
return Images.imageThumbUrls.length + mNumColumns;
}
diff --git a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageCache.java b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageCache.java
index 145c881..7021d2c 100644
--- a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageCache.java
+++ b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageCache.java
@@ -23,6 +23,7 @@
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
+import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
@@ -354,24 +355,26 @@
Bitmap bitmap = null;
if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
- final Iterator<SoftReference<Bitmap>> iterator = mReusableBitmaps.iterator();
- Bitmap item;
+ synchronized (mReusableBitmaps) {
+ final Iterator<SoftReference<Bitmap>> iterator = mReusableBitmaps.iterator();
+ Bitmap item;
- while (iterator.hasNext()) {
- item = iterator.next().get();
+ while (iterator.hasNext()) {
+ item = iterator.next().get();
- if (null != item && item.isMutable()) {
- // Check to see it the item can be used for inBitmap
- if (canUseForInBitmap(item, options)) {
- bitmap = item;
+ if (null != item && item.isMutable()) {
+ // Check to see it the item can be used for inBitmap
+ if (canUseForInBitmap(item, options)) {
+ bitmap = item;
- // Remove from reusable set so it can't be used again
+ // Remove from reusable set so it can't be used again
+ iterator.remove();
+ break;
+ }
+ } else {
+ // Remove from the set if the reference has been cleared.
iterator.remove();
- break;
}
- } else {
- // Remove from the set if the reference has been cleared.
- iterator.remove();
}
}
}
@@ -503,6 +506,7 @@
* @return true if <code>candidate</code> can be used for inBitmap re-use with
* <code>targetOptions</code>
*/
+ @TargetApi(VERSION_CODES.KITKAT)
private static boolean canUseForInBitmap(
Bitmap candidate, BitmapFactory.Options targetOptions) {
@@ -594,7 +598,7 @@
* @param value
* @return size in bytes
*/
- @TargetApi(12)
+ @TargetApi(VERSION_CODES.KITKAT)
public static int getBitmapSize(BitmapDrawable value) {
Bitmap bitmap = value.getBitmap();
@@ -618,7 +622,7 @@
* @return True if external storage is removable (like an SD card), false
* otherwise.
*/
- @TargetApi(9)
+ @TargetApi(VERSION_CODES.GINGERBREAD)
public static boolean isExternalStorageRemovable() {
if (Utils.hasGingerbread()) {
return Environment.isExternalStorageRemovable();
@@ -632,7 +636,7 @@
* @param context The context to use
* @return The external cache dir
*/
- @TargetApi(8)
+ @TargetApi(VERSION_CODES.FROYO)
public static File getExternalCacheDir(Context context) {
if (Utils.hasFroyo()) {
return context.getExternalCacheDir();
@@ -649,7 +653,7 @@
* @param path The path to check
* @return The space available in bytes
*/
- @TargetApi(9)
+ @TargetApi(VERSION_CODES.GINGERBREAD)
public static long getUsableSpace(File path) {
if (Utils.hasGingerbread()) {
return path.getUsableSpace();
diff --git a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/Utils.java b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/Utils.java
index 81b856a..31dc342 100644
--- a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/Utils.java
+++ b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/Utils.java
@@ -16,20 +16,22 @@
package com.example.android.bitmapfun.util;
-import com.example.android.bitmapfun.ui.ImageDetailActivity;
-import com.example.android.bitmapfun.ui.ImageGridActivity;
-
import android.annotation.TargetApi;
import android.os.Build;
+import android.os.Build.VERSION_CODES;
import android.os.StrictMode;
+import com.example.android.bitmapfun.ui.ImageDetailActivity;
+import com.example.android.bitmapfun.ui.ImageGridActivity;
+
/**
* Class containing some static utility methods.
*/
public class Utils {
private Utils() {};
- @TargetApi(11)
+
+ @TargetApi(VERSION_CODES.HONEYCOMB)
public static void enableStrictMode() {
if (Utils.hasGingerbread()) {
StrictMode.ThreadPolicy.Builder threadPolicyBuilder =