Fix broken tests in TextClassifierServiceTest.

We introduce SystemTCMetadata in ag/10605740 which centrializes the
needed information for the TCMS to process the requests. Because we
change the getPackageName() logic to return the calling package name
of SystemTCMetadata and the SystemTCMetadata is only created if the
event is from the SystemTC not other TCs. The tests fail because the
SelectionEvent.packageName is not populated.

This cl contains the following changes:
- Return the package name that the SelectionEvent or
TextClassificationContext originated from
- Fix incorrect javadoc of TextClassificationContext.getPackageName
- Always verify the calling package name for onSelectionEvent and
onTextClassifierEvent

Bug: 151614815
Test: atest TextClassifierServiceTest
Test: atest FrameworksCoreTests:android.view.textclassifier
Test: atest FrameworksCoreTests:android.widget.TextViewActivityTest
Test: atest CtsTextClassifierTestCases
Change-Id: Iabe1691d63e3ec6ac834984cf79b60faf9c0e765
diff --git a/core/java/android/view/textclassifier/SelectionEvent.java b/core/java/android/view/textclassifier/SelectionEvent.java
index 9a54544..6f9556b 100644
--- a/core/java/android/view/textclassifier/SelectionEvent.java
+++ b/core/java/android/view/textclassifier/SelectionEvent.java
@@ -158,7 +158,6 @@
         mEventType = in.readInt();
         mEntityType = in.readString();
         mWidgetVersion = in.readInt() > 0 ? in.readString() : null;
-        // TODO: remove mPackageName once aiai does not need it
         mPackageName = in.readString();
         mWidgetType = in.readString();
         mInvocationMethod = in.readInt();
@@ -186,7 +185,6 @@
         if (mWidgetVersion != null) {
             dest.writeString(mWidgetVersion);
         }
-        // TODO: remove mPackageName once aiai does not need it
         dest.writeString(mPackageName);
         dest.writeString(mWidgetType);
         dest.writeInt(mInvocationMethod);
@@ -406,7 +404,7 @@
      */
     @NonNull
     public String getPackageName() {
-        return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : "";
+        return mPackageName;
     }
 
     /**
diff --git a/core/java/android/view/textclassifier/TextClassificationContext.java b/core/java/android/view/textclassifier/TextClassificationContext.java
index f2323c6..5d5683f 100644
--- a/core/java/android/view/textclassifier/TextClassificationContext.java
+++ b/core/java/android/view/textclassifier/TextClassificationContext.java
@@ -31,7 +31,6 @@
  */
 public final class TextClassificationContext implements Parcelable {
 
-    // NOTE: Modify packageName only in the constructor or in setSystemTextClassifierMetadata()
     private String mPackageName;
     private final String mWidgetType;
     @Nullable private final String mWidgetVersion;
@@ -47,7 +46,7 @@
     }
 
     /**
-     * Returns the package name for the calling package.
+     * Returns the package name of the app that this context originated in.
      */
     @NonNull
     public String getPackageName() {
@@ -57,14 +56,10 @@
     /**
      * Sets the information about the {@link SystemTextClassifier} that sent this request.
      *
-     * <p><b>NOTE: </b>This will override the value returned in {@link getPackageName()}.
      * @hide
      */
     void setSystemTextClassifierMetadata(@Nullable SystemTextClassifierMetadata systemTcMetadata) {
         mSystemTcMetadata = systemTcMetadata;
-        if (mSystemTcMetadata != null) {
-            mPackageName = mSystemTcMetadata.getCallingPackageName();
-        }
     }
 
     /**
diff --git a/services/core/java/com/android/server/textclassifier/TextClassificationManagerService.java b/services/core/java/com/android/server/textclassifier/TextClassificationManagerService.java
index 74a6383..0d16fcc 100644
--- a/services/core/java/com/android/server/textclassifier/TextClassificationManagerService.java
+++ b/services/core/java/com/android/server/textclassifier/TextClassificationManagerService.java
@@ -234,7 +234,7 @@
 
         handleRequest(
                 event.getSystemTextClassifierMetadata(),
-                /* verifyCallingPackage= */ false,
+                /* verifyCallingPackage= */ true,
                 /* attemptToBind= */ false,
                 service -> service.onSelectionEvent(sessionId, event),
                 "onSelectionEvent",
@@ -253,7 +253,7 @@
 
         handleRequest(
                 systemTcMetadata,
-                /* verifyCallingPackage= */ false,
+                /* verifyCallingPackage= */ true,
                 /* attemptToBind= */ false,
                 service -> service.onTextClassifierEvent(sessionId, event),
                 "onTextClassifierEvent",