Example usage for android.content Context APP_OPS_SERVICE

List of usage examples for android.content Context APP_OPS_SERVICE

Introduction

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

Prototype

String APP_OPS_SERVICE

To view the source code for android.content Context APP_OPS_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.AppOpsManager for tracking application operations on the device.

Usage

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/*  w w w . ja v  a 2  s .  co m*/
        return true;
}

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  v a2s . c  o  m*/
    return mode == AppOpsManager.MODE_ALLOWED;
}

From source file:org.broeuschmeul.android.gps.usb.provider.driver.USBGpsManager.java

public boolean isMockLocationEnabled() {
    // Checks if mock location is enabled in settings

    boolean isMockLocation;

    try {//  w  w  w.  j av  a  2 s  .c  o  m
        //If marshmallow or higher then we need to check that this app is set as the provider
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            AppOpsManager opsManager = (AppOpsManager) appContext.getSystemService(Context.APP_OPS_SERVICE);
            isMockLocation = opsManager.checkOp(AppOpsManager.OPSTR_MOCK_LOCATION, android.os.Process.myUid(),
                    BuildConfig.APPLICATION_ID) == AppOpsManager.MODE_ALLOWED;

        } else {
            // Anything below it then we just need to check the tickbox is checked.
            isMockLocation = !android.provider.Settings.Secure
                    .getString(appContext.getContentResolver(), "mock_location").equals("0");
        }

    } catch (Exception e) {
        return false;
    }

    return isMockLocation;
}

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

protected boolean checkAccessedUsageStats() {
    try {/*  ww  w. java 2  s . c o  m*/
        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;
    }
}

From source file:com.android.settings.applications.CanBeOnSdCardChecker.java

@Override
public void onClick(DialogInterface dialog, int which) {
    if (mResetDialog == dialog) {
        final PackageManager pm = getActivity().getPackageManager();
        final IPackageManager mIPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
        final INotificationManager nm = INotificationManager.Stub
                .asInterface(ServiceManager.getService(Context.NOTIFICATION_SERVICE));
        final NetworkPolicyManager npm = NetworkPolicyManager.from(getActivity());
        final AppOpsManager aom = (AppOpsManager) getActivity().getSystemService(Context.APP_OPS_SERVICE);
        final Handler handler = new Handler(getActivity().getMainLooper());
        (new AsyncTask<Void, Void, Void>() {
            @Override/*w w  w  .  j  av  a2s  .  co m*/
            protected Void doInBackground(Void... params) {
                List<ApplicationInfo> apps = pm
                        .getInstalledApplications(PackageManager.GET_DISABLED_COMPONENTS);
                for (int i = 0; i < apps.size(); i++) {
                    ApplicationInfo app = apps.get(i);
                    try {
                        if (DEBUG)
                            Log.v(TAG, "Enabling notifications: " + app.packageName);
                        nm.setNotificationsEnabledForPackage(app.packageName, app.uid, true);
                    } catch (android.os.RemoteException ex) {
                    }
                    if (!app.enabled) {
                        if (DEBUG)
                            Log.v(TAG, "Enabling app: " + app.packageName);
                        if (pm.getApplicationEnabledSetting(
                                app.packageName) == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
                            pm.setApplicationEnabledSetting(app.packageName,
                                    PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
                                    PackageManager.DONT_KILL_APP);
                        }
                    }
                }
                try {
                    mIPm.resetPreferredActivities(UserHandle.myUserId());
                } catch (RemoteException e) {
                }
                aom.resetAllModes();
                final int[] restrictedUids = npm.getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND);
                final int currentUserId = ActivityManager.getCurrentUser();
                for (int uid : restrictedUids) {
                    // Only reset for current user
                    if (UserHandle.getUserId(uid) == currentUserId) {
                        if (DEBUG)
                            Log.v(TAG, "Clearing data policy: " + uid);
                        npm.setUidPolicy(uid, POLICY_NONE);
                    }
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (DEBUG)
                            Log.v(TAG, "Done clearing");
                        if (getActivity() != null && mActivityResumed) {
                            if (DEBUG)
                                Log.v(TAG, "Updating UI!");
                            for (int i = 0; i < mTabs.size(); i++) {
                                TabInfo tab = mTabs.get(i);
                                if (tab.mApplications != null) {
                                    tab.mApplications.pause();
                                }
                            }
                            if (mCurTab != null) {
                                mCurTab.resume(mSortOrder);
                            }
                        }
                    }
                });
                return null;
            }
        }).execute();
    }
}

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

@Override
public int mkdirs(String callingPkg, String appPath) {
    final int userId = UserHandle.getUserId(Binder.getCallingUid());
    final UserEnvironment userEnv = new UserEnvironment(userId);

    // Validate that reported package name belongs to caller
    final AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
    appOps.checkPackage(Binder.getCallingUid(), callingPkg);

    try {/*from ww w  . j  a  va2s  . c o  m*/
        appPath = new File(appPath).getCanonicalPath();
    } catch (IOException e) {
        Slog.e(TAG, "Failed to resolve " + appPath + ": " + e);
        return -1;
    }

    if (!appPath.endsWith("/")) {
        appPath = appPath + "/";
    }

    // Try translating the app path into a vold path, but require that it
    // belong to the calling package.
    String voldPath = maybeTranslatePathForVold(appPath, userEnv.buildExternalStorageAppDataDirs(callingPkg),
            userEnv.buildExternalStorageAppDataDirsForVold(callingPkg));
    if (voldPath != null) {
        try {
            mConnector.execute("volume", "mkdirs", voldPath);
            return 0;
        } catch (NativeDaemonConnectorException e) {
            return e.getCode();
        }
    }

    voldPath = maybeTranslatePathForVold(appPath, userEnv.buildExternalStorageAppObbDirs(callingPkg),
            userEnv.buildExternalStorageAppObbDirsForVold(callingPkg));
    if (voldPath != null) {
        try {
            mConnector.execute("volume", "mkdirs", voldPath);
            return 0;
        } catch (NativeDaemonConnectorException e) {
            return e.getCode();
        }
    }

    voldPath = maybeTranslatePathForVold(appPath, userEnv.buildExternalStorageAppMediaDirs(callingPkg),
            userEnv.buildExternalStorageAppMediaDirsForVold(callingPkg));
    if (voldPath != null) {
        try {
            mConnector.execute("volume", "mkdirs", voldPath);
            return 0;
        } catch (NativeDaemonConnectorException e) {
            return e.getCode();
        }
    }

    throw new SecurityException("Invalid mkdirs path: " + appPath);
}