1/*
2 * Copyright (C) 2014 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.tv.settings.accounts;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.Bitmap;
22import android.net.Uri;
23import android.os.Environment;
24import android.provider.MediaStore;
25import android.util.Log;
26
27import java.io.ByteArrayOutputStream;
28import java.io.File;
29import java.io.IOException;
30import java.text.SimpleDateFormat;
31import java.util.Date;
32import java.util.Locale;
33
34/**
35 * Utilities related to loading/saving contact photos.
36 * <p>
37 * Copied from packages/apps/Contacts.
38 */
39public class ContactPhotoUtils {
40    private static final String TAG = "ContactPhotoUtils";
41
42    private static final String PHOTO_DATE_FORMAT = "'IMG'_yyyyMMdd_HHmmss";
43    private static final String NEW_PHOTO_DIR_PATH =
44            Environment.getExternalStorageDirectory() + "/DCIM/Camera";
45
46
47    /**
48     * Generate a new, unique file to be used as an out-of-band communication
49     * channel, since hi-res Bitmaps are too big to serialize into a Bundle.
50     * This file will be passed to other activities (such as the gallery/camera/cropper/etc.),
51     * and read by us once they are finished writing it.
52     */
53    public static File generateTempPhotoFile(Context context) {
54        return new File(pathForCroppedPhoto(context, generateTempPhotoFileName()));
55    }
56
57    public static String pathForCroppedPhoto(Context context, String fileName) {
58        final File dir = new File(context.getExternalCacheDir() + "/tmp");
59        dir.mkdirs();
60        final File f = new File(dir, fileName);
61        return f.getAbsolutePath();
62    }
63
64    public static String pathForNewCameraPhoto(String fileName) {
65        final File dir = new File(NEW_PHOTO_DIR_PATH);
66        dir.mkdirs();
67        final File f = new File(dir, fileName);
68        return f.getAbsolutePath();
69    }
70
71    public static String generateTempPhotoFileName() {
72        Date date = new Date(System.currentTimeMillis());
73        SimpleDateFormat dateFormat = new SimpleDateFormat(PHOTO_DATE_FORMAT, Locale.US);
74        return "ContactPhoto-" + dateFormat.format(date) + ".jpg";
75    }
76
77    /**
78     * Creates a byte[] containing the PNG-compressed bitmap, or null if
79     * something goes wrong.
80     */
81    public static byte[] compressBitmap(Bitmap bitmap) {
82        final int size = bitmap.getWidth() * bitmap.getHeight() * 4;
83        final ByteArrayOutputStream out = new ByteArrayOutputStream(size);
84        try {
85            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
86            out.flush();
87            out.close();
88            return out.toByteArray();
89        } catch (IOException e) {
90            Log.w(TAG, "Unable to serialize photo: " + e.toString());
91            return null;
92        }
93    }
94
95    /**
96     * Adds common extras to gallery intents.
97     *
98     * @param intent The intent to add extras to.
99     * @param croppedPhotoUri The uri of the file to save the image to.
100     * @param photoSize The size of the photo to scale to.
101     */
102    public static void addGalleryIntentExtras(Intent intent, Uri croppedPhotoUri, int photoSize) {
103        intent.putExtra("crop", "true");
104        intent.putExtra("scale", true);
105        intent.putExtra("scaleUpIfNeeded", true);
106        intent.putExtra("aspectX", 1);
107        intent.putExtra("aspectY", 1);
108        intent.putExtra("outputX", photoSize);
109        intent.putExtra("outputY", photoSize);
110        intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedPhotoUri);
111    }
112}
113
114
115