Fix ephemeral post-install launching

Provide the ephemeral installer with some additional pieces of information:
1) instead of de-referencing the URL a second time, give the installer the
   exact package name
2) instead of relying on ephemeral apps to define verified links, give the
   installer a pending intent to launch when the ephemeral is installed
3) give the installer a pending intent to launch if the installer fails,
   for whatever reason, to install the ephemeral app

Bug: 25119046
Change-Id: I45f50481caee09d5d09451e4b2492e64b0faae82
diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java
index a27d1cb..a958953 100644
--- a/core/java/android/content/Intent.java
+++ b/core/java/android/content/Intent.java
@@ -3687,6 +3687,20 @@
     public static final String EXTRA_INITIAL_INTENTS = "android.intent.extra.INITIAL_INTENTS";
 
     /**
+     * A {@link IntentSender} to start after ephemeral installation success.
+     * @hide
+     */
+    @SystemApi
+    public static final String EXTRA_EPHEMERAL_SUCCESS = "android.intent.extra.EPHEMERAL_SUCCESS";
+
+    /**
+     * A {@link IntentSender} to start after ephemeral installation failure.
+     * @hide
+     */
+    @SystemApi
+    public static final String EXTRA_EPHEMERAL_FAILURE = "android.intent.extra.EPHEMERAL_FAILURE";
+
+    /**
      * A Bundle forming a mapping of potential target package names to different extras Bundles
      * to add to the default intent extras in {@link #EXTRA_INTENT} when used with
      * {@link #ACTION_CHOOSER}. Each key should be a package name. The package need not
diff --git a/core/java/android/content/pm/EphemeralResolveInfo.java b/core/java/android/content/pm/EphemeralResolveInfo.java
new file mode 100644
index 0000000..afb4c30
--- /dev/null
+++ b/core/java/android/content/pm/EphemeralResolveInfo.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.content.pm;
+
+import android.annotation.NonNull;
+import android.annotation.SystemApi;
+import android.content.IntentFilter;
+import android.net.Uri;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Information about an ephemeral application.
+ * @hide
+ */
+@SystemApi
+public final class EphemeralResolveInfo implements Parcelable {
+    /** Algorithm that will be used to generate the domain digest */
+    public static final String SHA_ALGORITHM = "SHA-256";
+
+    /** Full digest of the domain hash */
+    private final byte[] mDigestBytes;
+    /** The first 4 bytes of the domain hash */
+    private final int mDigestPrefix;
+    private final String mPackageName;
+    /** The filters used to match domain */
+    private final List<IntentFilter> mFilters = new ArrayList<IntentFilter>();
+
+    public EphemeralResolveInfo(@NonNull Uri uri, @NonNull String packageName,
+            @NonNull List<IntentFilter> filters) {
+        // validate arguments
+        if (uri == null
+                || packageName == null
+                || filters == null
+                || filters.size() == 0) {
+            throw new IllegalArgumentException();
+        }
+
+        mDigestBytes = generateDigest(uri);
+        mDigestPrefix =
+                mDigestBytes[0] << 24
+                | mDigestBytes[1] << 16
+                | mDigestBytes[2] << 8
+                | mDigestBytes[3] << 0;
+        mFilters.addAll(filters);
+        mPackageName = packageName;
+    }
+
+    EphemeralResolveInfo(Parcel in) {
+        mDigestBytes = in.createByteArray();
+        mDigestPrefix = in.readInt();
+        mPackageName = in.readString();
+        in.readList(mFilters, null /*loader*/);
+    }
+
+    public byte[] getDigestBytes() {
+        return mDigestBytes;
+    }
+
+    public int getDigestPrefix() {
+        return mDigestPrefix;
+    }
+
+    public String getPackageName() {
+        return mPackageName;
+    }
+
+    public List<IntentFilter> getFilters() {
+        return mFilters;
+    }
+
+    private static byte[] generateDigest(Uri uri) {
+        try {
+            final MessageDigest digest = MessageDigest.getInstance(SHA_ALGORITHM);
+            final byte[] hostBytes = uri.getHost().getBytes();
+            return digest.digest(hostBytes);
+        } catch (NoSuchAlgorithmException e) {
+            throw new IllegalStateException("could not find digest algorithm");
+        }
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public void writeToParcel(Parcel out, int flags) {
+        out.writeByteArray(mDigestBytes);
+        out.writeInt(mDigestPrefix);
+        out.writeString(mPackageName);
+        out.writeList(mFilters);
+    }
+
+    public static final Parcelable.Creator<EphemeralResolveInfo> CREATOR
+            = new Parcelable.Creator<EphemeralResolveInfo>() {
+        public EphemeralResolveInfo createFromParcel(Parcel in) {
+            return new EphemeralResolveInfo(in);
+        }
+
+        public EphemeralResolveInfo[] newArray(int size) {
+            return new EphemeralResolveInfo[size];
+        }
+    };
+
+    /** @hide */
+    public static final class EphemeralResolveIntentInfo extends IntentFilter {
+        private final EphemeralResolveInfo mResolveInfo;
+
+        public EphemeralResolveIntentInfo(@NonNull IntentFilter orig,
+                @NonNull EphemeralResolveInfo resolveInfo) {
+            super(orig);
+            this.mResolveInfo = resolveInfo;
+        }
+
+        public EphemeralResolveInfo getEphemeralResolveInfo() {
+            return mResolveInfo;
+        }
+    }
+}
diff --git a/core/java/android/content/pm/ResolveInfo.java b/core/java/android/content/pm/ResolveInfo.java
index a5fb451..d5d3007 100644
--- a/core/java/android/content/pm/ResolveInfo.java
+++ b/core/java/android/content/pm/ResolveInfo.java
@@ -61,6 +61,19 @@
     public ProviderInfo providerInfo;
 
     /**
+     * The ephemeral application that corresponds to this resolution match. This will
+     * only be set in specific circumstances.
+     * @hide
+     */
+    public EphemeralResolveInfo ephemeralResolveInfo;
+
+    /**
+     * A ResolveInfo that points at the ephemeral installer.
+     * @hide
+     */
+    public ResolveInfo ephemeralInstaller;
+
+    /**
      * The IntentFilter that was matched for this ResolveInfo.
      */
     public IntentFilter filter;
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.java b/core/java/com/android/internal/app/EphemeralResolveInfo.java
deleted file mode 100644
index 0e7ef05..0000000
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * Copyright (C) 2015 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.internal.app;
-
-import android.content.IntentFilter;
-import android.net.Uri;
-import android.os.Parcel;
-import android.os.Parcelable;
-
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Information that is returned when resolving ephemeral
- * applications.
- */
-public final class EphemeralResolveInfo implements Parcelable {
-    public static final String SHA_ALGORITHM = "SHA-256";
-    private byte[] mDigestBytes;
-    private int mDigestPrefix;
-    private final List<IntentFilter> mFilters = new ArrayList<IntentFilter>();
-
-    public EphemeralResolveInfo(Uri uri, List<IntentFilter> filters) {
-        generateDigest(uri);
-        mFilters.addAll(filters);
-    }
-
-    private EphemeralResolveInfo(Parcel in) {
-        readFromParcel(in);
-    }
-
-    public byte[] getDigestBytes() {
-        return mDigestBytes;
-    }
-
-    public int getDigestPrefix() {
-        return mDigestPrefix;
-    }
-
-    public List<IntentFilter> getFilters() {
-        return mFilters;
-    }
-
-    private void generateDigest(Uri uri) {
-        try {
-            final MessageDigest digest = MessageDigest.getInstance(SHA_ALGORITHM);
-            final byte[] hostBytes = uri.getHost().getBytes();
-            final byte[] digestBytes = digest.digest(hostBytes);
-            mDigestBytes = digestBytes;
-            mDigestPrefix =
-                    digestBytes[0] << 24
-                    | digestBytes[1] << 16
-                    | digestBytes[2] << 8
-                    | digestBytes[3] << 0;
-        } catch (NoSuchAlgorithmException e) {
-            throw new IllegalStateException("could not find digest algorithm");
-        }
-    }
-
-    @Override
-    public int describeContents() {
-        return 0;
-    }
-
-    @Override
-    public void writeToParcel(Parcel out, int flags) {
-        if (mDigestBytes == null) {
-            out.writeInt(0);
-        } else {
-            out.writeInt(mDigestBytes.length);
-            out.writeByteArray(mDigestBytes);
-        }
-        out.writeInt(mDigestPrefix);
-        out.writeList(mFilters);
-    }
-
-    private void readFromParcel(Parcel in) {
-        int digestBytesSize = in.readInt();
-        if (digestBytesSize > 0) {
-            mDigestBytes = new byte[digestBytesSize];
-            in.readByteArray(mDigestBytes);
-        }
-        mDigestPrefix = in.readInt();
-        in.readList(mFilters, null /*loader*/);
-    }
-
-    public static final Parcelable.Creator<EphemeralResolveInfo> CREATOR
-            = new Parcelable.Creator<EphemeralResolveInfo>() {
-        public EphemeralResolveInfo createFromParcel(Parcel in) {
-            return new EphemeralResolveInfo(in);
-        }
-
-        public EphemeralResolveInfo[] newArray(int size) {
-            return new EphemeralResolveInfo[size];
-        }
-    };
-}
diff --git a/core/java/com/android/internal/app/EphemeralResolverService.java b/core/java/com/android/internal/app/EphemeralResolverService.java
index 65530f2..6ba04a9 100644
--- a/core/java/com/android/internal/app/EphemeralResolverService.java
+++ b/core/java/com/android/internal/app/EphemeralResolverService.java
@@ -16,9 +16,11 @@
 
 package com.android.internal.app;
 
+import android.annotation.SystemApi;
 import android.app.Service;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.EphemeralResolveInfo;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
@@ -33,6 +35,7 @@
  * Base class for implementing the resolver service.
  * @hide
  */
+@SystemApi
 public abstract class EphemeralResolverService extends Service {
     public static final String EXTRA_RESOLVE_INFO = "com.android.internal.app.RESOLVE_INFO";
     public static final String EXTRA_SEQUENCE = "com.android.internal.app.SEQUENCE";