Example usage for android.os Binder getCallingUid

List of usage examples for android.os Binder getCallingUid

Introduction

In this page you can find the example usage for android.os Binder getCallingUid.

Prototype

public static final native int getCallingUid();

Source Link

Document

Return the Linux uid assigned to the process that sent you the current transaction that is being processed.

Usage

From source file:Main.java

/**
 * Gets the calling package names for the current transaction.
 * @param context The context to use for accessing the package manager.
 * @return The calling package names./*from   www.  j  av a2  s . c o m*/
 */
private static String[] getCallingPackages(Context context) {
    int callingUid = Binder.getCallingUid();
    PackageManager pm = context.getApplicationContext().getPackageManager();
    return pm.getPackagesForUid(callingUid);
}

From source file:Main.java

@SuppressWarnings("IncompatibleBitwiseMaskOperation")
public static boolean isFloatWindowOpAllowed(Context context) {
    if (Build.VERSION.SDK_INT >= 19) { // 19, 4.4, KITKAT
        final AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        final int mode = manager.checkOp(AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOW, Binder.getCallingUid(),
                context.getPackageName());
        return AppOpsManager.MODE_ALLOWED == mode;
    } else {//  w  w w  . j a va  2  s.c  om
        return (context.getApplicationInfo().flags & 1 << 27) == 1;
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean checkOp(Context context, int op) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 19) {
        AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        try {//from   w ww.j  a  v a 2 s.  co  m
            Class clazz = AppOpsManager.class;
            Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
            return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, op, Binder.getCallingUid(),
                    context.getPackageName());
        } catch (Exception e) {
            Log.e(TAG, Log.getStackTraceString(e));
        }
    } else {
        Log.e("", "Below API 19 cannot invoke!");
    }
    return false;
}

From source file:Main.java

