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