1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98

content / public / android / java / src / org / chromium / content_public / browser / ContactsPicker.java [blame]

// Copyright 2020 The Chromium Authors
// 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.VisibleForTesting;

/**
 * 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;

    /**
     * The object that represents the currently visible contacts picker UI, or null if none is
     * visible.
     */
    private static Object sPicker;

    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;
    }

    /**
     * @param webContents The web contents using the contacts API.
     * @return Whether the contacts picker should be shown.
     */
    @VisibleForTesting
    public static boolean canShowContactsPicker(WebContents webContents) {
        if (webContents == null || webContents.isDestroyed()) {
            return false;
        }

        return webContents.getVisibility() == Visibility.VISIBLE;
    }

    /**
     * Called to display the contacts picker.
     * @param webContents 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(
            WebContents webContents,
            ContactsPickerListener listener,
            boolean allowMultiple,
            boolean includeNames,
            boolean includeEmails,
            boolean includeTel,
            boolean includeAddresses,
            boolean includeIcons,
            String formattedOrigin) {
        if (sContactsPickerDelegate == null) return false;
        assert sPicker == null;

        if (!canShowContactsPicker(webContents)) {
            return false;
        }
        sPicker =
                sContactsPickerDelegate.showContactsPicker(
                        webContents,
                        listener,
                        allowMultiple,
                        includeNames,
                        includeEmails,
                        includeTel,
                        includeAddresses,
                        includeIcons,
                        formattedOrigin);
        return true;
    }

    /** Called when the contacts picker dialog has been dismissed. */
    public static void onContactsPickerDismissed() {
        assert sPicker != null;
        sPicker = null;
    }
}