• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 package android.net.wifi;
17 
18 import android.annotation.FlaggedApi;
19 import android.annotation.IntRange;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import androidx.annotation.NonNull;
24 
25 import com.android.wifi.flags.Flags;
26 
27 import java.util.Objects;
28 
29 /**
30  * Options for blocking a network through
31  * {@link WifiManager#disallowCurrentSuggestedNetwork(BlockingOption)}
32  */
33 @FlaggedApi(Flags.FLAG_BSSID_BLOCKLIST_FOR_SUGGESTION)
34 public final class BlockingOption implements Parcelable {
35     private final int mDisableTime;
36     private final boolean mBSSIDOnly;
37 
38     /**
39      * @hide
40      */
BlockingOption(int disableTime, boolean bssidOnly)41     public BlockingOption(int disableTime, boolean bssidOnly) {
42         mDisableTime = disableTime;
43         mBSSIDOnly = bssidOnly;
44     }
45 
46     /**
47      * @hide
48      */
BlockingOption(Parcel in)49     public BlockingOption(Parcel in) {
50         mDisableTime = in.readInt();
51         mBSSIDOnly = in.readBoolean();
52     }
53 
54     /**
55      * Get the blocking time which is set by {@link Builder#Builder(int)}
56      * @return Blocking time in seconds
57      */
getBlockingTimeSeconds()58     public int getBlockingTimeSeconds() {
59         return mDisableTime;
60     }
61 
62     /**
63      * Return whether or not a single BSSID is being blocked, which is set by
64      * {@link Builder#setBlockingBssidOnly(boolean)}
65      * @return True for blocking single BSSID, false otherwise.
66      */
isBlockingBssidOnly()67     public boolean isBlockingBssidOnly() {
68         return mBSSIDOnly;
69     }
70 
71     /**
72      * Builder used to create {@link BlockingOption} objects.
73      */
74     @FlaggedApi(Flags.FLAG_BSSID_BLOCKLIST_FOR_SUGGESTION)
75     public static final class Builder {
76         private int mDisableTime;
77         private boolean mBSSIDOnly;
78 
79         /**
80          * Create a {@link Builder} with blocking time for the network
81          *
82          * @param blockingTimeSec Time period to block the network in seconds
83          * @throws IllegalArgumentException if input is invalid.
84          */
Builder(@ntRangefrom = 1, to = 86400) int blockingTimeSec)85         public Builder(@IntRange(from = 1, to = 86400) int blockingTimeSec) {
86             if (blockingTimeSec < 1 || blockingTimeSec > 86400) {
87                 throw new IllegalArgumentException("blockingTimeSec should between 1 to 86400");
88             }
89             mDisableTime = blockingTimeSec;
90             mBSSIDOnly = false;
91         }
92 
93         /**
94          * Set to configure blocking the whole network or a single BSSID. By default, the whole
95          * network will be blocked.
96          * @param bssidOnly True for a single BSSID, otherwise the whole network will be blocked
97          * @return Instance of {@link Builder} to enable chaining of the builder method.
98          */
setBlockingBssidOnly(boolean bssidOnly)99         @NonNull public Builder setBlockingBssidOnly(boolean bssidOnly) {
100             mBSSIDOnly = bssidOnly;
101             return this;
102         }
103 
104         /**
105          * Create a BlockingOption object for use in
106          * {@link WifiManager#disallowCurrentSuggestedNetwork(BlockingOption)}.
107          */
build()108         @NonNull public BlockingOption build() {
109             return new BlockingOption(mDisableTime, mBSSIDOnly);
110         }
111     }
112 
113     @NonNull
114     public static final Creator<BlockingOption> CREATOR = new Creator<BlockingOption>() {
115         @Override
116         public BlockingOption createFromParcel(Parcel in) {
117             return new BlockingOption(in);
118         }
119 
120         @Override
121         public BlockingOption[] newArray(int size) {
122             return new BlockingOption[size];
123         }
124     };
125 
126     @Override
describeContents()127     public int describeContents() {
128         return 0;
129     }
130 
131     @Override
writeToParcel(@onNull Parcel dest, int flags)132     public void writeToParcel(@NonNull Parcel dest, int flags) {
133         dest.writeInt(mDisableTime);
134         dest.writeBoolean(mBSSIDOnly);
135     }
136 
137     @Override
equals(Object obj)138     public boolean equals(Object obj) {
139         if (this == obj) {
140             return true;
141         }
142         if (!(obj instanceof BlockingOption lhs)) {
143             return false;
144         }
145         return mBSSIDOnly == lhs.mBSSIDOnly && mDisableTime == lhs.mDisableTime;
146     }
147 
148     @Override
hashCode()149     public int hashCode() {
150         return Objects.hash(mBSSIDOnly, mDisableTime);
151     }
152 
153     @Override
toString()154     public String toString() {
155         return "BlockingOption[ "
156                 + "DisableTime=" + mDisableTime
157                 + ", BSSIDOnly=" + mBSSIDOnly;
158     }
159 }
160