Example usage for android.os UserHandle getUserId

List of usage examples for android.os UserHandle getUserId

Introduction

In this page you can find the example usage for android.os UserHandle getUserId.

Prototype

@UnsupportedAppUsage
public static @UserIdInt int getUserId(int uid) 

Source Link

Document

Returns the user id for a given uid.

Usage

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/*from  w  ww. ja v a2 s.  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

private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
    if (callerUid == android.os.Process.SYSTEM_UID) {
        return true;
    }//  w  w w .j av a  2  s. c o m

    if (packageName == null) {
        return false;
    }

    final int packageUid = mPms.getPackageUid(packageName, UserHandle.getUserId(callerUid));

    if (DEBUG_OBB) {
        Slog.d(TAG,
                "packageName = " + packageName + ", packageUid = " + packageUid + ", callerUid = " + callerUid);
    }

    return callerUid == packageUid;
}

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 w  w w .  j  ava  2  s .  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);
}