Home | History | Annotate | Download | only in query
      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.query;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.pm.ApplicationInfo;
     22 import android.content.pm.PackageManager;
     23 import android.net.Uri;
     24 import android.provider.Settings;
     25 
     26 import com.android.settings.intelligence.R;
     27 import com.android.settings.intelligence.nano.SettingsIntelligenceLogProto;
     28 import com.android.settings.intelligence.search.AppSearchResult;
     29 import com.android.settings.intelligence.search.ResultPayload;
     30 import com.android.settings.intelligence.search.SearchResult;
     31 import com.android.settings.intelligence.search.indexing.DatabaseIndexingUtils;
     32 import com.android.settings.intelligence.search.sitemap.SiteMapManager;
     33 
     34 import java.util.ArrayList;
     35 import java.util.Collections;
     36 import java.util.List;
     37 
     38 /**
     39  * Search loader for installed apps.
     40  */
     41 public class InstalledAppResultTask extends SearchQueryTask.QueryWorker {
     42 
     43     public static final int QUERY_WORKER_ID =
     44             SettingsIntelligenceLogProto.SettingsIntelligenceEvent.SEARCH_QUERY_INSTALLED_APPS;
     45 
     46     private final PackageManager mPackageManager;
     47     private final String INTENT_SCHEME = "package";
     48     private List<String> mBreadcrumb;
     49 
     50     public static SearchQueryTask newTask(Context context, SiteMapManager siteMapManager,
     51             String query) {
     52         return new SearchQueryTask(new InstalledAppResultTask(context, siteMapManager, query));
     53     }
     54 
     55     public InstalledAppResultTask(Context context, SiteMapManager siteMapManager,
     56             String query) {
     57         super(context, siteMapManager, query);
     58         mPackageManager = context.getPackageManager();
     59     }
     60 
     61     @Override
     62     protected int getQueryWorkerId() {
     63         return QUERY_WORKER_ID;
     64     }
     65 
     66     @Override
     67     protected List<? extends SearchResult> query() {
     68         final List<AppSearchResult> results = new ArrayList<>();
     69 
     70         List<ApplicationInfo> appsInfo = mPackageManager.getInstalledApplications(
     71                 PackageManager.MATCH_DISABLED_COMPONENTS
     72                         | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
     73                         | PackageManager.MATCH_INSTANT);
     74 
     75         for (ApplicationInfo info : appsInfo) {
     76             final CharSequence label = info.loadLabel(mPackageManager);
     77             final int wordDiff = SearchQueryUtils.getWordDifference(label.toString(), mQuery);
     78             if (wordDiff == SearchQueryUtils.NAME_NO_MATCH) {
     79                 continue;
     80             }
     81 
     82             final Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
     83                     .setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
     84                     .setData(
     85                             Uri.fromParts(INTENT_SCHEME, info.packageName, null /* fragment */))
     86                     .putExtra(DatabaseIndexingUtils.EXTRA_SOURCE_METRICS_CATEGORY,
     87                             DatabaseIndexingUtils.DASHBOARD_SEARCH_RESULTS);
     88 
     89             final AppSearchResult.Builder builder = new AppSearchResult.Builder();
     90             builder.setAppInfo(info)
     91                     .setDataKey(info.packageName)
     92                     .setTitle(info.loadLabel(mPackageManager))
     93                     .setRank(getRank(wordDiff))
     94                     .addBreadcrumbs(getBreadCrumb())
     95                     .setPayload(new ResultPayload(intent));
     96             results.add(builder.build());
     97         }
     98 
     99         Collections.sort(results);
    100         return results;
    101     }
    102 
    103     private List<String> getBreadCrumb() {
    104         if (mBreadcrumb == null || mBreadcrumb.isEmpty()) {
    105             mBreadcrumb = mSiteMapManager.buildBreadCrumb(
    106                     mContext, "com.android.settings.applications.ManageApplications",
    107                     mContext.getString(R.string.applications_settings));
    108         }
    109         return mBreadcrumb;
    110     }
    111 
    112     /**
    113      * A temporary ranking scheme for installed apps.
    114      *
    115      * @param wordDiff difference between query length and app name length.
    116      * @return the ranking.
    117      */
    118     private int getRank(int wordDiff) {
    119         if (wordDiff < 6) {
    120             return 2;
    121         }
    122         return 3;
    123     }
    124 }
    125 
    126