1 /* 2 * Copyright (C) 2018 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 package com.android.quickstep; 17 18 import android.annotation.TargetApi; 19 import android.content.Intent; 20 import android.content.pm.LauncherApps; 21 import android.content.pm.ResolveInfo; 22 import android.content.res.TypedArray; 23 import android.content.res.XmlResourceParser; 24 import android.database.Cursor; 25 import android.database.MatrixCursor; 26 import android.os.Build; 27 import android.provider.SearchIndexablesContract.XmlResource; 28 import android.provider.SearchIndexablesProvider; 29 import android.util.Xml; 30 31 import com.android.launcher3.R; 32 33 import org.xmlpull.v1.XmlPullParser; 34 import org.xmlpull.v1.XmlPullParserException; 35 36 import java.io.IOException; 37 38 import static android.provider.SearchIndexablesContract.INDEXABLES_RAW_COLUMNS; 39 import static android.provider.SearchIndexablesContract.INDEXABLES_XML_RES_COLUMNS; 40 import static android.provider.SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS; 41 42 @TargetApi(Build.VERSION_CODES.O) 43 public class LauncherSearchIndexablesProvider extends SearchIndexablesProvider { 44 @Override onCreate()45 public boolean onCreate() { 46 return true; 47 } 48 49 @Override queryXmlResources(String[] strings)50 public Cursor queryXmlResources(String[] strings) { 51 MatrixCursor cursor = new MatrixCursor(INDEXABLES_XML_RES_COLUMNS); 52 ResolveInfo settingsActivity = getContext().getPackageManager().resolveActivity( 53 new Intent(Intent.ACTION_APPLICATION_PREFERENCES) 54 .setPackage(getContext().getPackageName()), 0); 55 cursor.newRow() 56 .add(XmlResource.COLUMN_XML_RESID, R.xml.indexable_launcher_prefs) 57 .add(XmlResource.COLUMN_INTENT_ACTION, Intent.ACTION_APPLICATION_PREFERENCES) 58 .add(XmlResource.COLUMN_INTENT_TARGET_PACKAGE, getContext().getPackageName()) 59 .add(XmlResource.COLUMN_INTENT_TARGET_CLASS, settingsActivity.activityInfo.name); 60 return cursor; 61 } 62 63 @Override queryRawData(String[] projection)64 public Cursor queryRawData(String[] projection) { 65 return new MatrixCursor(INDEXABLES_RAW_COLUMNS); 66 } 67 68 @Override queryNonIndexableKeys(String[] projection)69 public Cursor queryNonIndexableKeys(String[] projection) { 70 MatrixCursor cursor = new MatrixCursor(NON_INDEXABLES_KEYS_COLUMNS); 71 if (!getContext().getSystemService(LauncherApps.class).hasShortcutHostPermission()) { 72 // We are not the current launcher. Hide all preferences 73 try (XmlResourceParser parser = getContext().getResources() 74 .getXml(R.xml.indexable_launcher_prefs)) { 75 final int depth = parser.getDepth(); 76 final int[] attrs = new int[] { android.R.attr.key }; 77 int type; 78 while (((type = parser.next()) != XmlPullParser.END_TAG || 79 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { 80 if (type == XmlPullParser.START_TAG) { 81 TypedArray a = getContext().obtainStyledAttributes( 82 Xml.asAttributeSet(parser), attrs); 83 cursor.addRow(new String[] {a.getString(0)}); 84 a.recycle(); 85 } 86 } 87 } catch (IOException |XmlPullParserException e) { 88 throw new RuntimeException(e); 89 } 90 } 91 return cursor; 92 } 93 } 94