192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao/*
292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * Copyright (C) 2016 The Android Open Source Project
392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao *
492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * Licensed under the Apache License, Version 2.0 (the "License");
592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * you may not use this file except in compliance with the License.
692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * You may obtain a copy of the License at
792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao *
892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao *      http://www.apache.org/licenses/LICENSE-2.0
992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao *
1092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * Unless required by applicable law or agreed to in writing, software
1192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * distributed under the License is distributed on an "AS IS" BASIS,
1292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * See the License for the specific language governing permissions and
1492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * limitations under the License.
1592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao */
1692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
1792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raopackage com.android.server.wifi.util;
1892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
1992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.app.ActivityManager;
20835e781643b79c30fabbab5595770cf01b5861fbRebecca Silbersteinimport android.app.AppGlobals;
210d14dbc1da819e72054b9f168c8e3db767dd34bfGlen Kuhneimport android.app.admin.DevicePolicyManagerInternal;
2292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.content.Context;
23835e781643b79c30fabbab5595770cf01b5861fbRebecca Silbersteinimport android.os.RemoteException;
2492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.os.UserHandle;
2592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
260d14dbc1da819e72054b9f168c8e3db767dd34bfGlen Kuhneimport com.android.server.LocalServices;
270d14dbc1da819e72054b9f168c8e3db767dd34bfGlen Kuhne
2892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport java.util.List;
2992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
3092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao/**
3192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * A wifi permissions dependency class to wrap around external
3292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * calls to static methods that enable testing.
3392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao */
3492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raopublic class WifiPermissionsWrapper {
3592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private static final String TAG = "WifiPermissionsWrapper";
3692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private final Context mContext;
3792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
3892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public WifiPermissionsWrapper(Context context) {
3992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mContext = context;
4092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
4192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
4292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public int getCurrentUser() {
4392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        return ActivityManager.getCurrentUser();
4492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
4592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
4692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
4792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Returns the user ID corresponding to the UID
4892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * @param uid Calling Uid
4992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * @return userid Corresponding user id
5092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
5192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public int getCallingUserId(int uid) {
5292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        return UserHandle.getUserId(uid);
5392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
5492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
5592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
5692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Get the PackageName of the top running task
5792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * @return String corresponding to the package
5892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
5992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public String getTopPkgName() {
6092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
6192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        String topTaskPkg = " ";
6292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
6392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        if (!tasks.isEmpty()) {
6492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            return tasks.get(0).topActivity.getPackageName();
6592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        }
6692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        return topTaskPkg;
6792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
6892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
6992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
7092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * API is wrap around ActivityManager class to
7192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * get location permissions for a certain UID
7292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * @param: Manifest permission string
7392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * @param: Uid to get permission for
7492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * @return: Permissions setting
7592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
7692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public int getUidPermission(String permissionType, int uid) {
7792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        return ActivityManager.checkUidPermission(permissionType, uid);
7892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
790d14dbc1da819e72054b9f168c8e3db767dd34bfGlen Kuhne
800d14dbc1da819e72054b9f168c8e3db767dd34bfGlen Kuhne    /**
810d14dbc1da819e72054b9f168c8e3db767dd34bfGlen Kuhne     * Gets the local service {link@ DevicePolicyManagerInternal}, can be null
820d14dbc1da819e72054b9f168c8e3db767dd34bfGlen Kuhne     */
830d14dbc1da819e72054b9f168c8e3db767dd34bfGlen Kuhne    public DevicePolicyManagerInternal getDevicePolicyManagerInternal() {
840d14dbc1da819e72054b9f168c8e3db767dd34bfGlen Kuhne        return LocalServices.getService(DevicePolicyManagerInternal.class);
850d14dbc1da819e72054b9f168c8e3db767dd34bfGlen Kuhne    }
86835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein
87835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    /**
88835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     * Determines if the caller has the override wifi config permission.
89835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     *
90835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     * @param uid to check the permission for
91835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     * @return int representation of success or denied
92835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     * @throws RemoteException
93835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     */
94835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    public int getOverrideWifiConfigPermission(int uid) throws RemoteException {
95835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        return AppGlobals.getPackageManager().checkUidPermission(
96835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein                android.Manifest.permission.OVERRIDE_WIFI_CONFIG, uid);
97835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    }
9892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao}
99