huangcm
2025-08-25 2f2fd745743ad500687c6985119d523146531958
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2016 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 com.android.server.wifi.util;
 
import android.Manifest;
import android.app.ActivityManager;
import android.app.AppGlobals;
import android.app.admin.DevicePolicyManagerInternal;
import android.content.Context;
import android.os.RemoteException;
import android.os.UserHandle;
 
import com.android.server.LocalServices;
 
import java.util.List;
 
/**
 * A wifi permissions dependency class to wrap around external
 * calls to static methods that enable testing.
 */
public class WifiPermissionsWrapper {
    private static final String TAG = "WifiPermissionsWrapper";
    private final Context mContext;
 
    public WifiPermissionsWrapper(Context context) {
        mContext = context;
    }
 
    public int getCurrentUser() {
        return ActivityManager.getCurrentUser();
    }
 
    /**
     * Returns the user ID corresponding to the UID
     * @param uid Calling Uid
     * @return userid Corresponding user id
     */
    public int getCallingUserId(int uid) {
        return UserHandle.getUserId(uid);
    }
 
    /**
     * Get the PackageName of the top running task
     * @return String corresponding to the package
     */
    public String getTopPkgName() {
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        String topTaskPkg = " ";
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            return tasks.get(0).topActivity.getPackageName();
        }
        return topTaskPkg;
    }
 
    /**
     * API is wrap around ActivityManager class to
     * get location permissions for a certain UID
     * @param: Manifest permission string
     * @param: Uid to get permission for
     * @return: Permissions setting
     */
    public int getUidPermission(String permissionType, int uid) {
        return ActivityManager.checkUidPermission(permissionType, uid);
    }
 
    /**
     * Gets the local service {link@ DevicePolicyManagerInternal}, can be null
     */
    public DevicePolicyManagerInternal getDevicePolicyManagerInternal() {
        return LocalServices.getService(DevicePolicyManagerInternal.class);
    }
 
    /**
     * Determines if the caller has the override wifi config permission.
     *
     * @param uid to check the permission for
     * @return int representation of success or denied
     * @throws RemoteException
     */
    public int getOverrideWifiConfigPermission(int uid) throws RemoteException {
        return AppGlobals.getPackageManager().checkUidPermission(
                android.Manifest.permission.OVERRIDE_WIFI_CONFIG, uid);
    }
 
    /**
     * Determines if the caller has the change wifi config permission.
     *
     * @param uid to check the permission for
     * @return int representation of success or denied
     * @throws RemoteException
     */
    public int getChangeWifiConfigPermission(int uid) throws RemoteException {
        return AppGlobals.getPackageManager().checkUidPermission(
                Manifest.permission.CHANGE_WIFI_STATE, uid);
    }
 
    /**
     * Determines if the caller has the access wifi state permission.
     *
     * @param uid to check the permission for
     * @return int representation of success or denied
     * @throws RemoteException
     */
    public int getAccessWifiStatePermission(int uid) throws RemoteException {
        return AppGlobals.getPackageManager().checkUidPermission(
                Manifest.permission.ACCESS_WIFI_STATE, uid);
    }
 
    /**
     * Determines if the caller has local mac address permission.
     *
     * @param uid to check the permission for
     * @return int representation of success or denied
     * @throws RemoteException
     */
    public int getLocalMacAddressPermission(int uid) throws RemoteException {
        return AppGlobals.getPackageManager().checkUidPermission(
                Manifest.permission.LOCAL_MAC_ADDRESS, uid);
    }
}