1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.bitmapfun.util; 18 19 import android.annotation.SuppressLint; 20 import android.app.ActivityManager; 21 import android.content.Context; 22 import android.graphics.Bitmap; 23 import android.os.Build; 24 import android.os.Environment; 25 import android.os.StatFs; 26 27 import java.io.File; 28 29 /** 30 * Class containing some static utility methods. 31 */ 32 public class Utils { 33 public static final int IO_BUFFER_SIZE = 8 * 1024; 34 Utils()35 private Utils() {}; 36 37 /** 38 * Workaround for bug pre-Froyo, see here for more info: 39 * http://android-developers.blogspot.com/2011/09/androids-http-clients.html 40 */ disableConnectionReuseIfNecessary()41 public static void disableConnectionReuseIfNecessary() { 42 // HTTP connection reuse which was buggy pre-froyo 43 if (hasHttpConnectionBug()) { 44 System.setProperty("http.keepAlive", "false"); 45 } 46 } 47 48 /** 49 * Get the size in bytes of a bitmap. 50 * @param bitmap 51 * @return size in bytes 52 */ 53 @SuppressLint("NewApi") getBitmapSize(Bitmap bitmap)54 public static int getBitmapSize(Bitmap bitmap) { 55 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { 56 return bitmap.getByteCount(); 57 } 58 // Pre HC-MR1 59 return bitmap.getRowBytes() * bitmap.getHeight(); 60 } 61 62 /** 63 * Check if external storage is built-in or removable. 64 * 65 * @return True if external storage is removable (like an SD card), false 66 * otherwise. 67 */ 68 @SuppressLint("NewApi") isExternalStorageRemovable()69 public static boolean isExternalStorageRemovable() { 70 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { 71 return Environment.isExternalStorageRemovable(); 72 } 73 return true; 74 } 75 76 /** 77 * Get the external app cache directory. 78 * 79 * @param context The context to use 80 * @return The external cache dir 81 */ 82 @SuppressLint("NewApi") getExternalCacheDir(Context context)83 public static File getExternalCacheDir(Context context) { 84 if (hasExternalCacheDir()) { 85 return context.getExternalCacheDir(); 86 } 87 88 // Before Froyo we need to construct the external cache dir ourselves 89 final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/"; 90 return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir); 91 } 92 93 /** 94 * Check how much usable space is available at a given path. 95 * 96 * @param path The path to check 97 * @return The space available in bytes 98 */ 99 @SuppressLint("NewApi") getUsableSpace(File path)100 public static long getUsableSpace(File path) { 101 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { 102 return path.getUsableSpace(); 103 } 104 final StatFs stats = new StatFs(path.getPath()); 105 return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks(); 106 } 107 108 /** 109 * Get the memory class of this device (approx. per-app memory limit) 110 * 111 * @param context 112 * @return 113 */ getMemoryClass(Context context)114 public static int getMemoryClass(Context context) { 115 return ((ActivityManager) context.getSystemService( 116 Context.ACTIVITY_SERVICE)).getMemoryClass(); 117 } 118 119 /** 120 * Check if OS version has a http URLConnection bug. See here for more information: 121 * http://android-developers.blogspot.com/2011/09/androids-http-clients.html 122 * 123 * @return 124 */ hasHttpConnectionBug()125 public static boolean hasHttpConnectionBug() { 126 return Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO; 127 } 128 129 /** 130 * Check if OS version has built-in external cache dir method. 131 * 132 * @return 133 */ hasExternalCacheDir()134 public static boolean hasExternalCacheDir() { 135 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO; 136 } 137 138 /** 139 * Check if ActionBar is available. 140 * 141 * @return 142 */ hasActionBar()143 public static boolean hasActionBar() { 144 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB; 145 } 146 } 147