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