165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.util.bluetooth;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.bluetooth.BluetoothClass;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.bluetooth.BluetoothDevice;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.regex.Pattern;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Class that decides whether a BluetoothDevice matches the parameters of the
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * type of device that is being looked for.
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * For example, does the device MAC address match the expected pattern and
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * does the device provide the types of services (audio, video, input, etc) that
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * are needed.
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class BluetoothDeviceCriteria {
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    // TODO add ability to determine matching device based on name
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final String GOOGLE_MAC_PATTERN = "^(00:1A:11|F8:8F:CA).*";
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final Pattern mAddressPattern;
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public BluetoothDeviceCriteria() {
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        this(".*");
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public BluetoothDeviceCriteria(String macAddressPattern) {
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAddressPattern = Pattern.compile(macAddressPattern, Pattern.CASE_INSENSITIVE);
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final boolean isMatchingDevice(BluetoothDevice device) {
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (device == null) {
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return false;
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (device.getAddress() == null || !isMatchingMacAddress(device.getAddress())) {
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return false;
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (!isMatchingMajorDeviceClass(device.getBluetoothClass().getMajorDeviceClass())) {
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return false;
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
61510203ee5e3350758dae4bf9af05b1585029af7dWally Yau        if (!isMatchingDeviceClass(device.getBluetoothClass().getDeviceClass())) {
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return false;
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return true;
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public boolean isMatchingMacAddress(String mac) {
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mAddressPattern.matcher(mac).matches();
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    /**
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * Override this method to restrict the major device classes that match.
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * @param majorDeviceClass constant from {@link BluetoothClass.Device.Major}.
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     */
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public boolean isMatchingMajorDeviceClass(int majorDeviceClass) {
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return true;
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    /**
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * Override this method to restrict specific device classes that match.
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * @param majorMinorClass constant from {@link BluetoothClass.Device}
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     */
84510203ee5e3350758dae4bf9af05b1585029af7dWally Yau    public boolean isMatchingDeviceClass(int majorMinorClass) {
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return true;
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
88