blob: 75e726bfad0df29288f984d3432249be5c3c81ca [file] [log] [blame]
Eugene Susla6ed45d82017-01-22 13:52:51 -08001/*
2 * Copyright (C) 2017 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
18package android.companion;
19
20import static android.text.TextUtils.firstNotEmpty;
21
22import android.annotation.NonNull;
23import android.annotation.Nullable;
Mathew Inwood70e89d52018-08-09 15:27:52 +010024import android.annotation.UnsupportedAppUsage;
Eugene Susla6ed45d82017-01-22 13:52:51 -080025import android.bluetooth.BluetoothDevice;
26import android.bluetooth.le.ScanFilter;
Eugene Susla36e866b2017-02-23 18:24:39 -080027import android.net.wifi.ScanResult;
Eugene Susla6ed45d82017-01-22 13:52:51 -080028import android.os.ParcelUuid;
Eugene Susla36e866b2017-02-23 18:24:39 -080029import android.os.Parcelable;
Eugene Susla6ed45d82017-01-22 13:52:51 -080030import android.util.Log;
31
32import java.util.Arrays;
Eugene Susla71516422019-03-01 15:33:26 -080033import java.util.Collections;
Eugene Susla6ed45d82017-01-22 13:52:51 -080034import java.util.List;
35import java.util.regex.Pattern;
36
37/** @hide */
38public class BluetoothDeviceFilterUtils {
39 private BluetoothDeviceFilterUtils() {}
40
41 private static final boolean DEBUG = false;
Eugene Susla3c9aa172017-03-28 15:04:12 -070042 private static final String LOG_TAG = "BluetoothDeviceFilterUtils";
Eugene Susla6ed45d82017-01-22 13:52:51 -080043
44 @Nullable
45 static String patternToString(@Nullable Pattern p) {
46 return p == null ? null : p.pattern();
47 }
48
49 @Nullable
50 static Pattern patternFromString(@Nullable String s) {
51 return s == null ? null : Pattern.compile(s);
52 }
53
54 static boolean matches(ScanFilter filter, BluetoothDevice device) {
Eugene Susla3c9aa172017-03-28 15:04:12 -070055 boolean result = matchesAddress(filter.getDeviceAddress(), device)
Eugene Susla6ed45d82017-01-22 13:52:51 -080056 && matchesServiceUuid(filter.getServiceUuid(), filter.getServiceUuidMask(), device);
Eugene Susla3c9aa172017-03-28 15:04:12 -070057 if (DEBUG) debugLogMatchResult(result, device, filter);
58 return result;
Eugene Susla6ed45d82017-01-22 13:52:51 -080059 }
60
61 static boolean matchesAddress(String deviceAddress, BluetoothDevice device) {
62 final boolean result = deviceAddress == null
Eugene Susla0c4a9262017-06-09 18:03:14 -070063 || (device != null && deviceAddress.equals(device.getAddress()));
Eugene Susla6ed45d82017-01-22 13:52:51 -080064 if (DEBUG) debugLogMatchResult(result, device, deviceAddress);
65 return result;
66 }
67
68 static boolean matchesServiceUuids(List<ParcelUuid> serviceUuids,
69 List<ParcelUuid> serviceUuidMasks, BluetoothDevice device) {
70 for (int i = 0; i < serviceUuids.size(); i++) {
71 ParcelUuid uuid = serviceUuids.get(i);
72 ParcelUuid uuidMask = serviceUuidMasks.get(i);
73 if (!matchesServiceUuid(uuid, uuidMask, device)) {
74 return false;
75 }
76 }
77 return true;
78 }
79
80 static boolean matchesServiceUuid(ParcelUuid serviceUuid, ParcelUuid serviceUuidMask,
81 BluetoothDevice device) {
Eugene Susla71516422019-03-01 15:33:26 -080082 ParcelUuid[] uuids = device.getUuids();
Eugene Susla6ed45d82017-01-22 13:52:51 -080083 final boolean result = serviceUuid == null ||
84 ScanFilter.matchesServiceUuids(
85 serviceUuid,
86 serviceUuidMask,
Eugene Susla71516422019-03-01 15:33:26 -080087 uuids == null ? Collections.emptyList() : Arrays.asList(uuids));
Eugene Susla6ed45d82017-01-22 13:52:51 -080088 if (DEBUG) debugLogMatchResult(result, device, serviceUuid);
89 return result;
90 }
91
92 static boolean matchesName(@Nullable Pattern namePattern, BluetoothDevice device) {
93 boolean result;
94 if (namePattern == null) {
95 result = true;
96 } else if (device == null) {
97 result = false;
98 } else {
99 final String name = device.getName();
100 result = name != null && namePattern.matcher(name).find();
101 }
102 if (DEBUG) debugLogMatchResult(result, device, namePattern);
103 return result;
104 }
105
Eugene Susla36e866b2017-02-23 18:24:39 -0800106 static boolean matchesName(@Nullable Pattern namePattern, ScanResult device) {
107 boolean result;
108 if (namePattern == null) {
109 result = true;
110 } else if (device == null) {
111 result = false;
112 } else {
113 final String name = device.SSID;
114 result = name != null && namePattern.matcher(name).find();
115 }
116 if (DEBUG) debugLogMatchResult(result, device, namePattern);
117 return result;
Eugene Suslae70e6aa2017-02-23 18:24:39 -0800118 }
119
Eugene Susla36e866b2017-02-23 18:24:39 -0800120 private static void debugLogMatchResult(
121 boolean result, BluetoothDevice device, Object criteria) {
122 Log.i(LOG_TAG, getDeviceDisplayNameInternal(device) + (result ? " ~ " : " !~ ") + criteria);
123 }
124
125 private static void debugLogMatchResult(
126 boolean result, ScanResult device, Object criteria) {
127 Log.i(LOG_TAG, getDeviceDisplayNameInternal(device) + (result ? " ~ " : " !~ ") + criteria);
128 }
129
Mathew Inwood70e89d52018-08-09 15:27:52 +0100130 @UnsupportedAppUsage
Eugene Susla36e866b2017-02-23 18:24:39 -0800131 public static String getDeviceDisplayNameInternal(@NonNull BluetoothDevice device) {
Eugene Susla6ed45d82017-01-22 13:52:51 -0800132 return firstNotEmpty(device.getAliasName(), device.getAddress());
133 }
Eugene Susla36e866b2017-02-23 18:24:39 -0800134
Mathew Inwood70e89d52018-08-09 15:27:52 +0100135 @UnsupportedAppUsage
Eugene Susla36e866b2017-02-23 18:24:39 -0800136 public static String getDeviceDisplayNameInternal(@NonNull ScanResult device) {
137 return firstNotEmpty(device.SSID, device.BSSID);
138 }
139
Mathew Inwood70e89d52018-08-09 15:27:52 +0100140 @UnsupportedAppUsage
Eugene Susla36e866b2017-02-23 18:24:39 -0800141 public static String getDeviceMacAddress(@NonNull Parcelable device) {
142 if (device instanceof BluetoothDevice) {
143 return ((BluetoothDevice) device).getAddress();
144 } else if (device instanceof ScanResult) {
145 return ((ScanResult) device).BSSID;
146 } else if (device instanceof android.bluetooth.le.ScanResult) {
147 return getDeviceMacAddress(((android.bluetooth.le.ScanResult) device).getDevice());
148 } else {
149 throw new IllegalArgumentException("Unknown device type: " + device);
150 }
151 }
Eugene Susla6ed45d82017-01-22 13:52:51 -0800152}