[automerger skipped] Encode authority part of uri before showing in UI am: 8550c37c18 am: 4bc54545ff am: bc8df147ea am: aa977a205d am: 3ccf26dc87 am: ce6f506a5e am: 88f6ff5d47 -s ours

am skip reason: Merged-In Ib4f5431bd80b7f4c72c4414f98d99eeb7ca900ed with SHA-1 890dc03ed2 is already in history

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/KeyChain/+/18281033

Change-Id: I7890f75d9f965a476e9f5695ba997784b0c0f03b
Signed-off-by: Automerger Merge Worker <[email protected]>
diff --git a/src/com/android/keychain/KeyChainActivity.java b/src/com/android/keychain/KeyChainActivity.java
index 45be472..54e7d3a 100644
--- a/src/com/android/keychain/KeyChainActivity.java
+++ b/src/com/android/keychain/KeyChainActivity.java
@@ -17,6 +17,8 @@
 package com.android.keychain;
 
 import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.app.ActivityManager;
 import android.app.AlertDialog;
 import android.app.PendingIntent;
 import android.app.admin.DevicePolicyEventLogger;
@@ -32,6 +34,7 @@
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.IBinder;
 import android.os.Looper;
 import android.os.RemoteException;
 import android.os.ServiceManager;
@@ -92,8 +95,6 @@
     private int mSenderUid;
     private String mSenderPackageName;
 
-    private PendingIntent mSender;
-
     // beware that some of these KeyStore operations such as saw and
     // get do file I/O in the remote keystore process and while they
     // do not cause StrictMode violations, they logically should not
@@ -133,20 +134,32 @@
         getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
     }
 
-    @Override public void onResume() {
+    /**
+     * Returns the package name which the activity with {@code activityToken} is launched from.
+     */
+    @Nullable
+    private static String getCallingAppPackageName(IBinder activityToken) {
+        String pkg = null;
+        try {
+            pkg = ActivityManager.getService().getLaunchedFromPackage(activityToken);
+        } catch (RemoteException e) {
+            Log.v(TAG, "Could not talk to activity manager.", e);
+        }
+        return pkg;
+    }
+
+    @Override
+    public void onResume() {
         super.onResume();
 
-        mSender = getIntent().getParcelableExtra(KeyChain.EXTRA_SENDER);
-        if (mSender == null) {
-            // if no sender, bail, we need to identify the app to the user securely.
+        final IBinder activityToken = getActivityToken();
+        mSenderPackageName = getCallingAppPackageName(activityToken);
+        if (mSenderPackageName == null) {
+            //if no sender, bail, we need to identify the app to the user securely.
             finish(null);
             return;
         }
         try {
-            // getTargetPackage guarantees that the returned string is
-            // supplied by the system, so that an application can not
-            // spoof its package.
-            mSenderPackageName = mSender.getIntentSender().getTargetPackage();
             mSenderUid = getPackageManager().getPackageInfo(
                     mSenderPackageName, 0).applicationInfo.uid;
         } catch (PackageManager.NameNotFoundException e) {