Move ConnectivityResources to service-connectivity

The ConnectivityResources class is only usable with QUERY_ALL_PACKAGES
permission, so it is not generally usable in framework-connectivity. It
is also backed by the ServiceConnectivityResources APK, which is
intended as resources for service-connectivity.

Move the class to service-connectivity and update callers. CTS needs a
way to determine the supported keepalive count as it used the
resources from KeepaliveUtils as @hide API, so provide a
ConnectivityManager @hide API for testing.

Bug: 279108992
Test: atest
Change-Id: I3c9a77c580b5ab87c922c32778bce15dc33b4d1d
This commit is contained in:
Remi NGUYEN VAN
2023-02-20 20:10:09 +09:00
committed by Motomu Utsumi
parent e50f1464bb
commit bee2ee14f3
25 changed files with 163 additions and 112 deletions

View File

@@ -2534,6 +2534,26 @@ public class ConnectivityManager {
return new TcpSocketKeepalive(mService, network, dup, executor, callback);
}
/**
* Get the supported keepalive count for each transport configured in resource overlays.
*
* @return An array of supported keepalive count for each transport type.
* @hide
*/
@RequiresPermission(anyOf = { android.Manifest.permission.NETWORK_SETTINGS,
// CTS 13 used QUERY_ALL_PACKAGES to get the resource value, which was implemented
// as below in KeepaliveUtils. Also allow that permission so that KeepaliveUtils can
// use this method and avoid breaking released CTS. Apps that have this permission
// can query the resource themselves anyway.
android.Manifest.permission.QUERY_ALL_PACKAGES })
public int[] getSupportedKeepalives() {
try {
return mService.getSupportedKeepalives();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Ensure that a network route exists to deliver traffic to the specified
* host via the specified network interface. An attempt to add a route that

View File

@@ -1,108 +0,0 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
import java.util.List;
/**
* Utility to obtain the {@link com.android.server.ConnectivityService} {@link Resources}, in the
* ServiceConnectivityResources APK.
* @hide
*/
public class ConnectivityResources {
private static final String RESOURCES_APK_INTENT =
"com.android.server.connectivity.intent.action.SERVICE_CONNECTIVITY_RESOURCES_APK";
private static final String RES_PKG_DIR = "/apex/com.android.tethering/";
@NonNull
private final Context mContext;
@Nullable
private Context mResourcesContext = null;
@Nullable
private static Context sTestResourcesContext = null;
public ConnectivityResources(Context context) {
mContext = context;
}
/**
* Convenience method to mock all resources for the duration of a test.
*
* Call with a null context to reset after the test.
*/
@VisibleForTesting
public static void setResourcesContextForTest(@Nullable Context testContext) {
sTestResourcesContext = testContext;
}
/**
* Get the {@link Context} of the resources package.
*/
public synchronized Context getResourcesContext() {
if (sTestResourcesContext != null) {
return sTestResourcesContext;
}
if (mResourcesContext != null) {
return mResourcesContext;
}
final List<ResolveInfo> pkgs = mContext.getPackageManager()
.queryIntentActivities(new Intent(RESOURCES_APK_INTENT), MATCH_SYSTEM_ONLY);
pkgs.removeIf(pkg -> !pkg.activityInfo.applicationInfo.sourceDir.startsWith(RES_PKG_DIR));
if (pkgs.size() > 1) {
Log.wtf(ConnectivityResources.class.getSimpleName(),
"More than one package found: " + pkgs);
}
if (pkgs.isEmpty()) {
throw new IllegalStateException("No connectivity resource package found");
}
final Context pkgContext;
try {
pkgContext = mContext.createPackageContext(
pkgs.get(0).activityInfo.applicationInfo.packageName, 0 /* flags */);
} catch (PackageManager.NameNotFoundException e) {
throw new IllegalStateException("Resolved package not found", e);
}
mResourcesContext = pkgContext;
return pkgContext;
}
/**
* Get the {@link Resources} of the ServiceConnectivityResources APK.
*/
public Resources get() {
return getResourcesContext().getResources();
}
}

View File

@@ -195,6 +195,8 @@ interface IConnectivityManager
void stopKeepalive(in ISocketKeepaliveCallback cb);
int[] getSupportedKeepalives();
String getCaptivePortalServerUrl();
byte[] getNetworkWatchlistConfigHash();

View File

@@ -19,9 +19,6 @@ package android.net.apf;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityResources;
import android.os.Parcel;
import android.os.Parcelable;
@@ -36,8 +33,6 @@ import android.os.Parcelable;
*/
@SystemApi
public final class ApfCapabilities implements Parcelable {
private static ConnectivityResources sResources;
/**
* Version of APF instruction set supported for packet filtering. 0 indicates no support for
* packet filtering using APF programs.
@@ -67,15 +62,6 @@ public final class ApfCapabilities implements Parcelable {
apfPacketFormat = in.readInt();
}
@NonNull
private static synchronized ConnectivityResources getResources(@NonNull Context ctx) {
if (sResources == null) {
sResources = new ConnectivityResources(ctx);
}
return sResources;
}
@Override
public int describeContents() {
return 0;

View File

@@ -18,11 +18,8 @@ package android.net.util;
import android.annotation.NonNull;
import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityResources;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.text.TextUtils;
import android.util.AndroidRuntimeException;
/**
* Collection of utilities for socket keepalive offload.
@@ -33,64 +30,20 @@ public final class KeepaliveUtils {
public static final String TAG = "KeepaliveUtils";
public static class KeepaliveDeviceConfigurationException extends AndroidRuntimeException {
public KeepaliveDeviceConfigurationException(final String msg) {
super(msg);
}
}
/**
* Read supported keepalive count for each transport type from overlay resource. This should be
* used to create a local variable store of resource customization, and use it as the input for
* {@link getSupportedKeepalivesForNetworkCapabilities}.
* {@link #getSupportedKeepalivesForNetworkCapabilities}.
*
* @param context The context to read resource from.
* @return An array of supported keepalive count for each transport type.
* @deprecated This is used by CTS 13, but can be removed after switching it to
* {@link ConnectivityManager#getSupportedKeepalives()}.
*/
@NonNull
@Deprecated
public static int[] getSupportedKeepalives(@NonNull Context context) {
String[] res = null;
try {
final ConnectivityResources connRes = new ConnectivityResources(context);
// TODO: use R.id.config_networkSupportedKeepaliveCount directly
final int id = connRes.get().getIdentifier("config_networkSupportedKeepaliveCount",
"array", connRes.getResourcesContext().getPackageName());
res = new ConnectivityResources(context).get().getStringArray(id);
} catch (Resources.NotFoundException unused) {
}
if (res == null) throw new KeepaliveDeviceConfigurationException("invalid resource");
final int[] ret = new int[NetworkCapabilities.MAX_TRANSPORT + 1];
for (final String row : res) {
if (TextUtils.isEmpty(row)) {
throw new KeepaliveDeviceConfigurationException("Empty string");
}
final String[] arr = row.split(",");
if (arr.length != 2) {
throw new KeepaliveDeviceConfigurationException("Invalid parameter length");
}
int transport;
int supported;
try {
transport = Integer.parseInt(arr[0]);
supported = Integer.parseInt(arr[1]);
} catch (NumberFormatException e) {
throw new KeepaliveDeviceConfigurationException("Invalid number format");
}
if (!NetworkCapabilities.isValidTransport(transport)) {
throw new KeepaliveDeviceConfigurationException("Invalid transport " + transport);
}
if (supported < 0) {
throw new KeepaliveDeviceConfigurationException(
"Invalid supported count " + supported + " for "
+ NetworkCapabilities.transportNameOf(transport));
}
ret[transport] = supported;
}
return ret;
return context.getSystemService(ConnectivityManager.class).getSupportedKeepalives();
}
/**