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.util;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.net.NetworkCapabilities;
23 import android.text.TextUtils;
24 import android.util.AndroidRuntimeException;
25 
26 import com.android.internal.R;
27 
28 /**
29  * Collection of utilities for socket keepalive offload.
30  *
31  * @hide
32  */
33 public final class KeepaliveUtils {
34 
35     public static final String TAG = "KeepaliveUtils";
36 
37     public static class KeepaliveDeviceConfigurationException extends AndroidRuntimeException {
KeepaliveDeviceConfigurationException(final String msg)38         public KeepaliveDeviceConfigurationException(final String msg) {
39             super(msg);
40         }
41     }
42 
43     /**
44      * Read supported keepalive count for each transport type from overlay resource. This should be
45      * used to create a local variable store of resource customization, and use it as the input for
46      * {@link getSupportedKeepalivesForNetworkCapabilities}.
47      *
48      * @param context The context to read resource from.
49      * @return An array of supported keepalive count for each transport type.
50      */
51     @NonNull
getSupportedKeepalives(@onNull Context context)52     public static int[] getSupportedKeepalives(@NonNull Context context) {
53         String[] res = null;
54         try {
55             res = context.getResources().getStringArray(
56                     R.array.config_networkSupportedKeepaliveCount);
57         } catch (Resources.NotFoundException unused) {
58         }
59         if (res == null) throw new KeepaliveDeviceConfigurationException("invalid resource");
60 
61         final int[] ret = new int[NetworkCapabilities.MAX_TRANSPORT + 1];
62         for (final String row : res) {
63             if (TextUtils.isEmpty(row)) {
64                 throw new KeepaliveDeviceConfigurationException("Empty string");
65             }
66             final String[] arr = row.split(",");
67             if (arr.length != 2) {
68                 throw new KeepaliveDeviceConfigurationException("Invalid parameter length");
69             }
70 
71             int transport;
72             int supported;
73             try {
74                 transport = Integer.parseInt(arr[0]);
75                 supported = Integer.parseInt(arr[1]);
76             } catch (NumberFormatException e) {
77                 throw new KeepaliveDeviceConfigurationException("Invalid number format");
78             }
79 
80             if (!NetworkCapabilities.isValidTransport(transport)) {
81                 throw new KeepaliveDeviceConfigurationException("Invalid transport " + transport);
82             }
83 
84             if (supported < 0) {
85                 throw new KeepaliveDeviceConfigurationException(
86                         "Invalid supported count " + supported + " for "
87                                 + NetworkCapabilities.transportNameOf(transport));
88             }
89             ret[transport] = supported;
90         }
91         return ret;
92     }
93 
94     /**
95      * Get supported keepalive count for the given {@link NetworkCapabilities}.
96      *
97      * @param supportedKeepalives An array of supported keepalive count for each transport type.
98      * @param nc The {@link NetworkCapabilities} of the network the socket keepalive is on.
99      *
100      * @return Supported keepalive count for the given {@link NetworkCapabilities}.
101      */
getSupportedKeepalivesForNetworkCapabilities( @onNull int[] supportedKeepalives, @NonNull NetworkCapabilities nc)102     public static int getSupportedKeepalivesForNetworkCapabilities(
103             @NonNull int[] supportedKeepalives, @NonNull NetworkCapabilities nc) {
104         final int[] transports = nc.getTransportTypes();
105         if (transports.length == 0) return 0;
106         int supportedCount = supportedKeepalives[transports[0]];
107         // Iterate through transports and return minimum supported value.
108         for (final int transport : transports) {
109             if (supportedCount > supportedKeepalives[transport]) {
110                 supportedCount = supportedKeepalives[transport];
111             }
112         }
113         return supportedCount;
114     }
115 }
116