1 /* 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 18 package android.companion; 19 20 import static android.text.TextUtils.firstNotEmpty; 21 22 import android.annotation.NonNull; 23 import android.annotation.Nullable; 24 import android.bluetooth.BluetoothDevice; 25 import android.bluetooth.le.ScanFilter; 26 import android.compat.annotation.UnsupportedAppUsage; 27 import android.net.wifi.ScanResult; 28 import android.os.Build; 29 import android.os.ParcelUuid; 30 import android.os.Parcelable; 31 import android.util.Log; 32 33 import java.util.Arrays; 34 import java.util.Collections; 35 import java.util.List; 36 import java.util.regex.Pattern; 37 38 /** @hide */ 39 public class BluetoothDeviceFilterUtils { BluetoothDeviceFilterUtils()40 private BluetoothDeviceFilterUtils() {} 41 42 private static final boolean DEBUG = false; 43 private static final String LOG_TAG = "BluetoothDeviceFilterUtils"; 44 45 @Nullable patternToString(@ullable Pattern p)46 static String patternToString(@Nullable Pattern p) { 47 return p == null ? null : p.pattern(); 48 } 49 50 @Nullable patternFromString(@ullable String s)51 static Pattern patternFromString(@Nullable String s) { 52 return s == null ? null : Pattern.compile(s); 53 } 54 matchesAddress(String deviceAddress, BluetoothDevice device)55 static boolean matchesAddress(String deviceAddress, BluetoothDevice device) { 56 final boolean result = deviceAddress == null 57 || (device != null && deviceAddress.equals(device.getAddress())); 58 if (DEBUG) debugLogMatchResult(result, device, deviceAddress); 59 return result; 60 } 61 matchesServiceUuids(List<ParcelUuid> serviceUuids, List<ParcelUuid> serviceUuidMasks, BluetoothDevice device)62 static boolean matchesServiceUuids(List<ParcelUuid> serviceUuids, 63 List<ParcelUuid> serviceUuidMasks, BluetoothDevice device) { 64 for (int i = 0; i < serviceUuids.size(); i++) { 65 ParcelUuid uuid = serviceUuids.get(i); 66 ParcelUuid uuidMask = serviceUuidMasks.get(i); 67 if (!matchesServiceUuid(uuid, uuidMask, device)) { 68 return false; 69 } 70 } 71 return true; 72 } 73 matchesServiceUuid(ParcelUuid serviceUuid, ParcelUuid serviceUuidMask, BluetoothDevice device)74 static boolean matchesServiceUuid(ParcelUuid serviceUuid, ParcelUuid serviceUuidMask, 75 BluetoothDevice device) { 76 ParcelUuid[] uuids = device.getUuids(); 77 final boolean result = serviceUuid == null || 78 ScanFilter.matchesServiceUuids( 79 serviceUuid, 80 serviceUuidMask, 81 uuids == null ? Collections.emptyList() : Arrays.asList(uuids)); 82 if (DEBUG) debugLogMatchResult(result, device, serviceUuid); 83 return result; 84 } 85 matchesName(@ullable Pattern namePattern, BluetoothDevice device)86 static boolean matchesName(@Nullable Pattern namePattern, BluetoothDevice device) { 87 boolean result; 88 if (namePattern == null) { 89 result = true; 90 } else if (device == null) { 91 result = false; 92 } else { 93 final String name = device.getName(); 94 result = name != null && namePattern.matcher(name).find(); 95 } 96 if (DEBUG) debugLogMatchResult(result, device, namePattern); 97 return result; 98 } 99 matchesName(@ullable Pattern namePattern, ScanResult device)100 static boolean matchesName(@Nullable Pattern namePattern, ScanResult device) { 101 boolean result; 102 if (namePattern == null) { 103 result = true; 104 } else if (device == null) { 105 result = false; 106 } else { 107 final String name = device.SSID; 108 result = name != null && namePattern.matcher(name).find(); 109 } 110 if (DEBUG) debugLogMatchResult(result, device, namePattern); 111 return result; 112 } 113 debugLogMatchResult( boolean result, BluetoothDevice device, Object criteria)114 private static void debugLogMatchResult( 115 boolean result, BluetoothDevice device, Object criteria) { 116 Log.i(LOG_TAG, getDeviceDisplayNameInternal(device) + (result ? " ~ " : " !~ ") + criteria); 117 } 118 debugLogMatchResult( boolean result, ScanResult device, Object criteria)119 private static void debugLogMatchResult( 120 boolean result, ScanResult device, Object criteria) { 121 Log.i(LOG_TAG, getDeviceDisplayNameInternal(device) + (result ? " ~ " : " !~ ") + criteria); 122 } 123 124 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) getDeviceDisplayNameInternal(@onNull BluetoothDevice device)125 public static String getDeviceDisplayNameInternal(@NonNull BluetoothDevice device) { 126 return firstNotEmpty(device.getAlias(), device.getAddress()); 127 } 128 129 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) getDeviceDisplayNameInternal(@onNull ScanResult device)130 public static String getDeviceDisplayNameInternal(@NonNull ScanResult device) { 131 return firstNotEmpty(device.SSID, device.BSSID); 132 } 133 134 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) getDeviceMacAddress(@onNull Parcelable device)135 public static String getDeviceMacAddress(@NonNull Parcelable device) { 136 if (device instanceof BluetoothDevice) { 137 return ((BluetoothDevice) device).getAddress(); 138 } else if (device instanceof ScanResult) { 139 return ((ScanResult) device).BSSID; 140 } else if (device instanceof android.bluetooth.le.ScanResult) { 141 return getDeviceMacAddress(((android.bluetooth.le.ScanResult) device).getDevice()); 142 } else { 143 throw new IllegalArgumentException("Unknown device type: " + device); 144 } 145 } 146 } 147