blob: 70cf09c7df5b083d659b7023dc697e67bd3e4bfd [file] [log] [blame]
Jeremy Joslind1daf6d2016-11-28 17:47:35 -08001/*
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
17package android.net;
18
Amin Shaikh3d18c622016-12-09 17:11:50 -080019import android.annotation.NonNull;
Jeremy Joslind1daf6d2016-11-28 17:47:35 -080020import android.annotation.Nullable;
21import android.annotation.SystemApi;
22import android.net.wifi.WifiConfiguration;
23import android.os.Parcel;
24import android.os.Parcelable;
25
26import com.android.internal.annotations.VisibleForTesting;
Amin Shaikh3d18c622016-12-09 17:11:50 -080027import com.android.internal.util.Preconditions;
Jeremy Joslind1daf6d2016-11-28 17:47:35 -080028
29/**
30 * The result of a network recommendation.
31 *
32 * @see {@link NetworkScoreManager#requestRecommendation(RecommendationRequest)}.
33 * @hide
34 */
35@SystemApi
36public final class RecommendationResult implements Parcelable {
37 private final WifiConfiguration mWifiConfiguration;
38
Amin Shaikh3d18c622016-12-09 17:11:50 -080039 /**
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 Joslind1daf6d2016-11-28 17:47:35 -080065 mWifiConfiguration = wifiConfiguration;
66 }
67
68 private RecommendationResult(Parcel in) {
69 mWifiConfiguration = in.readParcelable(WifiConfiguration.class.getClassLoader());
70 }
71
72 /**
Amin Shaikh3d18c622016-12-09 17:11:50 -080073 * @return {@code true} if a network recommendation exists. {@code false} indicates that
74 * no connection should be attempted at this time.
Jeremy Joslind1daf6d2016-11-28 17:47:35 -080075 */
Amin Shaikh3d18c622016-12-09 17:11:50 -080076 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 Joslind1daf6d2016-11-28 17:47:35 -080085 return mWifiConfiguration;
86 }
87
88 @Override
Amin Shaikh3d18c622016-12-09 17:11:50 -080089 public String toString() {
90 return "RecommendationResult{" +
91 "mWifiConfiguration=" + mWifiConfiguration +
92 "}";
93 }
94
95 @Override
Jeremy Joslind1daf6d2016-11-28 17:47:35 -080096 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}