1 /*
2  * Copyright (C) 2021 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.systemui.people;
18 
19 import android.content.ContentProvider;
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ProviderInfo;
24 import android.database.Cursor;
25 import android.net.Uri;
26 import android.os.Binder;
27 import android.os.Bundle;
28 import android.os.UserHandle;
29 import android.util.Log;
30 import android.widget.RemoteViews;
31 
32 import com.android.systemui.SystemUIAppComponentFactory;
33 import com.android.systemui.people.widget.PeopleSpaceWidgetManager;
34 import com.android.systemui.shared.system.PeopleProviderUtils;
35 
36 import javax.inject.Inject;
37 
38 /** API that returns a People Tile preview. */
39 public class PeopleProvider extends ContentProvider implements
40         SystemUIAppComponentFactory.ContextInitializer {
41     private static final String TAG = "PeopleProvider";
42     private static final boolean DEBUG = PeopleSpaceUtils.DEBUG;
43     private static final String EMPTY_STRING = "";
44     private SystemUIAppComponentFactory.ContextAvailableCallback mCallback;
45 
46     @Inject
47     PeopleSpaceWidgetManager mPeopleSpaceWidgetManager;
48 
49     @Override
call(String method, String arg, Bundle extras)50     public Bundle call(String method, String arg, Bundle extras) {
51         if (!doesCallerHavePermission()) {
52             String callingPackage = getCallingPackage();
53             Log.w(TAG, "API not accessible to calling package: " + callingPackage);
54             throw new SecurityException("API not accessible to calling package: " + callingPackage);
55         }
56         if (!PeopleProviderUtils.GET_PEOPLE_TILE_PREVIEW_METHOD.equals(method)) {
57             Log.w(TAG, "Invalid method");
58             throw new IllegalArgumentException("Invalid method");
59         }
60 
61         if (extras == null) {
62             Log.w(TAG, "Extras can't be null");
63             throw new IllegalArgumentException("Extras can't be null");
64         }
65 
66         String shortcutId = extras.getString(
67                 PeopleProviderUtils.EXTRAS_KEY_SHORTCUT_ID, EMPTY_STRING);
68         String packageName = extras.getString(
69                 PeopleProviderUtils.EXTRAS_KEY_PACKAGE_NAME, EMPTY_STRING);
70         UserHandle userHandle = extras.getParcelable(
71                 PeopleProviderUtils.EXTRAS_KEY_USER_HANDLE);
72         if (shortcutId.isEmpty()) {
73             Log.w(TAG, "Invalid shortcut id");
74             throw new IllegalArgumentException("Invalid shortcut id");
75         }
76 
77         if (packageName.isEmpty()) {
78             Log.w(TAG, "Invalid package name");
79             throw new IllegalArgumentException("Invalid package name");
80         }
81         if (userHandle == null) {
82             Log.w(TAG, "Null user handle");
83             throw new IllegalArgumentException("Null user handle");
84         }
85 
86         if (mPeopleSpaceWidgetManager == null) {
87             Log.e(TAG, "Could not initialize people widget manager");
88             return null;
89         }
90         RemoteViews view = mPeopleSpaceWidgetManager.getPreview(shortcutId, userHandle, packageName,
91                 extras);
92         if (view == null) {
93             if (DEBUG) Log.d(TAG, "No preview available for shortcutId: " + shortcutId);
94             return null;
95         }
96         final Bundle bundle = new Bundle();
97         bundle.putParcelable(PeopleProviderUtils.RESPONSE_KEY_REMOTE_VIEWS, view);
98         return bundle;
99     }
100 
doesCallerHavePermission()101     private boolean doesCallerHavePermission() {
102         return getContext().checkPermission(
103                 PeopleProviderUtils.GET_PEOPLE_TILE_PREVIEW_PERMISSION,
104                 Binder.getCallingPid(), Binder.getCallingUid())
105                 == PackageManager.PERMISSION_GRANTED;
106     }
107 
108     @Override
onCreate()109     public boolean onCreate() {
110         return true;
111     }
112 
113     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)114     public Cursor query(Uri uri, String[] projection, String selection,
115             String[] selectionArgs, String sortOrder) {
116         throw new IllegalArgumentException("Invalid method");
117     }
118 
119     @Override
getType(Uri uri)120     public String getType(Uri uri) {
121         throw new IllegalArgumentException("Invalid method");
122     }
123 
124     @Override
insert(Uri uri, ContentValues initialValues)125     public Uri insert(Uri uri, ContentValues initialValues) {
126         throw new IllegalArgumentException("Invalid method");
127     }
128 
129     @Override
delete(Uri uri, String selection, String[] selectionArgs)130     public int delete(Uri uri, String selection, String[] selectionArgs) {
131         throw new IllegalArgumentException("Invalid method");
132     }
133 
134     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)135     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
136         throw new IllegalArgumentException("Invalid method");
137     }
138 
139     @Override
attachInfo(Context context, ProviderInfo info)140     public void attachInfo(Context context, ProviderInfo info) {
141         mCallback.onContextAvailable(context);
142         super.attachInfo(context, info);
143     }
144 
145     @Override
setContextAvailableCallback( SystemUIAppComponentFactory.ContextAvailableCallback callback)146     public void setContextAvailableCallback(
147             SystemUIAppComponentFactory.ContextAvailableCallback callback) {
148         mCallback = callback;
149     }
150 }
151 
152