OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.text.SpannableString; |
| 9 import android.text.TextUtils; |
| 10 import android.view.View; |
| 11 |
| 12 import org.chromium.base.annotations.CalledByNative; |
| 13 import org.chromium.chrome.R; |
| 14 import org.chromium.chrome.browser.omnibox.OmniboxUrlEmphasizer; |
| 15 import org.chromium.chrome.browser.profiles.Profile; |
| 16 import org.chromium.ui.base.WindowAndroid; |
| 17 import org.chromium.ui.text.NoUnderlineClickableSpan; |
| 18 import org.chromium.ui.text.SpanApplier; |
| 19 import org.chromium.ui.text.SpanApplier.SpanInfo; |
| 20 |
| 21 /** |
| 22 * A dialog for showing available USB devices. This dialog is shown when a websi
te requests to |
| 23 * connect to a USB device (e.g. through a usb.requestDevice Javascript call). |
| 24 */ |
| 25 public class UsbChooserDialog implements ItemChooserDialog.ItemSelectedCallback
{ |
| 26 /** |
| 27 * The dialog to show to let the user pick a device. |
| 28 */ |
| 29 ItemChooserDialog mItemChooserDialog; |
| 30 |
| 31 /** |
| 32 * A pointer back to the native part of the implementation for this dialog. |
| 33 */ |
| 34 long mNativeUsbChooserDialogPtr; |
| 35 |
| 36 /** |
| 37 * Creates the UsbChooserDialog. |
| 38 */ |
| 39 private UsbChooserDialog(long nativeUsbChooserDialogPtr) { |
| 40 mNativeUsbChooserDialogPtr = nativeUsbChooserDialogPtr; |
| 41 } |
| 42 |
| 43 /** |
| 44 * Shows the UsbChooserDialog. |
| 45 * |
| 46 * @param context Context which is used for launching a dialog. |
| 47 * @param origin The origin for the site wanting to connect to the USB devic
e. |
| 48 * @param securityLevel The security level of the connection to the site wan
ting to connect to |
| 49 * the USB device. For valid values see SecurityStateMo
del::SecurityLevel. |
| 50 */ |
| 51 private void show(Context context, String origin, int securityLevel) { |
| 52 // Emphasize the origin. |
| 53 Profile profile = Profile.getLastUsedProfile(); |
| 54 SpannableString originSpannableString = new SpannableString(origin); |
| 55 OmniboxUrlEmphasizer.emphasizeUrl(originSpannableString, context.getReso
urces(), profile, |
| 56 securityLevel, false /* isInternalPage */, true /* useDarkColors
*/, |
| 57 true /* emphasizeHttpsScheme */); |
| 58 // Construct a full string and replace the origin text with emphasized v
ersion. |
| 59 SpannableString title = |
| 60 new SpannableString(context.getString(R.string.usb_chooser_dialo
g_prompt, origin)); |
| 61 int start = title.toString().indexOf(origin); |
| 62 TextUtils.copySpansFrom(originSpannableString, 0, originSpannableString.
length(), |
| 63 Object.class, title, start); |
| 64 |
| 65 String searching = ""; |
| 66 String noneFound = context.getString(R.string.usb_chooser_dialog_no_devi
ces_found_prompt); |
| 67 SpannableString statusIdleNoneFound = SpanApplier.applySpans( |
| 68 context.getString(R.string.usb_chooser_dialog_footnote_text), |
| 69 new SpanInfo("<link>", "</link>", new NoUnderlineClickab
leSpan() { |
| 70 @Override |
| 71 public void onClick(View view) { |
| 72 if (mNativeUsbChooserDialogPtr == 0) { |
| 73 return; |
| 74 } |
| 75 |
| 76 nativeLoadUsbHelpPage(mNativeUsbChooserDialogPtr
); |
| 77 |
| 78 // Get rid of the highlight background on select
ion. |
| 79 view.invalidate(); |
| 80 } |
| 81 })); |
| 82 SpannableString statusIdleSomeFound = statusIdleNoneFound; |
| 83 String positiveButton = context.getString(R.string.usb_chooser_dialog_co
nnect_button_text); |
| 84 |
| 85 ItemChooserDialog.ItemChooserLabels labels = |
| 86 new ItemChooserDialog.ItemChooserLabels(title, searching, noneFo
und, |
| 87 statusIdleNoneFound, statusIdleSomeFound, positiveButton
); |
| 88 mItemChooserDialog = new ItemChooserDialog(context, this, labels); |
| 89 } |
| 90 |
| 91 @Override |
| 92 public void onItemSelected(String id) { |
| 93 if (mNativeUsbChooserDialogPtr != 0) { |
| 94 if (id.isEmpty()) { |
| 95 nativeOnDialogCancelled(mNativeUsbChooserDialogPtr); |
| 96 } else { |
| 97 nativeOnItemSelected(mNativeUsbChooserDialogPtr, id); |
| 98 } |
| 99 } |
| 100 } |
| 101 |
| 102 @CalledByNative |
| 103 private static UsbChooserDialog create(WindowAndroid windowAndroid, String o
rigin, |
| 104 int securityLevel, long nativeUsbChooserDialogPtr) { |
| 105 Context context = windowAndroid.getActivity().get(); |
| 106 if (context == null) { |
| 107 return null; |
| 108 } |
| 109 |
| 110 UsbChooserDialog dialog = new UsbChooserDialog(nativeUsbChooserDialogPtr
); |
| 111 dialog.show(context, origin, securityLevel); |
| 112 return dialog; |
| 113 } |
| 114 |
| 115 @CalledByNative |
| 116 private void setIdleState() { |
| 117 mItemChooserDialog.setIdleState(); |
| 118 } |
| 119 |
| 120 @CalledByNative |
| 121 private void addDevice(String deviceId, String deviceName) { |
| 122 mItemChooserDialog.addItemToList( |
| 123 new ItemChooserDialog.ItemChooserRow(deviceId, deviceName)); |
| 124 } |
| 125 |
| 126 @CalledByNative |
| 127 private void removeDevice(String deviceId, String deviceName) { |
| 128 mItemChooserDialog.removeItemFromList( |
| 129 new ItemChooserDialog.ItemChooserRow(deviceId, deviceName)); |
| 130 } |
| 131 |
| 132 @CalledByNative |
| 133 private void closeDialog() { |
| 134 mNativeUsbChooserDialogPtr = 0; |
| 135 mItemChooserDialog.dismiss(); |
| 136 } |
| 137 |
| 138 private native void nativeOnItemSelected(long nativeUsbChooserDialogAndroid,
String deviceId); |
| 139 private native void nativeOnDialogCancelled(long nativeUsbChooserDialogAndro
id); |
| 140 private native void nativeLoadUsbHelpPage(long nativeUsbChooserDialogAndroid
); |
| 141 } |
OLD | NEW |