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 | |
Amin Shaikh | 3d18c62 | 2016-12-09 17:11:50 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
| 21 | import android.annotation.SystemApi; |
| 22 | import android.net.wifi.WifiConfiguration; |
| 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
| 25 | |
| 26 | import com.android.internal.annotations.VisibleForTesting; |
Amin Shaikh | 3d18c62 | 2016-12-09 17:11:50 -0800 | [diff] [blame] | 27 | import com.android.internal.util.Preconditions; |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * The result of a network recommendation. |
| 31 | * |
| 32 | * @see {@link NetworkScoreManager#requestRecommendation(RecommendationRequest)}. |
| 33 | * @hide |
| 34 | */ |
| 35 | @SystemApi |
| 36 | public final class RecommendationResult implements Parcelable { |
| 37 | private final WifiConfiguration mWifiConfiguration; |
| 38 | |
Amin Shaikh | 3d18c62 | 2016-12-09 17:11:50 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Create a {@link RecommendationResult} that indicates that no network connection should be |
| 41 | * attempted at this time. |
| 42 | * |
| 43 | * @return a {@link RecommendationResult} |
| 44 | */ |
| 45 | public static RecommendationResult createDoNotConnectRecommendation() { |
| 46 | return new RecommendationResult((WifiConfiguration) null); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Create a {@link RecommendationResult} that indicates that a connection attempt should be |
| 51 | * made for the given Wi-Fi network. |
| 52 | * |
| 53 | * @param wifiConfiguration {@link WifiConfiguration} with at least SSID and BSSID set. |
| 54 | * @return a {@link RecommendationResult} |
| 55 | */ |
| 56 | public static RecommendationResult createConnectRecommendation( |
| 57 | @NonNull WifiConfiguration wifiConfiguration) { |
| 58 | Preconditions.checkNotNull(wifiConfiguration, "wifiConfiguration must not be null"); |
| 59 | Preconditions.checkNotNull(wifiConfiguration.SSID, "SSID must not be null"); |
| 60 | Preconditions.checkNotNull(wifiConfiguration.BSSID, "BSSID must not be null"); |
| 61 | return new RecommendationResult(wifiConfiguration); |
| 62 | } |
| 63 | |
| 64 | private RecommendationResult(@Nullable WifiConfiguration wifiConfiguration) { |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 65 | mWifiConfiguration = wifiConfiguration; |
| 66 | } |
| 67 | |
| 68 | private RecommendationResult(Parcel in) { |
| 69 | mWifiConfiguration = in.readParcelable(WifiConfiguration.class.getClassLoader()); |
| 70 | } |
| 71 | |
| 72 | /** |
Amin Shaikh | 3d18c62 | 2016-12-09 17:11:50 -0800 | [diff] [blame] | 73 | * @return {@code true} if a network recommendation exists. {@code false} indicates that |
| 74 | * no connection should be attempted at this time. |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 75 | */ |
Amin Shaikh | 3d18c62 | 2016-12-09 17:11:50 -0800 | [diff] [blame] | 76 | public boolean hasRecommendation() { |
| 77 | return mWifiConfiguration != null; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @return The recommended {@link WifiConfiguration} to connect to. A {@code null} value |
| 82 | * is returned if {@link #hasRecommendation} returns {@code false}. |
| 83 | */ |
| 84 | @Nullable public WifiConfiguration getWifiConfiguration() { |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 85 | return mWifiConfiguration; |
| 86 | } |
| 87 | |
| 88 | @Override |
Amin Shaikh | 3d18c62 | 2016-12-09 17:11:50 -0800 | [diff] [blame] | 89 | public String toString() { |
| 90 | return "RecommendationResult{" + |
| 91 | "mWifiConfiguration=" + mWifiConfiguration + |
| 92 | "}"; |
| 93 | } |
| 94 | |
| 95 | @Override |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 96 | public int describeContents() { |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void writeToParcel(Parcel out, int flags) { |
| 102 | out.writeParcelable(mWifiConfiguration, flags); |
| 103 | } |
| 104 | |
| 105 | public static final Creator<RecommendationResult> CREATOR = |
| 106 | new Creator<RecommendationResult>() { |
| 107 | @Override |
| 108 | public RecommendationResult createFromParcel(Parcel in) { |
| 109 | return new RecommendationResult(in); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public RecommendationResult[] newArray(int size) { |
| 114 | return new RecommendationResult[size]; |
| 115 | } |
| 116 | }; |
| 117 | } |