Remi NGUYEN VAN | 5245c4c | 2021-03-17 23:14:53 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | |
| 19 | import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY; |
| 20 | |
| 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.pm.ResolveInfo; |
| 27 | import android.content.res.Resources; |
| 28 | import android.util.Log; |
| 29 | |
| 30 | import com.android.internal.annotations.VisibleForTesting; |
| 31 | |
| 32 | import java.util.List; |
| 33 | |
| 34 | /** |
| 35 | * Utility to obtain the {@link com.android.server.ConnectivityService} {@link Resources}, in the |
| 36 | * ServiceConnectivityResources APK. |
| 37 | * @hide |
| 38 | */ |
| 39 | public class ConnectivityResources { |
| 40 | private static final String RESOURCES_APK_INTENT = |
| 41 | "com.android.server.connectivity.intent.action.SERVICE_CONNECTIVITY_RESOURCES_APK"; |
| 42 | private static final String RES_PKG_DIR = "/apex/com.android.tethering/"; |
| 43 | |
| 44 | @NonNull |
| 45 | private final Context mContext; |
| 46 | |
| 47 | @Nullable |
| 48 | private Context mResourcesContext = null; |
| 49 | |
| 50 | @Nullable |
| 51 | private static Context sTestResourcesContext = null; |
| 52 | |
| 53 | public ConnectivityResources(Context context) { |
| 54 | mContext = context; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Convenience method to mock all resources for the duration of a test. |
| 59 | * |
| 60 | * Call with a null context to reset after the test. |
| 61 | */ |
| 62 | @VisibleForTesting |
| 63 | public static void setResourcesContextForTest(@Nullable Context testContext) { |
| 64 | sTestResourcesContext = testContext; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the {@link Context} of the resources package. |
| 69 | */ |
| 70 | public synchronized Context getResourcesContext() { |
| 71 | if (sTestResourcesContext != null) { |
| 72 | return sTestResourcesContext; |
| 73 | } |
| 74 | |
| 75 | if (mResourcesContext != null) { |
| 76 | return mResourcesContext; |
| 77 | } |
| 78 | |
| 79 | final List<ResolveInfo> pkgs = mContext.getPackageManager() |
| 80 | .queryIntentActivities(new Intent(RESOURCES_APK_INTENT), MATCH_SYSTEM_ONLY); |
| 81 | pkgs.removeIf(pkg -> !pkg.activityInfo.applicationInfo.sourceDir.startsWith(RES_PKG_DIR)); |
| 82 | if (pkgs.size() > 1) { |
| 83 | Log.wtf(ConnectivityResources.class.getSimpleName(), |
| 84 | "More than one package found: " + pkgs); |
| 85 | } |
| 86 | if (pkgs.isEmpty()) { |
| 87 | throw new IllegalStateException("No connectivity resource package found"); |
| 88 | } |
| 89 | |
| 90 | final Context pkgContext; |
| 91 | try { |
| 92 | pkgContext = mContext.createPackageContext( |
| 93 | pkgs.get(0).activityInfo.applicationInfo.packageName, 0 /* flags */); |
| 94 | } catch (PackageManager.NameNotFoundException e) { |
| 95 | throw new IllegalStateException("Resolved package not found", e); |
| 96 | } |
| 97 | |
| 98 | mResourcesContext = pkgContext; |
| 99 | return pkgContext; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the {@link Resources} of the ServiceConnectivityResources APK. |
| 104 | */ |
| 105 | public Resources get() { |
| 106 | return getResourcesContext().getResources(); |
| 107 | } |
| 108 | } |