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