1 /* 2 * Copyright (C) 2015 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 package com.android.contacts.editor; 17 18 import com.android.contacts.activities.CompactContactEditorActivity; 19 import com.android.contacts.activities.ContactEditorActivity; 20 import com.android.contacts.activities.ContactEditorBaseActivity; 21 import com.android.contacts.common.model.RawContactDeltaList; 22 import com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette; 23 24 import android.content.ContentValues; 25 import android.content.Intent; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.provider.ContactsContract; 29 import android.provider.ContactsContract.Contacts; 30 import android.text.TextUtils; 31 32 import java.util.ArrayList; 33 34 /** 35 * Creates Intents to edit contacts. 36 */ 37 public class EditorIntents { 38 39 private EditorIntents() { 40 } 41 42 /** 43 * Returns an Intent to start the {@link CompactContactEditorActivity} for an 44 * existing contact. 45 */ 46 public static Intent createCompactEditContactIntent(Uri contactLookupUri, 47 MaterialPalette materialPalette, Bundle updatedPhotos, long photoId, long nameId) { 48 final Intent intent = new Intent(Intent.ACTION_EDIT, contactLookupUri); 49 putMaterialPalette(intent, materialPalette); 50 putUpdatedPhotos(intent, updatedPhotos); 51 putPhotoId(intent, photoId); 52 putNameId(intent, nameId); 53 return intent; 54 } 55 56 /** 57 * Returns an Intent to start the {@link CompactContactEditorActivity} for a new contact. 58 */ 59 public static Intent createCompactInsertContactIntent() { 60 return createCompactInsertContactIntent(/* rawContactDeltaList =*/ null, 61 /* displayName =*/ null, /* phoneticName =*/ null, /* updatedPhotos =*/ null); 62 } 63 64 /** 65 * Returns an Intent to start the {@link CompactContactEditorActivity} for a new contact with 66 * the field values specified by rawContactDeltaList pre-populate in the form. 67 */ 68 public static Intent createCompactInsertContactIntent(RawContactDeltaList rawContactDeltaList, 69 String displayName, String phoneticName, Bundle updatedPhotos) { 70 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI); 71 if (rawContactDeltaList != null || displayName != null || phoneticName != null) { 72 putRawContactDeltaValues(intent, rawContactDeltaList, displayName, phoneticName); 73 } 74 putUpdatedPhotos(intent, updatedPhotos); 75 return intent; 76 } 77 78 /** 79 * Returns an Intent to edit a different contact (in the fully expaned editor) with whatever 80 * values were already entered on the currently displayed contact editor. 81 */ 82 public static Intent createEditOtherContactIntent(Uri contactLookupUri, 83 ArrayList<ContentValues> contentValues) { 84 final Intent intent = new Intent(Intent.ACTION_EDIT, contactLookupUri); 85 intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 86 | Intent.FLAG_ACTIVITY_FORWARD_RESULT); 87 intent.putExtra(ContactEditorFragment.INTENT_EXTRA_ADD_TO_DEFAULT_DIRECTORY, ""); 88 89 // Pass on all the data that has been entered so far 90 if (contentValues != null && contentValues.size() != 0) { 91 intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, contentValues); 92 } 93 return intent; 94 } 95 96 /** 97 * Returns an Intent to start the fully expanded {@link ContactEditorActivity} for a 98 * new contact. 99 */ 100 public static Intent createEditContactIntent(Uri contactLookupUri, 101 MaterialPalette materialPalette, long photoId, long nameId) { 102 final Intent intent = new Intent(ContactEditorBaseActivity.ACTION_EDIT, contactLookupUri); 103 addContactIntentFlags(intent); 104 putMaterialPalette(intent, materialPalette); 105 putPhotoId(intent, photoId); 106 putNameId(intent, nameId); 107 return intent; 108 } 109 110 /** 111 * Returns an Intent to start the fully expanded {@link ContactEditorActivity} for an 112 * existing contact. 113 */ 114 public static Intent createInsertContactIntent(RawContactDeltaList rawContactDeltaList, 115 String displayName, String phoneticName, Bundle updatedPhotos) { 116 final Intent intent = new Intent(ContactEditorBaseActivity.ACTION_INSERT, 117 Contacts.CONTENT_URI); 118 addContactIntentFlags(intent); 119 putRawContactDeltaValues(intent, rawContactDeltaList, displayName, phoneticName); 120 putUpdatedPhotos(intent, updatedPhotos); 121 return intent; 122 } 123 124 125 private static void addContactIntentFlags(Intent intent) { 126 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 127 | Intent.FLAG_ACTIVITY_FORWARD_RESULT); 128 } 129 130 private static void putMaterialPalette(Intent intent, MaterialPalette materialPalette) { 131 if (materialPalette != null) { 132 intent.putExtra(ContactEditorBaseFragment.INTENT_EXTRA_MATERIAL_PALETTE_PRIMARY_COLOR, 133 materialPalette.mPrimaryColor); 134 intent.putExtra(ContactEditorBaseFragment.INTENT_EXTRA_MATERIAL_PALETTE_SECONDARY_COLOR, 135 materialPalette.mSecondaryColor); 136 } 137 } 138 139 private static void putUpdatedPhotos(Intent intent, Bundle updatedPhotos) { 140 if (updatedPhotos != null && !updatedPhotos.isEmpty()) { 141 intent.putExtra(ContactEditorBaseFragment.INTENT_EXTRA_UPDATED_PHOTOS, updatedPhotos); 142 } 143 } 144 145 private static void putPhotoId(Intent intent, long photoId) { 146 if (photoId >= 0) { 147 intent.putExtra(ContactEditorBaseFragment.INTENT_EXTRA_PHOTO_ID, photoId); 148 } 149 } 150 151 private static void putNameId(Intent intent, long nameId) { 152 if (nameId >= 0) { 153 intent.putExtra(ContactEditorBaseFragment.INTENT_EXTRA_NAME_ID, nameId); 154 } 155 } 156 157 private static void putRawContactDeltaValues(Intent intent, 158 RawContactDeltaList rawContactDeltaList, String displayName, String phoneticName) { 159 // Pass on all the data that has been entered so far 160 if (rawContactDeltaList != null && !rawContactDeltaList.isEmpty()) { 161 ArrayList<ContentValues> contentValues = rawContactDeltaList.get(0).getContentValues(); 162 if (contentValues != null && contentValues.size() != 0) { 163 intent.putParcelableArrayListExtra( 164 ContactsContract.Intents.Insert.DATA, contentValues); 165 } 166 } 167 // Names must be passed separately since they are skipped in RawContactModifier.parseValues 168 if (!TextUtils.isEmpty(displayName)) { 169 intent.putExtra(ContactsContract.Intents.Insert.NAME, displayName); 170 } 171 if (!TextUtils.isEmpty(phoneticName)) { 172 intent.putExtra(ContactsContract.Intents.Insert.PHONETIC_NAME, phoneticName); 173 } 174 } 175 } 176