| Index: chrome/android/java/src/org/chromium/chrome/browser/UsbChooserDialog.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/UsbChooserDialog.java b/chrome/android/java/src/org/chromium/chrome/browser/UsbChooserDialog.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6946b0a6dcadb65ca5a4fe3c26ba22dff4364680
|
| --- /dev/null
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/UsbChooserDialog.java
|
| @@ -0,0 +1,180 @@
|
| +// Copyright 2016 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.chrome.browser;
|
| +
|
| +import android.content.Context;
|
| +import android.graphics.Color;
|
| +import android.text.SpannableString;
|
| +import android.text.TextPaint;
|
| +import android.text.TextUtils;
|
| +import android.text.style.ClickableSpan;
|
| +import android.view.View;
|
| +
|
| +import org.chromium.base.annotations.CalledByNative;
|
| +import org.chromium.chrome.R;
|
| +import org.chromium.chrome.browser.omnibox.OmniboxUrlEmphasizer;
|
| +import org.chromium.chrome.browser.profiles.Profile;
|
| +import org.chromium.ui.base.WindowAndroid;
|
| +import org.chromium.ui.text.SpanApplier;
|
| +import org.chromium.ui.text.SpanApplier.SpanInfo;
|
| +
|
| +import java.util.ArrayList;
|
| +import java.util.List;
|
| +
|
| +/**
|
| + * A dialog for showing available USB devices. This dialog is shown when a website requests to
|
| + * connect to a USB device (e.g. through a usb.requestDevice Javascript call).
|
| + */
|
| +public class UsbChooserDialog
|
| + implements ItemChooserDialog.ItemSelectedCallback, WindowAndroid.PermissionCallback {
|
| + // Values passed to nativeOnDialogFinished:eventType, and only used in the native function.
|
| + private static final int DIALOG_FINISHED_CANCELLED = 0;
|
| + private static final int DIALOG_FINISHED_SELECTED = 1;
|
| +
|
| + // The window that owns this dialog.
|
| + final WindowAndroid mWindowAndroid;
|
| +
|
| + // Always equal to mWindowAndroid.getActivity().get(), but stored separately to make sure it's
|
| + // not GC'ed.
|
| + final Context mContext;
|
| +
|
| + // The dialog to show to let the user pick a device.
|
| + ItemChooserDialog mItemChooserDialog;
|
| +
|
| + // The origin for the site wanting to connect to the USB device.
|
| + String mOrigin;
|
| +
|
| + // The security level of the connection to the site wanting to connect to the
|
| + // USB device. For valid values see SecurityStateModel::SecurityLevel.
|
| + int mSecurityLevel;
|
| +
|
| + // A pointer back to the native part of the implementation for this dialog.
|
| + long mNativeUsbChooserDialogPtr;
|
| +
|
| + /**
|
| + * Creates the UsbChooserDialog and displays it (and starts waiting for data).
|
| + *
|
| + * @param context Context which is used for launching a dialog.
|
| + */
|
| + private UsbChooserDialog(WindowAndroid windowAndroid, String origin, int securityLevel,
|
| + long nativeUsbChooserDialogPtr) {
|
| + mWindowAndroid = windowAndroid;
|
| + mContext = windowAndroid.getActivity().get();
|
| + assert mContext != null;
|
| + mOrigin = origin;
|
| + mSecurityLevel = securityLevel;
|
| + mNativeUsbChooserDialogPtr = nativeUsbChooserDialogPtr;
|
| + }
|
| +
|
| + /**
|
| + * Show the UsbChooserDialog.
|
| + */
|
| + private void show() {
|
| + // Emphasize the origin.
|
| + Profile profile = Profile.getLastUsedProfile();
|
| + SpannableString origin = new SpannableString(mOrigin);
|
| + OmniboxUrlEmphasizer.emphasizeUrl(origin, mContext.getResources(), profile, mSecurityLevel,
|
| + false /* isInternalPage */, true /* useDarkColors */,
|
| + true /* emphasizeHttpsScheme */);
|
| + // Construct a full string and replace the origin text with emphasized version.
|
| + SpannableString title = new SpannableString(
|
| + mContext.getString(R.string.usb_chooser_dialog_prompt, mOrigin));
|
| + int start = title.toString().indexOf(mOrigin);
|
| + TextUtils.copySpansFrom(origin, 0, origin.length(), Object.class, title, start);
|
| +
|
| + SpannableString searching = new SpannableString(new String(""));
|
| + SpannableString noneFound = new SpannableString(
|
| + mContext.getString(R.string.usb_chooser_dialog_no_devices_found_prompt));
|
| + SpannableString statusActive = SpanApplier.applySpans(
|
| + mContext.getString(R.string.usb_chooser_dialog_footnote_text),
|
| + new SpanInfo("<link>", "</link>", new NoUnderlineClickableSpan()));
|
| + SpannableString statusIdleNoneFound = statusActive;
|
| + SpannableString statusIdleSomeFound = statusActive;
|
| + String positiveButton = mContext.getString(R.string.usb_chooser_dialog_connect_button_text);
|
| +
|
| + ItemChooserDialog.ItemChooserLabels labels =
|
| + new ItemChooserDialog.ItemChooserLabels(title, searching, noneFound, statusActive,
|
| + statusIdleNoneFound, statusIdleSomeFound, positiveButton);
|
| + mItemChooserDialog = new ItemChooserDialog(mContext, this, labels);
|
| + }
|
| +
|
| + @Override
|
| + public void onItemSelected(String id) {
|
| + if (mNativeUsbChooserDialogPtr != 0) {
|
| + if (id.isEmpty()) {
|
| + nativeOnDialogFinished(mNativeUsbChooserDialogPtr, DIALOG_FINISHED_CANCELLED, "");
|
| + } else {
|
| + nativeOnDialogFinished(mNativeUsbChooserDialogPtr, DIALOG_FINISHED_SELECTED, id);
|
| + }
|
| + }
|
| + }
|
| +
|
| + @Override
|
| + public void onRequestPermissionsResult(String[] permissions, int[] grantResults) {}
|
| +
|
| + /**
|
| + * A helper class to show a clickable link with underlines turned off.
|
| + */
|
| + private class NoUnderlineClickableSpan extends ClickableSpan {
|
| + NoUnderlineClickableSpan() {}
|
| +
|
| + @Override
|
| + public void onClick(View view) {
|
| + if (mNativeUsbChooserDialogPtr == 0) {
|
| + return;
|
| + }
|
| +
|
| + nativeShowUsbOverviewLink(mNativeUsbChooserDialogPtr);
|
| +
|
| + // Get rid of the highlight background on selection.
|
| + view.invalidate();
|
| + }
|
| +
|
| + @Override
|
| + public void updateDrawState(TextPaint textPaint) {
|
| + super.updateDrawState(textPaint);
|
| + textPaint.bgColor = Color.TRANSPARENT;
|
| + textPaint.setUnderlineText(false);
|
| + }
|
| + }
|
| +
|
| + @CalledByNative
|
| + private static UsbChooserDialog create(WindowAndroid windowAndroid, String origin,
|
| + int securityLevel, long nativeUsbChooserDialogPtr) {
|
| + UsbChooserDialog dialog = new UsbChooserDialog(
|
| + windowAndroid, origin, securityLevel, nativeUsbChooserDialogPtr);
|
| + dialog.show();
|
| + return dialog;
|
| + }
|
| +
|
| + @CalledByNative
|
| + private void updateOptions(String[] deviceId, String[] deviceName) {
|
| + if (deviceName == null) return;
|
| + mItemChooserDialog.clear();
|
| + List<ItemChooserDialog.ItemChooserRow> devices =
|
| + new ArrayList<ItemChooserDialog.ItemChooserRow>();
|
| + int len = deviceId.length;
|
| + if (len == 0) {
|
| + mItemChooserDialog.setIdleState();
|
| + return;
|
| + }
|
| +
|
| + for (int i = 0; i < len; ++i) {
|
| + devices.add(new ItemChooserDialog.ItemChooserRow(deviceId[i], deviceName[i]));
|
| + }
|
| + mItemChooserDialog.addItemsToList(devices);
|
| + }
|
| +
|
| + @CalledByNative
|
| + private void closeDialog() {
|
| + mNativeUsbChooserDialogPtr = 0;
|
| + mItemChooserDialog.dismiss();
|
| + }
|
| +
|
| + private native void nativeOnDialogFinished(
|
| + long nativeUsbChooserAndroid, int eventType, String deviceId);
|
| + // Help links.
|
| + private native void nativeShowUsbOverviewLink(long nativeUsbChooserAndroid);
|
| +}
|
|
|