Chiao Cheng | ddd4e82 | 2012-12-06 12:18:01 -0800 | [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 | package com.android.contacts.editor; |
| 18 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 19 | import static android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
| 20 | import static android.provider.ContactsContract.CommonDataKinds.StructuredName; |
| 21 | |
| 22 | import static com.android.contacts.util.MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors; |
| 23 | |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 25 | import android.content.res.Resources; |
| 26 | import android.graphics.Bitmap; |
| 27 | import android.graphics.BitmapFactory; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 29 | import android.media.RingtoneManager; |
| 30 | import android.net.Uri; |
| 31 | import android.os.Build; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 32 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 33 | import android.provider.ContactsContract.CommonDataKinds.Event; |
| 34 | import android.provider.ContactsContract.CommonDataKinds.Im; |
| 35 | import android.provider.ContactsContract.CommonDataKinds.Note; |
| 36 | import android.provider.ContactsContract.CommonDataKinds.Organization; |
| 37 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 38 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
| 39 | import android.provider.ContactsContract.CommonDataKinds.Relation; |
| 40 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
| 41 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
| 42 | import android.provider.ContactsContract.CommonDataKinds.Website; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 43 | import android.text.TextUtils; |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 44 | import android.widget.ImageView; |
| 45 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 46 | import com.android.contacts.ContactPhotoManager; |
| 47 | import com.android.contacts.ContactPhotoManager.DefaultImageProvider; |
| 48 | import com.android.contacts.ContactPhotoManager.DefaultImageRequest; |
| 49 | import com.android.contacts.ContactsUtils; |
Chiao Cheng | ddd4e82 | 2012-12-06 12:18:01 -0800 | [diff] [blame] | 50 | import com.android.contacts.R; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 51 | import com.android.contacts.model.ValuesDelta; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 52 | import com.android.contacts.model.account.AccountDisplayInfo; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 53 | import com.android.contacts.model.dataitem.DataKind; |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 54 | import com.android.contacts.util.ContactPhotoUtils; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 55 | import com.android.contacts.util.MaterialColorMapUtils.MaterialPalette; |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 56 | import com.android.contacts.widget.QuickContactImageView; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 57 | |
Chiao Cheng | ddd4e82 | 2012-12-06 12:18:01 -0800 | [diff] [blame] | 58 | import com.google.common.collect.Maps; |
| 59 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 60 | import java.io.FileNotFoundException; |
Chiao Cheng | ddd4e82 | 2012-12-06 12:18:01 -0800 | [diff] [blame] | 61 | import java.util.HashMap; |
| 62 | |
| 63 | /** |
| 64 | * Utility methods for creating contact editor. |
| 65 | */ |
| 66 | public class EditorUiUtils { |
| 67 | |
| 68 | // Maps DataKind.mimeType to editor view layouts. |
| 69 | private static final HashMap<String, Integer> mimetypeLayoutMap = Maps.newHashMap(); |
| 70 | static { |
| 71 | // Generally there should be a layout mapped to each existing DataKind mimetype but lots of |
| 72 | // them use the default text_fields_editor_view which we return as default so they don't |
| 73 | // need to be mapped. |
| 74 | // |
| 75 | // Other possible mime mappings are: |
| 76 | // DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME |
| 77 | // Nickname.CONTENT_ITEM_TYPE |
| 78 | // Email.CONTENT_ITEM_TYPE |
| 79 | // StructuredPostal.CONTENT_ITEM_TYPE |
| 80 | // Im.CONTENT_ITEM_TYPE |
| 81 | // Note.CONTENT_ITEM_TYPE |
| 82 | // Organization.CONTENT_ITEM_TYPE |
| 83 | // Phone.CONTENT_ITEM_TYPE |
| 84 | // SipAddress.CONTENT_ITEM_TYPE |
| 85 | // Website.CONTENT_ITEM_TYPE |
| 86 | // Relation.CONTENT_ITEM_TYPE |
| 87 | // |
| 88 | // Un-supported mime types need to mapped with -1. |
| 89 | |
| 90 | mimetypeLayoutMap.put(DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME, |
| 91 | R.layout.phonetic_name_editor_view); |
| 92 | mimetypeLayoutMap.put(StructuredName.CONTENT_ITEM_TYPE, |
| 93 | R.layout.structured_name_editor_view); |
| 94 | mimetypeLayoutMap.put(GroupMembership.CONTENT_ITEM_TYPE, -1); |
| 95 | mimetypeLayoutMap.put(Photo.CONTENT_ITEM_TYPE, -1); |
| 96 | mimetypeLayoutMap.put(Event.CONTENT_ITEM_TYPE, R.layout.event_field_editor_view); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Fetches a layout for a given mimetype. |
| 101 | * |
| 102 | * @param mimetype The mime type (e.g. StructuredName.CONTENT_ITEM_TYPE) |
| 103 | * @return The layout resource id. |
| 104 | */ |
| 105 | public static int getLayoutResourceId(String mimetype) { |
| 106 | final Integer id = mimetypeLayoutMap.get(mimetype); |
| 107 | if (id == null) { |
| 108 | return R.layout.text_fields_editor_view; |
| 109 | } |
| 110 | return id; |
| 111 | } |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 112 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 113 | |
| 114 | public static String getAccountHeaderLabelForMyProfile(Context context, |
| 115 | AccountDisplayInfo displayableAccount) { |
| 116 | if (displayableAccount.isDeviceAccount()) { |
| 117 | return context.getString(R.string.local_profile_title); |
| 118 | } else { |
| 119 | return context.getString(R.string.external_profile_title, |
| 120 | displayableAccount.getTypeLabel()); |
Walter Jang | c4cecc7 | 2015-06-25 10:45:50 -0700 | [diff] [blame] | 121 | } |
Walter Jang | be5e1b1 | 2015-10-17 11:38:29 -0700 | [diff] [blame] | 122 | } |
Walter Jang | c4cecc7 | 2015-06-25 10:45:50 -0700 | [diff] [blame] | 123 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 124 | public static String getAccountTypeHeaderLabel(Context context, AccountDisplayInfo |
| 125 | displayableAccount) { |
| 126 | if (displayableAccount.isDeviceAccount()) { |
| 127 | // Do nothing. Type label should be "Device" |
| 128 | return displayableAccount.getTypeLabel().toString(); |
| 129 | } else if (displayableAccount.isGoogleAccount()) { |
| 130 | return context.getString(R.string.google_account_type_format, |
| 131 | displayableAccount.getTypeLabel()); |
Wenyi Wang | 541743c | 2016-06-30 12:24:39 -0700 | [diff] [blame] | 132 | } else { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 133 | return context.getString(R.string.account_type_format, |
| 134 | displayableAccount.getTypeLabel()); |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 135 | } |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns a content description String for the container of the account information |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 140 | * returned by {@link #getAccountTypeHeaderLabel(Context, AccountDisplayInfo)}. |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 141 | */ |
| 142 | public static String getAccountInfoContentDescription(CharSequence accountName, |
| 143 | CharSequence accountType) { |
| 144 | final StringBuilder builder = new StringBuilder(); |
| 145 | if (!TextUtils.isEmpty(accountType)) { |
| 146 | builder.append(accountType).append('\n'); |
| 147 | } |
| 148 | if (!TextUtils.isEmpty(accountName)) { |
Walter Jang | be5e1b1 | 2015-10-17 11:38:29 -0700 | [diff] [blame] | 149 | builder.append(accountName); |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 150 | } |
| 151 | return builder.toString(); |
| 152 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Return an icon that represents {@param mimeType}. |
| 156 | */ |
| 157 | public static Drawable getMimeTypeDrawable(Context context, String mimeType) { |
| 158 | switch (mimeType) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 159 | case StructuredName.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 160 | return context.getResources().getDrawable(R.drawable.quantum_ic_person_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 161 | case StructuredPostal.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 162 | return context.getResources().getDrawable(R.drawable.quantum_ic_place_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 163 | case SipAddress.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 164 | return context.getResources().getDrawable( |
| 165 | R.drawable.quantum_ic_dialer_sip_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 166 | case Phone.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 167 | return context.getResources().getDrawable(R.drawable.quantum_ic_phone_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 168 | case Im.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 169 | return context.getResources().getDrawable( |
| 170 | R.drawable.quantum_ic_message_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 171 | case Event.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 172 | return context.getResources().getDrawable(R.drawable.quantum_ic_event_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 173 | case Email.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 174 | return context.getResources().getDrawable(R.drawable.quantum_ic_email_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 175 | case Website.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 176 | return context.getResources().getDrawable(R.drawable.quantum_ic_public_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 177 | case Photo.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 178 | return context.getResources().getDrawable( |
| 179 | R.drawable.quantum_ic_camera_alt_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 180 | case GroupMembership.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 181 | return context.getResources().getDrawable(R.drawable.quantum_ic_label_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 182 | case Organization.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 183 | return context.getResources().getDrawable( |
| 184 | R.drawable.quantum_ic_business_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 185 | case Note.CONTENT_ITEM_TYPE: |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 186 | return context.getResources().getDrawable( |
| 187 | R.drawable.quantum_ic_insert_comment_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 188 | case Relation.CONTENT_ITEM_TYPE: |
| 189 | return context.getResources().getDrawable( |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 190 | R.drawable.quantum_ic_circles_ext_vd_theme_24); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 191 | default: |
| 192 | return null; |
| 193 | } |
| 194 | } |
Wenyi Wang | 3daa9a3 | 2015-09-25 09:59:06 -0700 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Returns a ringtone string based on the ringtone URI and version #. |
| 198 | */ |
| 199 | public static String getRingtoneStringFromUri(Uri pickedUri, int currentVersion) { |
| 200 | if (isNewerThanM(currentVersion)) { |
| 201 | if (pickedUri == null) return ""; // silent ringtone |
| 202 | if (RingtoneManager.isDefault(pickedUri)) return null; // default ringtone |
| 203 | } |
| 204 | if (pickedUri == null || RingtoneManager.isDefault(pickedUri)) return null; |
| 205 | return pickedUri.toString(); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Returns a ringtone URI, based on the string and version #. |
| 210 | */ |
| 211 | public static Uri getRingtoneUriFromString(String str, int currentVersion) { |
| 212 | if (str != null) { |
| 213 | if (isNewerThanM(currentVersion) && TextUtils.isEmpty(str)) return null; |
| 214 | return Uri.parse(str); |
| 215 | } |
| 216 | return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); |
| 217 | } |
| 218 | |
| 219 | private static boolean isNewerThanM(int currentVersion) { |
| 220 | return currentVersion > Build.VERSION_CODES.M; |
| 221 | } |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 222 | |
Walter Jang | 3f18d61 | 2015-10-07 16:01:05 -0700 | [diff] [blame] | 223 | /** Returns the {@link Photo#PHOTO_FILE_ID} from the given ValuesDelta. */ |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 224 | public static Long getPhotoFileId(ValuesDelta valuesDelta) { |
| 225 | if (valuesDelta == null) return null; |
Wenyi Wang | 2a4848a | 2016-03-29 01:44:01 +0000 | [diff] [blame] | 226 | if (valuesDelta.getAfter() == null || valuesDelta.getAfter().get(Photo.PHOTO) == null) { |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 227 | return valuesDelta.getAsLong(Photo.PHOTO_FILE_ID); |
| 228 | } |
| 229 | return null; |
| 230 | } |
| 231 | |
| 232 | /** Binds the full resolution image at the given Uri to the provided ImageView. */ |
| 233 | static void loadPhoto(ContactPhotoManager contactPhotoManager, ImageView imageView, |
| 234 | Uri photoUri) { |
| 235 | final DefaultImageProvider fallbackToPreviousImage = new DefaultImageProvider() { |
| 236 | @Override |
| 237 | public void applyDefaultImage(ImageView view, int extent, boolean darkTheme, |
| 238 | DefaultImageRequest defaultImageRequest) { |
| 239 | // Before we finish setting the full sized image, don't change the current |
| 240 | // image that is set in any way. |
| 241 | } |
| 242 | }; |
| 243 | contactPhotoManager.loadPhoto(imageView, photoUri, imageView.getWidth(), |
| 244 | /* darkTheme =*/ false, /* isCircular =*/ false, |
| 245 | /* defaultImageRequest =*/ null, fallbackToPreviousImage); |
| 246 | } |
| 247 | |
| 248 | /** Decodes the Bitmap from the photo bytes from the given ValuesDelta. */ |
| 249 | public static Bitmap getPhotoBitmap(ValuesDelta valuesDelta) { |
| 250 | if (valuesDelta == null) return null; |
| 251 | final byte[] bytes = valuesDelta.getAsByteArray(Photo.PHOTO); |
| 252 | if (bytes == null) return null; |
| 253 | return BitmapFactory.decodeByteArray(bytes, /* offset =*/ 0, bytes.length); |
| 254 | } |
| 255 | |
| 256 | /** Binds the default avatar to the given ImageView and tints it to match QuickContacts. */ |
| 257 | public static void setDefaultPhoto(ImageView imageView , Resources resources, |
| 258 | MaterialPalette materialPalette) { |
| 259 | // Use the default avatar drawable |
| 260 | imageView.setImageDrawable(ContactPhotoManager.getDefaultAvatarDrawableForContact( |
| 261 | resources, /* hires =*/ false, /* defaultImageRequest =*/ null)); |
| 262 | |
| 263 | // Tint it to match the quick contacts |
| 264 | if (imageView instanceof QuickContactImageView) { |
| 265 | ((QuickContactImageView) imageView).setTint(materialPalette == null |
| 266 | ? getDefaultPrimaryAndSecondaryColors(resources).mPrimaryColor |
| 267 | : materialPalette.mPrimaryColor); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** Returns compressed bitmap bytes from the given Uri, scaled to the thumbnail dimensions. */ |
| 272 | public static byte[] getCompressedThumbnailBitmapBytes(Context context, Uri uri) |
| 273 | throws FileNotFoundException { |
| 274 | final Bitmap bitmap = ContactPhotoUtils.getBitmapFromUri(context, uri); |
| 275 | final int size = ContactsUtils.getThumbnailSize(context); |
| 276 | final Bitmap bitmapScaled = Bitmap.createScaledBitmap( |
| 277 | bitmap, size, size, /* filter =*/ false); |
| 278 | return ContactPhotoUtils.compressBitmap(bitmapScaled); |
| 279 | } |
Walter Jang | 7b0970f | 2016-09-01 10:40:19 -0700 | [diff] [blame] | 280 | |
Chiao Cheng | ddd4e82 | 2012-12-06 12:18:01 -0800 | [diff] [blame] | 281 | } |