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.content.Context; 22 import android.content.pm.PackageManager; 23 import android.util.Log; 24 25 /** 26 * A wifi permissions dependency class to wrap around external 27 * calls to static methods that enable testing. 28 */ 29 public class WifiPermissionsWrapper { 30 private static final String TAG = "WifiPermissionsWrapper"; 31 private final Context mContext; 32 private boolean mVerboseLoggingEnabled; 33 WifiPermissionsWrapper(Context context)34 public WifiPermissionsWrapper(Context context) { 35 mContext = context; 36 } 37 getCurrentUser()38 public int getCurrentUser() { 39 return ActivityManager.getCurrentUser(); 40 } 41 42 /** 43 * Determine if a UID has a permission. 44 * @param permissionType permission string 45 * @param uid to get permission for 46 * @return Permissions setting 47 */ getUidPermission(String permissionType, int uid)48 public int getUidPermission(String permissionType, int uid) { 49 // We don't care about pid, pass in -1 50 int granted = mContext.checkPermission(permissionType, -1, uid); 51 if (mVerboseLoggingEnabled) { 52 Log.v(TAG, "getUidPermission(" + permissionType + ", " + uid + "): " 53 + (granted == PackageManager.PERMISSION_GRANTED)); 54 } 55 return granted; 56 } 57 58 /** 59 * Determines if the caller has the override wifi config permission. 60 * 61 * @param uid to check the permission for 62 * @return int representation of success or denied 63 */ getOverrideWifiConfigPermission(int uid)64 public int getOverrideWifiConfigPermission(int uid) { 65 return getUidPermission(android.Manifest.permission.OVERRIDE_WIFI_CONFIG, uid); 66 } 67 68 /** 69 * Determines if the caller has local mac address permission. 70 * 71 * @param uid to check the permission for 72 * @return int representation of success or denied 73 */ getLocalMacAddressPermission(int uid)74 public int getLocalMacAddressPermission(int uid) { 75 return getUidPermission(Manifest.permission.LOCAL_MAC_ADDRESS, uid); 76 } 77 78 /** 79 * Sets the verbose logging level. 80 */ enableVerboseLogging(boolean enabled)81 public void enableVerboseLogging(boolean enabled) { 82 mVerboseLoggingEnabled = enabled; 83 } 84 } 85