Example usage for android.content Context checkCallingOrSelfPermission

List of usage examples for android.content Context checkCallingOrSelfPermission

Introduction

In this page you can find the example usage for android.content Context checkCallingOrSelfPermission.

Prototype

@CheckResult(suggest = "#enforceCallingOrSelfPermission(String,String)")
@PackageManager.PermissionResult
public abstract int checkCallingOrSelfPermission(@NonNull String permission);

Source Link

Document

Determine whether the calling process of an IPC or you have been granted a particular permission.

Usage

From source file:Main.java

/**
 * Checks if the application is in the background (i.e behind another application's Activity).
 *
 * @param context {@link Context}/* w  w  w.  ja va2  s.c  o m*/
 * @return true if another application is above this one.
 */
public static boolean isApplicationBroughtToBackground(final Context context) {
    if (context.checkCallingOrSelfPermission(Manifest.permission.GET_TASKS) == PackageManager.PERMISSION_DENIED)
        return false;

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())
                && !topActivity.getPackageName().equals("com.google.android.voicesearch")) {
            return true;
        }
    }

    return false;
}

From source file:Main.java

public static boolean checkPermission(Context context, String permission) {
    return (PackageManager.PERMISSION_GRANTED == context.checkCallingOrSelfPermission(permission));
}

From source file:Main.java

/**
 * @param context/*from  www .  j a v a  2s  . c o  m*/
 * @return true if the device has an internet connection
 */
public final static boolean hasInternetConnection(Context context) {
    if (PackageManager.PERMISSION_GRANTED == context
            .checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE)) {
        final ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            return networkInfo.isConnected();
        }
    }
    return false;
}

From source file:org.iota.wallet.helper.PermissionRequestHelper.java

public static boolean hasReadExternalStoragePermission(Context context) {
    int result = context.checkCallingOrSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
    return result == PackageManager.PERMISSION_GRANTED;
}

From source file:org.iota.wallet.helper.PermissionRequestHelper.java

public static boolean hasCameraPermission(Context context) {
    int result = context.checkCallingOrSelfPermission(Manifest.permission.CAMERA);
    return result == PackageManager.PERMISSION_GRANTED;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context ctx) {
    int networkStatePermission = ctx.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);

    if (networkStatePermission == PackageManager.PERMISSION_GRANTED) {

        ConnectivityManager mConnectivity = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        // Skip if no connection, or background data disabled
        NetworkInfo info = mConnectivity.getActiveNetworkInfo();
        if (info == null) {
            return false;
        }//w  w  w  .j  ava 2 s  .c  o  m
        // Only update if WiFi
        int netType = info.getType();
        // int netSubtype = info.getSubtype();
        if ((netType == ConnectivityManager.TYPE_WIFI) || (netType == ConnectivityManager.TYPE_MOBILE)) {
            return info.isConnected();
        } else {
            return false;
        }
    } else {
        return true;
    }
}

From source file:Main.java

public static boolean checkedPermission(Context context, String permission) {
    boolean rs = false;
    try {// w  ww .java  2 s .co m
        rs = (context.checkCallingOrSelfPermission(
                android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return rs;
}

From source file:Main.java

@SuppressWarnings("MissingPermission")
public static String getBluetoothMAC(Context context) {
    String result = null;//from w w w. j  a v a2s  .  c o  m
    try {
        if (context.checkCallingOrSelfPermission(
                Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED) {
            BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
            result = bta.getAddress();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static boolean hasGetCurrentServingCellPermission(Context context) {
    String as[] = a;/*www . j  av a  2  s .  c o  m*/
    int i = as.length;
    for (int j = 0; j < i; j++) {
        if (context.checkCallingOrSelfPermission(as[j]) != 0) {
            return false;
        }
    }

    return true;
}

From source file:Main.java

/**
 * Returns true if the context has permission to access the network state and
 * access the internet.//from   w  w  w  .j  a  v  a  2  s. co m
 * 
 * @return True if the context has the correct permissions.
 */
public static boolean hasNetworkPermissions(Context context) {

    // get permissions for accessing the internet and network state
    int internetPerm = context.checkCallingOrSelfPermission(Manifest.permission.INTERNET);
    int networkStatePerm = context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);

    // are those permissions allowed
    boolean internetAllowed = internetPerm == PackageManager.PERMISSION_GRANTED;
    boolean networkAllowed = networkStatePerm == PackageManager.PERMISSION_GRANTED;

    // if they are check if we are connected to the network
    return (internetAllowed && networkAllowed);
}