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