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