blob: bd507a6b8bccf49d49983deb5b3c1b70cfbab560 [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;
33import java.util.List;
34import java.util.regex.Pattern;
35
36/** @hide */
37public class BluetoothDeviceFilterUtils {
38 private BluetoothDeviceFilterUtils() {}
39
40 private static final boolean DEBUG = false;
Eugene Susla3c9aa172017-03-28 15:04:12 -070041 private static final String LOG_TAG = "BluetoothDeviceFilterUtils";
Eugene Susla6ed45d82017-01-22 13:52:51 -080042
43 @Nullable
44 static String patternToString(@Nullable Pattern p) {
45 return p == null ? null : p.pattern();
46 }
47
48 @Nullable
49 static Pattern patternFromString(@Nullable String s) {
50 return s == null ? null : Pattern.compile(s);
51 }
52
53 static boolean matches(ScanFilter filter, BluetoothDevice device) {
Eugene Susla3c9aa172017-03-28 15:04:12 -070054 boolean result = matchesAddress(filter.getDeviceAddress(), device)
Eugene Susla6ed45d82017-01-22 13:52:51 -080055 && matchesServiceUuid(filter.getServiceUuid(), filter.getServiceUuidMask(), device);
Eugene Susla3c9aa172017-03-28 15:04:12 -070056 if (DEBUG) debugLogMatchResult(result, device, filter);
57 return result;
Eugene Susla6ed45d82017-01-22 13:52:51 -080058 }
59
60 static boolean matchesAddress(String deviceAddress, BluetoothDevice device) {
61 final boolean result = deviceAddress == null
Eugene Susla0c4a9262017-06-09 18:03:14 -070062 || (device != null && deviceAddress.equals(device.getAddress()));
Eugene Susla6ed45d82017-01-22 13:52:51 -080063 if (DEBUG) debugLogMatchResult(result, device, deviceAddress);
64 return result;
65 }
66
67 static boolean matchesServiceUuids(List<ParcelUuid> serviceUuids,
68 List<ParcelUuid> serviceUuidMasks, BluetoothDevice device) {
69 for (int i = 0; i < serviceUuids.size(); i++) {
70 ParcelUuid uuid = serviceUuids.get(i);
71 ParcelUuid uuidMask = serviceUuidMasks.get(i);
72 if (!matchesServiceUuid(uuid, uuidMask, device)) {
73 return false;
74 }
75 }
76 return true;
77 }
78
79 static boolean matchesServiceUuid(ParcelUuid serviceUuid, ParcelUuid serviceUuidMask,
80 BluetoothDevice device) {
81 final boolean result = serviceUuid == null ||
82 ScanFilter.matchesServiceUuids(
83 serviceUuid,
84 serviceUuidMask,
85 Arrays.asList(device.getUuids()));
86 if (DEBUG) debugLogMatchResult(result, device, serviceUuid);
87 return result;
88 }
89
90 static boolean matchesName(@Nullable Pattern namePattern, BluetoothDevice device) {
91 boolean result;
92 if (namePattern == null) {
93 result = true;
94 } else if (device == null) {
95 result = false;
96 } else {
97 final String name = device.getName();
98 result = name != null && namePattern.matcher(name).find();
99 }
100 if (DEBUG) debugLogMatchResult(result, device, namePattern);
101 return result;
102 }
103
Eugene Susla36e866b2017-02-23 18:24:39 -0800104 static boolean matchesName(@Nullable Pattern namePattern, ScanResult device) {
105 boolean result;
106 if (namePattern == null) {
107 result = true;
108 } else if (device == null) {
109 result = false;
110 } else {
111 final String name = device.SSID;
112 result = name != null && namePattern.matcher(name).find();
113 }
114 if (DEBUG) debugLogMatchResult(result, device, namePattern);
115 return result;
Eugene Suslae70e6aa2017-02-23 18:24:39 -0800116 }
117
Eugene Susla36e866b2017-02-23 18:24:39 -0800118 private static void debugLogMatchResult(
119 boolean result, BluetoothDevice device, Object criteria) {
120 Log.i(LOG_TAG, getDeviceDisplayNameInternal(device) + (result ? " ~ " : " !~ ") + criteria);
121 }
122
123 private static void debugLogMatchResult(
124 boolean result, ScanResult device, Object criteria) {
125 Log.i(LOG_TAG, getDeviceDisplayNameInternal(device) + (result ? " ~ " : " !~ ") + criteria);
126 }
127
Mathew Inwood70e89d52018-08-09 15:27:52 +0100128 @UnsupportedAppUsage
Eugene Susla36e866b2017-02-23 18:24:39 -0800129 public static String getDeviceDisplayNameInternal(@NonNull BluetoothDevice device) {
Eugene Susla6ed45d82017-01-22 13:52:51 -0800130 return firstNotEmpty(device.getAliasName(), device.getAddress());
131 }
Eugene Susla36e866b2017-02-23 18:24:39 -0800132
Mathew Inwood70e89d52018-08-09 15:27:52 +0100133 @UnsupportedAppUsage
Eugene Susla36e866b2017-02-23 18:24:39 -0800134 public static String getDeviceDisplayNameInternal(@NonNull ScanResult device) {
135 return firstNotEmpty(device.SSID, device.BSSID);
136 }
137
Mathew Inwood70e89d52018-08-09 15:27:52 +0100138 @UnsupportedAppUsage
Eugene Susla36e866b2017-02-23 18:24:39 -0800139 public static String getDeviceMacAddress(@NonNull Parcelable device) {
140 if (device instanceof BluetoothDevice) {
141 return ((BluetoothDevice) device).getAddress();
142 } else if (device instanceof ScanResult) {
143 return ((ScanResult) device).BSSID;
144 } else if (device instanceof android.bluetooth.le.ScanResult) {
145 return getDeviceMacAddress(((android.bluetooth.le.ScanResult) device).getDevice());
146 } else {
147 throw new IllegalArgumentException("Unknown device type: " + device);
148 }
149 }
Eugene Susla6ed45d82017-01-22 13:52:51 -0800150}