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 19 import static android.provider.ContactsContract.CommonDataKinds.Event; 20 import static android.provider.ContactsContract.CommonDataKinds.GroupMembership; 21 import static android.provider.ContactsContract.CommonDataKinds.Photo; 22 import static android.provider.ContactsContract.CommonDataKinds.StructuredName; 23 24 import android.content.Context; 25 import android.text.TextUtils; 26 import android.util.Pair; 27 import com.android.contacts.R; 28 import com.android.contacts.common.model.account.AccountType; 29 import com.android.contacts.common.model.account.GoogleAccountType; 30 import com.android.contacts.common.model.dataitem.DataKind; 31 import com.google.common.collect.Maps; 32 33 import java.util.HashMap; 34 35 /** 36 * Utility methods for creating contact editor. 37 */ 38 public class EditorUiUtils { 39 40 // Maps DataKind.mimeType to editor view layouts. 41 private static final HashMap<String, Integer> mimetypeLayoutMap = Maps.newHashMap(); 42 static { 43 // Generally there should be a layout mapped to each existing DataKind mimetype but lots of 44 // them use the default text_fields_editor_view which we return as default so they don't 45 // need to be mapped. 46 // 47 // Other possible mime mappings are: 48 // DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME 49 // Nickname.CONTENT_ITEM_TYPE 50 // Email.CONTENT_ITEM_TYPE 51 // StructuredPostal.CONTENT_ITEM_TYPE 52 // Im.CONTENT_ITEM_TYPE 53 // Note.CONTENT_ITEM_TYPE 54 // Organization.CONTENT_ITEM_TYPE 55 // Phone.CONTENT_ITEM_TYPE 56 // SipAddress.CONTENT_ITEM_TYPE 57 // Website.CONTENT_ITEM_TYPE 58 // Relation.CONTENT_ITEM_TYPE 59 // 60 // Un-supported mime types need to mapped with -1. 61 62 mimetypeLayoutMap.put(DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME, 63 R.layout.phonetic_name_editor_view); 64 mimetypeLayoutMap.put(StructuredName.CONTENT_ITEM_TYPE, 65 R.layout.structured_name_editor_view); 66 mimetypeLayoutMap.put(GroupMembership.CONTENT_ITEM_TYPE, -1); 67 mimetypeLayoutMap.put(Photo.CONTENT_ITEM_TYPE, -1); 68 mimetypeLayoutMap.put(Event.CONTENT_ITEM_TYPE, R.layout.event_field_editor_view); 69 } 70 71 /** 72 * Fetches a layout for a given mimetype. 73 * 74 * @param mimetype The mime type (e.g. StructuredName.CONTENT_ITEM_TYPE) 75 * @return The layout resource id. 76 */ 77 public static int getLayoutResourceId(String mimetype) { 78 final Integer id = mimetypeLayoutMap.get(mimetype); 79 if (id == null) { 80 return R.layout.text_fields_editor_view; 81 } 82 return id; 83 } 84 85 /** 86 * Returns a Pair of the account name and type to display for the given arguments or null 87 * in no account information should be displayed. The account name may also be null. 88 */ 89 public static Pair<String,String> getAccountInfo(Context context, boolean isProfile, 90 String accountName, AccountType accountType) { 91 CharSequence accountTypeDisplayLabel = accountType.getDisplayLabel(context); 92 93 if (isProfile) { 94 if (TextUtils.isEmpty(accountName)) { 95 return new Pair<>( 96 /* accountName =*/ null, 97 context.getString(R.string.local_profile_title)); 98 } 99 return new Pair<>( 100 accountName, 101 context.getString(R.string.external_profile_title, accountTypeDisplayLabel)); 102 } 103 if (!TextUtils.isEmpty(accountName)) { 104 final String accountNameDisplayLabel = 105 context.getString(R.string.from_account_format, accountName); 106 107 if (TextUtils.isEmpty(accountTypeDisplayLabel)) { 108 accountTypeDisplayLabel = context.getString(R.string.account_phone); 109 } 110 111 if (GoogleAccountType.ACCOUNT_TYPE.equals(accountType.accountType) 112 && accountType.dataSet == null) { 113 return new Pair<>( 114 accountNameDisplayLabel, 115 context.getString(R.string.google_account_type_format, 116 accountTypeDisplayLabel)); 117 } 118 return new Pair<>( 119 accountNameDisplayLabel, 120 context.getString(R.string.account_type_format, accountTypeDisplayLabel)); 121 } 122 return null; 123 } 124 125 /** 126 * Returns a content description String for the container of the account information 127 * returned by {@link #getAccountInfo}. 128 */ 129 public static String getAccountInfoContentDescription(CharSequence accountName, 130 CharSequence accountType) { 131 final StringBuilder builder = new StringBuilder(); 132 if (!TextUtils.isEmpty(accountType)) { 133 builder.append(accountType).append('\n'); 134 } 135 if (!TextUtils.isEmpty(accountName)) { 136 builder.append(accountName).append('\n'); 137 } 138 return builder.toString(); 139 } 140 } 141