junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
chiachangwang | 9ef4ffe | 2023-01-18 01:19:27 +0000 | [diff] [blame] | 19 | import static android.annotation.SystemApi.Client.PRIVILEGED_APPS; |
| 20 | |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 21 | import android.annotation.IntDef; |
| 22 | import android.annotation.IntRange; |
| 23 | import android.annotation.NonNull; |
paulhu | 2f7c345 | 2020-01-13 16:18:54 +0800 | [diff] [blame] | 24 | import android.annotation.SystemApi; |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 25 | import android.os.Binder; |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 26 | import android.os.ParcelFileDescriptor; |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 27 | import android.os.RemoteException; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 28 | |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 29 | import java.io.IOException; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 30 | import java.lang.annotation.Retention; |
| 31 | import java.lang.annotation.RetentionPolicy; |
| 32 | import java.util.concurrent.Executor; |
| 33 | |
| 34 | /** |
| 35 | * Allows applications to request that the system periodically send specific packets on their |
| 36 | * behalf, using hardware offload to save battery power. |
| 37 | * |
| 38 | * To request that the system send keepalives, call one of the methods that return a |
| 39 | * {@link SocketKeepalive} object, such as {@link ConnectivityManager#createSocketKeepalive}, |
| 40 | * passing in a non-null callback. If the {@link SocketKeepalive} is successfully |
| 41 | * started, the callback's {@code onStarted} method will be called. If an error occurs, |
| 42 | * {@code onError} will be called, specifying one of the {@code ERROR_*} constants in this |
| 43 | * class. |
| 44 | * |
| 45 | * To stop an existing keepalive, call {@link SocketKeepalive#stop}. The system will call |
| 46 | * {@link SocketKeepalive.Callback#onStopped} if the operation was successful or |
| 47 | * {@link SocketKeepalive.Callback#onError} if an error occurred. |
junyulai | 4dca18a | 2019-04-17 15:22:46 +0800 | [diff] [blame] | 48 | * |
Aaron Huang | ede0d50 | 2019-06-05 17:09:29 +0800 | [diff] [blame] | 49 | * For cellular, the device MUST support at least 1 keepalive slot. |
| 50 | * |
| 51 | * For WiFi, the device SHOULD support keepalive offload. If it does not, it MUST reply with |
junyulai | 4dca18a | 2019-04-17 15:22:46 +0800 | [diff] [blame] | 52 | * {@link SocketKeepalive.Callback#onError} with {@code ERROR_UNSUPPORTED} to any keepalive offload |
Aaron Huang | ede0d50 | 2019-06-05 17:09:29 +0800 | [diff] [blame] | 53 | * request. If it does, it MUST support at least 3 concurrent keepalive slots. |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 54 | */ |
| 55 | public abstract class SocketKeepalive implements AutoCloseable { |
Remi NGUYEN VAN | e55a88d | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 56 | /** @hide */ |
| 57 | protected static final String TAG = "SocketKeepalive"; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 58 | |
paulhu | 2f7c345 | 2020-01-13 16:18:54 +0800 | [diff] [blame] | 59 | /** |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 60 | * Success. It indicates there is no error. |
paulhu | 2f7c345 | 2020-01-13 16:18:54 +0800 | [diff] [blame] | 61 | * @hide |
| 62 | */ |
| 63 | @SystemApi |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 64 | public static final int SUCCESS = 0; |
| 65 | |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 66 | /** |
Chalard Jean | bdb8282 | 2023-01-19 23:14:02 +0900 | [diff] [blame] | 67 | * Success when trying to suspend. |
| 68 | * @hide |
| 69 | */ |
| 70 | public static final int SUCCESS_PAUSED = 1; |
| 71 | |
| 72 | /** |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 73 | * No keepalive. This should only be internally as it indicates There is no keepalive. |
| 74 | * It should not propagate to applications. |
| 75 | * @hide |
| 76 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 77 | public static final int NO_KEEPALIVE = -1; |
| 78 | |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 79 | /** |
| 80 | * Data received. |
| 81 | * @hide |
| 82 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 83 | public static final int DATA_RECEIVED = -2; |
| 84 | |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 85 | /** |
| 86 | * The binder died. |
| 87 | * @hide |
| 88 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 89 | public static final int BINDER_DIED = -10; |
| 90 | |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 91 | /** |
| 92 | * The invalid network. It indicates the specified {@code Network} is not connected. |
| 93 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 94 | public static final int ERROR_INVALID_NETWORK = -20; |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * The invalid IP addresses. Indicates the specified IP addresses are invalid. |
| 98 | * For example, the specified source IP address is not configured on the |
| 99 | * specified {@code Network}. |
| 100 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 101 | public static final int ERROR_INVALID_IP_ADDRESS = -21; |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * The port is invalid. |
| 105 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 106 | public static final int ERROR_INVALID_PORT = -22; |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * The length is invalid (e.g. too long). |
| 110 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 111 | public static final int ERROR_INVALID_LENGTH = -23; |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * The interval is invalid (e.g. too short). |
| 115 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 116 | public static final int ERROR_INVALID_INTERVAL = -24; |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * The socket is invalid. |
| 120 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 121 | public static final int ERROR_INVALID_SOCKET = -25; |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * The socket is not idle. |
| 125 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 126 | public static final int ERROR_SOCKET_NOT_IDLE = -26; |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 127 | |
junyulai | 75125f2 | 2019-09-03 18:51:04 +0800 | [diff] [blame] | 128 | /** |
| 129 | * The stop reason is uninitialized. This should only be internally used as initial state |
| 130 | * of stop reason, instead of propagating to application. |
| 131 | * @hide |
| 132 | */ |
| 133 | public static final int ERROR_STOP_REASON_UNINITIALIZED = -27; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 134 | |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 135 | /** |
| 136 | * The request is unsupported. |
| 137 | */ |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 138 | public static final int ERROR_UNSUPPORTED = -30; |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * There was a hardware error. |
| 142 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 143 | public static final int ERROR_HARDWARE_ERROR = -31; |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Resources are insufficient (e.g. all hardware slots are in use). |
| 147 | */ |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 148 | public static final int ERROR_INSUFFICIENT_RESOURCES = -32; |
| 149 | |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 150 | /** |
| 151 | * There was no such slot. This should only be internally as it indicates |
| 152 | * a programming error in the system server. It should not propagate to |
| 153 | * applications. |
| 154 | * @hide |
| 155 | */ |
| 156 | @SystemApi |
| 157 | public static final int ERROR_NO_SUCH_SLOT = -33; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 158 | |
| 159 | /** @hide */ |
| 160 | @Retention(RetentionPolicy.SOURCE) |
| 161 | @IntDef(prefix = { "ERROR_" }, value = { |
| 162 | ERROR_INVALID_NETWORK, |
| 163 | ERROR_INVALID_IP_ADDRESS, |
| 164 | ERROR_INVALID_PORT, |
| 165 | ERROR_INVALID_LENGTH, |
| 166 | ERROR_INVALID_INTERVAL, |
| 167 | ERROR_INVALID_SOCKET, |
lifr | 75764c8 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 168 | ERROR_SOCKET_NOT_IDLE, |
| 169 | ERROR_NO_SUCH_SLOT |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 170 | }) |
| 171 | public @interface ErrorCode {} |
| 172 | |
Chalard Jean | f5d1bfd | 2020-03-24 23:16:35 +0900 | [diff] [blame] | 173 | /** @hide */ |
| 174 | @Retention(RetentionPolicy.SOURCE) |
| 175 | @IntDef(value = { |
| 176 | SUCCESS, |
| 177 | ERROR_INVALID_LENGTH, |
| 178 | ERROR_UNSUPPORTED, |
Chalard Jean | 691a34d | 2020-03-27 15:00:38 +0900 | [diff] [blame] | 179 | ERROR_INSUFFICIENT_RESOURCES, |
Chalard Jean | f5d1bfd | 2020-03-24 23:16:35 +0900 | [diff] [blame] | 180 | }) |
| 181 | public @interface KeepaliveEvent {} |
| 182 | |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 183 | /** |
chiachangwang | 9ef4ffe | 2023-01-18 01:19:27 +0000 | [diff] [blame] | 184 | * Whether the system automatically toggles keepalive when no TCP connection is open on the VPN. |
| 185 | * |
| 186 | * If this flag is present, the system will monitor the VPN(s) running on top of the specified |
| 187 | * network for open TCP connections. When no such connections are open, it will turn off the |
| 188 | * keepalives to conserve battery power. When there is at least one such connection it will |
| 189 | * turn on the keepalives to make sure functionality is preserved. |
| 190 | * |
| 191 | * This only works with {@link NattSocketKeepalive}. |
| 192 | * @hide |
| 193 | */ |
| 194 | @SystemApi |
| 195 | public static final int FLAG_AUTOMATIC_ON_OFF = 1 << 0; |
| 196 | |
| 197 | /** @hide */ |
| 198 | @Retention(RetentionPolicy.SOURCE) |
| 199 | @IntDef(prefix = { "FLAG_"}, flag = true, value = { |
| 200 | FLAG_AUTOMATIC_ON_OFF |
| 201 | }) |
| 202 | public @interface StartFlags {} |
| 203 | |
| 204 | /** |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 205 | * The minimum interval in seconds between keepalive packet transmissions. |
| 206 | * |
| 207 | * @hide |
| 208 | **/ |
| 209 | public static final int MIN_INTERVAL_SEC = 10; |
| 210 | |
| 211 | /** |
| 212 | * The maximum interval in seconds between keepalive packet transmissions. |
| 213 | * |
| 214 | * @hide |
| 215 | **/ |
| 216 | public static final int MAX_INTERVAL_SEC = 3600; |
| 217 | |
junyulai | 011b1f1 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 218 | /** |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 219 | * An exception that embarks an error code. |
| 220 | * @hide |
| 221 | */ |
| 222 | public static class ErrorCodeException extends Exception { |
| 223 | public final int error; |
| 224 | public ErrorCodeException(final int error, final Throwable e) { |
| 225 | super(e); |
| 226 | this.error = error; |
| 227 | } |
| 228 | public ErrorCodeException(final int error) { |
| 229 | this.error = error; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * This socket is invalid. |
| 235 | * See the error code for details, and the optional cause. |
| 236 | * @hide |
| 237 | */ |
| 238 | public static class InvalidSocketException extends ErrorCodeException { |
| 239 | public InvalidSocketException(final int error, final Throwable e) { |
| 240 | super(error, e); |
| 241 | } |
| 242 | public InvalidSocketException(final int error) { |
| 243 | super(error); |
| 244 | } |
| 245 | } |
| 246 | |
Remi NGUYEN VAN | e55a88d | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 247 | /** @hide */ |
| 248 | @NonNull protected final IConnectivityManager mService; |
| 249 | /** @hide */ |
| 250 | @NonNull protected final Network mNetwork; |
| 251 | /** @hide */ |
| 252 | @NonNull protected final ParcelFileDescriptor mPfd; |
| 253 | /** @hide */ |
| 254 | @NonNull protected final Executor mExecutor; |
| 255 | /** @hide */ |
| 256 | @NonNull protected final ISocketKeepaliveCallback mCallback; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 257 | |
Remi NGUYEN VAN | e55a88d | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 258 | /** @hide */ |
| 259 | public SocketKeepalive(@NonNull IConnectivityManager service, @NonNull Network network, |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 260 | @NonNull ParcelFileDescriptor pfd, |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 261 | @NonNull Executor executor, @NonNull Callback callback) { |
| 262 | mService = service; |
| 263 | mNetwork = network; |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 264 | mPfd = pfd; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 265 | mExecutor = executor; |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 266 | mCallback = new ISocketKeepaliveCallback.Stub() { |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 267 | @Override |
Chalard Jean | f0b261e | 2023-02-03 22:11:20 +0900 | [diff] [blame] | 268 | public void onStarted() { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 269 | final long token = Binder.clearCallingIdentity(); |
| 270 | try { |
| 271 | mExecutor.execute(() -> { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 272 | callback.onStarted(); |
| 273 | }); |
| 274 | } finally { |
| 275 | Binder.restoreCallingIdentity(token); |
| 276 | } |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 277 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 278 | |
| 279 | @Override |
Chalard Jean | bdb8282 | 2023-01-19 23:14:02 +0900 | [diff] [blame] | 280 | public void onResumed() { |
| 281 | final long token = Binder.clearCallingIdentity(); |
| 282 | try { |
| 283 | mExecutor.execute(() -> { |
| 284 | callback.onResumed(); |
| 285 | }); |
| 286 | } finally { |
| 287 | Binder.restoreCallingIdentity(token); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | @Override |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 292 | public void onStopped() { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 293 | final long token = Binder.clearCallingIdentity(); |
| 294 | try { |
| 295 | executor.execute(() -> { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 296 | callback.onStopped(); |
| 297 | }); |
| 298 | } finally { |
| 299 | Binder.restoreCallingIdentity(token); |
| 300 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Override |
Chalard Jean | bdb8282 | 2023-01-19 23:14:02 +0900 | [diff] [blame] | 304 | public void onPaused() { |
| 305 | final long token = Binder.clearCallingIdentity(); |
| 306 | try { |
| 307 | executor.execute(() -> { |
| 308 | callback.onPaused(); |
| 309 | }); |
| 310 | } finally { |
| 311 | Binder.restoreCallingIdentity(token); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | @Override |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 316 | public void onError(int error) { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 317 | final long token = Binder.clearCallingIdentity(); |
| 318 | try { |
| 319 | executor.execute(() -> { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 320 | callback.onError(error); |
| 321 | }); |
| 322 | } finally { |
| 323 | Binder.restoreCallingIdentity(token); |
| 324 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public void onDataReceived() { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 329 | final long token = Binder.clearCallingIdentity(); |
| 330 | try { |
| 331 | executor.execute(() -> { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 332 | callback.onDataReceived(); |
| 333 | }); |
| 334 | } finally { |
| 335 | Binder.restoreCallingIdentity(token); |
| 336 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 337 | } |
| 338 | }; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
chiachangwang | 9ef4ffe | 2023-01-18 01:19:27 +0000 | [diff] [blame] | 342 | * Request that keepalive be started with the given {@code intervalSec}. |
| 343 | * |
| 344 | * See {@link SocketKeepalive}. If the remote binder dies, or the binder call throws an |
| 345 | * exception when invoking start or stop of the {@link SocketKeepalive}, a |
| 346 | * {@link RuntimeException} caused by a {@link RemoteException} will be thrown into the |
| 347 | * {@link Executor}. This is typically not important to catch because the remote party is |
| 348 | * the system, so if it is not in shape to communicate through binder the system is going |
| 349 | * down anyway. If the caller still cares, it can use a custom {@link Executor} to catch the |
| 350 | * {@link RuntimeException}. |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 351 | * |
| 352 | * @param intervalSec The target interval in seconds between keepalive packet transmissions. |
| 353 | * The interval should be between 10 seconds and 3600 seconds, otherwise |
| 354 | * {@link #ERROR_INVALID_INTERVAL} will be returned. |
| 355 | */ |
| 356 | public final void start(@IntRange(from = MIN_INTERVAL_SEC, to = MAX_INTERVAL_SEC) |
| 357 | int intervalSec) { |
chiachangwang | 676c84e | 2023-02-14 09:22:05 +0000 | [diff] [blame] | 358 | startImpl(intervalSec, 0 /* flags */, null /* underpinnedNetwork */); |
chiachangwang | 9ef4ffe | 2023-01-18 01:19:27 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Request that keepalive be started with the given {@code intervalSec}. |
| 363 | * |
| 364 | * See {@link SocketKeepalive}. If the remote binder dies, or the binder call throws an |
| 365 | * exception when invoking start or stop of the {@link SocketKeepalive}, a |
| 366 | * {@link RuntimeException} caused by a {@link RemoteException} will be thrown into the |
| 367 | * {@link Executor}. This is typically not important to catch because the remote party is |
| 368 | * the system, so if it is not in shape to communicate through binder the system is going |
| 369 | * down anyway. If the caller still cares, it can use a custom {@link Executor} to catch the |
| 370 | * {@link RuntimeException}. |
| 371 | * |
| 372 | * @param intervalSec The target interval in seconds between keepalive packet transmissions. |
| 373 | * The interval should be between 10 seconds and 3600 seconds. Otherwise, |
| 374 | * the supplied {@link Callback} will see a call to |
| 375 | * {@link Callback#onError(int)} with {@link #ERROR_INVALID_INTERVAL}. |
| 376 | * @param flags Flags to enable/disable available options on this keepalive. |
chiachangwang | 676c84e | 2023-02-14 09:22:05 +0000 | [diff] [blame] | 377 | * @param underpinnedNetwork The underpinned network of this keepalive. |
chiachangwang | 9ef4ffe | 2023-01-18 01:19:27 +0000 | [diff] [blame] | 378 | * @hide |
| 379 | */ |
| 380 | @SystemApi(client = PRIVILEGED_APPS) |
| 381 | public final void start(@IntRange(from = MIN_INTERVAL_SEC, to = MAX_INTERVAL_SEC) |
chiachangwang | 676c84e | 2023-02-14 09:22:05 +0000 | [diff] [blame] | 382 | int intervalSec, @StartFlags int flags, @NonNull Network underpinnedNetwork) { |
| 383 | startImpl(intervalSec, flags, underpinnedNetwork); |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 384 | } |
| 385 | |
Remi NGUYEN VAN | e55a88d | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 386 | /** @hide */ |
chiachangwang | 676c84e | 2023-02-14 09:22:05 +0000 | [diff] [blame] | 387 | protected abstract void startImpl(int intervalSec, @StartFlags int flags, |
| 388 | Network underpinnedNetwork); |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 389 | |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 390 | /** |
| 391 | * Requests that keepalive be stopped. The application must wait for {@link Callback#onStopped} |
| 392 | * before using the object. See {@link SocketKeepalive}. |
| 393 | */ |
| 394 | public final void stop() { |
| 395 | stopImpl(); |
| 396 | } |
| 397 | |
Remi NGUYEN VAN | e55a88d | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 398 | /** @hide */ |
| 399 | protected abstract void stopImpl(); |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 400 | |
| 401 | /** |
| 402 | * Deactivate this {@link SocketKeepalive} and free allocated resources. The instance won't be |
| 403 | * usable again if {@code close()} is called. |
| 404 | */ |
| 405 | @Override |
| 406 | public final void close() { |
| 407 | stop(); |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 408 | try { |
| 409 | mPfd.close(); |
| 410 | } catch (IOException e) { |
| 411 | // Nothing much can be done. |
| 412 | } |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /** |
| 416 | * The callback which app can use to learn the status changes of {@link SocketKeepalive}. See |
| 417 | * {@link SocketKeepalive}. |
| 418 | */ |
| 419 | public static class Callback { |
| 420 | /** The requested keepalive was successfully started. */ |
| 421 | public void onStarted() {} |
Chalard Jean | bdb8282 | 2023-01-19 23:14:02 +0900 | [diff] [blame] | 422 | /** |
| 423 | * The keepalive was resumed by the system after being suspended. |
| 424 | * @hide |
| 425 | **/ |
| 426 | public void onResumed() {} |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 427 | /** The keepalive was successfully stopped. */ |
| 428 | public void onStopped() {} |
Chalard Jean | bdb8282 | 2023-01-19 23:14:02 +0900 | [diff] [blame] | 429 | /** |
| 430 | * The keepalive was paused by the system because it's not necessary right now. |
| 431 | * @hide |
| 432 | **/ |
| 433 | public void onPaused() {} |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 434 | /** An error occurred. */ |
| 435 | public void onError(@ErrorCode int error) {} |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 436 | /** The keepalive on a TCP socket was stopped because the socket received data. This is |
| 437 | * never called for UDP sockets. */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 438 | public void onDataReceived() {} |
| 439 | } |
| 440 | } |