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
17package android.companion;
18
19import static android.companion.BluetoothDeviceFilterUtils.getDeviceDisplayNameInternal;
20import static android.companion.BluetoothDeviceFilterUtils.patternFromString;
21import static android.companion.BluetoothDeviceFilterUtils.patternToString;
22
23import android.annotation.NonNull;
24import android.annotation.Nullable;
25import android.annotation.SuppressLint;
26import android.bluetooth.BluetoothDevice;
27import android.bluetooth.le.ScanFilter;
28import android.net.wifi.ScanResult;
29import android.os.Parcel;
30import android.provider.OneTimeUseBuilder;
31
32import java.util.Objects;
33import java.util.regex.Pattern;
34
35/**
36 * A filter for Wifi devices
37 *
38 * @see ScanFilter
39 */
40public final class WifiDeviceFilter implements DeviceFilter<ScanResult> {
41
42    private final Pattern mNamePattern;
43
44    private WifiDeviceFilter(Pattern namePattern) {
45        mNamePattern = namePattern;
46    }
47
48    @SuppressLint("ParcelClassLoader")
49    private WifiDeviceFilter(Parcel in) {
50        this(patternFromString(in.readString()));
51    }
52
53    /** @hide */
54    @Nullable
55    public Pattern getNamePattern() {
56        return mNamePattern;
57    }
58
59
60    /** @hide */
61    @Override
62    public boolean matches(ScanResult device) {
63        return BluetoothDeviceFilterUtils.matchesName(getNamePattern(), device);
64    }
65
66    /** @hide */
67    @Override
68    public String getDeviceDisplayName(ScanResult device) {
69        return getDeviceDisplayNameInternal(device);
70    }
71
72    /** @hide */
73    @Override
74    public int getMediumType() {
75        return MEDIUM_TYPE_WIFI;
76    }
77
78    @Override
79    public boolean equals(Object o) {
80        if (this == o) return true;
81        if (o == null || getClass() != o.getClass()) return false;
82        WifiDeviceFilter that = (WifiDeviceFilter) o;
83        return Objects.equals(mNamePattern, that.mNamePattern);
84    }
85
86    @Override
87    public int hashCode() {
88        return Objects.hash(mNamePattern);
89    }
90
91    @Override
92    public void writeToParcel(Parcel dest, int flags) {
93        dest.writeString(patternToString(getNamePattern()));
94    }
95
96    @Override
97    public int describeContents() {
98        return 0;
99    }
100
101    public static final Creator<WifiDeviceFilter> CREATOR
102            = new Creator<WifiDeviceFilter>() {
103        @Override
104        public WifiDeviceFilter createFromParcel(Parcel in) {
105            return new WifiDeviceFilter(in);
106        }
107
108        @Override
109        public WifiDeviceFilter[] newArray(int size) {
110            return new WifiDeviceFilter[size];
111        }
112    };
113
114    /**
115     * Builder for {@link WifiDeviceFilter}
116     */
117    public static final class Builder extends OneTimeUseBuilder<WifiDeviceFilter> {
118        private Pattern mNamePattern;
119
120        /**
121         * @param regex if set, only devices with {@link BluetoothDevice#getName name} matching the
122         *              given regular expression will be shown
123         * @return self for chaining
124         */
125        public Builder setNamePattern(@Nullable Pattern regex) {
126            checkNotUsed();
127            mNamePattern = regex;
128            return this;
129        }
130
131        /** @inheritDoc */
132        @Override
133        @NonNull
134        public WifiDeviceFilter build() {
135            markUsed();
136            return new WifiDeviceFilter(mNamePattern);
137        }
138    }
139}
140