1 /*
2  * Copyright (C) 2016 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 package android.net.wifi.nl80211;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.util.Arrays;
25 import java.util.Objects;
26 
27 /**
28  * Configuration for a PNO (preferred network offload) network used in {@link PnoSettings}. A PNO
29  * network allows configuration of a specific network to search for.
30  *
31  * @hide
32  */
33 @SystemApi
34 public final class PnoNetwork implements Parcelable {
35     private boolean mIsHidden;
36     private byte[] mSsid;
37     private int[] mFrequencies;
38 
39     /**
40      * Indicates whether the PNO network configuration is for a hidden SSID - i.e. a network which
41      * does not broadcast its SSID and must be queried explicitly.
42      *
43      * @return True if the configuration is for a hidden network, false otherwise.
44      */
isHidden()45     public boolean isHidden() {
46         return mIsHidden;
47     }
48 
49     /**
50      * Configure whether the PNO network configuration is for a hidden SSID - i.e. a network which
51      * does not broadcast its SSID and must be queried explicitly.
52      *
53      * @param isHidden True if the configuration is for a hidden network, false otherwise.
54      */
setHidden(boolean isHidden)55     public void setHidden(boolean isHidden) {
56         mIsHidden = isHidden;
57     }
58 
59     /**
60      * Get the raw bytes for the SSID of the PNO network being scanned for.
61      *
62      * @return A byte array.
63      */
getSsid()64     @NonNull public byte[] getSsid() {
65         return mSsid;
66     }
67 
68     /**
69      * Set the raw bytes for the SSID of the PNO network being scanned for.
70      *
71      * @param ssid A byte array.
72      */
setSsid(@onNull byte[] ssid)73     public void setSsid(@NonNull byte[] ssid) {
74         if (ssid == null) {
75             throw new IllegalArgumentException("null argument");
76         }
77         this.mSsid = ssid;
78     }
79 
80     /**
81      * Get the frequencies (in MHz) on which to PNO scan for the current network is being searched
82      * for. A null return (i.e. no frequencies configured) indicates that the network is search for
83      * on all supported frequencies.
84      *
85      * @return A array of frequencies (in MHz), a null indicates no configured frequencies.
86      */
getFrequenciesMhz()87     @NonNull public int[] getFrequenciesMhz() {
88         return mFrequencies;
89     }
90 
91     /**
92      * Set the frequencies (in MHz) on which to PNO scan for the current network is being searched
93      * for. A null configuration (i.e. no frequencies configured) indicates that the network is
94      * search for on all supported frequencies.
95      *
96      * @param frequenciesMhz an array of frequencies (in MHz), null indicating no configured
97      *                       frequencies.
98      */
setFrequenciesMhz(@onNull int[] frequenciesMhz)99     public void setFrequenciesMhz(@NonNull int[] frequenciesMhz) {
100         if (frequenciesMhz == null) {
101             throw new IllegalArgumentException("null argument");
102         }
103         this.mFrequencies = frequenciesMhz;
104     }
105 
106     /** Construct an uninitialized PnoNetwork object */
PnoNetwork()107     public PnoNetwork() { }
108 
109     /** override comparator */
110     @Override
equals(Object rhs)111     public boolean equals(Object rhs) {
112         if (this == rhs) return true;
113         if (!(rhs instanceof PnoNetwork)) {
114             return false;
115         }
116         PnoNetwork network = (PnoNetwork) rhs;
117         return Arrays.equals(mSsid, network.mSsid)
118                 && Arrays.equals(mFrequencies, network.mFrequencies)
119                 && mIsHidden == network.mIsHidden;
120     }
121 
122     /** override hash code */
123     @Override
hashCode()124     public int hashCode() {
125         return Objects.hash(
126                 mIsHidden,
127                 Arrays.hashCode(mSsid),
128                 Arrays.hashCode(mFrequencies));
129     }
130 
131     /** implement Parcelable interface */
132     @Override
describeContents()133     public int describeContents() {
134         return 0;
135     }
136 
137     /**
138      * implement Parcelable interface
139      * |flag| is ignored.
140      */
141     @Override
writeToParcel(@onNull Parcel out, int flags)142     public void writeToParcel(@NonNull Parcel out, int flags) {
143         out.writeInt(mIsHidden ? 1 : 0);
144         out.writeByteArray(mSsid);
145         out.writeIntArray(mFrequencies);
146     }
147 
148     /** implement Parcelable interface */
149     @NonNull public static final Parcelable.Creator<PnoNetwork> CREATOR =
150             new Parcelable.Creator<PnoNetwork>() {
151         @Override
152         public PnoNetwork createFromParcel(Parcel in) {
153             PnoNetwork result = new PnoNetwork();
154             result.mIsHidden = in.readInt() != 0 ? true : false;
155             result.mSsid = in.createByteArray();
156             if (result.mSsid == null) {
157                 result.mSsid = new byte[0];
158             }
159             result.mFrequencies = in.createIntArray();
160             if (result.mFrequencies == null) {
161                 result.mFrequencies = new int[0];
162             }
163             return result;
164         }
165 
166         @Override
167         public PnoNetwork[] newArray(int size) {
168             return new PnoNetwork[size];
169         }
170     };
171 }
172