1 /*
2  * Copyright (C) 2022 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 
17 package com.android.settings.intelligence.search.sitemap;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 import android.util.ArrayMap;
22 import android.util.Log;
23 
24 import com.android.settings.intelligence.overlay.FeatureFactory;
25 import com.android.settings.intelligence.search.indexing.IndexData;
26 import com.android.settingslib.activityembedding.ActivityEmbeddingUtils;
27 
28 import java.util.Map;
29 import java.util.Map.Entry;
30 
31 public class HighlightableMenu {
32 
33     private static final String TAG = "HighlightableMenu";
34     private static final boolean DEBUG = false;
35 
36     public static final String MENU_KEY_NETWORK = "top_level_network";
37     public static final String MENU_KEY_APPS = "top_level_apps";
38     public static final String MENU_KEY_ACCESSIBILITY = "top_level_accessibility";
39     public static final String MENU_KEY_PRIVACY = "top_level_privacy";
40     public static final String MENU_KEY_SYSTEM = "top_level_system";
41 
42     private static final Map<String, String> sAuthorityToMenuKeyMap;
43     private static final Map<String, String> sPackageToMenuKeyMap;
44 
45     static {
46         sAuthorityToMenuKeyMap = new ArrayMap<>();
47         sAuthorityToMenuKeyMap.put(
48                 "com.android.permissioncontroller.role", MENU_KEY_APPS); // Default apps
49 
50         sPackageToMenuKeyMap = new ArrayMap<>();
51         sPackageToMenuKeyMap.put(
52                 "com.android.settings.network", MENU_KEY_NETWORK); // Settings Network page
53         sPackageToMenuKeyMap.put(
54                 "com.android.permissioncontroller", MENU_KEY_PRIVACY); // Permission manager
55     }
56 
HighlightableMenu()57     private HighlightableMenu() {
58     }
59 
isFeatureEnabled(Context context)60     public static boolean isFeatureEnabled(Context context) {
61         boolean enabled = ActivityEmbeddingUtils.isEmbeddingActivityEnabled(context);
62         Log.i(TAG, "isFeatureEnabled: " + enabled);
63         return enabled;
64     }
65 
getMenuKey(Context context, IndexData row)66     public static String getMenuKey(Context context, IndexData row) {
67         String menuKey;
68         SiteMapManager siteMap = FeatureFactory.get(context).searchFeatureProvider()
69                 .getSiteMapManager();
70 
71         // look up in SiteMap
72         SiteMapPair pair = siteMap.getTopLevelPair(context, row.className, row.screenTitle);
73         if (pair != null) {
74             menuKey = pair.getHighlightableMenuKey();
75             if (!TextUtils.isEmpty(menuKey)) {
76                 return menuKey;
77             }
78         }
79 
80         // look up in custom authority map
81         menuKey = sAuthorityToMenuKeyMap.get(row.authority);
82         if (!TextUtils.isEmpty(menuKey)) {
83             logD("Matched authority, title: " + row.updatedTitle + ", menuKey: " + menuKey);
84             return menuKey;
85         }
86 
87         // look up in custom package map (package match)
88         menuKey = sPackageToMenuKeyMap.get(row.packageName);
89         if (!TextUtils.isEmpty(menuKey)) {
90             logD("Matched package, title: " + row.updatedTitle + ", menuKey: " + menuKey);
91             return menuKey;
92         }
93 
94         // look up in custom package map (target package match)
95         menuKey = sPackageToMenuKeyMap.get(row.intentTargetPackage);
96         if (!TextUtils.isEmpty(menuKey)) {
97             logD("Matched target package, title: " + row.updatedTitle + ", menuKey: " + menuKey);
98             return menuKey;
99         }
100 
101         // look up in custom package map (class prefix match)
102         if (!TextUtils.isEmpty(row.className)) {
103             for (Entry<String, String> entry : sPackageToMenuKeyMap.entrySet()) {
104                 if (row.className.startsWith(entry.getKey())) {
105                     menuKey = entry.getValue();
106                     if (!TextUtils.isEmpty(menuKey)) {
107                         logD("Matched class prefix, title: " + row.updatedTitle
108                                 + ", menuKey: " + menuKey);
109                         return menuKey;
110                     }
111                 }
112             }
113         }
114 
115         logD("Cannot get menu key for: " + row.updatedTitle
116                 + ", data key: " + row.key
117                 + ", top-level: " + (pair != null ? pair.getParentTitle() : row.screenTitle)
118                 + ", package: " + row.packageName);
119         return menuKey;
120     }
121 
logD(String log)122     private static void logD(String log) {
123         if (DEBUG) {
124             Log.d(TAG, log);
125         }
126     }
127 }
128