Example usage for android.app AppOpsManager checkOpNoThrow

List of usage examples for android.app AppOpsManager checkOpNoThrow

Introduction

In this page you can find the example usage for android.app AppOpsManager checkOpNoThrow.

Prototype

@UnsupportedAppUsage
public int checkOpNoThrow(int op, int uid, String packageName) 

Source Link

Document

Like #checkOp but instead of throwing a SecurityException it returns #MODE_ERRORED .

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean isAppOpsAllowed(Context context, String appOpsPermission) {
    AppOpsManager aom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    return aom.checkOpNoThrow(appOpsPermission, Process.myUid(),
            context.getPackageName()) == AppOpsManager.MODE_ALLOWED;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean havePermission(Context context) {
    try {/*from   ww w . j av a2 s.c o  m*/
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid,
                applicationInfo.packageName);
        return (mode == AppOpsManager.MODE_ALLOWED);
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

From source file:Main.java

public static boolean isAppUsageEnabled(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return true;
    }//  w  ww  .ja  v a  2 s  .  c om
    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid,
                applicationInfo.packageName);
        return (mode == AppOpsManager.MODE_ALLOWED);
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

From source file:Main.java

public static boolean hasAppOp(Context context, String appOp) {
    final AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    final int mode = appOpsManager.checkOpNoThrow(appOp, Process.myUid(), context.getPackageName());
    return mode == AppOpsManager.MODE_ALLOWED;
}

From source file:nu.yona.app.utils.AppUtils.java

/**
 * Has permission boolean./*w  ww  .  ja  v  a  2s.  c o m*/
 *
 * @param context the context
 * @return false if user has not given permission for package access so far.
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean hasPermission(Context context) {
    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid,
                applicationInfo.packageName);
        return (mode == AppOpsManager.MODE_ALLOWED);
    } catch (PackageManager.NameNotFoundException e) {
        return true;
    }

}

From source file:fabiogentile.powertutor.ui.UMLogger.java

@TargetApi(Build.VERSION_CODES.KITKAT)
boolean hasUsageStatsPermission(Context context) {
    AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(),
            context.getPackageName());/* w  w w  .  j  ava 2s . c  o  m*/
    boolean granted = mode == AppOpsManager.MODE_ALLOWED;
    return granted;
}

From source file:com.tomer.alwayson.SettingsFragment.java

private boolean hasUsageAccess() throws PackageManager.NameNotFoundException {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid,
                applicationInfo.packageName);
        return mode == AppOpsManager.MODE_ALLOWED;
    } else//from   w  w  w  .j  a v  a 2  s.c o  m
        return true;
}

From source file:com.farmerbb.taskbar.MainActivity.java

