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 
18 package com.android.settings.search;
19 
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.provider.Settings;
25 import android.text.TextUtils;
26 
27 import com.android.settingslib.search.SearchIndexableResources;
28 import com.android.settingslib.search.SearchIndexableResourcesMobile;
29 
30 /**
31  * FeatureProvider for the refactored search code.
32  */
33 public class SearchFeatureProviderImpl implements SearchFeatureProvider {
34 
35     private static final String TAG = "SearchFeatureProvider";
36 
37     private SearchIndexableResources mSearchIndexableResources;
38 
39     @Override
verifyLaunchSearchResultPageCaller(Context context, ComponentName caller)40     public void verifyLaunchSearchResultPageCaller(Context context, ComponentName caller) {
41         if (caller == null) {
42             throw new IllegalArgumentException("ExternalSettingsTrampoline intents "
43                     + "must be called with startActivityForResult");
44         }
45         final String packageName = caller.getPackageName();
46         final boolean isSettingsPackage = TextUtils.equals(packageName, context.getPackageName())
47                 || TextUtils.equals(getSettingsIntelligencePkgName(context), packageName);
48         final boolean isWhitelistedPackage =
49                 isSignatureWhitelisted(context, caller.getPackageName());
50         if (isSettingsPackage || isWhitelistedPackage) {
51             return;
52         }
53         throw new SecurityException("Search result intents must be called with from a "
54                 + "whitelisted package.");
55     }
56 
57     @Override
getSearchIndexableResources()58     public SearchIndexableResources getSearchIndexableResources() {
59         if (mSearchIndexableResources == null) {
60             mSearchIndexableResources = new SearchIndexableResourcesMobile();
61         }
62         return mSearchIndexableResources;
63     }
64 
65     @Override
buildSearchIntent(Context context, int pageId)66     public Intent buildSearchIntent(Context context, int pageId) {
67         return new Intent(Settings.ACTION_APP_SEARCH_SETTINGS)
68                 .setPackage(getSettingsIntelligencePkgName(context))
69                 .putExtra(Intent.EXTRA_REFERRER, buildReferrer(context, pageId));
70     }
71 
isSignatureWhitelisted(Context context, String callerPackage)72     protected boolean isSignatureWhitelisted(Context context, String callerPackage) {
73         return false;
74     }
75 
buildReferrer(Context context, int pageId)76     private static Uri buildReferrer(Context context, int pageId) {
77         return new Uri.Builder()
78                 .scheme("android-app")
79                 .authority(context.getPackageName())
80                 .path(String.valueOf(pageId))
81                 .build();
82     }
83 }
84