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 
17 package com.android.packageinstaller.permission.service;
18 
19 import static android.provider.SearchIndexablesContract.INDEXABLES_RAW_COLUMNS;
20 import static android.provider.SearchIndexablesContract.RawData.COLUMN_INTENT_ACTION;
21 import static android.provider.SearchIndexablesContract.RawData.COLUMN_KEY;
22 import static android.provider.SearchIndexablesContract.RawData.COLUMN_KEYWORDS;
23 import static android.provider.SearchIndexablesContract.RawData.COLUMN_RANK;
24 import static android.provider.SearchIndexablesContract.RawData.COLUMN_TITLE;
25 
26 import android.content.Context;
27 import android.content.pm.PackageManager;
28 import android.database.Cursor;
29 import android.database.MatrixCursor;
30 import android.util.Log;
31 
32 import com.android.packageinstaller.permission.utils.Utils;
33 import com.android.permissioncontroller.R;
34 
35 import java.util.List;
36 
37 /**
38  * {@link android.provider.SearchIndexablesProvider} for permissions.
39  */
40 public class PermissionSearchIndexablesProvider extends BaseSearchIndexablesProvider {
41     private static final String LOG_TAG = PermissionSearchIndexablesProvider.class.getSimpleName();
42 
43     public static final String ACTION_MANAGE_PERMISSION_APPS =
44             "com.android.permissioncontroller.settingssearch.action.MANAGE_PERMISSION_APPS";
45 
46     @Override
queryRawData(String[] projection)47     public Cursor queryRawData(String[] projection) {
48         Context context = getContext();
49         PackageManager pm = context.getPackageManager();
50 
51         List<String> permissionGroupNames = Utils.getPlatformPermissionGroups();
52         MatrixCursor cursor = new MatrixCursor(INDEXABLES_RAW_COLUMNS);
53 
54         int numPermissionGroups = permissionGroupNames.size();
55         for (int i = 0; i < numPermissionGroups; i++) {
56             String groupName = permissionGroupNames.get(i);
57 
58             CharSequence label = getPermissionGroupLabel(groupName, pm);
59 
60             cursor.newRow().add(COLUMN_RANK, 0)
61                     .add(COLUMN_TITLE, label)
62                     .add(COLUMN_KEYWORDS, label + ", " + context.getString(
63                             R.string.permission_search_keyword))
64                     .add(COLUMN_KEY, createRawDataKey(groupName, context))
65                     .add(COLUMN_INTENT_ACTION, ACTION_MANAGE_PERMISSION_APPS);
66         }
67 
68         return cursor;
69     }
70 
getPermissionGroupLabel(String groupName, PackageManager pm)71     private CharSequence getPermissionGroupLabel(String groupName, PackageManager pm) {
72         try {
73             return pm.getPermissionGroupInfo(groupName, 0).loadLabel(pm);
74         } catch (PackageManager.NameNotFoundException e) {
75             Log.w(LOG_TAG, "Cannot find group label for " + groupName, e);
76         }
77         return null;
78     }
79 }
80