Search code examples
androidstorage-access-framework

DocumentsUI shows "Anonymous" application when requesting access to directory


One user reported that my app fails to request directory access when selecting a folder via the ACTION_OPEN_DOCUMENT_TREE intent. For some reason it does not show my application, instead "Anonymous":

enter image description here

Translated: "Allow Anonymous to access files in Camera. This will let Anonymous access current and future content stored in Camera".

The user has a MIUI 12 with Android 11 on a Mi Note 10 lite. I have the same just with a Mi Note 10, no issues ofc.

Checked the Android source code:

https://android.googlesource.com/platform/packages/apps/DocumentsUI/+/refs/heads/master/src/com/android/documentsui/picker/ConfirmFragment.java#82

case TYPE_OEPN_TREE:
    final Uri treeUri = mTarget.getTreeDocumentUri();
    final BaseActivity activity = (BaseActivity) getActivity();
    final String target = activity.getCurrentTitle();
    final String text = getString(R.string.open_tree_dialog_title,
            **getCallingAppName**(getActivity()), target);
    message = getString(R.string.open_tree_dialog_message,
            **getCallingAppName**(getActivity()), target);

    builder.setTitle(text);
    builder.setMessage(message);
    builder.setPositiveButton(
            R.string.allow,
            (DialogInterface dialog, int id) -> {
                pickResult.increaseActionCount();
                mActions.finishPicking(treeUri);
            });
    break;


@NonNull
public static String getCallingAppName(Activity activity) {
    final String anonymous = activity.getString(R.string.anonymous_application);
    final String packageName = getCallingPackageName(activity);
    if (TextUtils.isEmpty(packageName)) {
        return anonymous;
    }

    final PackageManager pm = activity.getPackageManager();
    ApplicationInfo ai;
    try {
        ai = pm.getApplicationInfo(packageName, 0);
    } catch (final PackageManager.NameNotFoundException e) {
        return anonymous;
    }

    CharSequence result = pm.getApplicationLabel(ai);
    return TextUtils.isEmpty(result) ? anonymous : result.toString();
}

public static String getCallingPackageName(Activity activity) {
    String callingPackage = activity.getCallingPackage();
    // System apps can set the calling package name using an extra.
    try {
        ApplicationInfo info =
                activity.getPackageManager().getApplicationInfo(callingPackage, 0);
        if (isSystemApp(info) || isUpdatedSystemApp(info)) {
            final String extra = activity.getIntent().getStringExtra(
                    Intent.EXTRA_PACKAGE_NAME);
            if (extra != null && !TextUtils.isEmpty(extra)) {
                callingPackage = extra;
            }
        }
    } catch (NameNotFoundException e) {
        // Couldn't lookup calling package info. This isn't really
        // gonna happen, given that we're getting the name of the
        // calling package from trusty old Activity.getCallingPackage.
        // For that reason, we ignore this exception.
    }
    return callingPackage;
}

...and it seems that for whatever reason my packagename isn't found. How can can happen? Asked him to install one of my other apps, and it happens there as well. Asked him then to install another app from the playstore (FX File Explorer) and there it does not happen. So it is specific to his device and my app.


Solution

  • So it turned out that this user having that issue turned off the MIUI Optimizations in the developer settings.enter image description hereenter image description here