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.graphics.Color; | |
9 import android.text.SpannableString; | |
10 import android.text.TextPaint; | |
11 import android.text.TextUtils; | |
12 import android.text.style.ClickableSpan; | |
13 import android.view.View; | |
14 | |
15 import org.chromium.base.annotations.CalledByNative; | |
16 import org.chromium.chrome.R; | |
17 import org.chromium.chrome.browser.omnibox.OmniboxUrlEmphasizer; | |
18 import org.chromium.chrome.browser.profiles.Profile; | |
19 import org.chromium.ui.base.WindowAndroid; | |
20 import org.chromium.ui.text.SpanApplier; | |
21 import org.chromium.ui.text.SpanApplier.SpanInfo; | |
22 | |
23 import java.util.ArrayList; | |
24 import java.util.List; | |
25 | |
26 /** | |
27 * A dialog for showing available USB devices. This dialog is shown when a websi te requests to | |
28 * connect to a USB device (e.g. through a usb.requestDevice Javascript call). | |
29 */ | |
30 public class UsbChooserDialog | |
31 implements ItemChooserDialog.ItemSelectedCallback, WindowAndroid.Permiss ionCallback { | |
32 // Values passed to nativeOnDialogFinished:eventType, and only used in the n ative function. | |
33 private static final int DIALOG_FINISHED_CANCELLED = 0; | |
34 private static final int DIALOG_FINISHED_SELECTED = 1; | |
35 | |
36 // The window that owns this dialog. | |
37 final WindowAndroid mWindowAndroid; | |
38 | |
39 // Always equal to mWindowAndroid.getActivity().get(), but stored separately to make sure it's | |
40 // not GC'ed. | |
Yaron
2016/03/07 21:51:49
This seems like the exact reason to *not* do this.
juncai
2016/03/09 01:26:50
Done.
| |
41 final Context mContext; | |
42 | |
43 // The dialog to show to let the user pick a device. | |
44 ItemChooserDialog mItemChooserDialog; | |
45 | |
46 // The origin for the site wanting to connect to the USB device. | |
47 String mOrigin; | |
Yaron
2016/03/07 21:51:49
I don't see why mOrigin and mSecurityLevel are mem
juncai
2016/03/09 01:26:50
Done.
| |
48 | |
49 // The security level of the connection to the site wanting to connect to th e | |
50 // USB device. For valid values see SecurityStateModel::SecurityLevel. | |
51 int mSecurityLevel; | |
52 | |
53 // A pointer back to the native part of the implementation for this dialog. | |
54 long mNativeUsbChooserDialogPtr; | |
55 | |
56 /** | |
57 * Creates the UsbChooserDialog and displays it (and starts waiting for data ). | |
58 * | |
59 * @param context Context which is used for launching a dialog. | |
60 */ | |
61 private UsbChooserDialog(WindowAndroid windowAndroid, String origin, int sec urityLevel, | |
62 long nativeUsbChooserDialogPtr) { | |
63 mWindowAndroid = windowAndroid; | |
64 mContext = windowAndroid.getActivity().get(); | |
65 assert mContext != null; | |
66 mOrigin = origin; | |
67 mSecurityLevel = securityLevel; | |
68 mNativeUsbChooserDialogPtr = nativeUsbChooserDialogPtr; | |
69 } | |
70 | |
71 /** | |
72 * Show the UsbChooserDialog. | |
73 */ | |
74 private void show() { | |
75 // Emphasize the origin. | |
76 Profile profile = Profile.getLastUsedProfile(); | |
77 SpannableString origin = new SpannableString(mOrigin); | |
78 OmniboxUrlEmphasizer.emphasizeUrl(origin, mContext.getResources(), profi le, mSecurityLevel, | |
79 false /* isInternalPage */, true /* useDarkColors */, | |
80 true /* emphasizeHttpsScheme */); | |
81 // Construct a full string and replace the origin text with emphasized v ersion. | |
82 SpannableString title = new SpannableString( | |
83 mContext.getString(R.string.usb_chooser_dialog_prompt, mOrigin)) ; | |
84 int start = title.toString().indexOf(mOrigin); | |
85 TextUtils.copySpansFrom(origin, 0, origin.length(), Object.class, title, start); | |
86 | |
87 SpannableString searching = new SpannableString(new String("")); | |
88 SpannableString noneFound = new SpannableString( | |
89 mContext.getString(R.string.usb_chooser_dialog_no_devices_found_ prompt)); | |
90 SpannableString statusActive = SpanApplier.applySpans( | |
91 mContext.getString(R.string.usb_chooser_dialog_footnote_text), | |
92 new SpanInfo("<link>", "</link>", new NoUnderlineClickableSpan() )); | |
93 SpannableString statusIdleNoneFound = statusActive; | |
94 SpannableString statusIdleSomeFound = statusActive; | |
95 String positiveButton = mContext.getString(R.string.usb_chooser_dialog_c onnect_button_text); | |
96 | |
97 ItemChooserDialog.ItemChooserLabels labels = | |
98 new ItemChooserDialog.ItemChooserLabels(title, searching, noneFo und, statusActive, | |
99 statusIdleNoneFound, statusIdleSomeFound, positiveButton ); | |
100 mItemChooserDialog = new ItemChooserDialog(mContext, this, labels); | |
101 } | |
102 | |
103 @Override | |
104 public void onItemSelected(String id) { | |
105 if (mNativeUsbChooserDialogPtr != 0) { | |
106 if (id.isEmpty()) { | |
107 nativeOnDialogFinished(mNativeUsbChooserDialogPtr, DIALOG_FINISH ED_CANCELLED, ""); | |
108 } else { | |
109 nativeOnDialogFinished(mNativeUsbChooserDialogPtr, DIALOG_FINISH ED_SELECTED, id); | |
110 } | |
111 } | |
112 } | |
113 | |
114 @Override | |
115 public void onRequestPermissionsResult(String[] permissions, int[] grantResu lts) {} | |
Yaron
2016/03/07 21:51:49
Remove this and the the corresponding interface de
Finnur
2016/03/08 10:26:20
Is there no special permission that is needed for
juncai
2016/03/09 01:26:50
Done.
juncai
2016/03/09 01:26:50
I think for Bluetooth on Android, Chromium needs t
Finnur
2016/03/09 13:47:02
Then yeah, removing this is the right thing to do.
| |
116 | |
117 /** | |
118 * A helper class to show a clickable link with underlines turned off. | |
Yaron
2016/03/07 21:51:49
I'm not a UI-guy (+newt - should probably look at
Finnur
2016/03/08 10:26:20
It is not a huge amount of savings, but we should
juncai
2016/03/09 01:26:50
Done.
juncai
2016/03/09 01:26:50
Added a class NoUnderlineClickableSpan that both B
| |
119 */ | |
120 private class NoUnderlineClickableSpan extends ClickableSpan { | |
121 NoUnderlineClickableSpan() {} | |
122 | |
123 @Override | |
124 public void onClick(View view) { | |
125 if (mNativeUsbChooserDialogPtr == 0) { | |
126 return; | |
127 } | |
128 | |
129 nativeShowUsbOverviewLink(mNativeUsbChooserDialogPtr); | |
130 | |
131 // Get rid of the highlight background on selection. | |
132 view.invalidate(); | |
133 } | |
134 | |
135 @Override | |
136 public void updateDrawState(TextPaint textPaint) { | |
137 super.updateDrawState(textPaint); | |
138 textPaint.bgColor = Color.TRANSPARENT; | |
139 textPaint.setUnderlineText(false); | |
140 } | |
141 } | |
142 | |
143 @CalledByNative | |
144 private static UsbChooserDialog create(WindowAndroid windowAndroid, String o rigin, | |
145 int securityLevel, long nativeUsbChooserDialogPtr) { | |
146 UsbChooserDialog dialog = new UsbChooserDialog( | |
147 windowAndroid, origin, securityLevel, nativeUsbChooserDialogPtr) ; | |
148 dialog.show(); | |
149 return dialog; | |
150 } | |
151 | |
152 @CalledByNative | |
153 private void updateOptions(String[] deviceId, String[] deviceName) { | |
Finnur
2016/03/08 10:26:20
Nit: seems to me like addDevices is a better descr
juncai
2016/03/09 01:26:50
Done.
| |
154 if (deviceName == null) return; | |
Yaron
2016/03/07 21:51:49
Do this on the native side
juncai
2016/03/09 01:26:50
Done.
| |
155 mItemChooserDialog.clear(); | |
Finnur
2016/03/08 10:26:20
Are you guaranteeing that you won't receive more c
juncai
2016/03/09 01:26:50
Receiving more calls to add devices can be handled
Finnur
2016/03/09 13:47:02
Hmmm... Let me rephrase the question.
Bluetooth
juncai
2016/03/10 01:22:22
ah, I see, the USB and Bluetooth dialog implementa
Finnur
2016/03/10 12:12:56
I see.
Yeah, the Bluetooth devices could drop in
juncai
2016/03/11 23:32:33
Thanks for the comments. I added addItemToList and
| |
156 List<ItemChooserDialog.ItemChooserRow> devices = | |
Yaron
2016/03/07 21:51:49
move allocation after early-return
juncai
2016/03/09 01:26:50
Done.
| |
157 new ArrayList<ItemChooserDialog.ItemChooserRow>(); | |
158 int len = deviceId.length; | |
159 if (len == 0) { | |
160 mItemChooserDialog.setIdleState(); | |
161 return; | |
162 } | |
163 | |
164 for (int i = 0; i < len; ++i) { | |
165 devices.add(new ItemChooserDialog.ItemChooserRow(deviceId[i], device Name[i])); | |
166 } | |
167 mItemChooserDialog.addItemsToList(devices); | |
168 } | |
169 | |
170 @CalledByNative | |
171 private void closeDialog() { | |
172 mNativeUsbChooserDialogPtr = 0; | |
173 mItemChooserDialog.dismiss(); | |
174 } | |
175 | |
176 private native void nativeOnDialogFinished( | |
177 long nativeUsbChooserAndroid, int eventType, String deviceId); | |
178 // Help links. | |
Finnur
2016/03/08 10:26:20
nit: Singular (link).
juncai
2016/03/09 01:26:50
Done.
| |
179 private native void nativeShowUsbOverviewLink(long nativeUsbChooserAndroid); | |
180 } | |
OLD | NEW |