Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.companiondevicemanager; |
| 18 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 19 | import static android.companion.BluetoothDeviceFilterUtils.getDeviceDisplayNameInternal; |
| 20 | import static android.companion.BluetoothDeviceFilterUtils.getDeviceMacAddress; |
| 21 | |
| 22 | import static com.android.internal.util.ArrayUtils.isEmpty; |
Eugene Susla | 0c4a926 | 2017-06-09 18:03:14 -0700 | [diff] [blame] | 23 | import static com.android.internal.util.CollectionUtils.emptyIfNull; |
| 24 | import static com.android.internal.util.CollectionUtils.size; |
Eugene Susla | 158d5e7 | 2018-02-27 14:32:21 -0800 | [diff] [blame] | 25 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 26 | |
| 27 | import android.annotation.NonNull; |
| 28 | import android.annotation.Nullable; |
| 29 | import android.app.PendingIntent; |
| 30 | import android.app.Service; |
| 31 | import android.bluetooth.BluetoothAdapter; |
| 32 | import android.bluetooth.BluetoothDevice; |
| 33 | import android.bluetooth.BluetoothManager; |
| 34 | import android.bluetooth.le.BluetoothLeScanner; |
| 35 | import android.bluetooth.le.ScanCallback; |
| 36 | import android.bluetooth.le.ScanFilter; |
| 37 | import android.bluetooth.le.ScanResult; |
| 38 | import android.bluetooth.le.ScanSettings; |
| 39 | import android.companion.AssociationRequest; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 40 | import android.companion.BluetoothDeviceFilter; |
Eugene Susla | 722463f | 2017-04-19 15:22:39 -0700 | [diff] [blame] | 41 | import android.companion.BluetoothLeDeviceFilter; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 42 | import android.companion.DeviceFilter; |
Svet Ganov | da0acdf | 2017-02-15 10:28:51 -0800 | [diff] [blame] | 43 | import android.companion.ICompanionDeviceDiscoveryService; |
| 44 | import android.companion.ICompanionDeviceDiscoveryServiceCallback; |
| 45 | import android.companion.IFindDeviceCallback; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 46 | import android.companion.WifiDeviceFilter; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 47 | import android.content.BroadcastReceiver; |
| 48 | import android.content.Context; |
| 49 | import android.content.Intent; |
| 50 | import android.content.IntentFilter; |
| 51 | import android.graphics.Color; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 52 | import android.graphics.drawable.Drawable; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 53 | import android.net.wifi.WifiManager; |
Eugene Susla | 158d5e7 | 2018-02-27 14:32:21 -0800 | [diff] [blame] | 54 | import android.os.Handler; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 55 | import android.os.IBinder; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 56 | import android.os.Parcelable; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 57 | import android.os.RemoteException; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 58 | import android.text.TextUtils; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 59 | import android.util.Log; |
| 60 | import android.view.View; |
| 61 | import android.view.ViewGroup; |
| 62 | import android.widget.ArrayAdapter; |
| 63 | import android.widget.TextView; |
| 64 | |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 65 | import com.android.internal.util.ArrayUtils; |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 66 | import com.android.internal.util.CollectionUtils; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 67 | import com.android.internal.util.Preconditions; |
| 68 | |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 69 | import java.util.ArrayList; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 70 | import java.util.List; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 71 | import java.util.Objects; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 72 | |
| 73 | public class DeviceDiscoveryService extends Service { |
| 74 | |
| 75 | private static final boolean DEBUG = false; |
| 76 | private static final String LOG_TAG = "DeviceDiscoveryService"; |
| 77 | |
Eugene Susla | 158d5e7 | 2018-02-27 14:32:21 -0800 | [diff] [blame] | 78 | private static final long SCAN_TIMEOUT = 20000; |
| 79 | |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 80 | static DeviceDiscoveryService sInstance; |
| 81 | |
| 82 | private BluetoothAdapter mBluetoothAdapter; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 83 | private WifiManager mWifiManager; |
Eugene Susla | 7a090a1 | 2017-06-28 10:58:46 -0700 | [diff] [blame] | 84 | @Nullable private BluetoothLeScanner mBLEScanner; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 85 | private ScanSettings mDefaultScanSettings = new ScanSettings.Builder().build(); |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 86 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 87 | private List<DeviceFilter<?>> mFilters; |
Eugene Susla | 722463f | 2017-04-19 15:22:39 -0700 | [diff] [blame] | 88 | private List<BluetoothLeDeviceFilter> mBLEFilters; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 89 | private List<BluetoothDeviceFilter> mBluetoothFilters; |
| 90 | private List<WifiDeviceFilter> mWifiFilters; |
| 91 | private List<ScanFilter> mBLEScanFilters; |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 92 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 93 | AssociationRequest mRequest; |
| 94 | List<DeviceFilterPair> mDevicesFound; |
| 95 | DeviceFilterPair mSelectedDevice; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 96 | DevicesAdapter mDevicesAdapter; |
Svet Ganov | da0acdf | 2017-02-15 10:28:51 -0800 | [diff] [blame] | 97 | IFindDeviceCallback mFindCallback; |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 98 | |
Svet Ganov | da0acdf | 2017-02-15 10:28:51 -0800 | [diff] [blame] | 99 | ICompanionDeviceDiscoveryServiceCallback mServiceCallback; |
Eugene Susla | 158d5e7 | 2018-02-27 14:32:21 -0800 | [diff] [blame] | 100 | boolean mIsScanning = false; |
| 101 | @Nullable DeviceChooserActivity mActivity = null; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 102 | |
Svet Ganov | da0acdf | 2017-02-15 10:28:51 -0800 | [diff] [blame] | 103 | private final ICompanionDeviceDiscoveryService mBinder = |
| 104 | new ICompanionDeviceDiscoveryService.Stub() { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 105 | @Override |
| 106 | public void startDiscovery(AssociationRequest request, |
Svet Ganov | da0acdf | 2017-02-15 10:28:51 -0800 | [diff] [blame] | 107 | String callingPackage, |
| 108 | IFindDeviceCallback findCallback, |
| 109 | ICompanionDeviceDiscoveryServiceCallback serviceCallback) { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 110 | if (DEBUG) { |
| 111 | Log.i(LOG_TAG, |
Svet Ganov | da0acdf | 2017-02-15 10:28:51 -0800 | [diff] [blame] | 112 | "startDiscovery() called with: filter = [" + request |
| 113 | + "], findCallback = [" + findCallback + "]" |
| 114 | + "], serviceCallback = [" + serviceCallback + "]"); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 115 | } |
Svet Ganov | da0acdf | 2017-02-15 10:28:51 -0800 | [diff] [blame] | 116 | mFindCallback = findCallback; |
| 117 | mServiceCallback = serviceCallback; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 118 | DeviceDiscoveryService.this.startDiscovery(request); |
| 119 | } |
| 120 | }; |
| 121 | |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 122 | private ScanCallback mBLEScanCallback; |
| 123 | private BluetoothBroadcastReceiver mBluetoothBroadcastReceiver; |
| 124 | private WifiBroadcastReceiver mWifiBroadcastReceiver; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 125 | |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 126 | @Override |
| 127 | public IBinder onBind(Intent intent) { |
| 128 | if (DEBUG) Log.i(LOG_TAG, "onBind(" + intent + ")"); |
| 129 | return mBinder.asBinder(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void onCreate() { |
| 134 | super.onCreate(); |
| 135 | |
| 136 | if (DEBUG) Log.i(LOG_TAG, "onCreate()"); |
| 137 | |
| 138 | mBluetoothAdapter = getSystemService(BluetoothManager.class).getAdapter(); |
| 139 | mBLEScanner = mBluetoothAdapter.getBluetoothLeScanner(); |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 140 | mWifiManager = getSystemService(WifiManager.class); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 141 | |
| 142 | mDevicesFound = new ArrayList<>(); |
| 143 | mDevicesAdapter = new DevicesAdapter(); |
| 144 | |
| 145 | sInstance = this; |
| 146 | } |
| 147 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 148 | private void startDiscovery(AssociationRequest request) { |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 149 | if (!request.equals(mRequest)) { |
| 150 | mRequest = request; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 151 | |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 152 | mFilters = request.getDeviceFilters(); |
| 153 | mWifiFilters = CollectionUtils.filter(mFilters, WifiDeviceFilter.class); |
| 154 | mBluetoothFilters = CollectionUtils.filter(mFilters, BluetoothDeviceFilter.class); |
Eugene Susla | 722463f | 2017-04-19 15:22:39 -0700 | [diff] [blame] | 155 | mBLEFilters = CollectionUtils.filter(mFilters, BluetoothLeDeviceFilter.class); |
| 156 | mBLEScanFilters = CollectionUtils.map(mBLEFilters, BluetoothLeDeviceFilter::getScanFilter); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 157 | |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 158 | reset(); |
Eugene Susla | 3c9aa17 | 2017-03-28 15:04:12 -0700 | [diff] [blame] | 159 | } else if (DEBUG) Log.i(LOG_TAG, "startDiscovery: duplicate request: " + request); |
| 160 | |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 161 | if (!ArrayUtils.isEmpty(mDevicesFound)) { |
| 162 | onReadyToShowUI(); |
| 163 | } |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 164 | |
Eugene Susla | 0c4a926 | 2017-06-09 18:03:14 -0700 | [diff] [blame] | 165 | // If filtering to get single device by mac address, also search in the set of already |
| 166 | // bonded devices to allow linking those directly |
| 167 | String singleMacAddressFilter = null; |
| 168 | if (mRequest.isSingleDevice()) { |
| 169 | int numFilters = size(mBluetoothFilters); |
| 170 | for (int i = 0; i < numFilters; i++) { |
| 171 | BluetoothDeviceFilter filter = mBluetoothFilters.get(i); |
| 172 | if (!TextUtils.isEmpty(filter.getAddress())) { |
| 173 | singleMacAddressFilter = filter.getAddress(); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | if (singleMacAddressFilter != null) { |
| 179 | for (BluetoothDevice dev : emptyIfNull(mBluetoothAdapter.getBondedDevices())) { |
| 180 | onDeviceFound(DeviceFilterPair.findMatch(dev, mBluetoothFilters)); |
| 181 | } |
| 182 | } |
| 183 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 184 | if (shouldScan(mBluetoothFilters)) { |
| 185 | final IntentFilter intentFilter = new IntentFilter(); |
| 186 | intentFilter.addAction(BluetoothDevice.ACTION_FOUND); |
| 187 | intentFilter.addAction(BluetoothDevice.ACTION_DISAPPEARED); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 188 | |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 189 | mBluetoothBroadcastReceiver = new BluetoothBroadcastReceiver(); |
| 190 | registerReceiver(mBluetoothBroadcastReceiver, intentFilter); |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 191 | mBluetoothAdapter.startDiscovery(); |
| 192 | } |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 193 | |
Eugene Susla | 7a090a1 | 2017-06-28 10:58:46 -0700 | [diff] [blame] | 194 | if (shouldScan(mBLEFilters) && mBLEScanner != null) { |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 195 | mBLEScanCallback = new BLEScanCallback(); |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 196 | mBLEScanner.startScan(mBLEScanFilters, mDefaultScanSettings, mBLEScanCallback); |
| 197 | } |
| 198 | |
| 199 | if (shouldScan(mWifiFilters)) { |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 200 | mWifiBroadcastReceiver = new WifiBroadcastReceiver(); |
| 201 | registerReceiver(mWifiBroadcastReceiver, |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 202 | new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); |
| 203 | mWifiManager.startScan(); |
| 204 | } |
Eugene Susla | 158d5e7 | 2018-02-27 14:32:21 -0800 | [diff] [blame] | 205 | mIsScanning = true; |
| 206 | Handler.getMain().sendMessageDelayed( |
| 207 | obtainMessage(DeviceDiscoveryService::stopScan, this), |
| 208 | SCAN_TIMEOUT); |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | private boolean shouldScan(List<? extends DeviceFilter> mediumSpecificFilters) { |
| 212 | return !isEmpty(mediumSpecificFilters) || isEmpty(mFilters); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | private void reset() { |
Eugene Susla | 3c9aa17 | 2017-03-28 15:04:12 -0700 | [diff] [blame] | 216 | if (DEBUG) Log.i(LOG_TAG, "reset()"); |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 217 | stopScan(); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 218 | mDevicesFound.clear(); |
| 219 | mSelectedDevice = null; |
| 220 | mDevicesAdapter.notifyDataSetChanged(); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public boolean onUnbind(Intent intent) { |
| 225 | stopScan(); |
| 226 | return super.onUnbind(intent); |
| 227 | } |
| 228 | |
| 229 | private void stopScan() { |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 230 | if (DEBUG) Log.i(LOG_TAG, "stopScan()"); |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 231 | |
Eugene Susla | 158d5e7 | 2018-02-27 14:32:21 -0800 | [diff] [blame] | 232 | if (!mIsScanning) return; |
| 233 | mIsScanning = false; |
| 234 | |
| 235 | DeviceChooserActivity activity = mActivity; |
| 236 | if (activity != null) { |
Eugene Susla | a6d5482 | 2018-04-23 11:16:47 -0700 | [diff] [blame] | 237 | if (activity.mDeviceListView != null) { |
| 238 | activity.mDeviceListView.removeFooterView(activity.mLoadingIndicator); |
| 239 | } |
Eugene Susla | 158d5e7 | 2018-02-27 14:32:21 -0800 | [diff] [blame] | 240 | mActivity = null; |
| 241 | } |
| 242 | |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 243 | mBluetoothAdapter.cancelDiscovery(); |
| 244 | if (mBluetoothBroadcastReceiver != null) { |
| 245 | unregisterReceiver(mBluetoothBroadcastReceiver); |
| 246 | mBluetoothBroadcastReceiver = null; |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 247 | } |
Eugene Susla | 7a090a1 | 2017-06-28 10:58:46 -0700 | [diff] [blame] | 248 | if (mBLEScanner != null) mBLEScanner.stopScan(mBLEScanCallback); |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 249 | if (mWifiBroadcastReceiver != null) { |
| 250 | unregisterReceiver(mWifiBroadcastReceiver); |
| 251 | mWifiBroadcastReceiver = null; |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 252 | } |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 255 | private void onDeviceFound(@Nullable DeviceFilterPair device) { |
Eugene Susla | 0c4a926 | 2017-06-09 18:03:14 -0700 | [diff] [blame] | 256 | if (device == null) return; |
| 257 | |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 258 | if (mDevicesFound.contains(device)) { |
| 259 | return; |
| 260 | } |
| 261 | |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 262 | if (DEBUG) Log.i(LOG_TAG, "Found device " + device); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 263 | |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 264 | if (mDevicesFound.isEmpty()) { |
| 265 | onReadyToShowUI(); |
| 266 | } |
| 267 | mDevicesFound.add(device); |
| 268 | mDevicesAdapter.notifyDataSetChanged(); |
| 269 | } |
| 270 | |
| 271 | //TODO also, on timeout -> call onFailure |
| 272 | private void onReadyToShowUI() { |
| 273 | try { |
Svet Ganov | da0acdf | 2017-02-15 10:28:51 -0800 | [diff] [blame] | 274 | mFindCallback.onSuccess(PendingIntent.getActivity( |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 275 | this, 0, |
| 276 | new Intent(this, DeviceChooserActivity.class), |
| 277 | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT |
| 278 | | PendingIntent.FLAG_IMMUTABLE)); |
| 279 | } catch (RemoteException e) { |
| 280 | throw new RuntimeException(e); |
| 281 | } |
| 282 | } |
| 283 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 284 | private void onDeviceLost(@Nullable DeviceFilterPair device) { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 285 | mDevicesFound.remove(device); |
| 286 | mDevicesAdapter.notifyDataSetChanged(); |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 287 | if (DEBUG) Log.i(LOG_TAG, "Lost device " + device.getDisplayName()); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Eugene Susla | 47aafbe | 2017-02-13 12:46:46 -0800 | [diff] [blame] | 290 | void onDeviceSelected(String callingPackage, String deviceAddress) { |
| 291 | try { |
| 292 | mServiceCallback.onDeviceSelected( |
| 293 | //TODO is this the right userId? |
| 294 | callingPackage, getUserId(), deviceAddress); |
| 295 | } catch (RemoteException e) { |
| 296 | Log.e(LOG_TAG, "Failed to record association: " |
| 297 | + callingPackage + " <-> " + deviceAddress); |
| 298 | } |
| 299 | } |
| 300 | |
Eugene Susla | 200c37f | 2017-03-28 15:40:44 -0700 | [diff] [blame] | 301 | void onCancel() { |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 302 | if (DEBUG) Log.i(LOG_TAG, "onCancel()"); |
Eugene Susla | 200c37f | 2017-03-28 15:40:44 -0700 | [diff] [blame] | 303 | try { |
| 304 | mServiceCallback.onDeviceSelectionCancel(); |
| 305 | } catch (RemoteException e) { |
| 306 | throw new RuntimeException(e); |
| 307 | } |
| 308 | } |
| 309 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 310 | class DevicesAdapter extends ArrayAdapter<DeviceFilterPair> { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 311 | private Drawable BLUETOOTH_ICON = icon(android.R.drawable.stat_sys_data_bluetooth); |
Eugene Susla | 254b373 | 2017-04-18 13:45:17 -0700 | [diff] [blame] | 312 | private Drawable WIFI_ICON = icon(com.android.internal.R.drawable.ic_wifi_signal_3); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 313 | |
| 314 | private Drawable icon(int drawableRes) { |
| 315 | Drawable icon = getResources().getDrawable(drawableRes, null); |
| 316 | icon.setTint(Color.DKGRAY); |
| 317 | return icon; |
| 318 | } |
| 319 | |
| 320 | public DevicesAdapter() { |
| 321 | super(DeviceDiscoveryService.this, 0, mDevicesFound); |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public View getView( |
| 326 | int position, |
| 327 | @Nullable View convertView, |
| 328 | @NonNull ViewGroup parent) { |
| 329 | TextView view = convertView instanceof TextView |
| 330 | ? (TextView) convertView |
| 331 | : newView(); |
| 332 | bind(view, getItem(position)); |
| 333 | return view; |
| 334 | } |
| 335 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 336 | private void bind(TextView textView, DeviceFilterPair device) { |
| 337 | textView.setText(device.getDisplayName()); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 338 | textView.setBackgroundColor( |
| 339 | device.equals(mSelectedDevice) |
| 340 | ? Color.GRAY |
| 341 | : Color.TRANSPARENT); |
Eugene Susla | 254b373 | 2017-04-18 13:45:17 -0700 | [diff] [blame] | 342 | textView.setCompoundDrawablesWithIntrinsicBounds( |
| 343 | device.device instanceof android.net.wifi.ScanResult |
| 344 | ? WIFI_ICON |
| 345 | : BLUETOOTH_ICON, |
| 346 | null, null, null); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 347 | textView.setOnClickListener((view) -> { |
| 348 | mSelectedDevice = device; |
| 349 | notifyDataSetChanged(); |
| 350 | }); |
| 351 | } |
| 352 | |
| 353 | //TODO move to a layout file |
| 354 | private TextView newView() { |
| 355 | final TextView textView = new TextView(DeviceDiscoveryService.this); |
| 356 | textView.setTextColor(Color.BLACK); |
| 357 | final int padding = DeviceChooserActivity.getPadding(getResources()); |
| 358 | textView.setPadding(padding, padding, padding, padding); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 359 | textView.setCompoundDrawablePadding(padding); |
| 360 | return textView; |
| 361 | } |
| 362 | } |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 363 | |
| 364 | /** |
| 365 | * A pair of device and a filter that matched this device if any. |
| 366 | * |
| 367 | * @param <T> device type |
| 368 | */ |
| 369 | static class DeviceFilterPair<T extends Parcelable> { |
| 370 | public final T device; |
| 371 | @Nullable |
| 372 | public final DeviceFilter<T> filter; |
| 373 | |
| 374 | private DeviceFilterPair(T device, @Nullable DeviceFilter<T> filter) { |
| 375 | this.device = device; |
| 376 | this.filter = filter; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * {@code (device, null)} if the filters list is empty or null |
| 381 | * {@code null} if none of the provided filters match the device |
| 382 | * {@code (device, filter)} where filter is among the list of filters and matches the device |
| 383 | */ |
| 384 | @Nullable |
| 385 | public static <T extends Parcelable> DeviceFilterPair<T> findMatch( |
| 386 | T dev, @Nullable List<? extends DeviceFilter<T>> filters) { |
| 387 | if (isEmpty(filters)) return new DeviceFilterPair<>(dev, null); |
Eugene Susla | 3c9aa17 | 2017-03-28 15:04:12 -0700 | [diff] [blame] | 388 | final DeviceFilter<T> matchingFilter |
| 389 | = CollectionUtils.find(filters, f -> f.matches(dev)); |
| 390 | |
| 391 | DeviceFilterPair<T> result = matchingFilter != null |
| 392 | ? new DeviceFilterPair<>(dev, matchingFilter) |
| 393 | : null; |
| 394 | if (DEBUG) Log.i(LOG_TAG, "findMatch(dev = " + dev + ", filters = " + filters + |
| 395 | ") -> " + result); |
| 396 | return result; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | public String getDisplayName() { |
| 400 | if (filter == null) { |
| 401 | Preconditions.checkNotNull(device); |
| 402 | if (device instanceof BluetoothDevice) { |
| 403 | return getDeviceDisplayNameInternal((BluetoothDevice) device); |
| 404 | } else if (device instanceof android.net.wifi.ScanResult) { |
| 405 | return getDeviceDisplayNameInternal((android.net.wifi.ScanResult) device); |
| 406 | } else if (device instanceof ScanResult) { |
| 407 | return getDeviceDisplayNameInternal(((ScanResult) device).getDevice()); |
| 408 | } else { |
| 409 | throw new IllegalArgumentException("Unknown device type: " + device.getClass()); |
| 410 | } |
| 411 | } |
| 412 | return filter.getDeviceDisplayName(device); |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public boolean equals(Object o) { |
| 417 | if (this == o) return true; |
| 418 | if (o == null || getClass() != o.getClass()) return false; |
| 419 | DeviceFilterPair<?> that = (DeviceFilterPair<?>) o; |
| 420 | return Objects.equals(getDeviceMacAddress(device), getDeviceMacAddress(that.device)); |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public int hashCode() { |
| 425 | return Objects.hash(getDeviceMacAddress(device)); |
| 426 | } |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 427 | |
| 428 | @Override |
| 429 | public String toString() { |
| 430 | return "DeviceFilterPair{" + |
| 431 | "device=" + device + |
| 432 | ", filter=" + filter + |
| 433 | '}'; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | private class BLEScanCallback extends ScanCallback { |
| 438 | |
| 439 | public BLEScanCallback() { |
| 440 | if (DEBUG) Log.i(LOG_TAG, "new BLEScanCallback() -> " + this); |
| 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public void onScanResult(int callbackType, ScanResult result) { |
| 445 | if (DEBUG) { |
| 446 | Log.i(LOG_TAG, |
| 447 | "BLE.onScanResult(callbackType = " + callbackType + ", result = " + result |
| 448 | + ")"); |
| 449 | } |
| 450 | final DeviceFilterPair<ScanResult> deviceFilterPair |
| 451 | = DeviceFilterPair.findMatch(result, mBLEFilters); |
| 452 | if (deviceFilterPair == null) return; |
| 453 | if (callbackType == ScanSettings.CALLBACK_TYPE_MATCH_LOST) { |
| 454 | onDeviceLost(deviceFilterPair); |
| 455 | } else { |
| 456 | onDeviceFound(deviceFilterPair); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | private class BluetoothBroadcastReceiver extends BroadcastReceiver { |
| 462 | @Override |
| 463 | public void onReceive(Context context, Intent intent) { |
| 464 | if (DEBUG) { |
| 465 | Log.i(LOG_TAG, |
| 466 | "BL.onReceive(context = " + context + ", intent = " + intent + ")"); |
| 467 | } |
| 468 | final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
| 469 | final DeviceFilterPair<BluetoothDevice> deviceFilterPair |
| 470 | = DeviceFilterPair.findMatch(device, mBluetoothFilters); |
| 471 | if (deviceFilterPair == null) return; |
| 472 | if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) { |
| 473 | onDeviceFound(deviceFilterPair); |
| 474 | } else { |
| 475 | onDeviceLost(deviceFilterPair); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | private class WifiBroadcastReceiver extends BroadcastReceiver { |
| 481 | @Override |
| 482 | public void onReceive(Context context, Intent intent) { |
| 483 | if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { |
| 484 | List<android.net.wifi.ScanResult> scanResults = mWifiManager.getScanResults(); |
| 485 | |
| 486 | if (DEBUG) { |
| 487 | Log.i(LOG_TAG, "Wifi scan results: " + TextUtils.join("\n", scanResults)); |
| 488 | } |
| 489 | |
| 490 | for (int i = 0; i < scanResults.size(); i++) { |
Eugene Susla | 0c4a926 | 2017-06-09 18:03:14 -0700 | [diff] [blame] | 491 | onDeviceFound(DeviceFilterPair.findMatch(scanResults.get(i), mWifiFilters)); |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
Eugene Susla | a7717e3 | 2017-04-17 19:13:31 -0700 | [diff] [blame] | 494 | } |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 495 | } |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 496 | } |