Fix app SearchTarget id generation to consider UserHandle.
Previously, `SearchTargetEventHelper#generateAppTargetIdForLogging` was
assuming Process.myUserHandle() was the UserHandle to use, instead of
the ItemInfo.users. In the case of work profile, this would generate the
incorrect id, and even worse create a name collision.
Bug: 277363943
Flag: Production bug fix. Flag not needed.
Test: Before: http://recall/clips/08f8a142-ceeb-475e-8c4c-99b78240c592
Test: After: http://recall/clips/af50580e-74d5-46d8-97d6-60147a778a59
Change-Id: Ie075467555a61b6edd3cc9453c5108815351c54a
diff --git a/searchuilib/src/com/android/app/search/SearchTargetEventHelper.java b/searchuilib/src/com/android/app/search/SearchTargetEventHelper.java
index a323625..da4b363 100644
--- a/searchuilib/src/com/android/app/search/SearchTargetEventHelper.java
+++ b/searchuilib/src/com/android/app/search/SearchTargetEventHelper.java
@@ -20,6 +20,7 @@
import android.app.search.SearchTarget;
import android.content.ComponentName;
import android.os.Process;
+import android.os.UserHandle;
import androidx.annotation.Nullable;
@@ -72,21 +73,24 @@
}
/**
- * Generate application target id similar to AiAi targetId for logging only 0-state.
- * For n-state, AiAi already populates the target id in right format.
- * AiAi target id is of format "resultType:userId:packageName:extraInfo"
+ * Generate application target id that matches the AiAi targetId for only 0-state.
+ * For n-state, AiAi already populates the target id in right format and it's unnecessary for
+ * Launcher to generate it itself.
+ * AiAi target id is of format "resultType:userHandle.Id:packageName:extraInfo"
*
- * When the apps from AiAi's AppPredictionService are converted to {@link SearchTarget}, we need
- * to construct the targetId using componentName.
+ * When the apps from AiAi's AppPredictionService are converted to {@link SearchTarget},
+ * we need to construct the targetId using {@link ComponentName} and {@link UserHandle}.
+ * Both are required to create a unique id for the SearchTarget.
*
* @return string appTargetId
* Example appTargetId for
* maps - APPLICATION:0:com.google.android.apps.maps:com.google.android.maps.MapsActivity
* clock - APPLICATION:0:com.google.android.deskclock:com.android.deskclock.DeskClock
*/
- public static String generateAppTargetIdForLogging(@Nullable ComponentName appComponentName) {
+ public static String generateAppTargetId(@Nullable ComponentName appComponentName,
+ UserHandle userHandle) {
StringBuilder appTargetId = new StringBuilder(
- "APPLICATION" + ":" + Process.myUserHandle().getIdentifier() + ":");
+ "APPLICATION" + ":" + userHandle.getIdentifier() + ":");
if (appComponentName == null) return appTargetId.append(" : ").toString();
return appTargetId + appComponentName.getPackageName() + ":"
+ appComponentName.getClassName();