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.search;
18 
19 import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO;
20 
21 import android.annotation.NonNull;
22 import android.app.Activity;
23 import android.app.settings.SettingsEnums;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.pm.PackageManager;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.Toolbar;
31 
32 import com.android.settings.R;
33 import com.android.settings.Utils;
34 import com.android.settings.overlay.FeatureFactory;
35 import com.android.settingslib.search.SearchIndexableResources;
36 
37 /**
38  * FeatureProvider for Settings Search
39  */
40 public interface SearchFeatureProvider {
41 
42     int REQUEST_CODE = 501;
43 
44     /**
45      * Ensures the caller has necessary privilege to launch search result page.
46      *
47      * @throws IllegalArgumentException when caller is null
48      * @throws SecurityException        when caller is not allowed to launch search result page
49      */
verifyLaunchSearchResultPageCaller(Context context, @NonNull ComponentName caller)50     void verifyLaunchSearchResultPageCaller(Context context, @NonNull ComponentName caller)
51             throws SecurityException, IllegalArgumentException;
52 
53     /**
54      * @return a {@link SearchIndexableResources} to be used for indexing search results.
55      */
getSearchIndexableResources()56     SearchIndexableResources getSearchIndexableResources();
57 
getSettingsIntelligencePkgName(Context context)58     default String getSettingsIntelligencePkgName(Context context) {
59         return context.getString(R.string.config_settingsintelligence_package_name);
60     }
61 
62     /**
63      * Initializes the search toolbar.
64      */
initSearchToolbar(Activity activity, Toolbar toolbar, int pageId)65     default void initSearchToolbar(Activity activity, Toolbar toolbar, int pageId) {
66         if (activity == null || toolbar == null) {
67             return;
68         }
69 
70         if (!Utils.isDeviceProvisioned(activity) ||
71                 !Utils.isPackageEnabled(activity, getSettingsIntelligencePkgName(activity))) {
72             final ViewGroup parent = (ViewGroup) toolbar.getParent();
73             if (parent != null) {
74                 parent.setVisibility(View.GONE);
75             }
76             return;
77         }
78         // Please forgive me for what I am about to do.
79         //
80         // Need to make the navigation icon non-clickable so that the entire card is clickable
81         // and goes to the search UI. Also set the background to null so there's no ripple.
82         final View navView = toolbar.getNavigationView();
83         navView.setClickable(false);
84         navView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
85         navView.setBackground(null);
86 
87         toolbar.setOnClickListener(tb -> {
88             final Context context = activity.getApplicationContext();
89             final Intent intent = buildSearchIntent(context, pageId);
90 
91             if (activity.getPackageManager().queryIntentActivities(intent,
92                     PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) {
93                 return;
94             }
95 
96             FeatureFactory.getFactory(context).getSlicesFeatureProvider()
97                     .indexSliceDataAsync(context);
98             FeatureFactory.getFactory(context).getMetricsFeatureProvider()
99                     .action(context, SettingsEnums.ACTION_SEARCH_RESULTS);
100             activity.startActivityForResult(intent, REQUEST_CODE);
101         });
102     }
103 
buildSearchIntent(Context context, int pageId)104     Intent buildSearchIntent(Context context, int pageId);
105 }
106