public static boolean check(Context context, String... premissions) {
    try {//www .  ja  v  a  2s  .  co  m
        if (null == context)
            throw new RuntimeException("Context is null.");
        for (int i = 0; i < premissions.length; i++) {
            Integer check = context.checkPermission(premissions[i], Binder.getCallingPid(),
                    Binder.getCallingUid());
            if (check == -1) {
                return false;
            }
        }
        return true;
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
        return false;
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean isAllowed(Context context, int op) {
    Log.d(TAG, "api level: " + Build.VERSION.SDK_INT);
    if (Build.VERSION.SDK_INT < 19) {
        return true;
    }//from w  ww. j  a v a  2s. c  o m
    Log.d(TAG, "op is " + op);
    String packageName = context.getApplicationContext().getPackageName();
    AppOpsManager aom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    Class<?>[] types = new Class[] { int.class, int.class, String.class };
    Object[] args = new Object[] { op, Binder.getCallingUid(), packageName };
    try {
        Method method = aom.getClass().getDeclaredMethod("checkOpNoThrow", types);
        Object mode = method.invoke(aom, args);
        Log.d(TAG, "invoke checkOpNoThrow: " + mode);
        if ((mode instanceof Integer) && ((Integer) mode == AppOpsManager.MODE_ALLOWED)) {
            Log.d(TAG, "allowed");
            return true;
        }
    } catch (Exception e) {
        Log.e(TAG, "invoke error: " + e);
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean checkOp(Context context, int op) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 19) {
        AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        try {//ww w  .j  av a  2s.  c om
            Class clazz = AppOpsManager.class;
            Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
            return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, op, Binder.getCallingUid(),
                    context.getPackageName());
        } catch (Exception e) {
            Log.e(TAG, Log.getStackTraceString(e));
        }
    } else {
        Log.e(TAG, "Below API 19 cannot invoke!");
    }
    return false;
}

From source file:org.mariotaku.twidere.util.PermissionsManager.java

public boolean checkCallingPermission(final String... requiredPermissions) {
    return checkPermission(Binder.getCallingUid(), requiredPermissions);
}

From source file:com.nextcloud.android.sso.InputStreamBinder.java

private boolean isValid(NextcloudRequest request) {
    String callingPackageName = context.getPackageManager().getNameForUid(Binder.getCallingUid());

    SharedPreferences sharedPreferences = context
            .getSharedPreferences(AccountAuthenticator.SSO_SHARED_PREFERENCE, Context.MODE_PRIVATE);
    String hash = sharedPreferences.getString(callingPackageName, "");
    return validateToken(hash, request.getToken());
}

From source file:org.mariotaku.twidere.provider.TwidereDataProvider.java

@Override
public Cursor query(final Uri uri, final String[] projection, final String selection,
        final String[] selectionArgs, final String sortOrder) {
    try {//  ww  w.j a v a 2  s.co m
        final int table_id = getTableId(uri);
        if (table_id == VIRTUAL_TABLE_ID_PERMISSIONS) {
            final Bundle bundle = new Bundle();
            bundle.putInt(INTENT_KEY_PERMISSIONS, mPermissionsManager.getPermissions(Binder.getCallingUid()));
            return new BundleCursor(bundle);
        }
        final String table = getTableNameById(table_id);
        checkReadPermission(table_id, table, projection);
        switch (table_id) {
        case VIRTUAL_TABLE_ID_CONSUMER_KEY_SECRET: {
            final String consumer_key = mPreferences.getString(PREFERENCE_KEY_CONSUMER_KEY,
                    TWITTER_CONSUMER_KEY);
            final String consumer_secret = mPreferences.getString(PREFERENCE_KEY_CONSUMER_SECRET,
                    TWITTER_CONSUMER_SECRET);
            final Bundle bundle = new Bundle();
            bundle.putString(PREFERENCE_KEY_CONSUMER_KEY,
                    isEmpty(consumer_key) ? TWITTER_CONSUMER_KEY : consumer_key);
            bundle.putString(PREFERENCE_KEY_CONSUMER_SECRET,
                    isEmpty(consumer_secret) ? TWITTER_CONSUMER_KEY : consumer_secret);
            return new BundleCursor(bundle);
        }
        case TABLE_ID_DIRECT_MESSAGES_CONVERSATION: {
            final List<String> segments = uri.getPathSegments();
            if (segments.size() != 3)
                return null;
            final String query = Conversation.QueryBuilder.buildByConversationId(projection,
                    Long.parseLong(segments.get(1)), Long.parseLong(segments.get(2)), selection, sortOrder);
            return mDatabase.rawQuery(query, selectionArgs);
        }
        case TABLE_ID_DIRECT_MESSAGES_CONVERSATION_SCREEN_NAME: {
            final List<String> segments = uri.getPathSegments();
            if (segments.size() != 3)
                return null;
            final String query = Conversation.QueryBuilder.buildByScreenName(projection,
                    Long.parseLong(segments.get(1)), segments.get(2), selection, sortOrder);
            return mDatabase.rawQuery(query, selectionArgs);
        }
        case TABLE_ID_DIRECT_MESSAGES: {
            final String query = DirectMessages.QueryBuilder.build(projection, selection, sortOrder);
            return mDatabase.rawQuery(query, selectionArgs);
        }
        case TABLE_ID_DIRECT_MESSAGES_CONVERSATIONS_ENTRY: {
            return mDatabase.rawQuery(ConversationsEntry.QueryBuilder.build(selection), null);
        }
        }
        if (table == null)
            return null;
        return mDatabase.query(table, projection, selection, selectionArgs, null, null, sortOrder);
    } catch (final SQLException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.android.server.MigratorService.java

private String getPackagename() {
    PackageManager pm = mContext.getPackageManager();
    callingUid = Binder.getCallingUid();
    String[] names = pm.getPackagesForUid(callingUid);
    if (names != null)
        return names[0];
    else//from   ww  w  .ja v  a 2  s . c om
        return "FAILED";
}