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.net.ConnectivityManager; 22 import android.net.NetworkCapabilities; 23 24 /** 25 * Collection of utilities for socket keepalive offload. 26 * 27 * @hide 28 */ 29 public final class KeepaliveUtils { 30 31 public static final String TAG = "KeepaliveUtils"; 32 33 /** 34 * Read supported keepalive count for each transport type from overlay resource. This should be 35 * used to create a local variable store of resource customization, and use it as the input for 36 * {@link #getSupportedKeepalivesForNetworkCapabilities}. 37 * 38 * @param context The context to read resource from. 39 * @return An array of supported keepalive count for each transport type. 40 * @deprecated This is used by CTS 13, but can be removed after switching it to 41 * {@link ConnectivityManager#getSupportedKeepalives()}. 42 */ 43 @NonNull 44 @Deprecated getSupportedKeepalives(@onNull Context context)45 public static int[] getSupportedKeepalives(@NonNull Context context) { 46 return context.getSystemService(ConnectivityManager.class).getSupportedKeepalives(); 47 } 48 49 /** 50 * Get supported keepalive count for the given {@link NetworkCapabilities}. 51 * 52 * @param supportedKeepalives An array of supported keepalive count for each transport type. 53 * @param nc The {@link NetworkCapabilities} of the network the socket keepalive is on. 54 * 55 * @return Supported keepalive count for the given {@link NetworkCapabilities}. 56 */ getSupportedKeepalivesForNetworkCapabilities( @onNull int[] supportedKeepalives, @NonNull NetworkCapabilities nc)57 public static int getSupportedKeepalivesForNetworkCapabilities( 58 @NonNull int[] supportedKeepalives, @NonNull NetworkCapabilities nc) { 59 final int[] transports = nc.getTransportTypes(); 60 if (transports.length == 0) return 0; 61 int supportedCount = supportedKeepalives[transports[0]]; 62 // Iterate through transports and return minimum supported value. 63 for (final int transport : transports) { 64 if (supportedCount > supportedKeepalives[transport]) { 65 supportedCount = supportedKeepalives[transport]; 66 } 67 } 68 return supportedCount; 69 } 70 } 71