Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 93932a7a authored by Ben Kwa's avatar Ben Kwa Committed by Android Git Automerger
Browse files

am c783a5e7: Merge "Enable apps to exclude their own roots from the...

am c783a5e7: Merge "Enable apps to exclude their own roots from the DocumentsUI roots list." into mnc-dev

* commit 'c783a5e762b0c4925756b8ca273eb26aab5e7985':
  Enable apps to exclude their own roots from the DocumentsUI roots list.
parents 0021e6da 408a7e2b
Loading
Loading
Loading
Loading
+41 −2
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executor;


@@ -32,6 +33,10 @@ import libcore.io.IoUtils;
import android.app.Activity;
import android.app.Activity;
import android.app.Fragment;
import android.app.Fragment;
import android.content.Intent;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.database.Cursor;
import android.database.Cursor;
import android.net.Uri;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.AsyncTask;
@@ -232,9 +237,38 @@ abstract class BaseActivity extends Activity {
        invalidateOptionsMenu();
        invalidateOptionsMenu();
    }
    }


    final List<String> getExcludedAuthorities() {
        List<String> authorities = new ArrayList<>();
        if (getIntent().getBooleanExtra(DocumentsContract.EXTRA_EXCLUDE_SELF, false)) {
            // Exclude roots provided by the calling package.
            String packageName = getCallingPackageMaybeExtra();
            try {
                PackageInfo pkgInfo = getPackageManager().getPackageInfo(packageName,
                        PackageManager.GET_PROVIDERS);
                for (ProviderInfo provider: pkgInfo.providers) {
                    authorities.add(provider.authority);
                }
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(mTag, "Calling package name does not resolve: " + packageName);
            }
        }
        return authorities;
    }

    final String getCallingPackageMaybeExtra() {
    final String getCallingPackageMaybeExtra() {
        String callingPackage = getCallingPackage();
        // System apps can set the calling package name using an extra.
        try {
            ApplicationInfo info = getPackageManager().getApplicationInfo(callingPackage, 0);
            if (info.isSystemApp() || info.isUpdatedSystemApp()) {
                final String extra = getIntent().getStringExtra(DocumentsContract.EXTRA_PACKAGE_NAME);
                final String extra = getIntent().getStringExtra(DocumentsContract.EXTRA_PACKAGE_NAME);
        return (extra != null) ? extra : getCallingPackage();
                if (extra != null) {
                    callingPackage = extra;
                }
            }
        } finally {
            return callingPackage;
        }
    }
    }


    public static BaseActivity get(Fragment fragment) {
    public static BaseActivity get(Fragment fragment) {
@@ -287,6 +321,9 @@ abstract class BaseActivity extends Activity {
        /** Currently copying file */
        /** Currently copying file */
        public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<DocumentInfo>();
        public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<DocumentInfo>();


        /** Name of the package that started DocsUI */
        public List<String> excludedAuthorities = new ArrayList<>();

        public static final int ACTION_OPEN = 1;
        public static final int ACTION_OPEN = 1;
        public static final int ACTION_CREATE = 2;
        public static final int ACTION_CREATE = 2;
        public static final int ACTION_GET_CONTENT = 3;
        public static final int ACTION_GET_CONTENT = 3;
@@ -327,6 +364,7 @@ abstract class BaseActivity extends Activity {
            out.writeString(currentSearch);
            out.writeString(currentSearch);
            out.writeMap(dirState);
            out.writeMap(dirState);
            out.writeList(selectedDocumentsForCopy);
            out.writeList(selectedDocumentsForCopy);
            out.writeList(excludedAuthorities);
        }
        }


        public static final Creator<State> CREATOR = new Creator<State>() {
        public static final Creator<State> CREATOR = new Creator<State>() {
@@ -348,6 +386,7 @@ abstract class BaseActivity extends Activity {
                state.currentSearch = in.readString();
                state.currentSearch = in.readString();
                in.readMap(state.dirState, null);
                in.readMap(state.dirState, null);
                in.readList(state.selectedDocumentsForCopy, null);
                in.readList(state.selectedDocumentsForCopy, null);
                in.readList(state.excludedAuthorities, null);
                return state;
                return state;
            }
            }


+2 −0
Original line number Original line Diff line number Diff line
@@ -256,6 +256,8 @@ public class DocumentsActivity extends BaseActivity {
                    BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, false);
                    BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, false);
        }
        }


        state.excludedAuthorities = getExcludedAuthorities();

        return state;
        return state;
    }
    }


+6 −0
Original line number Original line Diff line number Diff line
@@ -383,6 +383,12 @@ public class RootsCache {
                continue;
                continue;
            }
            }


            // Exclude roots from the calling package.
            if (state.excludedAuthorities.contains(root.authority)) {
                if (LOGD) Log.d(TAG, "Excluding root " + root.authority + " from calling package.");
                continue;
            }

            matching.add(root);
            matching.add(root);
        }
        }
        return matching;
        return matching;
+0 −2
Original line number Original line Diff line number Diff line
@@ -55,7 +55,6 @@ public class RootInfo implements Durable, Parcelable {
    public String mimeTypes;
    public String mimeTypes;


    /** Derived fields that aren't persisted */
    /** Derived fields that aren't persisted */
    public String derivedPackageName;
    public String[] derivedMimeTypes;
    public String[] derivedMimeTypes;
    public int derivedIcon;
    public int derivedIcon;


@@ -75,7 +74,6 @@ public class RootInfo implements Durable, Parcelable {
        availableBytes = -1;
        availableBytes = -1;
        mimeTypes = null;
        mimeTypes = null;


        derivedPackageName = null;
        derivedMimeTypes = null;
        derivedMimeTypes = null;
        derivedIcon = 0;
        derivedIcon = 0;
    }
    }
+25 −0
Original line number Original line Diff line number Diff line
@@ -114,6 +114,31 @@ public class RootsCacheTest extends AndroidTestCase {
                RootsCache.getMatchingRoots(mRoots, mState));
                RootsCache.getMatchingRoots(mRoots, mState));
    }
    }


    public void testExcludedAuthorities() throws Exception {
        final List<RootInfo> roots = Lists.newArrayList();

        // Set up some roots
        for (int i = 0; i < 5; ++i) {
            RootInfo root = new RootInfo();
            root.authority = "authority" + i;
            roots.add(root);
        }
        // Make some allowed authorities
        List<RootInfo> allowedRoots = Lists.newArrayList(
            roots.get(0), roots.get(2), roots.get(4));
        // Set up the excluded authority list
        for (RootInfo root: roots) {
            if (!allowedRoots.contains(root)) {
                mState.excludedAuthorities.add(root.authority);
            }
        }
        mState.acceptMimes = new String[] { "*/*" };

        assertContainsExactly(
            allowedRoots,
            RootsCache.getMatchingRoots(roots, mState));
    }

    private static void assertContainsExactly(List<?> expected, List<?> actual) {
    private static void assertContainsExactly(List<?> expected, List<?> actual) {
        assertEquals(expected.size(), actual.size());
        assertEquals(expected.size(), actual.size());
        for (Object o : expected) {
        for (Object o : expected) {