所有这些ID(uid,pid,userid,appid,sharedUserid)的关系和功能是什么?

时间:2012-10-18 12:07:59

标签: android android-intent

我知道pid(int)在创建时由os系统分配给进程,这是进程的unqiue标识符。 uid(int)是用户(或android中的应用程序)的unqiue标识符。

  • Q1:什么时候创建了一个uid并将其分配给应用程序?
  • Q2:它与AndroidManifest.xml中的属性或其他属性有关系吗? 我知道shareUerId通常是系统userId,例如。显然,sharedUserId是一个字符串。
  • 问题3:系统uid(int)和shareUserId(字符串)之间的映射在哪里? 在frameworks \ base \ core \ java \ android \ os中有一个UserId.java。你可以看到,方法getUid(int userId,int appId)可以从userId和appId获取一个uid。
  • 问题4:这里的userId和appId是什么?

    public static final int getCallingUserId(){         return getUserId(Binder.getCallingUid());     } 从上面的代码中,我可以得出结论,当IPC Binder使用uid而不是UserId时.If,UserId的功能是什么。

这里有很多问题!我认为所有这些对于理解android的IPC都很重要。任何人都可以回答我的问题吗?最好逐一列出所有答案。非常感谢提前!

公共最终类UserId {

     /**
     * Range of IDs allocated for a user.
     *
     * @hide
     */
    public static final int PER_USER_RANGE = 100000;

    public static final int USER_ALL = -1;

    /**
     * Enable multi-user related side effects. Set this to false if there are problems with single
     * user usecases.
     * */
    public static final boolean MU_ENABLED = true;

    /**
     * Checks to see if the user id is the same for the two uids, i.e., they belong to the same
     * user.
     * @hide
     */
    public static final boolean isSameUser(int uid1, int uid2) {
        return getUserId(uid1) == getUserId(uid2);
    }

    /**
     * Checks to see if both uids are referring to the same app id, ignoring the user id part of the
     * uids.
     * @param uid1 uid to compare
     * @param uid2 other uid to compare
     * @return whether the appId is the same for both uids
     * @hide
     */
    public static final boolean isSameApp(int uid1, int uid2) {
        return getAppId(uid1) == getAppId(uid2);
    }

    public static final boolean isIsolated(int uid) {
        uid = getAppId(uid);
        return uid >= Process.FIRST_ISOLATED_UID && uid <= Process.LAST_ISOLATED_UID;
    }

    public static boolean isApp(int uid) {
        if (uid > 0) {
            uid = UserId.getAppId(uid);
            return uid >= Process.FIRST_APPLICATION_UID && uid <= Process.LAST_APPLICATION_UID;
        } else {
            return false;
        }
    }

    /**
     * Returns the user id for a given uid.
     * @hide
     */
    public static final int getUserId(int uid) {
        if (MU_ENABLED) {
            return uid / PER_USER_RANGE;
        } else {
            return 0;
        }
    }

    public static final int getCallingUserId() {
        return getUserId(Binder.getCallingUid());
    }

    /**
     * Returns the uid that is composed from the userId and the appId.
     * @hide
     */
    public static final int getUid(int userId, int appId) {
        if (MU_ENABLED) {
            return userId * PER_USER_RANGE + (appId % PER_USER_RANGE);
        } else {
            return appId;
        }
    }

    /**
     * Returns the app id (or base uid) for a given uid, stripping out the user id from it.
     * @hide
     */
    public static final int getAppId(int uid) {
        return uid % PER_USER_RANGE;
    }

    /**
     * Returns the user id of the current process
     * @return user id of the current process
     */
    public static final int myUserId() {
        return getUserId(Process.myUid());
    }

0 个答案:

没有答案