blob: fb35b4bde30391116e6ad1224aeaa8f96cbaa5dc [file] [log] [blame]
Paul Jensen49e3edf2015-05-22 10:50:39 -04001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed urnder 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 */
16package android.net;
17
paulhu79b380c2019-03-15 17:17:02 +080018import android.annotation.NonNull;
Remi NGUYEN VANd0a93342019-01-21 21:07:10 +090019import android.annotation.SystemApi;
20import android.annotation.TestApi;
Paul Jensen49e3edf2015-05-22 10:50:39 -040021import android.os.IBinder;
22import android.os.Parcel;
23import android.os.Parcelable;
24import android.os.RemoteException;
25
26/**
27 * A class allowing apps handling the {@link ConnectivityManager#ACTION_CAPTIVE_PORTAL_SIGN_IN}
28 * activity to indicate to the system different outcomes of captive portal sign in. This class is
29 * passed as an extra named {@link ConnectivityManager#EXTRA_CAPTIVE_PORTAL} with the
30 * {@code ACTION_CAPTIVE_PORTAL_SIGN_IN} activity.
31 */
32public class CaptivePortal implements Parcelable {
Remi NGUYEN VANe501d2a2019-03-12 09:21:55 +090033 /**
34 * Response code from the captive portal application, indicating that the portal was dismissed
35 * and the network should be re-validated.
36 * @see ICaptivePortal#appResponse(int)
37 * @see android.net.INetworkMonitor#notifyCaptivePortalAppFinished(int)
38 * @hide
39 */
Remi NGUYEN VANd0a93342019-01-21 21:07:10 +090040 @SystemApi
41 @TestApi
Paul Jensen49e3edf2015-05-22 10:50:39 -040042 public static final int APP_RETURN_DISMISSED = 0;
Remi NGUYEN VANe501d2a2019-03-12 09:21:55 +090043 /**
44 * Response code from the captive portal application, indicating that the user did not login and
45 * does not want to use the captive portal network.
46 * @see ICaptivePortal#appResponse(int)
47 * @see android.net.INetworkMonitor#notifyCaptivePortalAppFinished(int)
48 * @hide
49 */
Remi NGUYEN VANd0a93342019-01-21 21:07:10 +090050 @SystemApi
51 @TestApi
Paul Jensen49e3edf2015-05-22 10:50:39 -040052 public static final int APP_RETURN_UNWANTED = 1;
Remi NGUYEN VANe501d2a2019-03-12 09:21:55 +090053 /**
54 * Response code from the captive portal application, indicating that the user does not wish to
55 * login but wants to use the captive portal network as-is.
56 * @see ICaptivePortal#appResponse(int)
57 * @see android.net.INetworkMonitor#notifyCaptivePortalAppFinished(int)
58 * @hide
59 */
Remi NGUYEN VANd0a93342019-01-21 21:07:10 +090060 @SystemApi
61 @TestApi
Paul Jensen49e3edf2015-05-22 10:50:39 -040062 public static final int APP_RETURN_WANTED_AS_IS = 2;
Chiachang Wang207b6cf2020-01-09 13:50:55 +080063 /** Event offset of request codes from captive portal application. */
64 private static final int APP_REQUEST_BASE = 100;
65 /**
66 * Request code from the captive portal application, indicating that the network condition may
67 * have changed and the network should be re-validated.
68 * @see ICaptivePortal#appRequest(int)
69 * @see android.net.INetworkMonitor#forceReevaluation(int)
70 * @hide
71 */
72 @SystemApi
73 @TestApi
74 public static final int APP_REQUEST_REEVALUATION_REQUIRED = APP_REQUEST_BASE + 0;
Paul Jensen49e3edf2015-05-22 10:50:39 -040075
76 private final IBinder mBinder;
77
78 /** @hide */
paulhu79b380c2019-03-15 17:17:02 +080079 public CaptivePortal(@NonNull IBinder binder) {
Paul Jensen49e3edf2015-05-22 10:50:39 -040080 mBinder = binder;
81 }
82
83 @Override
84 public int describeContents() {
85 return 0;
86 }
87
88 @Override
89 public void writeToParcel(Parcel out, int flags) {
90 out.writeStrongBinder(mBinder);
91 }
92
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070093 public static final @android.annotation.NonNull Parcelable.Creator<CaptivePortal> CREATOR
Paul Jensen49e3edf2015-05-22 10:50:39 -040094 = new Parcelable.Creator<CaptivePortal>() {
95 @Override
96 public CaptivePortal createFromParcel(Parcel in) {
97 return new CaptivePortal(in.readStrongBinder());
98 }
99
100 @Override
101 public CaptivePortal[] newArray(int size) {
102 return new CaptivePortal[size];
103 }
104 };
105
106 /**
107 * Indicate to the system that the captive portal has been
108 * dismissed. In response the framework will re-evaluate the network's
109 * connectivity and might take further action thereafter.
110 */
111 public void reportCaptivePortalDismissed() {
112 try {
113 ICaptivePortal.Stub.asInterface(mBinder).appResponse(APP_RETURN_DISMISSED);
114 } catch (RemoteException e) {
115 }
116 }
117
118 /**
119 * Indicate to the system that the user does not want to pursue signing in to the
120 * captive portal and the system should continue to prefer other networks
121 * without captive portals for use as the default active data network. The
122 * system will not retest the network for a captive portal so as to avoid
123 * disturbing the user with further sign in to network notifications.
124 */
125 public void ignoreNetwork() {
126 try {
127 ICaptivePortal.Stub.asInterface(mBinder).appResponse(APP_RETURN_UNWANTED);
128 } catch (RemoteException e) {
129 }
130 }
131
132 /**
133 * Indicate to the system the user wants to use this network as is, even though
134 * the captive portal is still in place. The system will treat the network
135 * as if it did not have a captive portal when selecting the network to use
136 * as the default active data network. This may result in this network
137 * becoming the default active data network, which could disrupt network
138 * connectivity for apps because the captive portal is still in place.
139 * @hide
140 */
Remi NGUYEN VAN111e1c32019-01-21 23:36:53 +0900141 @SystemApi
142 @TestApi
Paul Jensen49e3edf2015-05-22 10:50:39 -0400143 public void useNetwork() {
144 try {
145 ICaptivePortal.Stub.asInterface(mBinder).appResponse(APP_RETURN_WANTED_AS_IS);
146 } catch (RemoteException e) {
147 }
148 }
Remi NGUYEN VANd4a9de202019-01-30 22:01:20 +0900149
150 /**
Chiachang Wang207b6cf2020-01-09 13:50:55 +0800151 * Request that the system reevaluates the captive portal status.
152 * @hide
153 */
154 @SystemApi
155 @TestApi
156 public void reevaluateNetwork() {
157 try {
158 ICaptivePortal.Stub.asInterface(mBinder).appRequest(APP_REQUEST_REEVALUATION_REQUIRED);
159 } catch (RemoteException e) {
160 }
161 }
162
163 /**
Remi NGUYEN VANd4a9de202019-01-30 22:01:20 +0900164 * Log a captive portal login event.
Remi NGUYEN VAN59a06a02019-04-01 17:09:05 +0900165 * @param eventId one of the CAPTIVE_PORTAL_LOGIN_* constants in metrics_constants.proto.
166 * @param packageName captive portal application package name.
Remi NGUYEN VANd4a9de202019-01-30 22:01:20 +0900167 * @hide
168 */
169 @SystemApi
170 @TestApi
paulhu79b380c2019-03-15 17:17:02 +0800171 public void logEvent(int eventId, @NonNull String packageName) {
Remi NGUYEN VANd4a9de202019-01-30 22:01:20 +0900172 try {
173 ICaptivePortal.Stub.asInterface(mBinder).logEvent(eventId, packageName);
174 } catch (RemoteException e) {
175 }
176 }
Paul Jensen49e3edf2015-05-22 10:50:39 -0400177}