1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.app.search; 17 18 import static com.android.app.search.SearchTargetExtras.isRichAnswer; 19 20 import android.app.search.SearchTarget; 21 import android.content.ComponentName; 22 import android.os.Process; 23 24 import androidx.annotation.Nullable; 25 26 /** 27 * Helper class that defines helper methods for {@link android.app.search.SearchTargetEvent} to 28 * define the contract between Launcher and AiAi for notifyEvent. 29 */ 30 31 public class SearchTargetEventHelper { 32 33 public static final String PKG_NAME_AGSA = "com.google.android.googlequicksearchbox"; 34 35 /** 36 * Generate web target id similar to AiAi targetId for logging search button tap and Launcher 37 * sends raw query to AGA. 38 * AiAi target id is of format "resultType:userId:packageName:extraInfo" 39 * 40 * @return string webTargetId 41 * Example webTargetId for 42 * web suggestion - WEB_SUGGEST:0:com.google.android.googlequicksearchbox:SUGGESTION 43 */ generateWebTargetIdForRawQuery()44 public static String generateWebTargetIdForRawQuery() { 45 // For raw query, there is no search target, so we pass null. 46 return generateWebTargetIdForLogging(null); 47 } 48 49 /** 50 * Generate web target id similar to AiAi targetId for logging both 0-state and n-state. 51 * AiAi target id is of format "resultType:userId:packageName:extraInfo" 52 * 53 * @return string webTargetId 54 * Example webTargetId for 55 * web suggestion - WEB_SUGGEST:0:com.google.android.googlequicksearchbox:SUGGESTION 56 * rich answer - WEB_SUGGEST:0:com.google.android.googlequicksearchbox:RICH_ANSWER 57 */ generateWebTargetIdForLogging(@ullable SearchTarget webTarget)58 public static String generateWebTargetIdForLogging(@Nullable SearchTarget webTarget) { 59 StringBuilder webTargetId = new StringBuilder( 60 "WEB_SUGGEST" + ":" + Process.myUserHandle().getIdentifier() + ":"); 61 if (webTarget == null) { 62 webTargetId.append(PKG_NAME_AGSA + ":SUGGESTION"); 63 return webTargetId.toString(); 64 } 65 webTargetId.append(webTarget.getPackageName()); 66 if (isRichAnswer(webTarget)) { 67 webTargetId.append(":RICH_ANSWER"); 68 } else { 69 webTargetId.append(":SUGGESTION"); 70 } 71 return webTargetId.toString(); 72 } 73 74 /** 75 * Generate application target id similar to AiAi targetId for logging only 0-state. 76 * For n-state, AiAi already populates the target id in right format. 77 * AiAi target id is of format "resultType:userId:packageName:extraInfo" 78 * 79 * When the apps from AiAi's AppPredictionService are converted to {@link SearchTarget}, we need 80 * to construct the targetId using componentName. 81 * 82 * @return string appTargetId 83 * Example appTargetId for 84 * maps - APPLICATION:0:com.google.android.apps.maps:com.google.android.maps.MapsActivity 85 * clock - APPLICATION:0:com.google.android.deskclock:com.android.deskclock.DeskClock 86 */ generateAppTargetIdForLogging(@ullable ComponentName appComponentName)87 public static String generateAppTargetIdForLogging(@Nullable ComponentName appComponentName) { 88 StringBuilder appTargetId = new StringBuilder( 89 "APPLICATION" + ":" + Process.myUserHandle().getIdentifier() + ":"); 90 if (appComponentName == null) return appTargetId.append(" : ").toString(); 91 return appTargetId + appComponentName.getPackageName() + ":" 92 + appComponentName.getClassName(); 93 } 94 95 /** 96 * Generate gms play target id similar to AiAi targetId for logging only n-state. 97 * AiAi target id is of format "resultType:userId:packageName:extraInfo" 98 * 99 * @return string playTargetId 100 * Example playTargetId for Candy Crush 101 * PLAY:0:com.king.candycrushsaga:Gms 102 */ generatePlayTargetIdForLogging(String appPackage)103 public static String generatePlayTargetIdForLogging(String appPackage) { 104 return "PLAY" + ":" + Process.myUserHandle().getIdentifier() + ":" + appPackage + ":Gms"; 105 } 106 } 107