0

Move Contacts Picker code out of UiUtils.

The ui_utils_java target is not a suitable place for this code because
it needs to use WindowAndroid instead of Context (for WebLayer's sake),
and ui_utils_java can't depend on WindowAndroid.

This glue code is moved to org.chromium.content_public.browser.

Bug: 1117536,1016938
Change-Id: Ic18275e2f0eca0739b850418af6c71d2079c6da5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2380343
Reviewed-by: John Abd-El-Malek <jam@chromium.org>
Reviewed-by: David Trainor <dtrainor@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804505}
This commit is contained in:
Evan Stade
2020-09-04 01:55:43 +00:00
committed by Commit Bot
parent 70f09b810c
commit dd1293630f
12 changed files with 127 additions and 102 deletions
chrome/android/java/src/org/chromium/chrome/browser/init
components/browser_ui/contacts_picker/android
content/public/android
ui/android
BUILD.gn
java
src
org
chromium

@ -79,6 +79,7 @@ android_library("content_java") {
"//base:base_java",
"//base:jni_java",
"//components/download/public/common:public_java",
"//components/payments/mojom:mojom_java",
"//device/bluetooth:java",
"//device/gamepad:java",
"//media/base/android:media_java",
@ -277,6 +278,9 @@ android_library("content_java") {
"java/src/org/chromium/content_public/browser/BrowserTaskExecutor.java",
"java/src/org/chromium/content_public/browser/ChildProcessCreationParams.java",
"java/src/org/chromium/content_public/browser/ChildProcessLauncherHelper.java",
"java/src/org/chromium/content_public/browser/ContactsPicker.java",
"java/src/org/chromium/content_public/browser/ContactsPickerDelegate.java",
"java/src/org/chromium/content_public/browser/ContactsPickerListener.java",
"java/src/org/chromium/content_public/browser/ContentFeatureList.java",
"java/src/org/chromium/content_public/browser/ContentViewStatics.java",
"java/src/org/chromium/content_public/browser/DeviceUtils.java",

@ -11,8 +11,8 @@ import android.text.TextUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.ui.ContactsPickerListener;
import org.chromium.ui.UiUtils;
import org.chromium.content_public.browser.ContactsPicker;
import org.chromium.content_public.browser.ContactsPickerListener;
import org.chromium.ui.base.WindowAndroid;
import java.nio.ByteBuffer;
@ -53,8 +53,8 @@ public class ContactsDialogHost implements ContactsPickerListener {
}
if (mWindowAndroid.hasPermission(Manifest.permission.READ_CONTACTS)) {
if (!UiUtils.showContactsPicker(mWindowAndroid.getActivity().get(), this, multiple,
includeNames, includeEmails, includeTel, includeAddresses, includeIcons,
if (!ContactsPicker.showContactsPicker(mWindowAndroid, this, multiple, includeNames,
includeEmails, includeTel, includeAddresses, includeIcons,
formattedOrigin)) {
ContactsDialogHostJni.get().endWithPermissionDenied(mNativeContactsProviderAndroid);
}
@ -71,9 +71,9 @@ public class ContactsDialogHost implements ContactsPickerListener {
if (permissions.length == 1 && grantResults.length == 1
&& TextUtils.equals(permissions[0], Manifest.permission.READ_CONTACTS)
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (!UiUtils.showContactsPicker(mWindowAndroid.getActivity().get(), this,
multiple, includeNames, includeEmails, includeTel,
includeAddresses, includeIcons, formattedOrigin)) {
if (!ContactsPicker.showContactsPicker(mWindowAndroid, this, multiple,
includeNames, includeEmails, includeTel, includeAddresses,
includeIcons, formattedOrigin)) {
ContactsDialogHostJni.get().endWithPermissionDenied(
mNativeContactsProviderAndroid);
}

@ -0,0 +1,62 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content_public.browser;
import org.chromium.ui.base.WindowAndroid;
/**
* A utility class that allows the embedder to provide a Contacts Picker implementation to content.
*/
public final class ContactsPicker {
/**
* The current delegate for the contacts picker, or null if navigator.contacts is not
* supported.
*/
private static ContactsPickerDelegate sContactsPickerDelegate;
private ContactsPicker() {}
/**
* Allows setting a delegate for an Android contacts picker.
* @param delegate A {@link ContactsPickerDelegate} instance.
*/
public static void setContactsPickerDelegate(ContactsPickerDelegate delegate) {
sContactsPickerDelegate = delegate;
}
/**
* Called to display the contacts picker.
* @param windowAndroid The window of the Web Contents that triggered this call.
* @param listener The listener that will be notified of the action the user took in the
* picker.
* @param allowMultiple Whether to allow multiple contacts to be selected.
* @param includeNames Whether to include names of the shared contacts.
* @param includeEmails Whether to include emails of the shared contacts.
* @param includeTel Whether to include telephone numbers of the shared contacts.
* @param includeAddresses Whether to include addresses of the shared contacts.
* @param includeIcons Whether to include icons of the shared contacts.
* @param formattedOrigin The origin the data will be shared with, formatted for display with
* the scheme omitted.
* @return whether a contacts picker is successfully shown.
*/
public static boolean showContactsPicker(WindowAndroid windowAndroid,
ContactsPickerListener listener, boolean allowMultiple, boolean includeNames,
boolean includeEmails, boolean includeTel, boolean includeAddresses,
boolean includeIcons, String formattedOrigin) {
if (sContactsPickerDelegate == null) return false;
sContactsPickerDelegate.showContactsPicker(windowAndroid, listener, allowMultiple,
includeNames, includeEmails, includeTel, includeAddresses, includeIcons,
formattedOrigin);
return true;
}
/**
* Called when the contacts picker dialog has been dismissed.
*/
public static void onContactsPickerDismissed() {
if (sContactsPickerDelegate == null) return;
sContactsPickerDelegate.onContactsPickerDismissed();
}
}

@ -0,0 +1,35 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content_public.browser;
import org.chromium.ui.base.WindowAndroid;
/**
* A delegate interface for the contacts picker.
*/
public interface ContactsPickerDelegate {
/**
* Called to display the contacts picker.
* @param windowAndroid The window of the Web Contents that triggered the dialog.
* @param listener The listener that will be notified of the action the user took in the
* picker.
* @param allowMultiple Whether to allow multiple contacts to be picked.
* @param includeNames Whether to include names of the shared contacts.
* @param includeEmails Whether to include emails of the shared contacts.
* @param includeTel Whether to include telephone numbers of the shared contacts.
* @param includeAddresses Whether to include addresses of the shared contacts.
* @param includeIcons Whether to include icons of the shared contacts.
* @param formattedOrigin The origin the data will be shared with, formatted for display
* with the scheme omitted.
*/
void showContactsPicker(WindowAndroid windowAndroid, ContactsPickerListener listener,
boolean allowMultiple, boolean includeNames, boolean includeEmails, boolean includeTel,
boolean includeAddresses, boolean includeIcons, String formattedOrigin);
/**
* Called when the contacts picker dialog has been dismissed.
*/
void onContactsPickerDismissed();
}

@ -0,0 +1,84 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content_public.browser;
import androidx.annotation.IntDef;
import org.chromium.blink.mojom.ContactIconBlob;
import org.chromium.payments.mojom.PaymentAddress;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* The callback used to indicate what action the user took in the picker.
*/
public interface ContactsPickerListener {
/**
* A container class for exchanging contact details.
*/
public static class Contact {
public final List<String> names;
public final List<String> emails;
public final List<String> tel;
public final List<ByteBuffer> serializedAddresses;
public final List<ByteBuffer> serializedIcons;
public Contact(List<String> contactNames, List<String> contactEmails,
List<String> contactTel, List<PaymentAddress> contactAddresses,
List<ContactIconBlob> contactIcons) {
names = contactNames;
emails = contactEmails;
tel = contactTel;
if (contactAddresses != null) {
serializedAddresses = new ArrayList<ByteBuffer>();
for (PaymentAddress address : contactAddresses) {
serializedAddresses.add(address.serialize());
}
} else {
serializedAddresses = null;
}
if (contactIcons != null) {
serializedIcons = new ArrayList<ByteBuffer>();
for (ContactIconBlob icon : contactIcons) {
serializedIcons.add(icon.serialize());
}
} else {
serializedIcons = null;
}
}
}
/**
* The action the user took in the picker.
*/
@IntDef({ContactsPickerAction.CANCEL, ContactsPickerAction.CONTACTS_SELECTED,
ContactsPickerAction.SELECT_ALL, ContactsPickerAction.UNDO_SELECT_ALL})
@Retention(RetentionPolicy.SOURCE)
public @interface ContactsPickerAction {
int CANCEL = 0;
int CONTACTS_SELECTED = 1;
int SELECT_ALL = 2;
int UNDO_SELECT_ALL = 3;
int NUM_ENTRIES = 4;
}
/**
* Called when the user has selected an action. For possible actions see above.
*
* @param contacts The list of contacts selected.
* @param percentageShared How big a percentage of the full contact list was shared (for metrics
* purposes).
* @param propertiesRequested The properties requested by the website (names, emails,
* telephones).
*/
void onContactsPickerUserAction(@ContactsPickerAction int action, List<Contact> contacts,
int percentageShared, int propertiesRequested);
}