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. This dialog is shown when a
website requests to |
| 30 * pair with a certain class of Bluetooth devices (e.g. through a bluetooth.requ
estDevice Javascript |
| 31 * call). |
| 32 */ |
| 33 public class BluetoothChooserDialog implements ItemChooserDialog.ItemSelectedCal
lback { |
| 34 Context mContext; |
| 35 |
| 36 // The dialog to show to let the user pick a device. |
| 37 ItemChooserDialog mItemChooserDialog; |
| 38 |
| 39 // The origin for the site wanting to pair with the bluetooth devices. |
| 40 String mOrigin; |
| 41 |
| 42 // The security level of the connection to the site wanting to pair with the |
| 43 // bluetooth devices. For valid values see connection_security::SecurityLeve
l. |
| 44 int mSecurityLevel; |
| 45 |
| 46 // A pointer back to the native part of the implementation for this dialog. |
| 47 long mNativeBluetoothChooserDialogPtr; |
| 48 |
| 49 // The type of link that is shown within the dialog. |
| 50 private enum LinkType { |
| 51 EXPLAIN_BLUETOOTH, |
| 52 EXPLAIN_PARING, |
| 53 ADAPTER_OFF, |
| 54 ADAPTER_OFF_HELP, |
| 55 RESTART_SEARCH, |
| 56 } |
| 57 |
| 58 /** |
| 59 * Creates the BluetoothChooserDialog and displays it (and starts waiting fo
r data). |
| 60 * |
| 61 * @param context Context which is used for launching a dialog. |
| 62 */ |
| 63 private BluetoothChooserDialog(Context context, String origin, int securityL
evel, |
| 64 long nativeBluetoothChooserDialogPtr) { |
| 65 mContext = context; |
| 66 mOrigin = origin; |
| 67 mSecurityLevel = securityLevel; |
| 68 mNativeBluetoothChooserDialogPtr = nativeBluetoothChooserDialogPtr; |
| 69 } |
| 70 |
| 71 /** |
| 72 * Show the BluetoothChooserDialog. |
| 73 */ |
| 74 private void show() { |
| 75 // Emphasize the origin. |
| 76 Profile profile = Profile.getLastUsedProfile(); |
| 77 SpannableString origin = new SpannableString(mOrigin); |
| 78 OmniboxUrlEmphasizer.emphasizeUrl( |
| 79 origin, mContext.getResources(), profile, mSecurityLevel, false,
true, true); |
| 80 // Construct a full string and replace the origin text with emphasized v
ersion. |
| 81 String message = mContext.getString(R.string.bluetooth_dialog_title, mOr
igin); |
| 82 SpannableString title = SpanApplier.applySpans( |
| 83 message, new SpanInfo("<link>", "</link>", |
| 84 new NoUnderlineClickableSpan(LinkType.EXPLAIN_PARING, mC
ontext))); |
| 85 int start = title.toString().indexOf(mOrigin); |
| 86 TextUtils.copySpansFrom(origin, 0, origin.length(), Object.class, title,
start); |
| 87 |
| 88 message = mContext.getString(R.string.bluetooth_not_found); |
| 89 SpannableString noneFound = SpanApplier.applySpans( |
| 90 message, new SpanInfo("<link>", "</link>", |
| 91 new NoUnderlineClickableSpan(LinkType.RESTART_SEARCH, mC
ontext))); |
| 92 |
| 93 String searching = mContext.getString(R.string.bluetooth_searching); |
| 94 String positiveButton = mContext.getString(R.string.bluetooth_confirm_bu
tton); |
| 95 |
| 96 SpannableString status = SpanApplier.applySpans( |
| 97 mContext.getString(R.string.bluetooth_not_seeing_it), |
| 98 new SpanInfo("<link1>", "</link1>", |
| 99 new NoUnderlineClickableSpan(LinkType.RESTART_SEARCH, mC
ontext)), |
| 100 new SpanInfo("<link2>", "</link2>", |
| 101 new NoUnderlineClickableSpan(LinkType.EXPLAIN_BLUETOOTH,
mContext))); |
| 102 |
| 103 SpannableString errorMessage = SpanApplier.applySpans( |
| 104 mContext.getString(R.string.bluetooth_adapter_off), |
| 105 new SpanInfo("<link>", "</link>", |
| 106 new NoUnderlineClickableSpan(LinkType.ADAPTER_OFF, mCont
ext))); |
| 107 SpannableString errorStatus = SpanApplier.applySpans( |
| 108 mContext.getString(R.string.bluetooth_adapter_off_help), |
| 109 new SpanInfo("<link>", "</link>", |
| 110 new NoUnderlineClickableSpan(LinkType.ADAPTER_OFF_HELP,
mContext))); |
| 111 |
| 112 ItemChooserDialog.ItemChooserLabels labels = new ItemChooserDialog.ItemC
hooserLabels( |
| 113 title, searching, noneFound, status, errorMessage, errorStatus,
positiveButton); |
| 114 mItemChooserDialog = new ItemChooserDialog(mContext, this, labels); |
| 115 } |
| 116 |
| 117 @Override |
| 118 public void onItemSelected(String id) { |
| 119 if (mNativeBluetoothChooserDialogPtr != 0) { |
| 120 nativeOnDeviceSelected(mNativeBluetoothChooserDialogPtr, id); |
| 121 } |
| 122 } |
| 123 |
| 124 /** |
| 125 * A helper class to show a clickable link with underlines turned off. |
| 126 */ |
| 127 private class NoUnderlineClickableSpan extends ClickableSpan { |
| 128 // The type of link this span represents. |
| 129 private LinkType mLinkType; |
| 130 |
| 131 // TODO(finnur): Remove this variable when toasts have been eliminated. |
| 132 private Context mContext; |
| 133 |
| 134 NoUnderlineClickableSpan(LinkType linkType, Context context) { |
| 135 mLinkType = linkType; |
| 136 mContext = context; |
| 137 } |
| 138 |
| 139 @Override |
| 140 public void onClick(View view) { |
| 141 if (mNativeBluetoothChooserDialogPtr == 0) { |
| 142 return; |
| 143 } |
| 144 |
| 145 switch (mLinkType) { |
| 146 case EXPLAIN_BLUETOOTH: { |
| 147 nativeShowBluetoothOverviewLink(mNativeBluetoothChooserDialo
gPtr); |
| 148 closeDialog(); |
| 149 break; |
| 150 } |
| 151 case EXPLAIN_PARING: { |
| 152 nativeShowBluetoothPairingLink(mNativeBluetoothChooserDialog
Ptr); |
| 153 closeDialog(); |
| 154 break; |
| 155 } |
| 156 case ADAPTER_OFF: { |
| 157 Intent intent = new Intent(); |
| 158 intent.setAction(android.provider.Settings.ACTION_BLUETOOTH_
SETTINGS); |
| 159 mContext.startActivity(intent); |
| 160 break; |
| 161 } |
| 162 case ADAPTER_OFF_HELP: { |
| 163 nativeShowBluetoothAdapterOffLink(mNativeBluetoothChooserDia
logPtr); |
| 164 closeDialog(); |
| 165 break; |
| 166 } |
| 167 case RESTART_SEARCH: { |
| 168 mItemChooserDialog.clear(); |
| 169 nativeRestartSearch(mNativeBluetoothChooserDialogPtr); |
| 170 break; |
| 171 } |
| 172 default: |
| 173 assert false; |
| 174 } |
| 175 |
| 176 // Get rid of the highlight background on selection. |
| 177 view.invalidate(); |
| 178 } |
| 179 |
| 180 @Override |
| 181 public void updateDrawState(TextPaint textPaint) { |
| 182 super.updateDrawState(textPaint); |
| 183 textPaint.bgColor = Color.TRANSPARENT; |
| 184 textPaint.setUnderlineText(false); |
| 185 } |
| 186 } |
| 187 |
| 188 @CalledByNative |
| 189 private static BluetoothChooserDialog create(WindowAndroid windowAndroid, St
ring origin, |
| 190 int securityLevel, long nativeBluetoothChooserDialogPtr) { |
| 191 Activity activity = windowAndroid.getActivity().get(); |
| 192 assert activity != null; |
| 193 BluetoothChooserDialog dialog = new BluetoothChooserDialog( |
| 194 activity, origin, securityLevel, nativeBluetoothChooserDialogPtr
); |
| 195 dialog.show(); |
| 196 return dialog; |
| 197 } |
| 198 |
| 199 @CalledByNative |
| 200 private void addDevice(String deviceId, String deviceName) { |
| 201 List<ItemChooserDialog.ItemChooserRow> devices = |
| 202 new ArrayList<ItemChooserDialog.ItemChooserRow>(); |
| 203 devices.add(new ItemChooserDialog.ItemChooserRow(deviceId, deviceName)); |
| 204 mItemChooserDialog.showList(devices); |
| 205 } |
| 206 |
| 207 @CalledByNative |
| 208 private void closeDialog() { |
| 209 mNativeBluetoothChooserDialogPtr = 0; |
| 210 mItemChooserDialog.dismiss(); |
| 211 } |
| 212 |
| 213 @CalledByNative |
| 214 private void removeDevice(String deviceId) { |
| 215 mItemChooserDialog.setEnabled(deviceId, false); |
| 216 } |
| 217 |
| 218 @CalledByNative |
| 219 private void notifyAdapterTurnedOff() { |
| 220 mItemChooserDialog.setErrorState(); |
| 221 } |
| 222 |
| 223 private native void nativeOnDeviceSelected(long nativeBluetoothChooserAndroi
d, String deviceId); |
| 224 private native void nativeRestartSearch(long nativeBluetoothChooserAndroid); |
| 225 // Help links. |
| 226 private native void nativeShowBluetoothOverviewLink(long nativeBluetoothChoo
serAndroid); |
| 227 private native void nativeShowBluetoothPairingLink(long nativeBluetoothChoos
erAndroid); |
| 228 private native void nativeShowBluetoothAdapterOffLink(long nativeBluetoothCh
ooserAndroid); |
| 229 } |
OLD | NEW |