blob: 0810ab8f9672c060ae28042c7b13d0dd899063c5 [file] [log] [blame]
Chiao Chengddd4e822012-12-06 12:18:01 -08001/*
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
17package com.android.contacts.editor;
18
Gary Mai0a49afa2016-12-05 15:53:58 -080019import static android.provider.ContactsContract.CommonDataKinds.GroupMembership;
20import static android.provider.ContactsContract.CommonDataKinds.StructuredName;
21
22import static com.android.contacts.util.MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors;
23
Walter Jang2d3f31c2015-06-18 23:15:31 -070024import android.content.Context;
Walter Jang31a74ad2015-10-02 19:17:39 -070025import android.content.res.Resources;
26import android.graphics.Bitmap;
27import android.graphics.BitmapFactory;
Walter Jangf5dfea42015-09-16 12:30:36 -070028import android.graphics.drawable.Drawable;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070029import android.media.RingtoneManager;
30import android.net.Uri;
31import android.os.Build;
Walter Jangf5dfea42015-09-16 12:30:36 -070032import android.provider.ContactsContract.CommonDataKinds.Email;
33import android.provider.ContactsContract.CommonDataKinds.Event;
34import android.provider.ContactsContract.CommonDataKinds.Im;
35import android.provider.ContactsContract.CommonDataKinds.Note;
36import android.provider.ContactsContract.CommonDataKinds.Organization;
37import android.provider.ContactsContract.CommonDataKinds.Phone;
38import android.provider.ContactsContract.CommonDataKinds.Photo;
39import android.provider.ContactsContract.CommonDataKinds.Relation;
40import android.provider.ContactsContract.CommonDataKinds.SipAddress;
41import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
42import android.provider.ContactsContract.CommonDataKinds.Website;
Walter Jang2d3f31c2015-06-18 23:15:31 -070043import android.text.TextUtils;
Walter Jang31a74ad2015-10-02 19:17:39 -070044import android.widget.ImageView;
45
Gary Mai0a49afa2016-12-05 15:53:58 -080046import com.android.contacts.ContactPhotoManager;
47import com.android.contacts.ContactPhotoManager.DefaultImageProvider;
48import com.android.contacts.ContactPhotoManager.DefaultImageRequest;
49import com.android.contacts.ContactsUtils;
Chiao Chengddd4e822012-12-06 12:18:01 -080050import com.android.contacts.R;
Gary Mai69c182a2016-12-05 13:07:03 -080051import com.android.contacts.model.ValuesDelta;
Gary Mai69c182a2016-12-05 13:07:03 -080052import com.android.contacts.model.account.AccountDisplayInfo;
Gary Mai0a49afa2016-12-05 15:53:58 -080053import com.android.contacts.model.dataitem.DataKind;
Walter Jang31a74ad2015-10-02 19:17:39 -070054import com.android.contacts.util.ContactPhotoUtils;
Gary Mai0a49afa2016-12-05 15:53:58 -080055import com.android.contacts.util.MaterialColorMapUtils.MaterialPalette;
Walter Jang31a74ad2015-10-02 19:17:39 -070056import com.android.contacts.widget.QuickContactImageView;
Gary Mai0a49afa2016-12-05 15:53:58 -080057
Chiao Chengddd4e822012-12-06 12:18:01 -080058import com.google.common.collect.Maps;
59
Walter Jang31a74ad2015-10-02 19:17:39 -070060import java.io.FileNotFoundException;
Chiao Chengddd4e822012-12-06 12:18:01 -080061import java.util.HashMap;
62
63/**
64 * Utility methods for creating contact editor.
65 */
66public 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 Jang2d3f31c2015-06-18 23:15:31 -0700112
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700113
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 Jangc4cecc72015-06-25 10:45:50 -0700121 }
Walter Jangbe5e1b12015-10-17 11:38:29 -0700122 }
Walter Jangc4cecc72015-06-25 10:45:50 -0700123
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700124 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 Wang541743c2016-06-30 12:24:39 -0700132 } else {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700133 return context.getString(R.string.account_type_format,
134 displayableAccount.getTypeLabel());
Walter Jangdefc69c2015-08-25 16:46:43 -0700135 }
Walter Jang2d3f31c2015-06-18 23:15:31 -0700136 }
137
138 /**
139 * Returns a content description String for the container of the account information
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700140 * returned by {@link #getAccountTypeHeaderLabel(Context, AccountDisplayInfo)}.
Walter Jang2d3f31c2015-06-18 23:15:31 -0700141 */
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 Jangbe5e1b12015-10-17 11:38:29 -0700149 builder.append(accountName);
Walter Jang2d3f31c2015-06-18 23:15:31 -0700150 }
151 return builder.toString();
152 }
Walter Jangf5dfea42015-09-16 12:30:36 -0700153
154 /**
155 * Return an icon that represents {@param mimeType}.
156 */
157 public static Drawable getMimeTypeDrawable(Context context, String mimeType) {
158 switch (mimeType) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700159 case StructuredName.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800160 return context.getResources().getDrawable(R.drawable.quantum_ic_person_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700161 case StructuredPostal.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800162 return context.getResources().getDrawable(R.drawable.quantum_ic_place_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700163 case SipAddress.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800164 return context.getResources().getDrawable(
165 R.drawable.quantum_ic_dialer_sip_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700166 case Phone.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800167 return context.getResources().getDrawable(R.drawable.quantum_ic_phone_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700168 case Im.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800169 return context.getResources().getDrawable(
170 R.drawable.quantum_ic_message_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700171 case Event.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800172 return context.getResources().getDrawable(R.drawable.quantum_ic_event_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700173 case Email.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800174 return context.getResources().getDrawable(R.drawable.quantum_ic_email_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700175 case Website.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800176 return context.getResources().getDrawable(R.drawable.quantum_ic_public_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700177 case Photo.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800178 return context.getResources().getDrawable(
179 R.drawable.quantum_ic_camera_alt_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700180 case GroupMembership.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800181 return context.getResources().getDrawable(R.drawable.quantum_ic_label_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700182 case Organization.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800183 return context.getResources().getDrawable(
184 R.drawable.quantum_ic_business_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700185 case Note.CONTENT_ITEM_TYPE:
John Shaobd9ef3c2016-12-15 12:42:03 -0800186 return context.getResources().getDrawable(
187 R.drawable.quantum_ic_insert_comment_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700188 case Relation.CONTENT_ITEM_TYPE:
189 return context.getResources().getDrawable(
John Shaobd9ef3c2016-12-15 12:42:03 -0800190 R.drawable.quantum_ic_circles_ext_vd_theme_24);
Walter Jangf5dfea42015-09-16 12:30:36 -0700191 default:
192 return null;
193 }
194 }
Wenyi Wang3daa9a32015-09-25 09:59:06 -0700195
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 Jang31a74ad2015-10-02 19:17:39 -0700222
Walter Jang3f18d612015-10-07 16:01:05 -0700223 /** Returns the {@link Photo#PHOTO_FILE_ID} from the given ValuesDelta. */
Walter Jang31a74ad2015-10-02 19:17:39 -0700224 public static Long getPhotoFileId(ValuesDelta valuesDelta) {
225 if (valuesDelta == null) return null;
Wenyi Wang2a4848a2016-03-29 01:44:01 +0000226 if (valuesDelta.getAfter() == null || valuesDelta.getAfter().get(Photo.PHOTO) == null) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700227 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 Jang7b0970f2016-09-01 10:40:19 -0700280
Chiao Chengddd4e822012-12-06 12:18:01 -0800281}