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