Home | History | Annotate | Download | only in search
      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.text.TextUtils;
     23 
     24 import com.android.internal.annotations.VisibleForTesting;
     25 import com.android.settings.overlay.FeatureFactory;
     26 import com.android.settings.search.indexing.IndexData;
     27 
     28 import java.util.Locale;
     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 static final String METRICS_ACTION_SETTINGS_INDEX = "search_synchronous_indexing";
     38     private DatabaseIndexingManager mDatabaseIndexingManager;
     39     private SearchIndexableResources mSearchIndexableResources;
     40 
     41     @Override
     42     public void verifyLaunchSearchResultPageCaller(Context context, ComponentName caller) {
     43         if (caller == null) {
     44             throw new IllegalArgumentException("ExternalSettingsTrampoline intents "
     45                     + "must be called with startActivityForResult");
     46         }
     47         final String packageName = caller.getPackageName();
     48         final boolean isSettingsPackage = TextUtils.equals(packageName, context.getPackageName())
     49                 || TextUtils.equals(getSettingsIntelligencePkgName(), packageName);
     50         final boolean isWhitelistedPackage =
     51                 isSignatureWhitelisted(context, caller.getPackageName());
     52         if (isSettingsPackage || isWhitelistedPackage) {
     53             return;
     54         }
     55         throw new SecurityException("Search result intents must be called with from a "
     56                 + "whitelisted package.");
     57     }
     58 
     59     @Override
     60     public DatabaseIndexingManager getIndexingManager(Context context) {
     61         if (mDatabaseIndexingManager == null) {
     62             mDatabaseIndexingManager = new DatabaseIndexingManager(context.getApplicationContext());
     63         }
     64         return mDatabaseIndexingManager;
     65     }
     66 
     67     @Override
     68     public void updateIndex(Context context) {
     69         long indexStartTime = System.currentTimeMillis();
     70         getIndexingManager(context).performIndexing();
     71         int indexingTime = (int) (System.currentTimeMillis() - indexStartTime);
     72         FeatureFactory.getFactory(context).getMetricsFeatureProvider()
     73                 .histogram(context, METRICS_ACTION_SETTINGS_INDEX, indexingTime);
     74     }
     75 
     76     @Override
     77     public SearchIndexableResources getSearchIndexableResources() {
     78         if (mSearchIndexableResources == null) {
     79             mSearchIndexableResources = new SearchIndexableResourcesImpl();
     80         }
     81         return mSearchIndexableResources;
     82     }
     83 
     84     protected boolean isSignatureWhitelisted(Context context, String callerPackage) {
     85         return false;
     86     }
     87 
     88     /**
     89      * A generic method to make the query suitable for searching the database.
     90      *
     91      * @return the cleaned query string
     92      */
     93     @VisibleForTesting
     94     String cleanQuery(String query) {
     95         if (TextUtils.isEmpty(query)) {
     96             return null;
     97         }
     98         if (Locale.getDefault().equals(Locale.JAPAN)) {
     99             query = IndexData.normalizeJapaneseString(query);
    100         }
    101         return query.trim();
    102     }
    103 }
    104