GalleryUtils.java revision e7d1767e30d01270bf67714b1902a316960952c0
1/* 2 * Copyright (C) 2010 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 17package com.android.gallery3d.util; 18 19import android.app.Activity; 20import android.content.ActivityNotFoundException; 21import android.content.ComponentName; 22import android.content.Context; 23import android.content.Intent; 24import android.content.SharedPreferences; 25import android.content.pm.PackageManager; 26import android.content.pm.ResolveInfo; 27import android.content.res.Resources; 28import android.net.Uri; 29import android.os.ConditionVariable; 30import android.os.Environment; 31import android.os.StatFs; 32import android.preference.PreferenceManager; 33import android.provider.MediaStore; 34import android.util.DisplayMetrics; 35import android.util.Log; 36import android.view.WindowManager; 37 38import com.android.gallery3d.R; 39import com.android.gallery3d.app.PackagesMonitor; 40import com.android.gallery3d.data.DataManager; 41import com.android.gallery3d.data.MediaItem; 42import com.android.gallery3d.util.ThreadPool.CancelListener; 43import com.android.gallery3d.util.ThreadPool.JobContext; 44 45import java.util.Arrays; 46import java.util.List; 47import java.util.Locale; 48 49public class GalleryUtils { 50 private static final String TAG = "GalleryUtils"; 51 private static final String MAPS_PACKAGE_NAME = "com.google.android.apps.maps"; 52 private static final String MAPS_CLASS_NAME = "com.google.android.maps.MapsActivity"; 53 54 private static final String MIME_TYPE_IMAGE = "image/*"; 55 private static final String MIME_TYPE_VIDEO = "video/*"; 56 private static final String MIME_TYPE_ALL = "*/*"; 57 private static final String DIR_TYPE_IMAGE = "vnd.android.cursor.dir/image"; 58 private static final String DIR_TYPE_VIDEO = "vnd.android.cursor.dir/video"; 59 60 private static final String PREFIX_PHOTO_EDITOR_UPDATE = "editor-update-"; 61 private static final String PREFIX_HAS_PHOTO_EDITOR = "has-editor-"; 62 63 private static final String KEY_CAMERA_UPDATE = "camera-update"; 64 private static final String KEY_HAS_CAMERA = "has-camera"; 65 66 private static Context sContext; 67 68 69 static float sPixelDensity = -1f; 70 71 public static void initialize(Context context) { 72 sContext = context; 73 if (sPixelDensity < 0) { 74 DisplayMetrics metrics = new DisplayMetrics(); 75 WindowManager wm = (WindowManager) 76 context.getSystemService(Context.WINDOW_SERVICE); 77 wm.getDefaultDisplay().getMetrics(metrics); 78 sPixelDensity = metrics.density; 79 } 80 } 81 82 public static float dpToPixel(float dp) { 83 return sPixelDensity * dp; 84 } 85 86 public static int dpToPixel(int dp) { 87 return Math.round(dpToPixel((float) dp)); 88 } 89 90 public static int meterToPixel(float meter) { 91 // 1 meter = 39.37 inches, 1 inch = 160 dp. 92 return Math.round(dpToPixel(meter * 39.37f * 160)); 93 } 94 95 public static byte[] getBytes(String in) { 96 byte[] result = new byte[in.length() * 2]; 97 int output = 0; 98 for (char ch : in.toCharArray()) { 99 result[output++] = (byte) (ch & 0xFF); 100 result[output++] = (byte) (ch >> 8); 101 } 102 return result; 103 } 104 105 // Below are used the detect using database in the render thread. It only 106 // works most of the time, but that's ok because it's for debugging only. 107 108 private static volatile Thread sCurrentThread; 109 private static volatile boolean sWarned; 110 111 public static void setRenderThread() { 112 sCurrentThread = Thread.currentThread(); 113 } 114 115 public static void assertNotInRenderThread() { 116 if (!sWarned) { 117 if (Thread.currentThread() == sCurrentThread) { 118 sWarned = true; 119 Log.w(TAG, new Throwable("Should not do this in render thread")); 120 } 121 } 122 } 123 124 private static final double RAD_PER_DEG = Math.PI / 180.0; 125 private static final double EARTH_RADIUS_METERS = 6367000.0; 126 127 public static double fastDistanceMeters(double latRad1, double lngRad1, 128 double latRad2, double lngRad2) { 129 if ((Math.abs(latRad1 - latRad2) > RAD_PER_DEG) 130 || (Math.abs(lngRad1 - lngRad2) > RAD_PER_DEG)) { 131 return accurateDistanceMeters(latRad1, lngRad1, latRad2, lngRad2); 132 } 133 // Approximate sin(x) = x. 134 double sineLat = (latRad1 - latRad2); 135 136 // Approximate sin(x) = x. 137 double sineLng = (lngRad1 - lngRad2); 138 139 // Approximate cos(lat1) * cos(lat2) using 140 // cos((lat1 + lat2)/2) ^ 2 141 double cosTerms = Math.cos((latRad1 + latRad2) / 2.0); 142 cosTerms = cosTerms * cosTerms; 143 double trigTerm = sineLat * sineLat + cosTerms * sineLng * sineLng; 144 trigTerm = Math.sqrt(trigTerm); 145 146 // Approximate arcsin(x) = x 147 return EARTH_RADIUS_METERS * trigTerm; 148 } 149 150 public static double accurateDistanceMeters(double lat1, double lng1, 151 double lat2, double lng2) { 152 double dlat = Math.sin(0.5 * (lat2 - lat1)); 153 double dlng = Math.sin(0.5 * (lng2 - lng1)); 154 double x = dlat * dlat + dlng * dlng * Math.cos(lat1) * Math.cos(lat2); 155 return (2 * Math.atan2(Math.sqrt(x), Math.sqrt(Math.max(0.0, 156 1.0 - x)))) * EARTH_RADIUS_METERS; 157 } 158 159 160 public static final double toMile(double meter) { 161 return meter / 1609; 162 } 163 164 // For debugging, it will block the caller for timeout millis. 165 public static void fakeBusy(JobContext jc, int timeout) { 166 final ConditionVariable cv = new ConditionVariable(); 167 jc.setCancelListener(new CancelListener() { 168 public void onCancel() { 169 cv.open(); 170 } 171 }); 172 cv.block(timeout); 173 jc.setCancelListener(null); 174 } 175 176 public static boolean isEditorAvailable(Context context, String mimeType) { 177 int version = PackagesMonitor.getPackagesVersion(context); 178 179 String updateKey = PREFIX_PHOTO_EDITOR_UPDATE + mimeType; 180 String hasKey = PREFIX_HAS_PHOTO_EDITOR + mimeType; 181 182 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 183 if (prefs.getInt(updateKey, 0) != version) { 184 PackageManager packageManager = context.getPackageManager(); 185 List<ResolveInfo> infos = packageManager.queryIntentActivities( 186 new Intent(Intent.ACTION_EDIT).setType(mimeType), 0); 187 prefs.edit().putInt(updateKey, version) 188 .putBoolean(hasKey, !infos.isEmpty()) 189 .commit(); 190 } 191 192 return prefs.getBoolean(hasKey, true); 193 } 194 195 public static boolean isCameraAvailable(Context context) { 196 int version = PackagesMonitor.getPackagesVersion(context); 197 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 198 if (prefs.getInt(KEY_CAMERA_UPDATE, 0) != version) { 199 PackageManager packageManager = context.getPackageManager(); 200 List<ResolveInfo> infos = packageManager.queryIntentActivities( 201 new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA), 0); 202 prefs.edit().putInt(KEY_CAMERA_UPDATE, version) 203 .putBoolean(KEY_HAS_CAMERA, !infos.isEmpty()) 204 .commit(); 205 } 206 return prefs.getBoolean(KEY_HAS_CAMERA, true); 207 } 208 209 public static boolean isValidLocation(double latitude, double longitude) { 210 // TODO: change || to && after we fix the default location issue 211 return (latitude != MediaItem.INVALID_LATLNG || longitude != MediaItem.INVALID_LATLNG); 212 } 213 214 public static String formatLatitudeLongitude(String format, double latitude, 215 double longitude) { 216 // We need to specify the locale otherwise it may go wrong in some language 217 // (e.g. Locale.FRENCH) 218 return String.format(Locale.ENGLISH, format, latitude, longitude); 219 } 220 221 public static void showOnMap(Context context, double latitude, double longitude) { 222 try { 223 // We don't use "geo:latitude,longitude" because it only centers 224 // the MapView to the specified location, but we need a marker 225 // for further operations (routing to/from). 226 // The q=(lat, lng) syntax is suggested by geo-team. 227 String uri = formatLatitudeLongitude("http://maps.google.com/maps?f=q&q=(%f,%f)", 228 latitude, longitude); 229 ComponentName compName = new ComponentName(MAPS_PACKAGE_NAME, 230 MAPS_CLASS_NAME); 231 Intent mapsIntent = new Intent(Intent.ACTION_VIEW, 232 Uri.parse(uri)).setComponent(compName); 233 context.startActivity(mapsIntent); 234 } catch (ActivityNotFoundException e) { 235 // Use the "geo intent" if no GMM is installed 236 Log.e(TAG, "GMM activity not found!", e); 237 String url = formatLatitudeLongitude("geo:%f,%f", latitude, longitude); 238 Intent mapsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 239 context.startActivity(mapsIntent); 240 } 241 } 242 243 public static void setViewPointMatrix( 244 float matrix[], float x, float y, float z) { 245 // The matrix is 246 // -z, 0, x, 0 247 // 0, -z, y, 0 248 // 0, 0, 1, 0 249 // 0, 0, 1, -z 250 Arrays.fill(matrix, 0, 16, 0); 251 matrix[0] = matrix[5] = matrix[15] = -z; 252 matrix[8] = x; 253 matrix[9] = y; 254 matrix[10] = matrix[11] = 1; 255 } 256 257 public static int getBucketId(String path) { 258 return path.toLowerCase().hashCode(); 259 } 260 261 // Returns a (localized) string for the given duration (in seconds). 262 public static String formatDuration(final Context context, int duration) { 263 int h = duration / 3600; 264 int m = (duration - h * 3600) / 60; 265 int s = duration - (h * 3600 + m * 60); 266 String durationValue; 267 if (h == 0) { 268 durationValue = String.format(context.getString(R.string.details_ms), m, s); 269 } else { 270 durationValue = String.format(context.getString(R.string.details_hms), h, m, s); 271 } 272 return durationValue; 273 } 274 275 public static void setSpinnerVisibility(final Activity activity, 276 final boolean visible) { 277 SpinnerVisibilitySetter.getInstance(activity).setSpinnerVisibility(visible); 278 } 279 280 public static int determineTypeBits(Context context, Intent intent) { 281 int typeBits = 0; 282 String type = intent.resolveType(context); 283 284 if (MIME_TYPE_ALL.equals(type)) { 285 typeBits = DataManager.INCLUDE_ALL; 286 } else if (MIME_TYPE_IMAGE.equals(type) || 287 DIR_TYPE_IMAGE.equals(type)) { 288 typeBits = DataManager.INCLUDE_IMAGE; 289 } else if (MIME_TYPE_VIDEO.equals(type) || 290 DIR_TYPE_VIDEO.equals(type)) { 291 typeBits = DataManager.INCLUDE_VIDEO; 292 } else { 293 typeBits = DataManager.INCLUDE_ALL; 294 } 295 296 if (intent.getBooleanExtra(Intent.EXTRA_LOCAL_ONLY, false)) { 297 typeBits |= DataManager.INCLUDE_LOCAL_ONLY; 298 } 299 300 return typeBits; 301 } 302 303 public static int getSelectionModePrompt(int typeBits) { 304 if ((typeBits & DataManager.INCLUDE_VIDEO) != 0) { 305 return (typeBits & DataManager.INCLUDE_IMAGE) == 0 306 ? R.string.select_video 307 : R.string.select_item; 308 } 309 return R.string.select_image; 310 } 311 312 public static boolean hasSpaceForSize(long size) { 313 String state = Environment.getExternalStorageState(); 314 if (!Environment.MEDIA_MOUNTED.equals(state)) { 315 return false; 316 } 317 318 String path = Environment.getExternalStorageDirectory().getPath(); 319 try { 320 StatFs stat = new StatFs(path); 321 return stat.getAvailableBlocks() * (long) stat.getBlockSize() > size; 322 } catch (Exception e) { 323 Log.i(TAG, "Fail to access external storage", e); 324 } 325 return false; 326 } 327 328 public static boolean isPanorama(MediaItem item) { 329 if (item == null) return false; 330 int w = item.getWidth(); 331 int h = item.getHeight(); 332 return (h > 0 && w / h >= 2); 333 } 334 335 public static Intent getHelpIntent(int helpUrlResId, Context context) { 336 Resources res = context.getResources(); 337 String url = res.getString(helpUrlResId) 338 + "&hl=" + res.getConfiguration().locale.getLanguage(); 339 340 Intent i = new Intent(Intent.ACTION_VIEW); 341 i.setData(Uri.parse(url)); 342 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 343 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 344 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 345 return i; 346 } 347} 348