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