GalleryUtils.java revision 402237b43fd853377e5dba4e048839ad23b99d72
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 void startCameraActivity(Context context) { 210 Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA) 211 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 212 | Intent.FLAG_ACTIVITY_NEW_TASK); 213 context.startActivity(intent); 214 } 215 216 public static boolean isValidLocation(double latitude, double longitude) { 217 // TODO: change || to && after we fix the default location issue 218 return (latitude != MediaItem.INVALID_LATLNG || longitude != MediaItem.INVALID_LATLNG); 219 } 220 221 public static String formatLatitudeLongitude(String format, double latitude, 222 double longitude) { 223 // We need to specify the locale otherwise it may go wrong in some language 224 // (e.g. Locale.FRENCH) 225 return String.format(Locale.ENGLISH, format, latitude, longitude); 226 } 227 228 public static void showOnMap(Context context, double latitude, double longitude) { 229 try { 230 // We don't use "geo:latitude,longitude" because it only centers 231 // the MapView to the specified location, but we need a marker 232 // for further operations (routing to/from). 233 // The q=(lat, lng) syntax is suggested by geo-team. 234 String uri = formatLatitudeLongitude("http://maps.google.com/maps?f=q&q=(%f,%f)", 235 latitude, longitude); 236 ComponentName compName = new ComponentName(MAPS_PACKAGE_NAME, 237 MAPS_CLASS_NAME); 238 Intent mapsIntent = new Intent(Intent.ACTION_VIEW, 239 Uri.parse(uri)).setComponent(compName); 240 context.startActivity(mapsIntent); 241 } catch (ActivityNotFoundException e) { 242 // Use the "geo intent" if no GMM is installed 243 Log.e(TAG, "GMM activity not found!", e); 244 String url = formatLatitudeLongitude("geo:%f,%f", latitude, longitude); 245 Intent mapsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 246 context.startActivity(mapsIntent); 247 } 248 } 249 250 public static void setViewPointMatrix( 251 float matrix[], float x, float y, float z) { 252 // The matrix is 253 // -z, 0, x, 0 254 // 0, -z, y, 0 255 // 0, 0, 1, 0 256 // 0, 0, 1, -z 257 Arrays.fill(matrix, 0, 16, 0); 258 matrix[0] = matrix[5] = matrix[15] = -z; 259 matrix[8] = x; 260 matrix[9] = y; 261 matrix[10] = matrix[11] = 1; 262 } 263 264 public static int getBucketId(String path) { 265 return path.toLowerCase().hashCode(); 266 } 267 268 // Returns a (localized) string for the given duration (in seconds). 269 public static String formatDuration(final Context context, int duration) { 270 int h = duration / 3600; 271 int m = (duration - h * 3600) / 60; 272 int s = duration - (h * 3600 + m * 60); 273 String durationValue; 274 if (h == 0) { 275 durationValue = String.format(context.getString(R.string.details_ms), m, s); 276 } else { 277 durationValue = String.format(context.getString(R.string.details_hms), h, m, s); 278 } 279 return durationValue; 280 } 281 282 public static void setSpinnerVisibility(final Activity activity, 283 final boolean visible) { 284 SpinnerVisibilitySetter.getInstance(activity).setSpinnerVisibility(visible); 285 } 286 287 public static int determineTypeBits(Context context, Intent intent) { 288 int typeBits = 0; 289 String type = intent.resolveType(context); 290 291 if (MIME_TYPE_ALL.equals(type)) { 292 typeBits = DataManager.INCLUDE_ALL; 293 } else if (MIME_TYPE_IMAGE.equals(type) || 294 DIR_TYPE_IMAGE.equals(type)) { 295 typeBits = DataManager.INCLUDE_IMAGE; 296 } else if (MIME_TYPE_VIDEO.equals(type) || 297 DIR_TYPE_VIDEO.equals(type)) { 298 typeBits = DataManager.INCLUDE_VIDEO; 299 } else { 300 typeBits = DataManager.INCLUDE_ALL; 301 } 302 303 if (intent.getBooleanExtra(Intent.EXTRA_LOCAL_ONLY, false)) { 304 typeBits |= DataManager.INCLUDE_LOCAL_ONLY; 305 } 306 307 return typeBits; 308 } 309 310 public static int getSelectionModePrompt(int typeBits) { 311 if ((typeBits & DataManager.INCLUDE_VIDEO) != 0) { 312 return (typeBits & DataManager.INCLUDE_IMAGE) == 0 313 ? R.string.select_video 314 : R.string.select_item; 315 } 316 return R.string.select_image; 317 } 318 319 public static boolean hasSpaceForSize(long size) { 320 String state = Environment.getExternalStorageState(); 321 if (!Environment.MEDIA_MOUNTED.equals(state)) { 322 return false; 323 } 324 325 String path = Environment.getExternalStorageDirectory().getPath(); 326 try { 327 StatFs stat = new StatFs(path); 328 return stat.getAvailableBlocks() * (long) stat.getBlockSize() > size; 329 } catch (Exception e) { 330 Log.i(TAG, "Fail to access external storage", e); 331 } 332 return false; 333 } 334 335 public static boolean isPanorama(MediaItem item) { 336 if (item == null) return false; 337 int w = item.getWidth(); 338 int h = item.getHeight(); 339 return (h > 0 && w / h >= 2); 340 } 341 342 public static Intent getHelpIntent(int helpUrlResId, Context context) { 343 Resources res = context.getResources(); 344 String url = res.getString(helpUrlResId) 345 + "&hl=" + res.getConfiguration().locale.getLanguage(); 346 347 Intent i = new Intent(Intent.ACTION_VIEW); 348 i.setData(Uri.parse(url)); 349 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 350 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 351 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 352 return i; 353 } 354} 355