Dmitry Fink
Dmitry Fink

Reputation: 1062

What is android:sharedUserLabel and what added value does it add on top of android:sharedUserID?

The documentation (http://developer.android.com/guide/topics/manifest/manifest-element.html#uid) only states I can't use raw strings and the API level it was added, but doesn't explain why I would want to use it. If I already set android:sharedUserID to "com.foo.bar" what value should I put in the string referenced by android:sharedUserLabel, and most importantly why!?

Thank you

Upvotes: 14

Views: 2992

Answers (1)

Yury
Yury

Reputation: 20936

As far as I understand from the AOSP actually you can use this label just to display a pretty name to a user (if you have several processes in the same uid). For instance, here is a part of code in the RunningState.java file:

    // If we couldn't get information about the overall
    // process, try to find something about the uid.
    String[] pkgs = pm.getPackagesForUid(mUid);

    // If there is one package with this uid, that is what we want.
    if (pkgs.length == 1) {
        try {
            ApplicationInfo ai = pm.getApplicationInfo(pkgs[0], 0);
            mDisplayLabel = ai.loadLabel(pm);
            mLabel = mDisplayLabel.toString();
            mPackageInfo = ai;
            return;
        } catch (PackageManager.NameNotFoundException e) {
        }
    }

    // If there are multiple, see if one gives us the official name
    // for this uid.
    for (String name : pkgs) {
        try {
            PackageInfo pi = pm.getPackageInfo(name, 0);
            if (pi.sharedUserLabel != 0) {
                CharSequence nm = pm.getText(name,
                        pi.sharedUserLabel, pi.applicationInfo);
                if (nm != null) {
                    mDisplayLabel = nm;
                    mLabel = nm.toString();
                    mPackageInfo = pi.applicationInfo;
                    return;
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
        }
    }

Basically, it does the following things. At first, it tries to get information about the overall process. If it has not find, it tries to get information using UID of the application as a parameter (this is a part of code that I've given here). If there is only one package with this UID the information about the process is got from this package. But if there are several packages (using shareUserId) then it iterates and tries to find official (pretty) name.

As a confirmation to my words I found the following string in MediaProvider:

<!-- Label to show to user for all apps using this UID. -->
<string name="uid_label">Media</string>

Thus, all process that uses android:sharedUserId="android.media" will have name Media.

I do not think that this feature will be used a lot by ordinary developers and is useful for them.

Upvotes: 11

Related Questions