Home | History | Annotate | Download | only in editor
      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 android.content.ContentValues;
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.net.Uri;
     22 import android.provider.ContactsContract;
     23 import android.provider.ContactsContract.Contacts;
     24 import android.text.TextUtils;
     25 
     26 import com.android.contacts.activities.ContactEditorActivity;
     27 import com.android.contacts.activities.ContactEditorSpringBoardActivity;
     28 import com.android.contacts.model.RawContactDeltaList;
     29 import com.android.contacts.util.MaterialColorMapUtils.MaterialPalette;
     30 
     31 import java.util.ArrayList;
     32 
     33 /**
     34  * Creates Intents to edit contacts.
     35  */
     36 public class EditorIntents {
     37 
     38     private EditorIntents() {
     39     }
     40 
     41     /**
     42      * Returns an Intent to start the {@link ContactEditorSpringBoardActivity} for an
     43      * existing contact.
     44      */
     45     public static Intent createEditContactIntent(Context context, Uri uri,
     46             MaterialPalette materialPalette, long photoId) {
     47         final Intent intent = new Intent(Intent.ACTION_EDIT, uri, context,
     48                 ContactEditorSpringBoardActivity.class);
     49         putMaterialPalette(intent, materialPalette);
     50         putPhotoId(intent, photoId);
     51         return intent;
     52     }
     53 
     54     public static Intent createViewLinkedContactsIntent(Context context, Uri uri,
     55             MaterialPalette materialPalette) {
     56         final Intent intent = createEditContactIntent(context, uri, materialPalette,
     57                 /* photoId */ -1);
     58         intent.putExtra(ContactEditorSpringBoardActivity.EXTRA_SHOW_READ_ONLY, true);
     59 
     60         return intent;
     61     }
     62 
     63     /**
     64      * Returns an Intent to start the {@link ContactEditorActivity} for the given raw contact.
     65      */
     66     public static Intent createEditContactIntentForRawContact(Context context,
     67             Uri uri, long rawContactId, MaterialPalette materialPalette) {
     68         final Intent intent = new Intent(Intent.ACTION_EDIT, uri, context,
     69                 ContactEditorActivity.class);
     70         intent.putExtra(ContactEditorFragment.INTENT_EXTRA_RAW_CONTACT_ID_TO_DISPLAY_ALONE,
     71                 rawContactId);
     72         putMaterialPalette(intent, materialPalette);
     73         return intent;
     74     }
     75 
     76     /**
     77      * Returns an Intent to start the {@link ContactEditorActivity} for a new contact with
     78      * the field values specified by rawContactDeltaList pre-populate in the form.
     79      */
     80     public static Intent createInsertContactIntent(Context context,
     81             RawContactDeltaList rawContactDeltaList, String displayName, String phoneticName,
     82             /* Bundle updatedPhotos, */ boolean isNewLocalProfile) {
     83         final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI,
     84                 context, ContactEditorActivity.class);
     85         intent.putExtra(
     86                 ContactEditorFragment.INTENT_EXTRA_NEW_LOCAL_PROFILE, isNewLocalProfile);
     87         putRawContactDeltaValues(intent, rawContactDeltaList, displayName, phoneticName);
     88         return intent;
     89     }
     90 
     91     /**
     92      * Returns an Intent to edit a different raw contact in the editor with whatever
     93      * values were already entered on the current editor.
     94      */
     95     public static Intent createEditOtherRawContactIntent(Context context, Uri uri,
     96             long rawContactId, ArrayList<ContentValues> contentValues) {
     97         final Intent intent = new Intent(Intent.ACTION_EDIT, uri, context,
     98                 ContactEditorActivity.class);
     99         intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    100                 | Intent.FLAG_ACTIVITY_FORWARD_RESULT);
    101         intent.putExtra(ContactEditorFragment.INTENT_EXTRA_ADD_TO_DEFAULT_DIRECTORY, "");
    102         intent.putExtra(ContactEditorFragment.INTENT_EXTRA_RAW_CONTACT_ID_TO_DISPLAY_ALONE,
    103                 rawContactId);
    104         // Pass on all the data that has been entered so far
    105         if (contentValues != null && contentValues.size() != 0) {
    106             intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, contentValues);
    107         }
    108         return intent;
    109     }
    110 
    111     private static void putMaterialPalette(Intent intent, MaterialPalette materialPalette) {
    112         if (materialPalette != null) {
    113             intent.putExtra(
    114                     ContactEditorFragment.INTENT_EXTRA_MATERIAL_PALETTE_PRIMARY_COLOR,
    115                     materialPalette.mPrimaryColor);
    116             intent.putExtra(
    117                     ContactEditorFragment.INTENT_EXTRA_MATERIAL_PALETTE_SECONDARY_COLOR,
    118                     materialPalette.mSecondaryColor);
    119         }
    120     }
    121 
    122     private static void putPhotoId(Intent intent, long photoId) {
    123         if (photoId >= 0) {
    124             intent.putExtra(ContactEditorFragment.INTENT_EXTRA_PHOTO_ID, photoId);
    125         }
    126     }
    127 
    128     private static void putRawContactDeltaValues(Intent intent,
    129             RawContactDeltaList rawContactDeltaList, String displayName, String phoneticName) {
    130         // Pass on all the data that has been entered so far
    131         if (rawContactDeltaList != null && !rawContactDeltaList.isEmpty()) {
    132             ArrayList<ContentValues> contentValues = rawContactDeltaList.get(0).getContentValues();
    133             if (contentValues != null && contentValues.size() != 0) {
    134                 intent.putParcelableArrayListExtra(
    135                         ContactsContract.Intents.Insert.DATA, contentValues);
    136             }
    137         }
    138         // Names must be passed separately since they are skipped in RawContactModifier.parseValues
    139         if (!TextUtils.isEmpty(displayName)) {
    140             intent.putExtra(ContactsContract.Intents.Insert.NAME, displayName);
    141         }
    142         if (!TextUtils.isEmpty(phoneticName)) {
    143             intent.putExtra(ContactsContract.Intents.Insert.PHONETIC_NAME, phoneticName);
    144         }
    145     }
    146 }
    147