Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.payments; | 5 package org.chromium.chrome.browser.payments; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.text.TextUtils; | 8 import android.text.TextUtils; |
| 9 | 9 |
| 10 import org.chromium.chrome.R; | 10 import org.chromium.chrome.R; |
| 11 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; | 11 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
| 12 import org.chromium.chrome.browser.payments.ui.PaymentOption; | 12 import org.chromium.chrome.browser.payments.ui.PaymentOption; |
| 13 | 13 |
| 14 import javax.annotation.Nullable; | 14 import javax.annotation.Nullable; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * The locally stored contact details. | 17 * The locally stored contact details. |
| 18 */ | 18 */ |
| 19 public class AutofillContact extends PaymentOption { | 19 public class AutofillContact extends PaymentOption { |
| 20 private final AutofillProfile mProfile; | 20 private final AutofillProfile mProfile; |
| 21 private final Context mContext; | 21 private final Context mContext; |
| 22 private int mCompletionStatus; | |
| 23 private boolean mRequestName; | |
| 24 private boolean mRequestPhone; | |
| 25 private boolean mRequestEmail; | |
| 22 @Nullable private String mPayerName; | 26 @Nullable private String mPayerName; |
| 23 @Nullable private String mPayerPhone; | 27 @Nullable private String mPayerPhone; |
| 24 @Nullable private String mPayerEmail; | 28 @Nullable private String mPayerEmail; |
| 25 | 29 |
| 26 /** | 30 /** |
| 27 * Builds contact details. | 31 * Builds contact details. |
| 28 * | 32 * |
| 29 * @param context The application context. | 33 * @param context The application context. |
| 30 * @param profile The autofill profile where this contact data live s. | 34 * @param profile The autofill profile where this contact data live s. |
| 31 * @param name The payer name. If not empty, this will be the pr imary label. | 35 * @param name The payer name. If not empty, this will be the pr imary label. |
| 32 * @param phone The phone number. If name is empty, this will be the primary label. | 36 * @param phone The phone number. If name is empty, this will be the primary label. |
| 33 * @param email The email address. If name and phone are empty, t his will be the | 37 * @param email The email address. If name and phone are empty, t his will be the |
| 34 * primary label. | 38 * primary label. |
| 35 * @param completionStatus The completion status of this contact. | 39 * @param completionStatus The completion status of this contact. |
| 40 * @param requestName Whether the merchant requests a payer name. | |
| 41 * @param requestPhone Whether the merchant requests a payer phone numbe r. | |
| 42 * @param requestEmail Whether the merchant requests a payer email addre ss. | |
| 36 */ | 43 */ |
| 37 public AutofillContact(Context context, AutofillProfile profile, @Nullable S tring name, | 44 public AutofillContact(Context context, AutofillProfile profile, @Nullable S tring name, |
| 38 @Nullable String phone, @Nullable String email, | 45 @Nullable String phone, @Nullable String email, |
| 39 @ContactEditor.CompletionStatus int completionStatus) { | 46 @ContactEditor.CompletionStatus int completionStatus, boolean reques tName, |
| 47 boolean requestPhone, boolean requestEmail) { | |
| 40 super(profile.getGUID(), null, null, null, null); | 48 super(profile.getGUID(), null, null, null, null); |
| 41 mContext = context; | 49 mContext = context; |
| 42 mProfile = profile; | 50 mProfile = profile; |
| 51 mRequestName = requestName; | |
| 52 mRequestPhone = requestPhone; | |
| 53 mRequestEmail = requestEmail; | |
| 43 mIsEditable = true; | 54 mIsEditable = true; |
| 44 setContactInfo(profile.getGUID(), name, phone, email); | 55 setContactInfo(profile.getGUID(), name, phone, email); |
| 45 updateCompletionStatus(completionStatus); | 56 updateCompletionStatus(completionStatus); |
| 46 } | 57 } |
| 47 | 58 |
| 48 /** @return Payer name. Null if the merchant did not request it or data is i ncomplete. */ | 59 /** @return Payer name. Null if the merchant did not request it or data is i ncomplete. */ |
| 49 @Nullable public String getPayerName() { | 60 @Nullable public String getPayerName() { |
| 50 return mPayerName; | 61 return mPayerName; |
| 51 } | 62 } |
| 52 | 63 |
| 53 /** @return Email address. Null if the merchant did not request it or data i s incomplete. */ | |
| 54 @Nullable public String getPayerEmail() { | |
| 55 return mPayerEmail; | |
| 56 } | |
| 57 | |
| 58 /** @return Phone number. Null if the merchant did not request it or data is incomplete. */ | 64 /** @return Phone number. Null if the merchant did not request it or data is incomplete. */ |
| 59 @Nullable public String getPayerPhone() { | 65 @Nullable public String getPayerPhone() { |
| 60 return mPayerPhone; | 66 return mPayerPhone; |
| 61 } | 67 } |
| 62 | 68 |
| 69 /** @return Email address. Null if the merchant did not request it or data i s incomplete. */ | |
| 70 @Nullable | |
| 71 public String getPayerEmail() { | |
| 72 return mPayerEmail; | |
| 73 } | |
| 74 | |
| 63 /** @return The autofill profile where this contact data lives. */ | 75 /** @return The autofill profile where this contact data lives. */ |
| 64 public AutofillProfile getProfile() { | 76 public AutofillProfile getProfile() { |
| 65 return mProfile; | 77 return mProfile; |
| 66 } | 78 } |
| 67 | 79 |
| 68 /** @return Whether the contact is complete and ready to be sent to the merc hant as-is. */ | 80 /** @return Whether the contact is complete and ready to be sent to the merc hant as-is. */ |
| 69 public boolean isComplete() { | 81 public boolean isComplete() { |
| 70 return mIsComplete; | 82 return mIsComplete; |
| 71 } | 83 } |
| 72 | 84 |
| 73 /** | 85 /** |
| 74 * Updates the profile guid, payer name, email address, and phone number and marks this | 86 * Updates the profile guid, payer name, email address, and phone number and marks this |
| 75 * information "complete." Called after the user has edited this contact inf ormation. | 87 * information "complete." Called after the user has edited this contact inf ormation. |
| 76 * Update the identifier, label, sublabel, and tertiarylabel. | 88 * Update the identifier, label, sublabel, and tertiarylabel. |
| 77 * | 89 * |
| 78 * @param guid The new identifier to use. Should not be null or empty. | 90 * @param guid The new identifier to use. Should not be null or empty. |
| 79 * @param name The new payer name to use. If not empty, this will be the pr imary label. | 91 * @param name The new payer name to use. If not empty, this will be the pr imary label. |
| 80 * @param phone The new phone number to use. If name is empty, this will be the primary label. | 92 * @param phone The new phone number to use. If name is empty, this will be the primary label. |
| 81 * @param email The new email address to use. If email and phone are empty, this will be the | 93 * @param email The new email address to use. If email and phone are empty, this will be the |
| 82 * primary label. | 94 * primary label. |
| 83 */ | 95 */ |
| 84 public void completeContact(String guid, @Nullable String name, | 96 public void completeContact(String guid, @Nullable String name, |
| 85 @Nullable String phone, @Nullable String email) { | 97 @Nullable String phone, @Nullable String email) { |
| 86 setContactInfo(guid, name, phone, email); | 98 setContactInfo(guid, name, phone, email); |
| 87 updateCompletionStatus(ContactEditor.COMPLETE); | 99 updateCompletionStatus(ContactEditor.COMPLETE); |
| 88 } | 100 } |
| 89 | 101 |
| 102 /** | |
| 103 * Returns whether this contact is equal or a superset of the specified cont act considering the | |
| 104 * information requested by the merchant. | |
| 105 * | |
| 106 * @param contact The contact to compare to. | |
| 107 * @return Whether this contact is equal to or a superset of the other. | |
| 108 */ | |
| 109 public boolean isEqualOrSupersetOf(AutofillContact contact) { | |
|
please use gerrit instead
2017/01/05 18:59:23
nit: assert contact != null;
sebsg
2017/01/05 19:29:12
Done.
| |
| 110 // This contact is not equal to or a superset of the other if for a requ ested field: | |
| 111 // 1- This contact's field is null and the other's is not. | |
| 112 // 2- The field values are not equal. | |
| 113 if (mRequestName) { | |
| 114 if (mPayerName == null && contact.mPayerName != null) return false; | |
| 115 if (mPayerName != null && contact.mPayerName != null | |
| 116 && !mPayerName.equalsIgnoreCase(contact.mPayerName)) { | |
| 117 return false; | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 if (mRequestPhone) { | |
| 122 if (mPayerPhone == null && contact.mPayerPhone != null) return false ; | |
| 123 if (mPayerPhone != null && contact.mPayerPhone != null | |
| 124 && !TextUtils.equals(mPayerPhone, contact.mPayerPhone)) { | |
| 125 return false; | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 if (mRequestEmail) { | |
| 130 if (mPayerEmail == null && contact.mPayerEmail != null) return false ; | |
| 131 if (mPayerEmail != null && contact.mPayerEmail != null | |
| 132 && !mPayerEmail.equalsIgnoreCase(contact.mPayerEmail)) { | |
| 133 return false; | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 return true; | |
| 138 } | |
| 139 | |
| 140 /** | |
| 141 * @return Returns the relevance score of this contact, based on the validit y of the information | |
| 142 * requested by the merchant. | |
| 143 */ | |
| 144 public int getRelevanceScore() { | |
| 145 int score = 0; | |
| 146 | |
| 147 if (mRequestName && (mCompletionStatus & ContactEditor.INVALID_NAME) == 0) ++score; | |
| 148 if (mRequestPhone && (mCompletionStatus & ContactEditor.INVALID_PHONE_NU MBER) == 0) ++score; | |
| 149 if (mRequestEmail && (mCompletionStatus & ContactEditor.INVALID_EMAIL) = = 0) ++score; | |
| 150 | |
| 151 return score; | |
| 152 } | |
| 153 | |
| 90 private void setContactInfo(String guid, @Nullable String name, | 154 private void setContactInfo(String guid, @Nullable String name, |
| 91 @Nullable String phone, @Nullable String email) { | 155 @Nullable String phone, @Nullable String email) { |
| 92 mPayerName = TextUtils.isEmpty(name) ? null : name; | 156 mPayerName = TextUtils.isEmpty(name) ? null : name; |
| 93 mPayerPhone = TextUtils.isEmpty(phone) ? null : phone; | 157 mPayerPhone = TextUtils.isEmpty(phone) ? null : phone; |
| 94 mPayerEmail = TextUtils.isEmpty(email) ? null : email; | 158 mPayerEmail = TextUtils.isEmpty(email) ? null : email; |
| 95 | 159 |
| 96 if (mPayerName == null) { | 160 if (mPayerName == null) { |
| 97 updateIdentifierAndLabels(guid, mPayerPhone == null ? mPayerEmail : mPayerPhone, | 161 updateIdentifierAndLabels(guid, mPayerPhone == null ? mPayerEmail : mPayerPhone, |
| 98 mPayerPhone == null ? null : mPayerEmail); | 162 mPayerPhone == null ? null : mPayerEmail); |
| 99 } else { | 163 } else { |
| 100 updateIdentifierAndLabels(guid, mPayerName, | 164 updateIdentifierAndLabels(guid, mPayerName, |
| 101 mPayerPhone == null ? mPayerEmail : mPayerPhone, | 165 mPayerPhone == null ? mPayerEmail : mPayerPhone, |
| 102 mPayerPhone == null ? null : mPayerEmail); | 166 mPayerPhone == null ? null : mPayerEmail); |
| 103 } | 167 } |
| 104 } | 168 } |
| 105 | 169 |
| 106 private void updateCompletionStatus(int completionStatus) { | 170 private void updateCompletionStatus(int completionStatus) { |
| 171 mCompletionStatus = completionStatus; | |
| 107 mIsComplete = completionStatus == ContactEditor.COMPLETE; | 172 mIsComplete = completionStatus == ContactEditor.COMPLETE; |
| 108 | 173 |
| 109 switch (completionStatus) { | 174 switch (completionStatus) { |
| 110 case ContactEditor.COMPLETE: | 175 case ContactEditor.COMPLETE: |
| 111 mEditMessage = null; | 176 mEditMessage = null; |
| 112 mEditTitle = mContext.getString(R.string.payments_edit_contact_d etails_label); | 177 mEditTitle = mContext.getString(R.string.payments_edit_contact_d etails_label); |
| 113 break; | 178 break; |
| 114 case ContactEditor.INVALID_NAME: | 179 case ContactEditor.INVALID_NAME: |
| 115 mEditMessage = mContext.getString(R.string.payments_name_require d); | 180 mEditMessage = mContext.getString(R.string.payments_name_require d); |
| 116 mEditTitle = mContext.getString(R.string.payments_add_name); | 181 mEditTitle = mContext.getString(R.string.payments_add_name); |
| 117 break; | 182 break; |
| 118 case ContactEditor.INVALID_EMAIL: | 183 case ContactEditor.INVALID_EMAIL: |
| 119 mEditMessage = mContext.getString(R.string.payments_email_requir ed); | 184 mEditMessage = mContext.getString(R.string.payments_email_requir ed); |
| 120 mEditTitle = mContext.getString(R.string.payments_add_email); | 185 mEditTitle = mContext.getString(R.string.payments_add_email); |
| 121 break; | 186 break; |
| 122 case ContactEditor.INVALID_PHONE_NUMBER: | 187 case ContactEditor.INVALID_PHONE_NUMBER: |
| 123 mEditMessage = mContext.getString(R.string.payments_phone_number _required); | 188 mEditMessage = mContext.getString(R.string.payments_phone_number _required); |
| 124 mEditTitle = mContext.getString(R.string.payments_add_phone_numb er); | 189 mEditTitle = mContext.getString(R.string.payments_add_phone_numb er); |
| 125 break; | 190 break; |
| 126 case ContactEditor.INVALID_MULTIPLE_FIELDS: | 191 default: |
| 192 // Multiple invalid fields. | |
| 127 mEditMessage = mContext.getString(R.string.payments_more_informa tion_required); | 193 mEditMessage = mContext.getString(R.string.payments_more_informa tion_required); |
| 128 mEditTitle = mContext.getString(R.string.payments_add_more_infor mation); | 194 mEditTitle = mContext.getString(R.string.payments_add_more_infor mation); |
| 129 break; | 195 break; |
| 130 default: | |
| 131 assert false : "Invalid completion status code"; | |
| 132 } | 196 } |
| 133 } | 197 } |
| 134 } | 198 } |
| OLD | NEW |