Home | History | Annotate | Download | only in util
      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 
     17 package com.android.server.wifi.util;
     18 
     19 import android.Manifest;
     20 import android.app.ActivityManager;
     21 import android.app.AppGlobals;
     22 import android.app.admin.DevicePolicyManagerInternal;
     23 import android.content.Context;
     24 import android.os.RemoteException;
     25 import android.os.UserHandle;
     26 
     27 import com.android.server.LocalServices;
     28 
     29 import java.util.List;
     30 
     31 /**
     32  * A wifi permissions dependency class to wrap around external
     33  * calls to static methods that enable testing.
     34  */
     35 public 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     /**
    113      * Determines if the caller has the access wifi state permission.
    114      *
    115      * @param uid to check the permission for
    116      * @return int representation of success or denied
    117      * @throws RemoteException
    118      */
    119     public int getAccessWifiStatePermission(int uid) throws RemoteException {
    120         return AppGlobals.getPackageManager().checkUidPermission(
    121                 Manifest.permission.ACCESS_WIFI_STATE, uid);
    122     }
    123 
    124     /**
    125      * Determines if the caller has local mac address permission.
    126      *
    127      * @param uid to check the permission for
    128      * @return int representation of success or denied
    129      * @throws RemoteException
    130      */
    131     public int getLocalMacAddressPermission(int uid) throws RemoteException {
    132         return AppGlobals.getPackageManager().checkUidPermission(
    133                 Manifest.permission.LOCAL_MAC_ADDRESS, uid);
    134     }
    135 }
    136