private void proceedWithAppLaunch(Bundle savedInstanceState) {
    setContentView(R.layout.main);/*  w ww.  j  ava2  s.c o  m*/

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setCustomView(R.layout.switch_layout);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
    }

    theSwitch = (SwitchCompat) findViewById(R.id.the_switch);
    if (theSwitch != null) {
        final SharedPreferences pref = U.getSharedPreferences(this);
        theSwitch.setChecked(pref.getBoolean("taskbar_active", false));

        theSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                if (U.canDrawOverlays(this)) {
                    boolean firstRun = pref.getBoolean("first_run", true);
                    startTaskbarService();

                    if (firstRun && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !U.isSystemApp(this)) {
                        ApplicationInfo applicationInfo = null;
                        try {
                            applicationInfo = getPackageManager().getApplicationInfo(BuildConfig.APPLICATION_ID,
                                    0);
                        } catch (PackageManager.NameNotFoundException e) {
                            /* Gracefully fail */ }

                        if (applicationInfo != null) {
                            AppOpsManager appOpsManager = (AppOpsManager) getSystemService(
                                    Context.APP_OPS_SERVICE);
                            int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
                                    applicationInfo.uid, applicationInfo.packageName);

                            if (mode != AppOpsManager.MODE_ALLOWED) {
                                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                builder.setTitle(R.string.pref_header_recent_apps)
                                        .setMessage(R.string.enable_recent_apps)
                                        .setPositiveButton(R.string.action_ok, (dialog, which) -> {
                                            try {
                                                startActivity(
                                                        new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
                                                U.showToastLong(MainActivity.this,
                                                        R.string.usage_stats_message);
                                            } catch (ActivityNotFoundException e) {
                                                U.showErrorDialog(MainActivity.this, "GET_USAGE_STATS");
                                            }
                                        }).setNegativeButton(R.string.action_cancel, null);

                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    }
                } else {
                    U.showPermissionDialog(MainActivity.this);
                    compoundButton.setChecked(false);
                }
            } else
                stopTaskbarService();
        });
    }

    if (savedInstanceState == null) {
        if (!getIntent().hasExtra("theme_change"))
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, new AboutFragment(), "AboutFragment").commit();
        else
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, new AppearanceFragment(), "AppearanceFragment").commit();
    }

    if (!BuildConfig.APPLICATION_ID.equals(BuildConfig.BASE_APPLICATION_ID) && freeVersionInstalled()) {
        final SharedPreferences pref = U.getSharedPreferences(this);
        if (!pref.getBoolean("dont_show_uninstall_dialog", false)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.settings_imported_successfully).setMessage(R.string.import_dialog_message)
                    .setPositiveButton(R.string.action_uninstall, (dialog, which) -> {
                        pref.edit().putBoolean("uninstall_dialog_shown", true).apply();

                        try {
                            startActivity(new Intent(Intent.ACTION_DELETE,
                                    Uri.parse("package:" + BuildConfig.BASE_APPLICATION_ID)));
                        } catch (ActivityNotFoundException e) {
                            /* Gracefully fail */ }
                    });

            if (pref.getBoolean("uninstall_dialog_shown", false))
                builder.setNegativeButton(R.string.action_dont_show_again, (dialogInterface, i) -> pref.edit()
                        .putBoolean("dont_show_uninstall_dialog", true).apply());

            AlertDialog dialog = builder.create();
            dialog.show();
            dialog.setCancelable(false);
        }

        if (!pref.getBoolean("uninstall_dialog_shown", false)) {
            if (theSwitch != null)
                theSwitch.setChecked(false);

            SharedPreferences.Editor editor = pref.edit();

            String iconPack = pref.getString("icon_pack", BuildConfig.BASE_APPLICATION_ID);
            if (iconPack.contains(BuildConfig.BASE_APPLICATION_ID)) {
                editor.putString("icon_pack", BuildConfig.APPLICATION_ID);
            } else {
                U.refreshPinnedIcons(this);
            }

            editor.putBoolean("first_run", true);
            editor.apply();
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        if (shortcutManager.getDynamicShortcuts().size() == 0) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setClass(this, StartTaskbarActivity.class);
            intent.putExtra("is_launching_shortcut", true);

            ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "start_taskbar")
                    .setShortLabel(getString(R.string.start_taskbar))
                    .setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon_start)).setIntent(intent)
                    .build();

            Intent intent2 = new Intent(Intent.ACTION_MAIN);
            intent2.setClass(this, ShortcutActivity.class);
            intent2.putExtra("is_launching_shortcut", true);

            ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "freeform_mode")
                    .setShortLabel(getString(R.string.pref_header_freeform))
                    .setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon_freeform))
                    .setIntent(intent2).build();

            shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut, shortcut2));
        }
    }
}

From source file:com.hmatalonga.greenhub.ui.TaskListActivity.java

@TargetApi(21)
private boolean hasSpecialPermission(final Context context) {
    AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(),
            context.getPackageName());//  w  ww. j  a  va 2 s  . c o  m
    return mode == AppOpsManager.MODE_ALLOWED;
}

From source file:com.nbplus.vbroadlauncher.HomeLauncherActivity.java

protected boolean checkAccessedUsageStats() {
    try {// w ww.  ja  v a 2s  . c om
        PackageManager packageManager = getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid,
                applicationInfo.packageName);
        return (mode == AppOpsManager.MODE_ALLOWED);

    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}