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