summaryrefslogtreecommitdiff
path: root/framework/src/android/net/ConnectivityResources.java
blob: 18f0de0cc9a17a5e2bbc53727de9b912848be55d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * 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();
    }
}