Java Code Examples for android.content.Context#checkCallingOrSelfPermission()

The following examples show how to use android.content.Context#checkCallingOrSelfPermission() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PushService.java    From java-unified-sdk with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
private static boolean isConnected(Context context) {
  try {
    int hasPermission = context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);
    if (PackageManager.PERMISSION_GRANTED != hasPermission) {
      Log.w(TAG,"android.Manifest.permission.ACCESS_NETWORK_STATE is not granted.");
    } else {
      ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
      if (null != networkInfo && networkInfo.isConnected()) {
        return true;
      }
    }
  } catch (Exception ex) {
    Log.w(TAG,"failed to detect networking status.", ex);
  }
  return false;
}
 
Example 2
Source File: BaiduOauthImplicitGrant.java    From dcs-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化accesTokenManager等信息
 *
 * @param context 当前执行的上下文环境
 */
public void init(Context context) {
    if (context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE)
            != PackageManager.PERMISSION_GRANTED) {
        Log.w(LOG_TAG, "App miss permission android.permission.ACCESS_NETWORK_STATE! "
                + "Some mobile's WebView don't display page!");
    }
    this.accessTokenManager = new AccessTokenManager(context);
    this.accessTokenManager.initToken();
}
 
Example 3
Source File: NetworkStatsAccess.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/** Returns the {@link NetworkStatsAccess.Level} for the given caller. */
public static @NetworkStatsAccess.Level int checkAccessLevel(
        Context context, int callingUid, String callingPackage) {
    final DevicePolicyManagerInternal dpmi = LocalServices.getService(
            DevicePolicyManagerInternal.class);
    final TelephonyManager tm = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    boolean hasCarrierPrivileges = tm != null &&
            tm.checkCarrierPrivilegesForPackage(callingPackage) ==
                    TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
    boolean isDeviceOwner = dpmi != null && dpmi.isActiveAdminWithPolicy(callingUid,
            DeviceAdminInfo.USES_POLICY_DEVICE_OWNER);
    if (hasCarrierPrivileges || isDeviceOwner
            || UserHandle.getAppId(callingUid) == android.os.Process.SYSTEM_UID) {
        // Carrier-privileged apps and device owners, and the system can access data usage for
        // all apps on the device.
        return NetworkStatsAccess.Level.DEVICE;
    }

    boolean hasAppOpsPermission = hasAppOpsPermission(context, callingUid, callingPackage);
    if (hasAppOpsPermission || context.checkCallingOrSelfPermission(
            READ_NETWORK_USAGE_HISTORY) == PackageManager.PERMISSION_GRANTED) {
        return NetworkStatsAccess.Level.DEVICESUMMARY;
    }

    boolean isProfileOwner = dpmi != null && dpmi.isActiveAdminWithPolicy(callingUid,
            DeviceAdminInfo.USES_POLICY_PROFILE_OWNER);
    if (isProfileOwner) {
        // Apps with the AppOps permission, profile owners, and apps with the privileged
        // permission can access data usage for all apps in this user/profile.
        return NetworkStatsAccess.Level.USER;
    }

    // Everyone else gets default access (only to their own UID).
    return NetworkStatsAccess.Level.DEFAULT;
}
 
Example 4
Source File: SKUtilities.java    From SensingKit-Android with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean checkPermission(Context context, String permission) throws SKException {

        if (context == null) {
            throw new SKException(TAG, "Context cannot be null.", SKExceptionErrorCode.UNKNOWN_ERROR);
        }

        int res = context.checkCallingOrSelfPermission(permission);
        return (res == PackageManager.PERMISSION_GRANTED);
    }
 
Example 5
Source File: Galgo.java    From RxAndroidBootstrap with Apache License 2.0 5 votes vote down vote up
private static void checkPermission(Context context) {
    String permission = "android.permission.SYSTEM_ALERT_WINDOW";
    int status = context.checkCallingOrSelfPermission(permission);
    if (status == PackageManager.PERMISSION_DENIED) {
        throw new IllegalStateException("in order to use Galgo, " +
                "please add the permission " + permission + " to your AndroidManifest.xml");
    }
}
 
Example 6
Source File: LocationManagerImplementation.java    From Leanplum-Android-SDK with Apache License 2.0 5 votes vote down vote up
private boolean isPermissionGranted() {
  Context context = Leanplum.getContext();
  try {
    return context.checkCallingOrSelfPermission(PERMISSION) == PackageManager.PERMISSION_GRANTED;
  } catch (RuntimeException ignored) {
    return false;
  }
}
 
