WifiPermissionsWrapper.java revision 0d14dbc1da819e72054b9f168c8e3db767dd34bf
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.app.ActivityManager;
20import android.app.admin.DevicePolicyManagerInternal;
21import android.content.Context;
22import android.os.UserHandle;
23
24import com.android.server.LocalServices;
25
26import java.util.List;
27
28/**
29 * A wifi permissions dependency class to wrap around external
30 * calls to static methods that enable testing.
31 */
32public class WifiPermissionsWrapper {
33    private static final String TAG = "WifiPermissionsWrapper";
34    private final Context mContext;
35
36    public WifiPermissionsWrapper(Context context) {
37        mContext = context;
38    }
39
40    public int getCurrentUser() {
41        return ActivityManager.getCurrentUser();
42    }
43
44    /**
45     * Returns the user ID corresponding to the UID
46     * @param uid Calling Uid
47     * @return userid Corresponding user id
48     */
49    public int getCallingUserId(int uid) {
50        return UserHandle.getUserId(uid);
51    }
52
53    /**
54     * Get the PackageName of the top running task
55     * @return String corresponding to the package
56     */
57    public String getTopPkgName() {
58        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
59        String topTaskPkg = " ";
60        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
61        if (!tasks.isEmpty()) {
62            return tasks.get(0).topActivity.getPackageName();
63        }
64        return topTaskPkg;
65    }
66
67    /**
68     * API is wrap around ActivityManager class to
69     * get location permissions for a certain UID
70     * @param: Manifest permission string
71     * @param: Uid to get permission for
72     * @return: Permissions setting
73     */
74    public int getUidPermission(String permissionType, int uid) {
75        return ActivityManager.checkUidPermission(permissionType, uid);
76    }
77
78    /**
79     * Gets the local service {link@ DevicePolicyManagerInternal}, can be null
80     */
81    public DevicePolicyManagerInternal getDevicePolicyManagerInternal() {
82        return LocalServices.getService(DevicePolicyManagerInternal.class);
83    }
84}
85