Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 18 | package com.android.contacts.util; |
| 19 | |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 20 | import android.content.ClipData; |
Josh Gargus | ebc1792 | 2012-05-04 18:47:09 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Chiao Cheng | 61414c2 | 2012-06-18 16:59:06 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 24 | import android.graphics.BitmapFactory; |
Chiao Cheng | 61414c2 | 2012-06-18 16:59:06 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
Chiao Cheng | 61414c2 | 2012-06-18 16:59:06 -0700 | [diff] [blame] | 26 | import android.provider.MediaStore; |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 27 | import android.support.v4.content.FileProvider; |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | |
Paul Soulos | fc98ff9 | 2014-09-12 10:45:43 -0700 | [diff] [blame] | 30 | import com.android.contacts.R; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 31 | |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 32 | import com.google.common.io.Closeables; |
| 33 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 34 | import java.io.ByteArrayOutputStream; |
| 35 | import java.io.File; |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 36 | import java.io.FileNotFoundException; |
| 37 | import java.io.FileOutputStream; |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 38 | import java.io.IOException; |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 39 | import java.io.InputStream; |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 40 | import java.text.SimpleDateFormat; |
| 41 | import java.util.Date; |
Johan Redestig | 9bcfc32 | 2012-10-05 08:23:51 +0200 | [diff] [blame] | 42 | import java.util.Locale; |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Utilities related to loading/saving contact photos. |
| 46 | * |
| 47 | */ |
| 48 | public class ContactPhotoUtils { |
| 49 | private static final String TAG = "ContactPhotoUtils"; |
| 50 | |
| 51 | private static final String PHOTO_DATE_FORMAT = "'IMG'_yyyyMMdd_HHmmss"; |
| 52 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Generate a new, unique file to be used as an out-of-band communication |
| 55 | * channel, since hi-res Bitmaps are too big to serialize into a Bundle. |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 56 | * This file will be passed (as a uri) to other activities (such as the gallery/camera/ |
| 57 | * cropper/etc.), and read by us once they are finished writing it. |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 58 | */ |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 59 | public static Uri generateTempImageUri(Context context) { |
Paul Soulos | fc98ff9 | 2014-09-12 10:45:43 -0700 | [diff] [blame] | 60 | final String fileProviderAuthority = context.getResources().getString( |
| 61 | R.string.photo_file_provider_authority); |
| 62 | return FileProvider.getUriForFile(context, fileProviderAuthority, |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 63 | new File(pathForTempPhoto(context, generateTempPhotoFileName()))); |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 66 | public static Uri generateTempCroppedImageUri(Context context) { |
Paul Soulos | fc98ff9 | 2014-09-12 10:45:43 -0700 | [diff] [blame] | 67 | final String fileProviderAuthority = context.getResources().getString( |
| 68 | R.string.photo_file_provider_authority); |
| 69 | return FileProvider.getUriForFile(context, fileProviderAuthority, |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 70 | new File(pathForTempPhoto(context, generateTempCroppedPhotoFileName()))); |
| 71 | } |
| 72 | |
| 73 | private static String pathForTempPhoto(Context context, String fileName) { |
| 74 | final File dir = context.getCacheDir(); |
Josh Gargus | ebc1792 | 2012-05-04 18:47:09 -0700 | [diff] [blame] | 75 | dir.mkdirs(); |
| 76 | final File f = new File(dir, fileName); |
| 77 | return f.getAbsolutePath(); |
| 78 | } |
| 79 | |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 80 | private static String generateTempPhotoFileName() { |
| 81 | final Date date = new Date(System.currentTimeMillis()); |
Johan Redestig | 9bcfc32 | 2012-10-05 08:23:51 +0200 | [diff] [blame] | 82 | SimpleDateFormat dateFormat = new SimpleDateFormat(PHOTO_DATE_FORMAT, Locale.US); |
Josh Gargus | ebc1792 | 2012-05-04 18:47:09 -0700 | [diff] [blame] | 83 | return "ContactPhoto-" + dateFormat.format(date) + ".jpg"; |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 86 | private static String generateTempCroppedPhotoFileName() { |
| 87 | final Date date = new Date(System.currentTimeMillis()); |
| 88 | SimpleDateFormat dateFormat = new SimpleDateFormat(PHOTO_DATE_FORMAT, Locale.US); |
| 89 | return "ContactPhoto-" + dateFormat.format(date) + "-cropped.jpg"; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Given a uri pointing to a bitmap, reads it into a bitmap and returns it. |
| 94 | * @throws FileNotFoundException |
| 95 | */ |
| 96 | public static Bitmap getBitmapFromUri(Context context, Uri uri) throws FileNotFoundException { |
| 97 | final InputStream imageStream = context.getContentResolver().openInputStream(uri); |
| 98 | try { |
| 99 | return BitmapFactory.decodeStream(imageStream); |
| 100 | } finally { |
| 101 | Closeables.closeQuietly(imageStream); |
| 102 | } |
| 103 | } |
| 104 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Creates a byte[] containing the PNG-compressed bitmap, or null if |
| 107 | * something goes wrong. |
| 108 | */ |
| 109 | public static byte[] compressBitmap(Bitmap bitmap) { |
| 110 | final int size = bitmap.getWidth() * bitmap.getHeight() * 4; |
| 111 | final ByteArrayOutputStream out = new ByteArrayOutputStream(size); |
| 112 | try { |
| 113 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); |
| 114 | out.flush(); |
| 115 | out.close(); |
| 116 | return out.toByteArray(); |
| 117 | } catch (IOException e) { |
| 118 | Log.w(TAG, "Unable to serialize photo: " + e.toString()); |
| 119 | return null; |
| 120 | } |
| 121 | } |
Chiao Cheng | 61414c2 | 2012-06-18 16:59:06 -0700 | [diff] [blame] | 122 | |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 123 | public static void addCropExtras(Intent intent, int photoSize) { |
Chiao Cheng | 61414c2 | 2012-06-18 16:59:06 -0700 | [diff] [blame] | 124 | intent.putExtra("crop", "true"); |
| 125 | intent.putExtra("scale", true); |
| 126 | intent.putExtra("scaleUpIfNeeded", true); |
| 127 | intent.putExtra("aspectX", 1); |
| 128 | intent.putExtra("aspectY", 1); |
| 129 | intent.putExtra("outputX", photoSize); |
| 130 | intent.putExtra("outputY", photoSize); |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Adds common extras to gallery intents. |
| 135 | * |
| 136 | * @param intent The intent to add extras to. |
| 137 | * @param photoUri The uri of the file to save the image to. |
| 138 | */ |
| 139 | public static void addPhotoPickerExtras(Intent intent, Uri photoUri) { |
| 140 | intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); |
| 141 | intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | |
| 142 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 143 | intent.setClipData(ClipData.newRawUri(MediaStore.EXTRA_OUTPUT, photoUri)); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Given an input photo stored in a uri, save it to a destination uri |
| 148 | */ |
| 149 | public static boolean savePhotoFromUriToUri(Context context, Uri inputUri, Uri outputUri, |
| 150 | boolean deleteAfterSave) { |
Jay Shrauner | a943371 | 2015-02-27 15:47:09 -0800 | [diff] [blame] | 151 | if (inputUri == null || outputUri == null) { |
| 152 | return false; |
| 153 | } |
Paul Duffin | f43973f | 2015-05-27 13:33:10 +0100 | [diff] [blame] | 154 | try (FileOutputStream outputStream = context.getContentResolver() |
| 155 | .openAssetFileDescriptor(outputUri, "rw").createOutputStream(); |
| 156 | InputStream inputStream = context.getContentResolver().openInputStream(inputUri)) { |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 157 | |
| 158 | final byte[] buffer = new byte[16 * 1024]; |
| 159 | int length; |
| 160 | int totalLength = 0; |
| 161 | while ((length = inputStream.read(buffer)) > 0) { |
| 162 | outputStream.write(buffer, 0, length); |
| 163 | totalLength += length; |
| 164 | } |
| 165 | Log.v(TAG, "Wrote " + totalLength + " bytes for photo " + inputUri.toString()); |
Jay Shrauner | b716f50 | 2015-01-14 16:11:08 -0800 | [diff] [blame] | 166 | } catch (IOException | NullPointerException e) { |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 167 | Log.e(TAG, "Failed to write photo: " + inputUri.toString() + " because: " + e); |
| 168 | return false; |
| 169 | } finally { |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 170 | if (deleteAfterSave) { |
| 171 | context.getContentResolver().delete(inputUri, null, null); |
| 172 | } |
| 173 | } |
| 174 | return true; |
Chiao Cheng | 61414c2 | 2012-06-18 16:59:06 -0700 | [diff] [blame] | 175 | } |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 176 | } |