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