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