1 /*
2  * Copyright (C) 2019 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.role.service;
18 
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.database.MatrixCursor;
22 import android.os.Binder;
23 import android.os.Process;
24 import android.provider.SearchIndexablesContract;
25 import android.util.ArrayMap;
26 
27 import androidx.annotation.Nullable;
28 
29 import com.android.permissioncontroller.R;
30 import com.android.permissioncontroller.permission.service.BaseSearchIndexablesProvider;
31 import com.android.permissioncontroller.role.model.RoleParserInitializer;
32 import com.android.role.controller.model.Role;
33 import com.android.role.controller.model.Roles;
34 
35 /**
36  * {@link android.provider.SearchIndexablesProvider} for roles.
37  */
38 public class RoleSearchIndexablesProvider extends BaseSearchIndexablesProvider {
39 
40     public static final String ACTION_MANAGE_DEFAULT_APP =
41             "com.android.permissioncontroller.settingssearch.action.MANAGE_DEFAULT_APP";
42 
43     public static final String ACTION_MANAGE_SPECIAL_APP_ACCESS =
44             "com.android.permissioncontroller.settingssearch.action.MANAGE_SPECIAL_APP_ACCESS";
45 
46     @Override
onCreate()47     public boolean onCreate() {
48         RoleParserInitializer.initialize();
49         return true;
50     }
51 
52     @Nullable
53     @Override
queryRawData(@ullable String[] projection)54     public Cursor queryRawData(@Nullable String[] projection) {
55         MatrixCursor cursor = new MatrixCursor(SearchIndexablesContract.INDEXABLES_RAW_COLUMNS);
56         Context context = getContext();
57         ArrayMap<String, Role> roles = Roles.get(context);
58         int rolesSize = roles.size();
59         for (int i = 0; i < rolesSize; i++) {
60             Role role = roles.valueAt(i);
61 
62             long token = Binder.clearCallingIdentity();
63             try {
64                 if (!role.isAvailableAsUser(Process.myUserHandle(), context)
65                         || !role.isVisibleAsUser(Process.myUserHandle(), context)) {
66                     continue;
67                 }
68             } finally {
69                 Binder.restoreCallingIdentity(token);
70             }
71 
72             String label = context.getString(role.getLabelResource());
73             int searchKeywordsResource = role.getSearchKeywordsResource();
74             boolean isExclusive = role.isExclusive();
75             cursor.newRow()
76                     .add(SearchIndexablesContract.RawData.COLUMN_RANK, 0)
77                     .add(SearchIndexablesContract.RawData.COLUMN_TITLE, label)
78                     .add(SearchIndexablesContract.RawData.COLUMN_KEYWORDS, label
79                             + (searchKeywordsResource != 0 ? ", " + context.getString(
80                             searchKeywordsResource) : "")
81                             + ", " + context.getString(isExclusive
82                             ? R.string.default_app_search_keyword
83                             : R.string.special_app_access_search_keyword))
84                     .add(SearchIndexablesContract.RawData.COLUMN_KEY, createRawDataKey(
85                             role.getName(), context))
86                     .add(SearchIndexablesContract.RawData.COLUMN_INTENT_ACTION, isExclusive
87                             ? ACTION_MANAGE_DEFAULT_APP : ACTION_MANAGE_SPECIAL_APP_ACCESS)
88                     .add(SearchIndexablesContract.RawData.COLUMN_SCREEN_TITLE,
89                             context.getString(R.string.default_apps));
90         }
91         return cursor;
92     }
93 }
94