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