Support persistable Uri permission grants.

When granting a Uri permission with new PERSIST_GRANT_URI_PERMISSION
flag, persist that grant across device reboots until explicitly
revoked.  Adds new persistedModeFlags dimension to UriPermission,
and moves all flag mutation into UriPermission for clarity.  Adds
flag documentation.  Only inflate HashSet as needed.

Write persisted grants into XML file, saving based on source and
target package name and user handle.  Sanity check grants when
parsing.

Wipe all grants from/to a package when uninstalled, and wipe any
transient grants when a package or user is force stopped.

Persistable grants are always considered "needed."

Change-Id: I3f001571b498fd607456a1257a6383f904d19497
diff --git a/services/java/com/android/server/am/UriPermissionOwner.java b/services/java/com/android/server/am/UriPermissionOwner.java
index 68a2e0f..90ac88d 100644
--- a/services/java/com/android/server/am/UriPermissionOwner.java
+++ b/services/java/com/android/server/am/UriPermissionOwner.java
@@ -67,24 +67,16 @@
         if ((mode&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0
                 && readUriPermissions != null) {
             for (UriPermission perm : readUriPermissions) {
-                perm.readOwners.remove(this);
-                if (perm.readOwners.size() == 0 && (perm.globalModeFlags
-                        &Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0) {
-                    perm.modeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
-                    service.removeUriPermissionIfNeededLocked(perm);
-                }
+                perm.removeReadOwner(this);
+                service.removeUriPermissionIfNeededLocked(perm);
             }
             readUriPermissions = null;
         }
         if ((mode&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0
                 && writeUriPermissions != null) {
             for (UriPermission perm : writeUriPermissions) {
-                perm.writeOwners.remove(this);
-                if (perm.writeOwners.size() == 0 && (perm.globalModeFlags
-                        &Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0) {
-                    perm.modeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
-                    service.removeUriPermissionIfNeededLocked(perm);
-                }
+                perm.removeWriteOwner(this);
+                service.removeUriPermissionIfNeededLocked(perm);
             }
             writeUriPermissions = null;
         }
@@ -97,12 +89,8 @@
             while (it.hasNext()) {
                 UriPermission perm = it.next();
                 if (uri.equals(perm.uri)) {
-                    perm.readOwners.remove(this);
-                    if (perm.readOwners.size() == 0 && (perm.globalModeFlags
-                            &Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0) {
-                        perm.modeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
-                        service.removeUriPermissionIfNeededLocked(perm);
-                    }
+                    perm.removeReadOwner(this);
+                    service.removeUriPermissionIfNeededLocked(perm);
                     it.remove();
                 }
             }
@@ -116,12 +104,8 @@
             while (it.hasNext()) {
                 UriPermission perm = it.next();
                 if (uri.equals(perm.uri)) {
-                    perm.writeOwners.remove(this);
-                    if (perm.writeOwners.size() == 0 && (perm.globalModeFlags
-                            &Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0) {
-                        perm.modeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
-                        service.removeUriPermissionIfNeededLocked(perm);
-                    }
+                    perm.removeWriteOwner(this);
+                    service.removeUriPermissionIfNeededLocked(perm);
                     it.remove();
                 }
             }