blob: 9e4e97a011faf572a28399b500914f8377830a11 [file] [log] [blame]
Jason Monkd9edfa942017-09-25 12:38:53 -04001/*
2 * Copyright (C) 2017 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 */
Jason Monkd18651f2017-10-05 14:18:49 -040016package android.app.slice;
Jason Monkd9edfa942017-09-25 12:38:53 -040017
Mady Mellor3b0a72f2017-10-19 10:12:09 -070018import android.annotation.NonNull;
Jason Monke8f8be72018-01-21 10:10:35 -050019import android.app.PendingIntent;
20import android.content.ComponentName;
Jason Monkd9edfa942017-09-25 12:38:53 -040021import android.content.ContentProvider;
22import android.content.ContentResolver;
23import android.content.ContentValues;
Jason Monke8f8be72018-01-21 10:10:35 -050024import android.content.Context;
Jason Monkb40dad52017-11-01 16:01:40 -040025import android.content.Intent;
Mady Mellor3b0a72f2017-10-19 10:12:09 -070026import android.content.IntentFilter;
Jason Monke8f8be72018-01-21 10:10:35 -050027import android.content.pm.PackageManager;
28import android.content.pm.PackageManager.NameNotFoundException;
29import android.content.pm.ProviderInfo;
Jason Monkd9edfa942017-09-25 12:38:53 -040030import android.database.ContentObserver;
31import android.database.Cursor;
32import android.net.Uri;
Jason Monkb40dad52017-11-01 16:01:40 -040033import android.os.Binder;
Jason Monkd9edfa942017-09-25 12:38:53 -040034import android.os.Bundle;
35import android.os.CancellationSignal;
36import android.os.Handler;
Jason Monkb40dad52017-11-01 16:01:40 -040037import android.os.Process;
Jason Monkd18651f2017-10-05 14:18:49 -040038import android.os.StrictMode;
39import android.os.StrictMode.ThreadPolicy;
Jason Monk199286b2018-04-12 19:47:50 -040040import android.util.ArraySet;
Jason Monkd9edfa942017-09-25 12:38:53 -040041import android.util.Log;
42
Jason Monk5f8cc272018-01-16 17:57:20 -050043import java.util.ArrayList;
Jason Monk106387f2018-03-06 16:32:28 -050044import java.util.Arrays;
Jason Monk5f8cc272018-01-16 17:57:20 -050045import java.util.Collection;
46import java.util.Collections;
Jason Monk2af19982017-11-07 19:38:27 -050047import java.util.List;
Jason Monk2d3932e2018-03-08 11:31:26 -050048import java.util.Set;
Jason Monkd9edfa942017-09-25 12:38:53 -040049
50/**
Mady Mellor3b0a72f2017-10-19 10:12:09 -070051 * A SliceProvider allows an app to provide content to be displayed in system spaces. This content
52 * is templated and can contain actions, and the behavior of how it is surfaced is specific to the
53 * system surface.
54 * <p>
55 * Slices are not currently live content. They are bound once and shown to the user. If the content
56 * changes due to a callback from user interaction, then
57 * {@link ContentResolver#notifyChange(Uri, ContentObserver)} should be used to notify the system.
58 * </p>
59 * <p>
60 * The provider needs to be declared in the manifest to provide the authority for the app. The
61 * authority for most slices is expected to match the package of the application.
62 * </p>
Jason Monkd9edfa942017-09-25 12:38:53 -040063 *
Jason Monkd9edfa942017-09-25 12:38:53 -040064 * <pre class="prettyprint">
65 * {@literal
66 * <provider
Jason Monkad594512018-03-08 11:35:55 -050067 * android:name="com.example.mypkg.MySliceProvider"
68 * android:authorities="com.example.mypkg" />}
Jason Monkd9edfa942017-09-25 12:38:53 -040069 * </pre>
Mady Mellor3b0a72f2017-10-19 10:12:09 -070070 * <p>
71 * Slices can be identified by a Uri or by an Intent. To link an Intent with a slice, the provider
72 * must have an {@link IntentFilter} matching the slice intent. When a slice is being requested via
73 * an intent, {@link #onMapIntentToUri(Intent)} can be called and is expected to return an
74 * appropriate Uri representing the slice.
75 *
76 * <pre class="prettyprint">
77 * {@literal
78 * <provider
Jason Monkad594512018-03-08 11:35:55 -050079 * android:name="com.example.mypkg.MySliceProvider"
80 * android:authorities="com.example.mypkg">
Mady Mellor3b0a72f2017-10-19 10:12:09 -070081 * <intent-filter>
Jason Monkad594512018-03-08 11:35:55 -050082 * <action android:name="com.example.mypkg.intent.action.MY_SLICE_INTENT" />
Jason Monk5e676a22018-03-08 14:18:55 -050083 * <category android:name="android.app.slice.category.SLICE" />
Mady Mellor3b0a72f2017-10-19 10:12:09 -070084 * </intent-filter>
85 * </provider>}
86 * </pre>
Jason Monkd9edfa942017-09-25 12:38:53 -040087 *
88 * @see Slice
Jason Monkd9edfa942017-09-25 12:38:53 -040089 */
90public abstract class SliceProvider extends ContentProvider {
Jason Monkd18651f2017-10-05 14:18:49 -040091 /**
Jason Monka9b3d732018-01-22 15:37:00 -050092 * This is the Android platform's MIME type for a URI
Jason Monkd18651f2017-10-05 14:18:49 -040093 * containing a slice implemented through {@link SliceProvider}.
94 */
95 public static final String SLICE_TYPE = "vnd.android.slice";
96
Jason Monkd9edfa942017-09-25 12:38:53 -040097 private static final String TAG = "SliceProvider";
98 /**
99 * @hide
100 */
101 public static final String EXTRA_BIND_URI = "slice_uri";
102 /**
103 * @hide
104 */
Jason Monk2af19982017-11-07 19:38:27 -0500105 public static final String EXTRA_SUPPORTED_SPECS = "supported_specs";
106 /**
107 * @hide
108 */
Jason Monkd9edfa942017-09-25 12:38:53 -0400109 public static final String METHOD_SLICE = "bind_slice";
110 /**
111 * @hide
112 */
Mady Mellor3b0a72f2017-10-19 10:12:09 -0700113 public static final String METHOD_MAP_INTENT = "map_slice";
114 /**
115 * @hide
116 */
Jason Monk7b8fef22018-01-30 16:04:14 -0500117 public static final String METHOD_MAP_ONLY_INTENT = "map_only";
118 /**
119 * @hide
120 */
Jason Monk74f5e362017-12-06 08:56:33 -0500121 public static final String METHOD_PIN = "pin";
122 /**
123 * @hide
124 */
125 public static final String METHOD_UNPIN = "unpin";
126 /**
127 * @hide
128 */
Jason Monk5f8cc272018-01-16 17:57:20 -0500129 public static final String METHOD_GET_DESCENDANTS = "get_descendants";
130 /**
131 * @hide
132 */
Mady Mellor3b0a72f2017-10-19 10:12:09 -0700133 public static final String EXTRA_INTENT = "slice_intent";
134 /**
135 * @hide
136 */
Jason Monkd9edfa942017-09-25 12:38:53 -0400137 public static final String EXTRA_SLICE = "slice";
Jason Monk5f8cc272018-01-16 17:57:20 -0500138 /**
139 * @hide
140 */
141 public static final String EXTRA_SLICE_DESCENDANTS = "slice_descendants";
Jason Monke8f8be72018-01-21 10:10:35 -0500142 /**
143 * @hide
144 */
145 public static final String EXTRA_PKG = "pkg";
146 /**
147 * @hide
148 */
149 public static final String EXTRA_PROVIDER_PKG = "provider_pkg";
Jason Monkd9edfa942017-09-25 12:38:53 -0400150
151 private static final boolean DEBUG = false;
152
Jason Monk66cffd52018-03-12 16:42:48 -0400153 private static final long SLICE_BIND_ANR = 2000;
Jason Monk42e03f82018-03-30 11:26:56 -0400154 private final String[] mAutoGrantPermissions;
Jason Monke8f8be72018-01-21 10:10:35 -0500155
Jason Monk66cffd52018-03-12 16:42:48 -0400156 private String mCallback;
157 private SliceManager mSliceManager;
Jason Monke8f8be72018-01-21 10:10:35 -0500158
Jason Monk42e03f82018-03-30 11:26:56 -0400159 /**
160 * A version of constructing a SliceProvider that allows autogranting slice permissions
161 * to apps that hold specific platform permissions.
162 * <p>
163 * When an app tries to bind a slice from this provider that it does not have access to,
164 * This provider will check if the caller holds permissions to any of the autoGrantPermissions
165 * specified, if they do they will be granted persisted uri access to all slices of this
166 * provider.
167 *
168 * @param autoGrantPermissions List of permissions that holders are auto-granted access
169 * to slices.
170 */
171 public SliceProvider(@NonNull String... autoGrantPermissions) {
172 mAutoGrantPermissions = autoGrantPermissions;
173 }
174
175 public SliceProvider() {
176 mAutoGrantPermissions = new String[0];
177 }
178
Jason Monke8f8be72018-01-21 10:10:35 -0500179 @Override
180 public void attachInfo(Context context, ProviderInfo info) {
181 super.attachInfo(context, info);
182 mSliceManager = context.getSystemService(SliceManager.class);
183 }
184
Jason Monkd9edfa942017-09-25 12:38:53 -0400185 /**
Jason Monk66cffd52018-03-12 16:42:48 -0400186 * Implemented to create a slice.
Jason Monkd18651f2017-10-05 14:18:49 -0400187 * <p>
188 * onBindSlice should return as quickly as possible so that the UI tied
189 * to this slice can be responsive. No network or other IO will be allowed
190 * during onBindSlice. Any loading that needs to be done should happen
Jason Monk66cffd52018-03-12 16:42:48 -0400191 * in the background with a call to {@link ContentResolver#notifyChange(Uri, ContentObserver)}
Jason Monkd18651f2017-10-05 14:18:49 -0400192 * when the app is ready to provide the complete data in onBindSlice.
193 * <p>
Jason Monk2af19982017-11-07 19:38:27 -0500194 * The slice returned should have a spec that is compatible with one of
195 * the supported specs.
Jason Monkd18651f2017-10-05 14:18:49 -0400196 *
Jason Monk2af19982017-11-07 19:38:27 -0500197 * @param sliceUri Uri to bind.
198 * @param supportedSpecs List of supported specs.
Jason Monkd9edfa942017-09-25 12:38:53 -0400199 * @see {@link Slice}.
Jason Monkd18651f2017-10-05 14:18:49 -0400200 * @see {@link Slice#HINT_PARTIAL}
Jason Monkd9edfa942017-09-25 12:38:53 -0400201 */
Jason Monk2d3932e2018-03-08 11:31:26 -0500202 public Slice onBindSlice(Uri sliceUri, Set<SliceSpec> supportedSpecs) {
203 return onBindSlice(sliceUri, new ArrayList<>(supportedSpecs));
204 }
205
206 /**
207 * @deprecated TO BE REMOVED
Jeff Sharkey3990ee12018-04-11 10:19:55 -0600208 * @removed
Jason Monk2d3932e2018-03-08 11:31:26 -0500209 */
210 @Deprecated
Jason Monk2af19982017-11-07 19:38:27 -0500211 public Slice onBindSlice(Uri sliceUri, List<SliceSpec> supportedSpecs) {
Jason Monk2af19982017-11-07 19:38:27 -0500212 return null;
213 }
Jason Monkd9edfa942017-09-25 12:38:53 -0400214
Mady Mellor3b0a72f2017-10-19 10:12:09 -0700215 /**
Jason Monke2c64512017-12-11 15:14:54 -0500216 * Called to inform an app that a slice has been pinned.
217 * <p>
218 * Pinning is a way that slice hosts use to notify apps of which slices
219 * they care about updates for. When a slice is pinned the content is
220 * expected to be relatively fresh and kept up to date.
221 * <p>
222 * Being pinned does not provide any escalated privileges for the slice
223 * provider. So apps should do things such as turn on syncing or schedule
224 * a job in response to a onSlicePinned.
225 * <p>
226 * Pinned state is not persisted through a reboot, and apps can expect a
227 * new call to onSlicePinned for any slices that should remain pinned
228 * after a reboot occurs.
229 *
230 * @param sliceUri The uri of the slice being unpinned.
231 * @see #onSliceUnpinned(Uri)
Jason Monk74f5e362017-12-06 08:56:33 -0500232 */
233 public void onSlicePinned(Uri sliceUri) {
234 }
235
236 /**
Jason Monke2c64512017-12-11 15:14:54 -0500237 * Called to inform an app that a slices is no longer pinned.
238 * <p>
239 * This means that no other apps on the device care about updates to this
240 * slice anymore and therefore it is not important to be updated. Any syncs
241 * or jobs related to this slice should be cancelled.
242 * @see #onSlicePinned(Uri)
Jason Monk74f5e362017-12-06 08:56:33 -0500243 */
244 public void onSliceUnpinned(Uri sliceUri) {
245 }
246
247 /**
Jason Monk5f8cc272018-01-16 17:57:20 -0500248 * Obtains a list of slices that are descendants of the specified Uri.
249 * <p>
250 * Implementing this is optional for a SliceProvider, but does provide a good
251 * discovery mechanism for finding slice Uris.
252 *
253 * @param uri The uri to look for descendants under.
254 * @return All slices within the space.
255 * @see SliceManager#getSliceDescendants(Uri)
256 */
257 public @NonNull Collection<Uri> onGetSliceDescendants(@NonNull Uri uri) {
258 return Collections.emptyList();
259 }
260
261 /**
Mady Mellor3b0a72f2017-10-19 10:12:09 -0700262 * This method must be overridden if an {@link IntentFilter} is specified on the SliceProvider.
263 * In that case, this method can be called and is expected to return a non-null Uri representing
264 * a slice. Otherwise this will throw {@link UnsupportedOperationException}.
265 *
Jason Monk5e676a22018-03-08 14:18:55 -0500266 * Any intent filter added to a slice provider should also contain
267 * {@link SliceManager#CATEGORY_SLICE}, because otherwise it will not be detected by
268 * {@link SliceManager#mapIntentToUri(Intent)}.
269 *
Mady Mellor3b0a72f2017-10-19 10:12:09 -0700270 * @return Uri representing the slice associated with the provided intent.
Jason Monk5e676a22018-03-08 14:18:55 -0500271 * @see Slice
272 * @see SliceManager#mapIntentToUri(Intent)
Mady Mellor3b0a72f2017-10-19 10:12:09 -0700273 */
274 public @NonNull Uri onMapIntentToUri(Intent intent) {
275 throw new UnsupportedOperationException(
276 "This provider has not implemented intent to uri mapping");
277 }
278
Jason Monkac112382018-03-23 15:06:35 -0400279 /**
280 * Called when an app requests a slice it does not have write permission
281 * to the uri for.
282 * <p>
283 * The return value will be the action on a slice that prompts the user that
284 * the calling app wants to show slices from this app. The default implementation
285 * launches a dialog that allows the user to grant access to this slice. Apps
286 * that do not want to allow this user grant, can override this and instead
287 * launch their own dialog with different behavior.
288 *
289 * @param sliceUri the Uri of the slice attempting to be bound.
290 * @see #getCallingPackage()
291 */
292 public @NonNull PendingIntent onCreatePermissionRequest(Uri sliceUri) {
293 return createPermissionIntent(getContext(), sliceUri, getCallingPackage());
294 }
295
Jason Monkd9edfa942017-09-25 12:38:53 -0400296 @Override
297 public final int update(Uri uri, ContentValues values, String selection,
298 String[] selectionArgs) {
299 if (DEBUG) Log.d(TAG, "update " + uri);
300 return 0;
301 }
302
303 @Override
304 public final int delete(Uri uri, String selection, String[] selectionArgs) {
305 if (DEBUG) Log.d(TAG, "delete " + uri);
306 return 0;
307 }
308
309 @Override
310 public final Cursor query(Uri uri, String[] projection, String selection,
311 String[] selectionArgs, String sortOrder) {
312 if (DEBUG) Log.d(TAG, "query " + uri);
313 return null;
314 }
315
316 @Override
317 public final Cursor query(Uri uri, String[] projection, String selection, String[]
318 selectionArgs, String sortOrder, CancellationSignal cancellationSignal) {
319 if (DEBUG) Log.d(TAG, "query " + uri);
320 return null;
321 }
322
323 @Override
324 public final Cursor query(Uri uri, String[] projection, Bundle queryArgs,
325 CancellationSignal cancellationSignal) {
326 if (DEBUG) Log.d(TAG, "query " + uri);
327 return null;
328 }
329
330 @Override
331 public final Uri insert(Uri uri, ContentValues values) {
332 if (DEBUG) Log.d(TAG, "insert " + uri);
333 return null;
334 }
335
336 @Override
337 public final String getType(Uri uri) {
338 if (DEBUG) Log.d(TAG, "getType " + uri);
Jason Monkd18651f2017-10-05 14:18:49 -0400339 return SLICE_TYPE;
Jason Monkd9edfa942017-09-25 12:38:53 -0400340 }
341
342 @Override
Jason Monkd18651f2017-10-05 14:18:49 -0400343 public Bundle call(String method, String arg, Bundle extras) {
Jason Monkd9edfa942017-09-25 12:38:53 -0400344 if (method.equals(METHOD_SLICE)) {
Jason Monk74848ae2018-01-21 17:11:57 -0500345 Uri uri = getUriWithoutUserId(extras.getParcelable(EXTRA_BIND_URI));
Jason Monk2af19982017-11-07 19:38:27 -0500346 List<SliceSpec> supportedSpecs = extras.getParcelableArrayList(EXTRA_SUPPORTED_SPECS);
Jason Monkd9edfa942017-09-25 12:38:53 -0400347
Jason Monke8f8be72018-01-21 10:10:35 -0500348 String callingPackage = getCallingPackage();
Jason Monk3a1d2e972018-01-29 16:58:11 -0500349 int callingUid = Binder.getCallingUid();
350 int callingPid = Binder.getCallingPid();
Jason Monkac112382018-03-23 15:06:35 -0400351
Jason Monk3a1d2e972018-01-29 16:58:11 -0500352 Slice s = handleBindSlice(uri, supportedSpecs, callingPackage, callingUid, callingPid);
Jason Monkd9edfa942017-09-25 12:38:53 -0400353 Bundle b = new Bundle();
354 b.putParcelable(EXTRA_SLICE, s);
355 return b;
Mady Mellor3b0a72f2017-10-19 10:12:09 -0700356 } else if (method.equals(METHOD_MAP_INTENT)) {
Mady Mellor3b0a72f2017-10-19 10:12:09 -0700357 Intent intent = extras.getParcelable(EXTRA_INTENT);
Jason Monk74f5e362017-12-06 08:56:33 -0500358 if (intent == null) return null;
Mady Mellor3b0a72f2017-10-19 10:12:09 -0700359 Uri uri = onMapIntentToUri(intent);
Jason Monk2af19982017-11-07 19:38:27 -0500360 List<SliceSpec> supportedSpecs = extras.getParcelableArrayList(EXTRA_SUPPORTED_SPECS);
Mady Mellor3b0a72f2017-10-19 10:12:09 -0700361 Bundle b = new Bundle();
362 if (uri != null) {
Jason Monk3a1d2e972018-01-29 16:58:11 -0500363 Slice s = handleBindSlice(uri, supportedSpecs, getCallingPackage(),
364 Binder.getCallingUid(), Binder.getCallingPid());
Mady Mellor3b0a72f2017-10-19 10:12:09 -0700365 b.putParcelable(EXTRA_SLICE, s);
366 } else {
367 b.putParcelable(EXTRA_SLICE, null);
368 }
369 return b;
Jason Monk7b8fef22018-01-30 16:04:14 -0500370 } else if (method.equals(METHOD_MAP_ONLY_INTENT)) {
371 Intent intent = extras.getParcelable(EXTRA_INTENT);
372 if (intent == null) return null;
373 Uri uri = onMapIntentToUri(intent);
374 Bundle b = new Bundle();
375 b.putParcelable(EXTRA_SLICE, uri);
376 return b;
Jason Monk74f5e362017-12-06 08:56:33 -0500377 } else if (method.equals(METHOD_PIN)) {
Jason Monk74848ae2018-01-21 17:11:57 -0500378 Uri uri = getUriWithoutUserId(extras.getParcelable(EXTRA_BIND_URI));
Jason Monke8f8be72018-01-21 10:10:35 -0500379 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
380 throw new SecurityException("Only the system can pin/unpin slices");
Jason Monk74f5e362017-12-06 08:56:33 -0500381 }
382 handlePinSlice(uri);
383 } else if (method.equals(METHOD_UNPIN)) {
Jason Monk74848ae2018-01-21 17:11:57 -0500384 Uri uri = getUriWithoutUserId(extras.getParcelable(EXTRA_BIND_URI));
Jason Monke8f8be72018-01-21 10:10:35 -0500385 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
386 throw new SecurityException("Only the system can pin/unpin slices");
Jason Monk74f5e362017-12-06 08:56:33 -0500387 }
388 handleUnpinSlice(uri);
Jason Monk5f8cc272018-01-16 17:57:20 -0500389 } else if (method.equals(METHOD_GET_DESCENDANTS)) {
Jason Monk74848ae2018-01-21 17:11:57 -0500390 Uri uri = getUriWithoutUserId(extras.getParcelable(EXTRA_BIND_URI));
Jason Monk5f8cc272018-01-16 17:57:20 -0500391 Bundle b = new Bundle();
392 b.putParcelableArrayList(EXTRA_SLICE_DESCENDANTS,
393 new ArrayList<>(handleGetDescendants(uri)));
394 return b;
Jason Monkd9edfa942017-09-25 12:38:53 -0400395 }
396 return super.call(method, arg, extras);
397 }
398
Jason Monk5f8cc272018-01-16 17:57:20 -0500399 private Collection<Uri> handleGetDescendants(Uri uri) {
Jason Monk66cffd52018-03-12 16:42:48 -0400400 mCallback = "onGetSliceDescendants";
401 Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR);
402 try {
Jason Monk5f8cc272018-01-16 17:57:20 -0500403 return onGetSliceDescendants(uri);
Jason Monk66cffd52018-03-12 16:42:48 -0400404 } finally {
405 Handler.getMain().removeCallbacks(mAnr);
Jason Monk5f8cc272018-01-16 17:57:20 -0500406 }
407 }
408
Jason Monk74f5e362017-12-06 08:56:33 -0500409 private void handlePinSlice(Uri sliceUri) {
Jason Monk66cffd52018-03-12 16:42:48 -0400410 mCallback = "onSlicePinned";
411 Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR);
412 try {
Jason Monk74f5e362017-12-06 08:56:33 -0500413 onSlicePinned(sliceUri);
Jason Monk66cffd52018-03-12 16:42:48 -0400414 } finally {
415 Handler.getMain().removeCallbacks(mAnr);
Jason Monk74f5e362017-12-06 08:56:33 -0500416 }
417 }
418
419 private void handleUnpinSlice(Uri sliceUri) {
Jason Monk66cffd52018-03-12 16:42:48 -0400420 mCallback = "onSliceUnpinned";
421 Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR);
422 try {
Jason Monk74f5e362017-12-06 08:56:33 -0500423 onSliceUnpinned(sliceUri);
Jason Monk66cffd52018-03-12 16:42:48 -0400424 } finally {
425 Handler.getMain().removeCallbacks(mAnr);
Jason Monk74f5e362017-12-06 08:56:33 -0500426 }
427 }
428
Jason Monke8f8be72018-01-21 10:10:35 -0500429 private Slice handleBindSlice(Uri sliceUri, List<SliceSpec> supportedSpecs,
Jason Monk3a1d2e972018-01-29 16:58:11 -0500430 String callingPkg, int callingUid, int callingPid) {
Jason Monke8f8be72018-01-21 10:10:35 -0500431 // This can be removed once Slice#bindSlice is removed and everyone is using
432 // SliceManager#bindSlice.
433 String pkg = callingPkg != null ? callingPkg
Jason Monk3a1d2e972018-01-29 16:58:11 -0500434 : getContext().getPackageManager().getNameForUid(callingUid);
Jason Monkac112382018-03-23 15:06:35 -0400435 try {
436 mSliceManager.enforceSlicePermission(sliceUri, pkg,
Jason Monk42e03f82018-03-30 11:26:56 -0400437 callingPid, callingUid, mAutoGrantPermissions);
Jason Monkac112382018-03-23 15:06:35 -0400438 } catch (SecurityException e) {
439 return createPermissionSlice(getContext(), sliceUri, pkg);
Jason Monke8f8be72018-01-21 10:10:35 -0500440 }
Jason Monk66cffd52018-03-12 16:42:48 -0400441 mCallback = "onBindSlice";
442 Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR);
443 try {
444 return onBindSliceStrict(sliceUri, supportedSpecs);
445 } finally {
446 Handler.getMain().removeCallbacks(mAnr);
Lucas Dupine0653fa2017-10-23 14:31:45 -0700447 }
448 }
449
Jason Monke8f8be72018-01-21 10:10:35 -0500450 /**
451 * @hide
452 */
Jason Monkac112382018-03-23 15:06:35 -0400453 public Slice createPermissionSlice(Context context, Uri sliceUri,
Jason Monke8f8be72018-01-21 10:10:35 -0500454 String callingPackage) {
Jason Monkac112382018-03-23 15:06:35 -0400455 PendingIntent action;
456 mCallback = "onCreatePermissionRequest";
457 Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR);
458 try {
459 action = onCreatePermissionRequest(sliceUri);
460 } finally {
461 Handler.getMain().removeCallbacks(mAnr);
462 }
Mady Mellor33c5a842018-03-19 16:23:15 -0700463 Slice.Builder parent = new Slice.Builder(sliceUri);
464 Slice.Builder childAction = new Slice.Builder(parent)
465 .addHints(Arrays.asList(Slice.HINT_TITLE, Slice.HINT_SHORTCUT))
466 .addAction(action, new Slice.Builder(parent).build(), null);
467
468 parent.addSubSlice(new Slice.Builder(sliceUri.buildUpon().appendPath("permission").build())
469 .addText(getPermissionString(context, callingPackage), null,
470 Collections.emptyList())
471 .addSubSlice(childAction.build(), null)
472 .build(), null);
473 return parent.addHints(Arrays.asList(Slice.HINT_PERMISSION_REQUEST)).build();
Jason Monke8f8be72018-01-21 10:10:35 -0500474 }
475
476 /**
477 * @hide
478 */
479 public static PendingIntent createPermissionIntent(Context context, Uri sliceUri,
480 String callingPackage) {
481 Intent intent = new Intent(SliceManager.ACTION_REQUEST_SLICE_PERMISSION);
482 intent.setComponent(new ComponentName("com.android.systemui",
483 "com.android.systemui.SlicePermissionActivity"));
484 intent.putExtra(EXTRA_BIND_URI, sliceUri);
485 intent.putExtra(EXTRA_PKG, callingPackage);
486 intent.putExtra(EXTRA_PROVIDER_PKG, context.getPackageName());
487 // Unique pending intent.
488 intent.setData(sliceUri.buildUpon().appendQueryParameter("package", callingPackage)
489 .build());
490
491 return PendingIntent.getActivity(context, 0, intent, 0);
492 }
493
494 /**
495 * @hide
496 */
497 public static CharSequence getPermissionString(Context context, String callingPackage) {
498 PackageManager pm = context.getPackageManager();
499 try {
500 return context.getString(
501 com.android.internal.R.string.slices_permission_request,
502 pm.getApplicationInfo(callingPackage, 0).loadLabel(pm),
503 context.getApplicationInfo().loadLabel(pm));
504 } catch (NameNotFoundException e) {
505 // This shouldn't be possible since the caller is verified.
506 throw new RuntimeException("Unknown calling app", e);
507 }
508 }
509
Jason Monk66cffd52018-03-12 16:42:48 -0400510 private Slice onBindSliceStrict(Uri sliceUri, List<SliceSpec> supportedSpecs) {
Lucas Dupine0653fa2017-10-23 14:31:45 -0700511 ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
Jason Monkd9edfa942017-09-25 12:38:53 -0400512 try {
Lucas Dupine0653fa2017-10-23 14:31:45 -0700513 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
514 .detectAll()
515 .penaltyDeath()
516 .build());
Jason Monk199286b2018-04-12 19:47:50 -0400517 return onBindSlice(sliceUri, new ArraySet<>(supportedSpecs));
Lucas Dupine0653fa2017-10-23 14:31:45 -0700518 } finally {
519 StrictMode.setThreadPolicy(oldPolicy);
Jason Monkd9edfa942017-09-25 12:38:53 -0400520 }
521 }
Jason Monk66cffd52018-03-12 16:42:48 -0400522
523 private final Runnable mAnr = () -> {
524 Process.sendSignal(Process.myPid(), Process.SIGNAL_QUIT);
525 Log.wtf(TAG, "Timed out while handling slice callback " + mCallback);
526 };
Jason Monkd9edfa942017-09-25 12:38:53 -0400527}