Propagate callingPackage explicitly within CDM

Activity.getCallingPackage seems to be returning null in come cases

Fixes: 170461416
Bug: 167592848
Test: manual
Change-Id: I507b9434e792f79e7905f1983879a062d1a6fec7
(cherry picked from commit 2a7a57711e8dbe2ac8705350e42a9d3f26a8edae)
diff --git a/core/java/android/companion/AssociationRequest.java b/core/java/android/companion/AssociationRequest.java
index 1f57c7d..417cbc6b 100644
--- a/core/java/android/companion/AssociationRequest.java
+++ b/core/java/android/companion/AssociationRequest.java
@@ -46,6 +46,7 @@
 
     private final boolean mSingleDevice;
     private final List<DeviceFilter<?>> mDeviceFilters;
+    private String mCallingPackage;
 
     private AssociationRequest(
             boolean singleDevice, @Nullable List<DeviceFilter<?>> deviceFilters) {
@@ -57,6 +58,7 @@
         this(
             in.readByte() != 0,
             in.readParcelableList(new ArrayList<>(), AssociationRequest.class.getClassLoader()));
+        setCallingPackage(in.readString());
     }
 
     /** @hide */
@@ -72,32 +74,45 @@
         return mDeviceFilters;
     }
 
+    /** @hide */
+    public String getCallingPackage() {
+        return mCallingPackage;
+    }
+
+    /** @hide */
+    public void setCallingPackage(String pkg) {
+        mCallingPackage = pkg;
+    }
+
     @Override
     public boolean equals(Object o) {
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;
         AssociationRequest that = (AssociationRequest) o;
-        return mSingleDevice == that.mSingleDevice &&
-                Objects.equals(mDeviceFilters, that.mDeviceFilters);
+        return mSingleDevice == that.mSingleDevice
+                && Objects.equals(mDeviceFilters, that.mDeviceFilters)
+                && Objects.equals(mCallingPackage, that.mCallingPackage);
     }
 
     @Override
     public int hashCode() {
-        return Objects.hash(mSingleDevice, mDeviceFilters);
+        return Objects.hash(mSingleDevice, mDeviceFilters, mCallingPackage);
     }
 
     @Override
     public String toString() {
-        return "AssociationRequest{" +
-                "mSingleDevice=" + mSingleDevice +
-                ", mDeviceFilters=" + mDeviceFilters +
-                '}';
+        return "AssociationRequest{"
+                + "mSingleDevice=" + mSingleDevice
+                + ", mDeviceFilters=" + mDeviceFilters
+                + ", mCallingPackage=" + mCallingPackage
+                + '}';
     }
 
     @Override
     public void writeToParcel(Parcel dest, int flags) {
         dest.writeByte((byte) (mSingleDevice ? 1 : 0));
         dest.writeParcelableList(mDeviceFilters, flags);
+        dest.writeString(mCallingPackage);
     }
 
     @Override
diff --git a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/DeviceChooserActivity.java b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/DeviceChooserActivity.java
index 16ef59f..e501e12 100644
--- a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/DeviceChooserActivity.java
+++ b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/DeviceChooserActivity.java
@@ -18,6 +18,8 @@
 
 import static android.companion.BluetoothDeviceFilterUtils.getDeviceMacAddress;
 
+import static java.util.Objects.requireNonNull;
+
 import android.app.Activity;
 import android.companion.CompanionDeviceManager;
 import android.content.Intent;
@@ -117,6 +119,11 @@
     }
 
     @Override
+    public String getCallingPackage() {
+        return requireNonNull(getService().mRequest.getCallingPackage());
+    }
+
+    @Override
     public void setTitle(CharSequence title) {
         final TextView titleView = findViewById(R.id.title);
         final int padding = getPadding(getResources());
diff --git a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
index 29fc1674..b581e0b 100644
--- a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
+++ b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
@@ -300,6 +300,7 @@
             mFindDeviceCallback = callback;
             mRequest = request;
             mCallingPackage = callingPackage;
+            request.setCallingPackage(callingPackage);
             callback.asBinder().linkToDeath(CompanionDeviceManagerService.this /* recipient */, 0);
 
             final long callingIdentity = Binder.clearCallingIdentity();