WifiPermissionsWrapper.java revision a082e33e3f96e2b8394ae42b89601cad3f28679c
1/*
2 * Copyright (C) 2016 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
17package com.android.server.wifi.util;
18
19import android.Manifest;
20import android.app.ActivityManager;
21import android.app.AppGlobals;
22import android.app.admin.DevicePolicyManagerInternal;
23import android.content.Context;
24import android.os.RemoteException;
25import android.os.UserHandle;
26
27import com.android.server.LocalServices;
28
29import java.util.List;
30
31/**
32 * A wifi permissions dependency class to wrap around external
33 * calls to static methods that enable testing.
34 */
35public class WifiPermissionsWrapper {
36    private static final String TAG = "WifiPermissionsWrapper";
37    private final Context mContext;
38
39    public WifiPermissionsWrapper(Context context) {
40        mContext = context;
41    }
42
43    public int getCurrentUser() {
44        return ActivityManager.getCurrentUser();
45    }
46
47    /**
48     * Returns the user ID corresponding to the UID
49     * @param uid Calling Uid
50     * @return userid Corresponding user id
51     */
52    public int getCallingUserId(int uid) {
53        return UserHandle.getUserId(uid);
54    }
55
56    /**
57     * Get the PackageName of the top running task
58     * @return String corresponding to the package
59     */
60    public String getTopPkgName() {
61        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
62        String topTaskPkg = " ";
63        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
64        if (!tasks.isEmpty()) {
65            return tasks.get(0).topActivity.getPackageName();
66        }
67        return topTaskPkg;
68    }
69
70    /**
71     * API is wrap around ActivityManager class to
72     * get location permissions for a certain UID
73     * @param: Manifest permission string
74     * @param: Uid to get permission for
75     * @return: Permissions setting
76     */
77    public int getUidPermission(String permissionType, int uid) {
78        return ActivityManager.checkUidPermission(permissionType, uid);
79    }
80
81    /**
82     * Gets the local service {link@ DevicePolicyManagerInternal}, can be null
83     */
84    public DevicePolicyManagerInternal getDevicePolicyManagerInternal() {
85        return LocalServices.getService(DevicePolicyManagerInternal.class);
86    }
87
88    /**
89     * Determines if the caller has the override wifi config permission.
90     *
91     * @param uid to check the permission for
92     * @return int representation of success or denied
93     * @throws RemoteException
94     */
95    public int getOverrideWifiConfigPermission(int uid) throws RemoteException {
96        return AppGlobals.getPackageManager().checkUidPermission(
97                android.Manifest.permission.OVERRIDE_WIFI_CONFIG, uid);
98    }
99
100    /**
101     * Determines if the caller has the change wifi config permission.
102     *
103     * @param uid to check the permission for
104     * @return int representation of success or denied
105     * @throws RemoteException
106     */
107    public int getChangeWifiConfigPermission(int uid) throws RemoteException {
108        return AppGlobals.getPackageManager().checkUidPermission(
109                Manifest.permission.CHANGE_WIFI_STATE, uid);
110    }
111}
112