Example 7
Source File: Utils.java    From discreet-app-rate with Apache License 2.0 5 votes vote down vote up
public static boolean isOnline(Context context) {
    int res = context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);
    if (res == PackageManager.PERMISSION_GRANTED) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

    return true;
}
 
Example 8
Source File: NetworkHelper.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
public static boolean hasInternetPermission(Context context)
{
    return context == null || context.checkCallingOrSelfPermission("android.permission.INTERNET") == 0;
}
 
Example 9
Source File: Utils.java    From ForceDoze with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isSecureSettingsPermissionGranted(Context context) {
    if (context.checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) == PackageManager.PERMISSION_GRANTED)
        return true;
    else return false;
}
 
Example 10
Source File: Utils.java    From find-client-android with MIT License 4 votes vote down vote up
public static boolean hasAnyLocationPermission(Context context) {
    int fineLocationPermission = context.checkCallingOrSelfPermission("android.permission.ACCESS_FINE_LOCATION");
    int coarseLocationPermission = context.checkCallingOrSelfPermission("android.permission.ACCESS_COARSE_LOCATION");
    return fineLocationPermission == 0 || coarseLocationPermission == 0;
}
 
Example 11
Source File: StorageUtils.java    From Learning-Resources with MIT License 4 votes vote down vote up
private static boolean hasExternalStoragePermission(Context context) {
    int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
    return perm == PackageManager.PERMISSION_GRANTED;
}
 
Example 12
Source File: DozeManager.java    From always-on-amoled with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isDevicePowerPermissionGranted(Context context) {
    return context.checkCallingOrSelfPermission("android.permission.DEVICE_POWER") == PackageManager.PERMISSION_GRANTED;
}
 
Example 13
Source File: FileUtils.java    From monolog-android with MIT License 4 votes vote down vote up
private static boolean hasExternalStoragePermission(Context context) {
    int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
    return perm == PackageManager.PERMISSION_GRANTED;
}
 
Example 14
Source File: StorageUtils.java    From candybar with Apache License 2.0 4 votes vote down vote up
private static boolean hasExternalStoragePermission(Context context) {
    int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
    return perm == PackageManager.PERMISSION_GRANTED;
}
 
Example 15
Source File: Utils.java    From find-client-android with MIT License 4 votes vote down vote up
public static boolean hasAnyLocationPermission(Context context) {
    int fineLocationPermission = context.checkCallingOrSelfPermission("android.permission.ACCESS_FINE_LOCATION");
    int coarseLocationPermission = context.checkCallingOrSelfPermission("android.permission.ACCESS_COARSE_LOCATION");
    return fineLocationPermission == 0 || coarseLocationPermission == 0;
}
 
Example 16
Source File: StorageUtils.java    From Qshp with MIT License 4 votes vote down vote up
private static boolean hasExternalStoragePermission(Context context) {
    int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
    return perm == PackageManager.PERMISSION_GRANTED;
}
 
Example 17
Source File: MarvelTestRunner.java    From marvel with MIT License 4 votes vote down vote up
void disableAnimations(Context context) {
    int permStatus = context.checkCallingOrSelfPermission(android.Manifest.permission.SET_ANIMATION_SCALE);
    if (permStatus == PackageManager.PERMISSION_GRANTED) {
        setSystemAnimationsScale(0.0f);
    }
}
 
Example 18
Source File: Elephant.java    From Elephant with Apache License 2.0 4 votes vote down vote up
public static boolean hasExternalStoragePermission(Context context) {
    int perm = context.checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE");
    return perm == 0;
}
 
Example 19
Source File: StorageUtils.java    From letv with Apache License 2.0 4 votes vote down vote up
private static boolean hasExternalStoragePermission(Context context) {
    return context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION) == 0;
}
 
Example 20
Source File: Utils.java    From lantern with Apache License 2.0 2 votes vote down vote up
/**
 * Check for camera permission boolean.
 *
 * @param context the context
 * @return the boolean
 */
boolean checkForCameraPermission(Context context) {
    return context.checkCallingOrSelfPermission(Manifest.permission.CAMERA)
            == PackageManager.PERMISSION_GRANTED;
}