• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.captiveportal;
18 
19 import androidx.annotation.NonNull;
20 import androidx.annotation.Nullable;
21 
22 /**
23  * Result of calling isCaptivePortal().
24  * @hide
25  */
26 public final class CaptivePortalProbeResult {
27     public static final int SUCCESS_CODE = 204;
28     public static final int FAILED_CODE = 599;
29     public static final int PORTAL_CODE = 302;
30     // Set partial connectivity http response code to -1 to prevent conflict with the other http
31     // response codes. Besides the default http response code of probe result is set as 599 in
32     // NetworkMonitor#sendParallelHttpProbes(), so response code will be set as -1 only when
33     // NetworkMonitor detects partial connectivity.
34     /**
35      * @hide
36      */
37     public static final int PARTIAL_CODE = -1;
38 
39     @NonNull
40     public static final CaptivePortalProbeResult FAILED = new CaptivePortalProbeResult(FAILED_CODE);
41     @NonNull
42     public static final CaptivePortalProbeResult SUCCESS =
43             new CaptivePortalProbeResult(SUCCESS_CODE);
44     public static final CaptivePortalProbeResult PARTIAL =
45             new CaptivePortalProbeResult(PARTIAL_CODE);
46 
47     private final int mHttpResponseCode;  // HTTP response code returned from Internet probe.
48     @Nullable
49     public final String redirectUrl;      // Redirect destination returned from Internet probe.
50     @Nullable
51     public final String detectUrl;        // URL where a 204 response code indicates
52                                           // captive portal has been appeased.
53     @Nullable
54     public final CaptivePortalProbeSpec probeSpec;
55 
CaptivePortalProbeResult(int httpResponseCode)56     public CaptivePortalProbeResult(int httpResponseCode) {
57         this(httpResponseCode, null, null);
58     }
59 
CaptivePortalProbeResult(int httpResponseCode, @Nullable String redirectUrl, @Nullable String detectUrl)60     public CaptivePortalProbeResult(int httpResponseCode, @Nullable String redirectUrl,
61             @Nullable String detectUrl) {
62         this(httpResponseCode, redirectUrl, detectUrl, null);
63     }
64 
CaptivePortalProbeResult(int httpResponseCode, @Nullable String redirectUrl, @Nullable String detectUrl, @Nullable CaptivePortalProbeSpec probeSpec)65     public CaptivePortalProbeResult(int httpResponseCode, @Nullable String redirectUrl,
66             @Nullable String detectUrl, @Nullable CaptivePortalProbeSpec probeSpec) {
67         mHttpResponseCode = httpResponseCode;
68         this.redirectUrl = redirectUrl;
69         this.detectUrl = detectUrl;
70         this.probeSpec = probeSpec;
71     }
72 
isSuccessful()73     public boolean isSuccessful() {
74         return mHttpResponseCode == SUCCESS_CODE;
75     }
76 
isPortal()77     public boolean isPortal() {
78         return !isSuccessful() && (mHttpResponseCode >= 200) && (mHttpResponseCode <= 399);
79     }
80 
isFailed()81     public boolean isFailed() {
82         return !isSuccessful() && !isPortal();
83     }
84 
isPartialConnectivity()85     public boolean isPartialConnectivity() {
86         return mHttpResponseCode == PARTIAL_CODE;
87     }
88 }
89