| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.device.bluetooth; | 5 package org.chromium.device.bluetooth; |
| 6 | 6 |
| 7 import android.Manifest; | 7 import android.Manifest; |
| 8 import android.annotation.TargetApi; | 8 import android.annotation.TargetApi; |
| 9 import android.bluetooth.BluetoothDevice; | 9 import android.bluetooth.BluetoothDevice; |
| 10 import android.bluetooth.le.ScanFilter; | 10 import android.bluetooth.le.ScanFilter; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 36 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 37 class Fakes { | 37 class Fakes { |
| 38 private static final String TAG = "cr.Bluetooth"; | 38 private static final String TAG = "cr.Bluetooth"; |
| 39 | 39 |
| 40 /** | 40 /** |
| 41 * Fakes android.bluetooth.BluetoothAdapter. | 41 * Fakes android.bluetooth.BluetoothAdapter. |
| 42 */ | 42 */ |
| 43 static class FakeBluetoothAdapter extends Wrappers.BluetoothAdapterWrapper { | 43 static class FakeBluetoothAdapter extends Wrappers.BluetoothAdapterWrapper { |
| 44 private final FakeContext mFakeContext; | 44 private final FakeContext mFakeContext; |
| 45 private final FakeBluetoothLeScanner mFakeScanner; | 45 private final FakeBluetoothLeScanner mFakeScanner; |
| 46 private boolean mPowered = true; |
| 46 final long mNativeBluetoothTestAndroid; | 47 final long mNativeBluetoothTestAndroid; |
| 47 | 48 |
| 48 /** | 49 /** |
| 49 * Creates a FakeBluetoothAdapter. | 50 * Creates a FakeBluetoothAdapter. |
| 50 */ | 51 */ |
| 51 @CalledByNative("FakeBluetoothAdapter") | 52 @CalledByNative("FakeBluetoothAdapter") |
| 52 public static FakeBluetoothAdapter create(long nativeBluetoothTestAndroi
d) { | 53 public static FakeBluetoothAdapter create(long nativeBluetoothTestAndroi
d) { |
| 53 Log.v(TAG, "FakeBluetoothAdapter created."); | 54 Log.v(TAG, "FakeBluetoothAdapter created."); |
| 54 return new FakeBluetoothAdapter(nativeBluetoothTestAndroid); | 55 return new FakeBluetoothAdapter(nativeBluetoothTestAndroid); |
| 55 } | 56 } |
| 56 | 57 |
| 57 private FakeBluetoothAdapter(long nativeBluetoothTestAndroid) { | 58 private FakeBluetoothAdapter(long nativeBluetoothTestAndroid) { |
| 58 super(null, new FakeContext(), new FakeBluetoothLeScanner()); | 59 super(null, new FakeContext()); |
| 59 mNativeBluetoothTestAndroid = nativeBluetoothTestAndroid; | 60 mNativeBluetoothTestAndroid = nativeBluetoothTestAndroid; |
| 60 mFakeContext = (FakeContext) mContext; | 61 mFakeContext = (FakeContext) mContext; |
| 61 mFakeScanner = (FakeBluetoothLeScanner) mScanner; | 62 mFakeScanner = new FakeBluetoothLeScanner(); |
| 62 } | 63 } |
| 63 | 64 |
| 64 @CalledByNative("FakeBluetoothAdapter") | 65 @CalledByNative("FakeBluetoothAdapter") |
| 65 public void denyPermission() { | 66 public void denyPermission() { |
| 66 mFakeContext.mPermissions.clear(); | 67 mFakeContext.mPermissions.clear(); |
| 67 } | 68 } |
| 68 | 69 |
| 69 /** | 70 /** |
| 70 * Creates and discovers a new device. | 71 * Creates and discovers a new device. |
| 71 */ | 72 */ |
| 72 @CalledByNative("FakeBluetoothAdapter") | 73 @CalledByNative("FakeBluetoothAdapter") |
| 73 public void discoverLowEnergyDevice(int deviceOrdinal) { | 74 public void discoverLowEnergyDevice(int deviceOrdinal) { |
| 75 if (mFakeScanner == null) { |
| 76 return; |
| 77 } |
| 78 |
| 74 switch (deviceOrdinal) { | 79 switch (deviceOrdinal) { |
| 75 case 1: { | 80 case 1: { |
| 76 ArrayList<ParcelUuid> uuids = new ArrayList<ParcelUuid>(2); | 81 ArrayList<ParcelUuid> uuids = new ArrayList<ParcelUuid>(2); |
| 77 uuids.add(ParcelUuid.fromString("00001800-0000-1000-8000-008
05f9b34fb")); | 82 uuids.add(ParcelUuid.fromString("00001800-0000-1000-8000-008
05f9b34fb")); |
| 78 uuids.add(ParcelUuid.fromString("00001801-0000-1000-8000-008
05f9b34fb")); | 83 uuids.add(ParcelUuid.fromString("00001801-0000-1000-8000-008
05f9b34fb")); |
| 79 | 84 |
| 80 mFakeScanner.mScanCallback.onScanResult(ScanSettings.CALLBAC
K_TYPE_ALL_MATCHES, | 85 mFakeScanner.mScanCallback.onScanResult(ScanSettings.CALLBAC
K_TYPE_ALL_MATCHES, |
| 81 new FakeScanResult(new FakeBluetoothDevice(this, "01
:00:00:90:1E:BE", | 86 new FakeScanResult(new FakeBluetoothDevice(this, "01
:00:00:90:1E:BE", |
| 82 "FakeBluetoothDevice"), | 87 "FakeBluetoothDevice"), |
| 83 uuids)); | 88 uuids)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 108 mFakeScanner.mScanCallback.onScanResult( | 113 mFakeScanner.mScanCallback.onScanResult( |
| 109 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, | 114 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, |
| 110 new FakeScanResult( | 115 new FakeScanResult( |
| 111 new FakeBluetoothDevice(this, "02:00:00:8B:7
4:63", ""), uuids)); | 116 new FakeBluetoothDevice(this, "02:00:00:8B:7
4:63", ""), uuids)); |
| 112 | 117 |
| 113 break; | 118 break; |
| 114 } | 119 } |
| 115 } | 120 } |
| 116 } | 121 } |
| 117 | 122 |
| 123 @CalledByNative("FakeBluetoothAdapter") |
| 124 public void forceIllegalStateException() { |
| 125 if (mFakeScanner != null) { |
| 126 mFakeScanner.forceIllegalStateException(); |
| 127 } |
| 128 } |
| 129 |
| 118 // ---------------------------------------------------------------------
-------------------- | 130 // ---------------------------------------------------------------------
-------------------- |
| 119 // BluetoothAdapterWrapper overrides: | 131 // BluetoothAdapterWrapper overrides: |
| 120 | 132 |
| 121 @Override | 133 @Override |
| 122 public boolean isEnabled() { | 134 public boolean disable() { |
| 135 mPowered = false; |
| 123 return true; | 136 return true; |
| 124 } | 137 } |
| 125 | 138 |
| 139 @Override |
| 140 public boolean enable() { |
| 141 mPowered = true; |
| 142 return true; |
| 143 } |
| 144 |
| 126 @Override | 145 @Override |
| 127 public String getAddress() { | 146 public String getAddress() { |
| 128 return "A1:B2:C3:D4:E5:F6"; | 147 return "A1:B2:C3:D4:E5:F6"; |
| 129 } | 148 } |
| 130 | 149 |
| 131 @Override | 150 @Override |
| 151 public Wrappers.BluetoothLeScannerWrapper getBluetoothLeScanner() { |
| 152 if (isEnabled()) { |
| 153 return mFakeScanner; |
| 154 } |
| 155 return null; |
| 156 } |
| 157 |
| 158 @Override |
| 132 public String getName() { | 159 public String getName() { |
| 133 return "FakeBluetoothAdapter"; | 160 return "FakeBluetoothAdapter"; |
| 134 } | 161 } |
| 135 | 162 |
| 136 @Override | 163 @Override |
| 137 public int getScanMode() { | 164 public int getScanMode() { |
| 138 return android.bluetooth.BluetoothAdapter.SCAN_MODE_NONE; | 165 return android.bluetooth.BluetoothAdapter.SCAN_MODE_NONE; |
| 139 } | 166 } |
| 140 | 167 |
| 141 @Override | 168 @Override |
| 169 public boolean isEnabled() { |
| 170 return mPowered; |
| 171 } |
| 172 |
| 173 @Override |
| 142 public boolean isDiscovering() { | 174 public boolean isDiscovering() { |
| 143 return false; | 175 return false; |
| 144 } | 176 } |
| 145 } | 177 } |
| 146 | 178 |
| 147 /** | 179 /** |
| 148 * Fakes android.content.Context. | 180 * Fakes android.content.Context. |
| 149 */ | 181 */ |
| 150 static class FakeContext extends Wrappers.ContextWrapper { | 182 static class FakeContext extends Wrappers.ContextWrapper { |
| 151 public final Set<String> mPermissions = new HashSet<String>(); | 183 public final Set<String> mPermissions = new HashSet<String>(); |
| 152 | 184 |
| 153 public FakeContext() { | 185 public FakeContext() { |
| 154 super(null); | 186 super(null); |
| 155 mPermissions.add(Manifest.permission.ACCESS_COARSE_LOCATION); | 187 mPermissions.add(Manifest.permission.ACCESS_COARSE_LOCATION); |
| 156 } | 188 } |
| 157 | 189 |
| 158 @Override | 190 @Override |
| 159 public boolean checkPermission(String permission) { | 191 public boolean checkPermission(String permission) { |
| 160 return mPermissions.contains(permission); | 192 return mPermissions.contains(permission); |
| 161 } | 193 } |
| 162 } | 194 } |
| 163 | 195 |
| 164 /** | 196 /** |
| 165 * Fakes android.bluetooth.le.BluetoothLeScanner. | 197 * Fakes android.bluetooth.le.BluetoothLeScanner. |
| 166 */ | 198 */ |
| 167 static class FakeBluetoothLeScanner extends Wrappers.BluetoothLeScannerWrapp
er { | 199 static class FakeBluetoothLeScanner extends Wrappers.BluetoothLeScannerWrapp
er { |
| 168 public Wrappers.ScanCallbackWrapper mScanCallback; | 200 public Wrappers.ScanCallbackWrapper mScanCallback; |
| 201 private boolean mThrowException; |
| 169 | 202 |
| 170 private FakeBluetoothLeScanner() { | 203 private FakeBluetoothLeScanner() { |
| 171 super(null); | 204 super(null); |
| 172 } | 205 } |
| 173 | 206 |
| 174 @Override | 207 @Override |
| 175 public void startScan(List<ScanFilter> filters, int scanSettingsScanMode
, | 208 public void startScan(List<ScanFilter> filters, int scanSettingsScanMode
, |
| 176 Wrappers.ScanCallbackWrapper callback) { | 209 Wrappers.ScanCallbackWrapper callback) { |
| 177 if (mScanCallback != null) { | 210 if (mScanCallback != null) { |
| 178 throw new IllegalArgumentException( | 211 throw new IllegalArgumentException( |
| 179 "FakeBluetoothLeScanner does not support multiple scans.
"); | 212 "FakeBluetoothLeScanner does not support multiple scans.
"); |
| 180 } | 213 } |
| 214 if (mThrowException) { |
| 215 throw new IllegalStateException("Adapter is off."); |
| 216 } |
| 181 mScanCallback = callback; | 217 mScanCallback = callback; |
| 182 } | 218 } |
| 183 | 219 |
| 184 @Override | 220 @Override |
| 185 public void stopScan(Wrappers.ScanCallbackWrapper callback) { | 221 public void stopScan(Wrappers.ScanCallbackWrapper callback) { |
| 186 if (mScanCallback != callback) { | 222 if (mScanCallback != callback) { |
| 187 throw new IllegalArgumentException("No scan in progress."); | 223 throw new IllegalArgumentException("No scan in progress."); |
| 188 } | 224 } |
| 225 if (mThrowException) { |
| 226 throw new IllegalStateException("Adapter is off."); |
| 227 } |
| 189 mScanCallback = null; | 228 mScanCallback = null; |
| 190 } | 229 } |
| 230 |
| 231 void forceIllegalStateException() { |
| 232 mThrowException = true; |
| 233 } |
| 191 } | 234 } |
| 192 | 235 |
| 193 /** | 236 /** |
| 194 * Fakes android.bluetooth.le.ScanResult | 237 * Fakes android.bluetooth.le.ScanResult |
| 195 */ | 238 */ |
| 196 static class FakeScanResult extends Wrappers.ScanResultWrapper { | 239 static class FakeScanResult extends Wrappers.ScanResultWrapper { |
| 197 private final FakeBluetoothDevice mDevice; | 240 private final FakeBluetoothDevice mDevice; |
| 198 private final ArrayList<ParcelUuid> mUuids; | 241 private final ArrayList<ParcelUuid> mUuids; |
| 199 | 242 |
| 200 FakeScanResult(FakeBluetoothDevice device, ArrayList<ParcelUuid> uuids)
{ | 243 FakeScanResult(FakeBluetoothDevice device, ArrayList<ParcelUuid> uuids)
{ |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 return mGatt; | 324 return mGatt; |
| 282 } | 325 } |
| 283 | 326 |
| 284 @Override | 327 @Override |
| 285 public String getAddress() { | 328 public String getAddress() { |
| 286 return mAddress; | 329 return mAddress; |
| 287 } | 330 } |
| 288 | 331 |
| 289 @Override | 332 @Override |
| 290 public int getBluetoothClass_getDeviceClass() { | 333 public int getBluetoothClass_getDeviceClass() { |
| 291 return 0x1F00; // Unspecified Device Class | 334 return Wrappers.DEVICE_CLASS_UNSPECIFIED; |
| 292 } | 335 } |
| 293 | 336 |
| 294 @Override | 337 @Override |
| 295 public int getBondState() { | 338 public int getBondState() { |
| 296 return BluetoothDevice.BOND_NONE; | 339 return BluetoothDevice.BOND_NONE; |
| 297 } | 340 } |
| 298 | 341 |
| 299 @Override | 342 @Override |
| 300 public String getName() { | 343 public String getName() { |
| 301 return mName; | 344 return mName; |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 long nativeBluetoothTestAndroid); | 738 long nativeBluetoothTestAndroid); |
| 696 | 739 |
| 697 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteCharacteristic. | 740 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteCharacteristic. |
| 698 private static native void nativeOnFakeBluetoothGattWriteCharacteristic( | 741 private static native void nativeOnFakeBluetoothGattWriteCharacteristic( |
| 699 long nativeBluetoothTestAndroid, byte[] value); | 742 long nativeBluetoothTestAndroid, byte[] value); |
| 700 | 743 |
| 701 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteDescriptor. | 744 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteDescriptor. |
| 702 private static native void nativeOnFakeBluetoothGattWriteDescriptor( | 745 private static native void nativeOnFakeBluetoothGattWriteDescriptor( |
| 703 long nativeBluetoothTestAndroid, byte[] value); | 746 long nativeBluetoothTestAndroid, byte[] value); |
| 704 } | 747 } |
| OLD | NEW |