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 static com.android.settings.intelligence.search.sitemap.HighlightableMenu.MENU_KEY_ACCESSIBILITY; 20 21 import android.accessibilityservice.AccessibilityServiceInfo; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.content.pm.ServiceInfo; 28 import android.graphics.drawable.Drawable; 29 import android.view.accessibility.AccessibilityManager; 30 31 import com.android.settings.intelligence.R; 32 import com.android.settings.intelligence.nano.SettingsIntelligenceLogProto; 33 import com.android.settings.intelligence.search.ResultPayload; 34 import com.android.settings.intelligence.search.SearchResult; 35 import com.android.settings.intelligence.search.indexing.DatabaseIndexingUtils; 36 import com.android.settings.intelligence.search.sitemap.SiteMapManager; 37 38 import java.util.ArrayList; 39 import java.util.Collections; 40 import java.util.List; 41 42 public class AccessibilityServiceResultTask extends SearchQueryTask.QueryWorker { 43 44 public static final int QUERY_WORKER_ID = SettingsIntelligenceLogProto.SettingsIntelligenceEvent 45 .SEARCH_QUERY_ACCESSIBILITY_SERVICES; 46 47 private static final int NAME_NO_MATCH = -1; 48 49 private final AccessibilityManager mAccessibilityManager; 50 private final PackageManager mPackageManager; 51 52 private List<String> mBreadcrumb; 53 newTask(Context context, SiteMapManager manager, String query)54 public static SearchQueryTask newTask(Context context, SiteMapManager manager, 55 String query) { 56 return new SearchQueryTask(new AccessibilityServiceResultTask(context, manager, query)); 57 } 58 AccessibilityServiceResultTask(Context context, SiteMapManager mapManager, String query)59 public AccessibilityServiceResultTask(Context context, SiteMapManager mapManager, 60 String query) { 61 super(context, mapManager, query); 62 mPackageManager = context.getPackageManager(); 63 mAccessibilityManager = 64 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE); 65 } 66 67 @Override query()68 protected List<? extends SearchResult> query() { 69 final List<SearchResult> results = new ArrayList<>(); 70 final List<AccessibilityServiceInfo> services = mAccessibilityManager 71 .getInstalledAccessibilityServiceList(); 72 final String screenTitle = mContext.getString(R.string.accessibility_settings); 73 for (AccessibilityServiceInfo service : services) { 74 if (service == null) { 75 continue; 76 } 77 final ResolveInfo resolveInfo = service.getResolveInfo(); 78 if (service.getResolveInfo() == null) { 79 continue; 80 } 81 final ServiceInfo serviceInfo = resolveInfo.serviceInfo; 82 final CharSequence title = resolveInfo.loadLabel(mPackageManager); 83 final int wordDiff = SearchQueryUtils.getWordDifference(title.toString(), mQuery); 84 if (wordDiff == NAME_NO_MATCH) { 85 continue; 86 } 87 final Drawable icon = serviceInfo.loadIcon(mPackageManager); 88 final String componentName = new ComponentName(serviceInfo.packageName, 89 serviceInfo.name).flattenToString(); 90 final Intent intent = DatabaseIndexingUtils.buildSearchTrampolineIntent( 91 mContext.getString(R.string.accessibility_settings_classname), 92 componentName, screenTitle, MENU_KEY_ACCESSIBILITY); 93 94 results.add(new SearchResult.Builder() 95 .setTitle(title) 96 .addBreadcrumbs(getBreadCrumb()) 97 .setPayload(new ResultPayload(intent)) 98 .setRank(wordDiff) 99 .setIcon(icon) 100 .setDataKey(componentName) 101 .build()); 102 } 103 Collections.sort(results); 104 return results; 105 } 106 107 @Override getQueryWorkerId()108 protected int getQueryWorkerId() { 109 return QUERY_WORKER_ID; 110 } 111 getBreadCrumb()112 private List<String> getBreadCrumb() { 113 if (mBreadcrumb == null || mBreadcrumb.isEmpty()) { 114 mBreadcrumb = mSiteMapManager.buildBreadCrumb( 115 mContext, mContext.getString(R.string.accessibility_settings_classname), 116 mContext.getString(R.string.accessibility_settings)); 117 } 118 return mBreadcrumb; 119 } 120 } 121