Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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.app.Activity; | |
| 8 import android.content.Context; | |
| 9 import android.content.Intent; | |
| 10 import android.graphics.Color; | |
| 11 import android.text.SpannableString; | |
| 12 import android.text.TextPaint; | |
| 13 import android.text.TextUtils; | |
| 14 import android.text.style.ClickableSpan; | |
| 15 import android.view.View; | |
| 16 | |
| 17 import org.chromium.base.annotations.CalledByNative; | |
| 18 import org.chromium.chrome.R; | |
| 19 import org.chromium.chrome.browser.omnibox.OmniboxUrlEmphasizer; | |
| 20 import org.chromium.chrome.browser.profiles.Profile; | |
| 21 import org.chromium.ui.base.WindowAndroid; | |
| 22 import org.chromium.ui.text.SpanApplier; | |
| 23 import org.chromium.ui.text.SpanApplier.SpanInfo; | |
| 24 | |
| 25 import java.util.ArrayList; | |
| 26 import java.util.List; | |
| 27 | |
| 28 /** | |
| 29 * A dialog for picking available Bluetooth devices. | |
|
newt (away)
2015/09/01 21:51:23
Could you add a bit more context here. E.g. when i
Finnur
2015/09/02 16:01:43
Done.
| |
| 30 */ | |
| 31 public class BluetoothChooserDialog implements ItemChooserDialog.ItemSelectedCal lback { | |
| 32 Context mContext; | |
| 33 | |
| 34 // The dialog to show to let the user pick a device. | |
| 35 ItemChooserDialog mItemChooserDialog; | |
| 36 | |
| 37 // The origin for the site wanting to pair with the bluetooth devices. | |
| 38 String mOrigin; | |
| 39 | |
| 40 // The security level of the connection to the site wanting to pair with the | |
| 41 // bluetooth devices. | |
| 42 int mSecurityLevel; | |
|
newt (away)
2015/09/01 21:51:23
What are the valid values for this variable? Are t
Finnur
2015/09/02 16:01:43
Done.
| |
| 43 | |
| 44 // A pointer back to the native part of the implementation for this dialog. | |
| 45 long mNativeBluetoothChooserDialogPtr; | |
| 46 | |
| 47 // The type of link that is shown within the dialog. | |
| 48 private enum LinkType { | |
| 49 EXPLAIN_BLUETOOTH, | |
| 50 EXPLAIN_PARING, | |
| 51 ADAPTER_OFF, | |
| 52 ADAPTER_OFF_HELP, | |
| 53 RESTART_SEARCH, | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * Creates the BluetoothChooserDialog and displays it (and starts waiting fo r data). | |
| 58 * | |
| 59 * @param context Context which is used for launching a dialog. | |
| 60 */ | |
| 61 public BluetoothChooserDialog(Context context, String origin, | |
|
newt (away)
2015/09/01 21:51:23
If this is only called from create(), then make th
Finnur
2015/09/02 16:01:43
It didn't use to be, but is now. Fixed.
| |
| 62 int securityLevel, long nativeBluetoothChooserDialogPtr) { | |
| 63 mContext = context; | |
| 64 mOrigin = origin; | |
| 65 mSecurityLevel = securityLevel; | |
| 66 mNativeBluetoothChooserDialogPtr = nativeBluetoothChooserDialogPtr; | |
| 67 } | |
| 68 | |
| 69 /** | |
| 70 * Show the BluetoothChooserDialog. | |
| 71 */ | |
| 72 public void show() { | |
| 73 // Emphasize the origin. | |
| 74 Profile profile = Profile.getLastUsedProfile(); | |
| 75 SpannableString origin = new SpannableString(mOrigin); | |
| 76 OmniboxUrlEmphasizer.emphasizeUrl(origin, mContext.getResources(), | |
| 77 profile, mSecurityLevel, false, true, true); | |
| 78 // Construct a full string and replace the origin text with emphasized v ersion. | |
| 79 String message = mContext.getString(R.string.bluetooth_dialog_title, mOr igin); | |
| 80 SpannableString title = SpanApplier.applySpans( | |
| 81 message, new SpanInfo("<link>", "</link>", | |
| 82 new NoUnderlineClickableSpan(LinkType.EXPLAIN_PARING, mC ontext))); | |
| 83 int start = title.toString().indexOf(mOrigin); | |
|
newt (away)
2015/09/01 21:51:23
Instead of indexOf(), I'd use title.getSpanStart(t
Finnur
2015/09/02 16:01:43
I'n not sure how to use getSpanStart to accomplish
newt (away)
2015/09/02 17:48:42
Ah, I thought the span was wrapped around the orig
| |
| 84 TextUtils.copySpansFrom(origin, 0, origin.length(), Object.class, title, start); | |
| 85 | |
| 86 message = mContext.getString(R.string.bluetooth_not_found); | |
| 87 SpannableString noneFound = SpanApplier.applySpans( | |
| 88 message, new SpanInfo("<link>", "</link>", | |
| 89 new NoUnderlineClickableSpan(LinkType.RESTART_SEARCH, mC ontext))); | |
| 90 | |
| 91 String searching = mContext.getString(R.string.bluetooth_searching); | |
| 92 String positiveButton = mContext.getString(R.string.bluetooth_confirm_bu tton); | |
| 93 | |
| 94 SpannableString status = SpanApplier.applySpans( | |
| 95 mContext.getString(R.string.bluetooth_not_seeing_it), | |
| 96 new SpanInfo("<link1>", "</link1>", | |
| 97 new NoUnderlineClickableSpan(LinkType.RESTART_SEARCH, mC ontext)), | |
| 98 new SpanInfo("<link2>", "</link2>", | |
| 99 new NoUnderlineClickableSpan(LinkType.EXPLAIN_BLUETOOTH, mContext))); | |
| 100 | |
| 101 SpannableString errorMessage = SpanApplier.applySpans( | |
| 102 mContext.getString(R.string.bluetooth_adapter_off), | |
| 103 new SpanInfo("<link>", "</link>", | |
| 104 new NoUnderlineClickableSpan(LinkType.ADAPTER_OFF, mCont ext))); | |
| 105 SpannableString errorStatus = SpanApplier.applySpans( | |
| 106 mContext.getString(R.string.bluetooth_adapter_off_help), | |
| 107 new SpanInfo("<link>", "</link>", | |
| 108 new NoUnderlineClickableSpan(LinkType.ADAPTER_OFF_HELP, mContext))); | |
| 109 | |
| 110 ItemChooserDialog.ItemChooserLabels labels = | |
| 111 new ItemChooserDialog.ItemChooserLabels(title, searching, noneFo und, status, | |
| 112 errorMessage, errorStatus, positiveButton); | |
| 113 mItemChooserDialog = new ItemChooserDialog(mContext, this, labels); | |
| 114 } | |
| 115 | |
| 116 @Override | |
| 117 public void onItemSelected(String id) { | |
| 118 if (mNativeBluetoothChooserDialogPtr != 0) { | |
| 119 nativeReportDeviceSelected(mNativeBluetoothChooserDialogPtr, id); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * A helper class to show a clickable link with underlines turned off. | |
| 125 */ | |
| 126 private class NoUnderlineClickableSpan extends ClickableSpan { | |
| 127 // The type of link this span represents. | |
| 128 private LinkType mLinkType; | |
| 129 | |
| 130 // TODO(finnur): Remove this variable when toasts have been eliminated. | |
| 131 private Context mContext; | |
| 132 | |
| 133 NoUnderlineClickableSpan(LinkType linkType, Context context) { | |
| 134 mLinkType = linkType; | |
| 135 mContext = context; | |
| 136 } | |
| 137 | |
| 138 @Override | |
| 139 public void onClick(View view) { | |
| 140 if (mNativeBluetoothChooserDialogPtr == 0) { | |
| 141 return; | |
| 142 } | |
| 143 | |
| 144 switch (mLinkType) { | |
| 145 case EXPLAIN_BLUETOOTH: { | |
| 146 if (mNativeBluetoothChooserDialogPtr != 0) { | |
|
newt (away)
2015/09/01 21:51:23
This if check is redundant, since you already chec
Finnur
2015/09/02 16:01:43
Ah. Forgot about this. Fixed.
| |
| 147 nativeShowBluetoothOverviewLink(mNativeBluetoothChooserD ialogPtr); | |
| 148 closeDialog(); | |
| 149 } | |
| 150 break; | |
| 151 } | |
| 152 case EXPLAIN_PARING: { | |
| 153 if (mNativeBluetoothChooserDialogPtr != 0) { | |
| 154 nativeShowBluetoothPairingLink(mNativeBluetoothChooserDi alogPtr); | |
| 155 closeDialog(); | |
| 156 } | |
| 157 break; | |
| 158 } | |
| 159 case ADAPTER_OFF: { | |
| 160 Intent intent = new Intent(); | |
| 161 intent.setAction(android.provider.Settings.ACTION_BLUETOOTH_ SETTINGS); | |
| 162 mContext.startActivity(intent); | |
| 163 break; | |
| 164 } | |
| 165 case ADAPTER_OFF_HELP: { | |
| 166 if (mNativeBluetoothChooserDialogPtr != 0) { | |
| 167 nativeShowBluetoothAdapterOffLink(mNativeBluetoothChoose rDialogPtr); | |
| 168 closeDialog(); | |
| 169 } | |
| 170 break; | |
| 171 } | |
| 172 case RESTART_SEARCH: { | |
| 173 mItemChooserDialog.clear(); | |
| 174 if (mNativeBluetoothChooserDialogPtr != 0) { | |
| 175 nativeReportRestartSearch(mNativeBluetoothChooserDialogP tr); | |
| 176 } | |
| 177 break; | |
| 178 } | |
| 179 default: | |
| 180 assert false; | |
| 181 } | |
| 182 | |
| 183 // Get rid of the highlight background on selection. | |
| 184 view.invalidate(); | |
| 185 } | |
| 186 | |
| 187 @Override | |
| 188 public void updateDrawState(TextPaint textPaint) { | |
| 189 super.updateDrawState(textPaint); | |
| 190 textPaint.bgColor = Color.TRANSPARENT; | |
| 191 textPaint.setUnderlineText(false); | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 @CalledByNative | |
| 196 private static BluetoothChooserDialog create( | |
| 197 WindowAndroid windowAndroid, String origin, int securityLevel, | |
| 198 long nativeBluetoothChooserDialogPtr) { | |
| 199 Activity activity = windowAndroid.getActivity().get(); | |
|
newt (away)
2015/09/01 21:51:23
windowAndroid.getActivity().get() can be null
Finnur
2015/09/02 16:01:43
Yes, I thought about that. Can we expect it to be
Ted C
2015/09/02 17:27:47
There are a couple cases where it can be null.
1.
Finnur
2015/09/03 11:51:45
Thanks for the explanation! I think I'll go with a
| |
| 200 BluetoothChooserDialog dialog = new BluetoothChooserDialog( | |
| 201 activity, origin, securityLevel, nativeBluetoothChooserDialogPtr ); | |
| 202 dialog.show(); | |
| 203 return dialog; | |
| 204 } | |
| 205 | |
| 206 @CalledByNative | |
| 207 private void addDevice(String deviceId, String deviceName) { | |
| 208 List<ItemChooserDialog.ItemChooserRow> devices = | |
| 209 new ArrayList<ItemChooserDialog.ItemChooserRow>(); | |
| 210 devices.add(new ItemChooserDialog.ItemChooserRow(deviceId, deviceName)); | |
| 211 mItemChooserDialog.showList(devices); | |
| 212 } | |
| 213 | |
| 214 @CalledByNative | |
| 215 private void closeDialog() { | |
|
newt (away)
2015/09/01 21:51:23
If the user closes the dialog via Java (e.g. by pr
Finnur
2015/09/02 16:01:43
The native side is owned by the RequestDeviceSessi
| |
| 216 mNativeBluetoothChooserDialogPtr = 0; | |
| 217 mItemChooserDialog.dismiss(); | |
| 218 } | |
| 219 | |
| 220 @CalledByNative | |
| 221 private void removeDevice(String deviceId) { | |
| 222 mItemChooserDialog.setAvailability(deviceId, false); | |
| 223 } | |
| 224 | |
| 225 @CalledByNative | |
| 226 private void notifyAdapterTurnedOff() { | |
| 227 mItemChooserDialog.setErrorState(); | |
| 228 } | |
| 229 | |
| 230 private native void nativeReportDeviceSelected( | |
| 231 long nativeBluetoothChooserAndroid, String deviceId); | |
| 232 private native void nativeReportRestartSearch(long nativeBluetoothChooserAnd roid); | |
| 233 // Help links. | |
| 234 private native void nativeShowBluetoothOverviewLink(long nativeBluetoothChoo serAndroid); | |
| 235 private native void nativeShowBluetoothPairingLink(long nativeBluetoothChoos erAndroid); | |
| 236 private native void nativeShowBluetoothAdapterOffLink(long nativeBluetoothCh ooserAndroid); | |
| 237 } | |
| OLD | NEW |