1 /*
2  * Copyright (C) 2017 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.indexing;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.text.TextUtils;
23 
24 /**
25  * Utility class for {@like DatabaseIndexingManager} to handle the mapping between Payloads
26  * and Preference controllers, and managing indexable classes.
27  */
28 public class DatabaseIndexingUtils {
29 
30     private static final String TAG = "DatabaseIndexingUtils";
31 
32     // frameworks/base/proto/src/metrics_constants.proto#DASHBOARD_SEARCH_RESULTS
33     // Have to hardcode value here because we can't depend on internal framework constants.
34     public static final int DASHBOARD_SEARCH_RESULTS = 34;
35 
36     /**
37      * Below are internal contract between Settings/SettingsIntelligence to launch a search result
38      * page.
39      */
40     public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
41     public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
42     public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
43     public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
44     public static final String SEARCH_RESULT_TRAMPOLINE_ACTION =
45             "com.android.settings.SEARCH_RESULT_TRAMPOLINE";
46 
47     /**
48      * Builds intent that launches the search destination as a sub-setting.
49      */
buildSearchTrampolineIntent(Context context, String className, String key, String screenTitle)50     public static Intent buildSearchTrampolineIntent(Context context, String className, String key,
51             String screenTitle) {
52         final Intent intent = new Intent(SEARCH_RESULT_TRAMPOLINE_ACTION);
53         intent.putExtra(EXTRA_SHOW_FRAGMENT, className)
54                 .putExtra(EXTRA_SHOW_FRAGMENT_TITLE, screenTitle)
55                 .putExtra(EXTRA_SOURCE_METRICS_CATEGORY, DASHBOARD_SEARCH_RESULTS)
56                 .putExtra(EXTRA_FRAGMENT_ARG_KEY, key);
57         return intent;
58     }
59 
buildDirectSearchResultIntent(String action, String targetPackage, String targetClass, String key)60     public static Intent buildDirectSearchResultIntent(String action, String targetPackage,
61             String targetClass, String key) {
62         final Intent intent = new Intent(action).putExtra(EXTRA_FRAGMENT_ARG_KEY, key);
63         if (!TextUtils.isEmpty(targetPackage) && !TextUtils.isEmpty(targetClass)) {
64             final ComponentName component = new ComponentName(targetPackage, targetClass);
65             intent.setComponent(component);
66         }
67         return intent;
68     }
69 
70     // TODO REFACTOR (b/62807132) Add inline support with proper intents.
71 //    /**
72 //     * @param className which wil provide the map between from {@link Uri}s to
73 //     *                  {@link PreferenceControllerMixin}
74 //     * @return A map between {@link Uri}s and {@link PreferenceControllerMixin}s to get the
75 // payload
76 //     * types for Settings.
77 //     */
78 //    public static Map<String, PreferenceControllerMixin> getPreferenceControllerUriMap(
79 //            String className, Context context) {
80 //        if (context == null) {
81 //            return null;
82 //        }
83 //
84 //        final Class<?> clazz = getIndexableClass(className);
85 //
86 //        if (clazz == null) {
87 //            Log.d(TAG, "SearchIndexableResource '" + className +
88 //                    "' should implement the " + Indexable.class.getName() + " interface!");
89 //            return null;
90 //        }
91 //
92 //        // Will be non null only for a Local provider implementing a
93 //        // SEARCH_INDEX_DATA_PROVIDER field
94 //        final Indexable.SearchIndexProvider provider = getSearchIndexProvider(clazz);
95 //
96 //        List<AbstractPreferenceController> controllers =
97 //                provider.getPreferenceControllers(context);
98 //
99 //        if (controllers == null) {
100 //            return null;
101 //        }
102 //
103 //        ArrayMap<String, PreferenceControllerMixin> map = new ArrayMap<>();
104 //
105 //        for (AbstractPreferenceController controller : controllers) {
106 //            if (controller instanceof PreferenceControllerMixin) {
107 //                map.put(controller.getPreferenceKey(), (PreferenceControllerMixin) controller);
108 //            } else {
109 //                throw new IllegalStateException(controller.getClass().getName()
110 //                        + " must implement " + PreferenceControllerMixin.class.getName());
111 //            }
112 //        }
113 //
114 //        return map;
115 //    }
116 //
117 //    /**
118 //     * @param uriMap Map between the {@link PreferenceControllerMixin} keys
119 //     *               and the controllers themselves.
120 //     * @param key    The look-up key
121 //     * @return The Payload from the {@link PreferenceControllerMixin} specified by the key,
122 //     * if it exists. Otherwise null.
123 //     */
124 //    public static ResultPayload getPayloadFromUriMap(Map<String, PreferenceControllerMixin>
125 // uriMap,
126 //            String key) {
127 //        if (uriMap == null) {
128 //            return null;
129 //        }
130 //
131 //        PreferenceControllerMixin controller = uriMap.get(key);
132 //        if (controller == null) {
133 //            return null;
134 //        }
135 //
136 //        return controller.getResultPayload();
137 //    }
138 
139 }
140