Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 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; |
| 18 | |
| 19 | |
| 20 | import android.annotation.SystemApi; |
| 21 | import android.net.wifi.ScanResult; |
| 22 | import android.net.wifi.WifiConfiguration; |
| 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
| 25 | |
| 26 | import com.android.internal.annotations.VisibleForTesting; |
| 27 | |
| 28 | /** |
| 29 | * A request for a network recommendation. |
| 30 | * |
| 31 | * @see {@link NetworkScoreManager#requestRecommendation(RecommendationRequest)}. |
| 32 | * @hide |
| 33 | */ |
| 34 | @SystemApi |
| 35 | public final class RecommendationRequest implements Parcelable { |
| 36 | private final ScanResult[] mScanResults; |
| 37 | private final WifiConfiguration mCurrentSelectedConfig; |
| 38 | private final NetworkCapabilities mRequiredCapabilities; |
| 39 | |
| 40 | /** |
| 41 | * Builder class for constructing {@link RecommendationRequest} instances. |
| 42 | * @hide |
| 43 | */ |
Joe LaPenna | 010e8b0 | 2017-01-04 00:44:22 -0800 | [diff] [blame] | 44 | @SystemApi |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 45 | public static final class Builder { |
| 46 | private ScanResult[] mScanResults; |
| 47 | private WifiConfiguration mCurrentConfig; |
| 48 | private NetworkCapabilities mNetworkCapabilities; |
| 49 | |
| 50 | public Builder setScanResults(ScanResult[] scanResults) { |
| 51 | mScanResults = scanResults; |
| 52 | return this; |
| 53 | } |
| 54 | |
| 55 | public Builder setCurrentRecommendedWifiConfig(WifiConfiguration config) { |
| 56 | this.mCurrentConfig = config; |
| 57 | return this; |
| 58 | } |
| 59 | |
| 60 | public Builder setNetworkCapabilities(NetworkCapabilities capabilities) { |
| 61 | mNetworkCapabilities = capabilities; |
| 62 | return this; |
| 63 | } |
| 64 | |
| 65 | public RecommendationRequest build() { |
| 66 | return new RecommendationRequest(mScanResults, mCurrentConfig, mNetworkCapabilities); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @return the array of {@link ScanResult}s the recommendation must be constrained to i.e. if a |
| 72 | * non-null wifi config recommendation is returned then it must be able to connect to |
| 73 | * one of the networks in the results list. |
| 74 | * |
| 75 | * If the array is {@code null} or empty then there is no constraint. |
| 76 | */ |
| 77 | public ScanResult[] getScanResults() { |
| 78 | return mScanResults; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return The best recommendation at the time this {@code RecommendationRequest} instance |
| 83 | * was created. This may be null which indicates that no recommendation is available. |
| 84 | */ |
| 85 | public WifiConfiguration getCurrentSelectedConfig() { |
| 86 | return mCurrentSelectedConfig; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * |
| 91 | * @return The set of {@link NetworkCapabilities} the recommendation must be constrained to. |
| 92 | * This may be {@code null} which indicates that there are no constraints on the |
| 93 | * capabilities of the recommended network. |
| 94 | */ |
| 95 | public NetworkCapabilities getRequiredCapabilities() { |
| 96 | return mRequiredCapabilities; |
| 97 | } |
| 98 | |
| 99 | @VisibleForTesting |
| 100 | RecommendationRequest(ScanResult[] scanResults, |
| 101 | WifiConfiguration currentSelectedConfig, |
| 102 | NetworkCapabilities requiredCapabilities) { |
| 103 | mScanResults = scanResults; |
| 104 | mCurrentSelectedConfig = currentSelectedConfig; |
| 105 | mRequiredCapabilities = requiredCapabilities; |
| 106 | } |
| 107 | |
| 108 | protected RecommendationRequest(Parcel in) { |
Jeremy Joslin | ab60cb6 | 2016-12-16 17:48:13 -0800 | [diff] [blame] | 109 | final int resultCount = in.readInt(); |
| 110 | if (resultCount > 0) { |
| 111 | mScanResults = new ScanResult[resultCount]; |
| 112 | for (int i = 0; i < resultCount; i++) { |
| 113 | mScanResults[i] = in.readParcelable(ScanResult.class.getClassLoader()); |
| 114 | } |
| 115 | } else { |
| 116 | mScanResults = null; |
| 117 | } |
| 118 | |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 119 | mCurrentSelectedConfig = in.readParcelable(WifiConfiguration.class.getClassLoader()); |
| 120 | mRequiredCapabilities = in.readParcelable(NetworkCapabilities.class.getClassLoader()); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public int describeContents() { |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void writeToParcel(Parcel dest, int flags) { |
Jeremy Joslin | ab60cb6 | 2016-12-16 17:48:13 -0800 | [diff] [blame] | 130 | if (mScanResults != null) { |
| 131 | dest.writeInt(mScanResults.length); |
| 132 | for (int i = 0; i < mScanResults.length; i++) { |
| 133 | dest.writeParcelable(mScanResults[i], flags); |
| 134 | } |
| 135 | } else { |
| 136 | dest.writeInt(0); |
| 137 | } |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 138 | dest.writeParcelable(mCurrentSelectedConfig, flags); |
| 139 | dest.writeParcelable(mRequiredCapabilities, flags); |
| 140 | } |
| 141 | |
| 142 | public static final Creator<RecommendationRequest> CREATOR = |
| 143 | new Creator<RecommendationRequest>() { |
| 144 | @Override |
| 145 | public RecommendationRequest createFromParcel(Parcel in) { |
| 146 | return new RecommendationRequest(in); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public RecommendationRequest[] newArray(int size) { |
| 151 | return new RecommendationRequest[size]; |
| 152 | } |
| 153 | }; |
| 154 | } |