1 /* 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 */ 16 package android.app.slice; 17 18 import static android.app.slice.Slice.SUBTYPE_COLOR; 19 20 import android.annotation.NonNull; 21 import android.app.PendingIntent; 22 import android.content.ComponentName; 23 import android.content.ContentProvider; 24 import android.content.ContentResolver; 25 import android.content.ContentValues; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.IntentFilter; 29 import android.content.pm.PackageManager; 30 import android.content.pm.PackageManager.NameNotFoundException; 31 import android.content.pm.ProviderInfo; 32 import android.database.ContentObserver; 33 import android.database.Cursor; 34 import android.graphics.drawable.Icon; 35 import android.net.Uri; 36 import android.os.Binder; 37 import android.os.Bundle; 38 import android.os.CancellationSignal; 39 import android.os.Handler; 40 import android.os.Process; 41 import android.os.StrictMode; 42 import android.os.StrictMode.ThreadPolicy; 43 import android.util.ArraySet; 44 import android.util.Log; 45 import android.util.TypedValue; 46 import android.view.ContextThemeWrapper; 47 48 import java.util.ArrayList; 49 import java.util.Arrays; 50 import java.util.Collection; 51 import java.util.Collections; 52 import java.util.List; 53 import java.util.Set; 54 55 /** 56 * A SliceProvider allows an app to provide content to be displayed in system spaces. This content 57 * is templated and can contain actions, and the behavior of how it is surfaced is specific to the 58 * system surface. 59 * <p> 60 * Slices are not currently live content. They are bound once and shown to the user. If the content 61 * changes due to a callback from user interaction, then 62 * {@link ContentResolver#notifyChange(Uri, ContentObserver)} should be used to notify the system. 63 * </p> 64 * <p> 65 * The provider needs to be declared in the manifest to provide the authority for the app. The 66 * authority for most slices is expected to match the package of the application. 67 * </p> 68 * 69 * <pre class="prettyprint"> 70 * {@literal 71 * <provider 72 * android:name="com.example.mypkg.MySliceProvider" 73 * android:authorities="com.example.mypkg" />} 74 * </pre> 75 * <p> 76 * Slices can be identified by a Uri or by an Intent. To link an Intent with a slice, the provider 77 * must have an {@link IntentFilter} matching the slice intent. When a slice is being requested via 78 * an intent, {@link #onMapIntentToUri(Intent)} can be called and is expected to return an 79 * appropriate Uri representing the slice. 80 * 81 * <pre class="prettyprint"> 82 * {@literal 83 * <provider 84 * android:name="com.example.mypkg.MySliceProvider" 85 * android:authorities="com.example.mypkg"> 86 * <intent-filter> 87 * <action android:name="com.example.mypkg.intent.action.MY_SLICE_INTENT" /> 88 * <category android:name="android.app.slice.category.SLICE" /> 89 * </intent-filter> 90 * </provider>} 91 * </pre> 92 * 93 * @see Slice 94 * @deprecated Slice framework has been deprecated, it will not receive any updates from 95 * {@link android.os.Build.VANILLA_ICE_CREAM} and forward. If you are looking for a 96 * framework that sends displayable data from one app to another, consider using 97 * {@link android.app.appsearch.AppSearchManager}. 98 */ 99 @Deprecated 100 public abstract class SliceProvider extends ContentProvider { 101 /** 102 * This is the Android platform's MIME type for a URI 103 * containing a slice implemented through {@link SliceProvider}. 104 */ 105 public static final String SLICE_TYPE = "vnd.android.slice"; 106 107 private static final String TAG = "SliceProvider"; 108 /** 109 * @hide 110 */ 111 public static final String EXTRA_BIND_URI = "slice_uri"; 112 /** 113 * @hide 114 */ 115 public static final String EXTRA_SUPPORTED_SPECS = "supported_specs"; 116 /** 117 * @hide 118 */ 119 public static final String METHOD_SLICE = "bind_slice"; 120 /** 121 * @hide 122 */ 123 public static final String METHOD_MAP_INTENT = "map_slice"; 124 /** 125 * @hide 126 */ 127 public static final String METHOD_MAP_ONLY_INTENT = "map_only"; 128 /** 129 * @hide 130 */ 131 public static final String METHOD_PIN = "pin"; 132 /** 133 * @hide 134 */ 135 public static final String METHOD_UNPIN = "unpin"; 136 /** 137 * @hide 138 */ 139 public static final String METHOD_GET_DESCENDANTS = "get_descendants"; 140 /** 141 * @hide 142 */ 143 public static final String METHOD_GET_PERMISSIONS = "get_permissions"; 144 /** 145 * @hide 146 */ 147 public static final String EXTRA_INTENT = "slice_intent"; 148 /** 149 * @hide 150 */ 151 public static final String EXTRA_SLICE = "slice"; 152 /** 153 * @hide 154 */ 155 public static final String EXTRA_SLICE_DESCENDANTS = "slice_descendants"; 156 /** 157 * @hide 158 */ 159 public static final String EXTRA_PKG = "pkg"; 160 /** 161 * @hide 162 */ 163 public static final String EXTRA_RESULT = "result"; 164 165 private static final boolean DEBUG = false; 166 167 private static final long SLICE_BIND_ANR = 2000; 168 private final String[] mAutoGrantPermissions; 169 170 private String mCallback; 171 private SliceManager mSliceManager; 172 173 /** 174 * A version of constructing a SliceProvider that allows autogranting slice permissions 175 * to apps that hold specific platform permissions. 176 * <p> 177 * When an app tries to bind a slice from this provider that it does not have access to, 178 * This provider will check if the caller holds permissions to any of the autoGrantPermissions 179 * specified, if they do they will be granted persisted uri access to all slices of this 180 * provider. 181 * 182 * @param autoGrantPermissions List of permissions that holders are auto-granted access 183 * to slices. 184 */ SliceProvider(@onNull String... autoGrantPermissions)185 public SliceProvider(@NonNull String... autoGrantPermissions) { 186 mAutoGrantPermissions = autoGrantPermissions; 187 } 188 SliceProvider()189 public SliceProvider() { 190 mAutoGrantPermissions = new String[0]; 191 } 192 193 @Override attachInfo(Context context, ProviderInfo info)194 public void attachInfo(Context context, ProviderInfo info) { 195 super.attachInfo(context, info); 196 mSliceManager = context.getSystemService(SliceManager.class); 197 } 198 199 /** 200 * Implemented to create a slice. 201 * <p> 202 * onBindSlice should return as quickly as possible so that the UI tied 203 * to this slice can be responsive. No network or other IO will be allowed 204 * during onBindSlice. Any loading that needs to be done should happen 205 * in the background with a call to {@link ContentResolver#notifyChange(Uri, ContentObserver)} 206 * when the app is ready to provide the complete data in onBindSlice. 207 * <p> 208 * The slice returned should have a spec that is compatible with one of 209 * the supported specs. 210 * 211 * @param sliceUri Uri to bind. 212 * @param supportedSpecs List of supported specs. 213 * @see Slice 214 * @see Slice#HINT_PARTIAL 215 */ onBindSlice(Uri sliceUri, Set<SliceSpec> supportedSpecs)216 public Slice onBindSlice(Uri sliceUri, Set<SliceSpec> supportedSpecs) { 217 return null; 218 } 219 220 /** 221 * Called to inform an app that a slice has been pinned. 222 * <p> 223 * Pinning is a way that slice hosts use to notify apps of which slices 224 * they care about updates for. When a slice is pinned the content is 225 * expected to be relatively fresh and kept up to date. 226 * <p> 227 * Being pinned does not provide any escalated privileges for the slice 228 * provider. So apps should do things such as turn on syncing or schedule 229 * a job in response to a onSlicePinned. 230 * <p> 231 * Pinned state is not persisted through a reboot, and apps can expect a 232 * new call to onSlicePinned for any slices that should remain pinned 233 * after a reboot occurs. 234 * 235 * @param sliceUri The uri of the slice being unpinned. 236 * @see #onSliceUnpinned(Uri) 237 */ onSlicePinned(Uri sliceUri)238 public void onSlicePinned(Uri sliceUri) { 239 } 240 241 /** 242 * Called to inform an app that a slices is no longer pinned. 243 * <p> 244 * This means that no other apps on the device care about updates to this 245 * slice anymore and therefore it is not important to be updated. Any syncs 246 * or jobs related to this slice should be cancelled. 247 * @see #onSlicePinned(Uri) 248 */ onSliceUnpinned(Uri sliceUri)249 public void onSliceUnpinned(Uri sliceUri) { 250 } 251 252 /** 253 * Obtains a list of slices that are descendants of the specified Uri. 254 * <p> 255 * Implementing this is optional for a SliceProvider, but does provide a good 256 * discovery mechanism for finding slice Uris. 257 * 258 * @param uri The uri to look for descendants under. 259 * @return All slices within the space. 260 * @see SliceManager#getSliceDescendants(Uri) 261 */ onGetSliceDescendants(@onNull Uri uri)262 public @NonNull Collection<Uri> onGetSliceDescendants(@NonNull Uri uri) { 263 return Collections.emptyList(); 264 } 265 266 /** 267 * This method must be overridden if an {@link IntentFilter} is specified on the SliceProvider. 268 * In that case, this method can be called and is expected to return a non-null Uri representing 269 * a slice. Otherwise this will throw {@link UnsupportedOperationException}. 270 * 271 * Any intent filter added to a slice provider should also contain 272 * {@link SliceManager#CATEGORY_SLICE}, because otherwise it will not be detected by 273 * {@link SliceManager#mapIntentToUri(Intent)}. 274 * 275 * @return Uri representing the slice associated with the provided intent. 276 * @see Slice 277 * @see SliceManager#mapIntentToUri(Intent) 278 */ onMapIntentToUri(Intent intent)279 public @NonNull Uri onMapIntentToUri(Intent intent) { 280 throw new UnsupportedOperationException( 281 "This provider has not implemented intent to uri mapping"); 282 } 283 284 /** 285 * Called when an app requests a slice it does not have write permission 286 * to the uri for. 287 * <p> 288 * The return value will be the action on a slice that prompts the user that 289 * the calling app wants to show slices from this app. The default implementation 290 * launches a dialog that allows the user to grant access to this slice. Apps 291 * that do not want to allow this user grant, can override this and instead 292 * launch their own dialog with different behavior. 293 * 294 * @param sliceUri the Uri of the slice attempting to be bound. 295 * @see #getCallingPackage() 296 */ onCreatePermissionRequest(Uri sliceUri)297 public @NonNull PendingIntent onCreatePermissionRequest(Uri sliceUri) { 298 return createPermissionPendingIntent(getContext(), sliceUri, getCallingPackage()); 299 } 300 301 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)302 public final int update(Uri uri, ContentValues values, String selection, 303 String[] selectionArgs) { 304 if (DEBUG) Log.d(TAG, "update " + uri); 305 return 0; 306 } 307 308 @Override delete(Uri uri, String selection, String[] selectionArgs)309 public final int delete(Uri uri, String selection, String[] selectionArgs) { 310 if (DEBUG) Log.d(TAG, "delete " + uri); 311 return 0; 312 } 313 314 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)315 public final Cursor query(Uri uri, String[] projection, String selection, 316 String[] selectionArgs, String sortOrder) { 317 if (DEBUG) Log.d(TAG, "query " + uri); 318 return null; 319 } 320 321 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)322 public final Cursor query(Uri uri, String[] projection, String selection, String[] 323 selectionArgs, String sortOrder, CancellationSignal cancellationSignal) { 324 if (DEBUG) Log.d(TAG, "query " + uri); 325 return null; 326 } 327 328 @Override query(Uri uri, String[] projection, Bundle queryArgs, CancellationSignal cancellationSignal)329 public final Cursor query(Uri uri, String[] projection, Bundle queryArgs, 330 CancellationSignal cancellationSignal) { 331 if (DEBUG) Log.d(TAG, "query " + uri); 332 return null; 333 } 334 335 @Override insert(Uri uri, ContentValues values)336 public final Uri insert(Uri uri, ContentValues values) { 337 if (DEBUG) Log.d(TAG, "insert " + uri); 338 return null; 339 } 340 341 @Override getType(Uri uri)342 public final String getType(Uri uri) { 343 if (DEBUG) Log.d(TAG, "getType " + uri); 344 return SLICE_TYPE; 345 } 346 347 @Override call(String method, String arg, Bundle extras)348 public Bundle call(String method, String arg, Bundle extras) { 349 if (method.equals(METHOD_SLICE)) { 350 Uri uri = getUriWithoutUserId(validateIncomingUriOrNull( 351 extras.getParcelable(EXTRA_BIND_URI, android.net.Uri.class))); 352 List<SliceSpec> supportedSpecs = extras.getParcelableArrayList(EXTRA_SUPPORTED_SPECS, android.app.slice.SliceSpec.class); 353 354 String callingPackage = getCallingPackage(); 355 int callingUid = Binder.getCallingUid(); 356 int callingPid = Binder.getCallingPid(); 357 358 Slice s = handleBindSlice(uri, supportedSpecs, callingPackage, callingUid, callingPid); 359 Bundle b = new Bundle(); 360 b.putParcelable(EXTRA_SLICE, s); 361 return b; 362 } else if (method.equals(METHOD_MAP_INTENT)) { 363 Intent intent = extras.getParcelable(EXTRA_INTENT, android.content.Intent.class); 364 if (intent == null) return null; 365 Uri uri = validateIncomingUriOrNull(onMapIntentToUri(intent)); 366 List<SliceSpec> supportedSpecs = extras.getParcelableArrayList(EXTRA_SUPPORTED_SPECS, android.app.slice.SliceSpec.class); 367 Bundle b = new Bundle(); 368 if (uri != null) { 369 Slice s = handleBindSlice(uri, supportedSpecs, getCallingPackage(), 370 Binder.getCallingUid(), Binder.getCallingPid()); 371 b.putParcelable(EXTRA_SLICE, s); 372 } else { 373 b.putParcelable(EXTRA_SLICE, null); 374 } 375 return b; 376 } else if (method.equals(METHOD_MAP_ONLY_INTENT)) { 377 Intent intent = extras.getParcelable(EXTRA_INTENT, android.content.Intent.class); 378 if (intent == null) return null; 379 Uri uri = validateIncomingUriOrNull(onMapIntentToUri(intent)); 380 Bundle b = new Bundle(); 381 b.putParcelable(EXTRA_SLICE, uri); 382 return b; 383 } else if (method.equals(METHOD_PIN)) { 384 Uri uri = getUriWithoutUserId(validateIncomingUriOrNull( 385 extras.getParcelable(EXTRA_BIND_URI, android.net.Uri.class))); 386 if (Binder.getCallingUid() != Process.SYSTEM_UID) { 387 throw new SecurityException("Only the system can pin/unpin slices"); 388 } 389 handlePinSlice(uri); 390 } else if (method.equals(METHOD_UNPIN)) { 391 Uri uri = getUriWithoutUserId(validateIncomingUriOrNull( 392 extras.getParcelable(EXTRA_BIND_URI, android.net.Uri.class))); 393 if (Binder.getCallingUid() != Process.SYSTEM_UID) { 394 throw new SecurityException("Only the system can pin/unpin slices"); 395 } 396 handleUnpinSlice(uri); 397 } else if (method.equals(METHOD_GET_DESCENDANTS)) { 398 Uri uri = getUriWithoutUserId( 399 validateIncomingUriOrNull(extras.getParcelable(EXTRA_BIND_URI, android.net.Uri.class))); 400 Bundle b = new Bundle(); 401 b.putParcelableArrayList(EXTRA_SLICE_DESCENDANTS, 402 new ArrayList<>(handleGetDescendants(uri))); 403 return b; 404 } else if (method.equals(METHOD_GET_PERMISSIONS)) { 405 if (Binder.getCallingUid() != Process.SYSTEM_UID) { 406 throw new SecurityException("Only the system can get permissions"); 407 } 408 Bundle b = new Bundle(); 409 b.putStringArray(EXTRA_RESULT, mAutoGrantPermissions); 410 return b; 411 } 412 return super.call(method, arg, extras); 413 } 414 validateIncomingUriOrNull(Uri uri)415 private Uri validateIncomingUriOrNull(Uri uri) { 416 return uri == null ? null : validateIncomingUri(uri); 417 } 418 handleGetDescendants(Uri uri)419 private Collection<Uri> handleGetDescendants(Uri uri) { 420 mCallback = "onGetSliceDescendants"; 421 return onGetSliceDescendants(uri); 422 } 423 handlePinSlice(Uri sliceUri)424 private void handlePinSlice(Uri sliceUri) { 425 mCallback = "onSlicePinned"; 426 Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR); 427 try { 428 onSlicePinned(sliceUri); 429 } finally { 430 Handler.getMain().removeCallbacks(mAnr); 431 } 432 } 433 handleUnpinSlice(Uri sliceUri)434 private void handleUnpinSlice(Uri sliceUri) { 435 mCallback = "onSliceUnpinned"; 436 Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR); 437 try { 438 onSliceUnpinned(sliceUri); 439 } finally { 440 Handler.getMain().removeCallbacks(mAnr); 441 } 442 } 443 handleBindSlice(Uri sliceUri, List<SliceSpec> supportedSpecs, String callingPkg, int callingUid, int callingPid)444 private Slice handleBindSlice(Uri sliceUri, List<SliceSpec> supportedSpecs, 445 String callingPkg, int callingUid, int callingPid) { 446 // This can be removed once Slice#bindSlice is removed and everyone is using 447 // SliceManager#bindSlice. 448 String pkg = callingPkg != null ? callingPkg 449 : getContext().getPackageManager().getNameForUid(callingUid); 450 try { 451 mSliceManager.enforceSlicePermission(sliceUri, callingPid, callingUid, 452 mAutoGrantPermissions); 453 } catch (SecurityException e) { 454 return createPermissionSlice(getContext(), sliceUri, pkg); 455 } 456 mCallback = "onBindSlice"; 457 Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR); 458 try { 459 return onBindSliceStrict(sliceUri, supportedSpecs); 460 } finally { 461 Handler.getMain().removeCallbacks(mAnr); 462 } 463 } 464 465 /** 466 * @hide 467 */ createPermissionSlice(Context context, Uri sliceUri, String callingPackage)468 public Slice createPermissionSlice(Context context, Uri sliceUri, 469 String callingPackage) { 470 PendingIntent action; 471 mCallback = "onCreatePermissionRequest"; 472 Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR); 473 try { 474 action = onCreatePermissionRequest(sliceUri); 475 } finally { 476 Handler.getMain().removeCallbacks(mAnr); 477 } 478 Slice.Builder parent = new Slice.Builder(sliceUri, null); 479 Slice.Builder childAction = new Slice.Builder(parent) 480 .addIcon(Icon.createWithResource(context, 481 com.android.internal.R.drawable.ic_permission), null, 482 Collections.emptyList()) 483 .addHints(Arrays.asList(Slice.HINT_TITLE, Slice.HINT_SHORTCUT)) 484 .addAction(action, new Slice.Builder(parent).build(), null); 485 486 TypedValue tv = new TypedValue(); 487 new ContextThemeWrapper(context, android.R.style.Theme_DeviceDefault_Light) 488 .getTheme().resolveAttribute(android.R.attr.colorAccent, tv, true); 489 int deviceDefaultAccent = tv.data; 490 491 Uri subSliceUri = sliceUri.buildUpon().appendPath("permission").build(); 492 Slice.Builder subSlice = new Slice.Builder(subSliceUri, null) 493 .addIcon(Icon.createWithResource(context, 494 com.android.internal.R.drawable.ic_arrow_forward), null, 495 Collections.emptyList()) 496 .addText(getPermissionString(context, callingPackage), null, 497 Collections.emptyList()) 498 .addInt(deviceDefaultAccent, SUBTYPE_COLOR, 499 Collections.emptyList()) 500 .addSubSlice(childAction.build(), null); 501 parent.addSubSlice(subSlice.build(), null); 502 return parent.addHints(Arrays.asList(Slice.HINT_PERMISSION_REQUEST)).build(); 503 } 504 505 /** 506 * @hide 507 */ createPermissionPendingIntent(Context context, Uri sliceUri, String callingPackage)508 public static PendingIntent createPermissionPendingIntent(Context context, Uri sliceUri, 509 String callingPackage) { 510 return PendingIntent.getActivity(context, 0, 511 createPermissionIntent(context, sliceUri, callingPackage), 512 PendingIntent.FLAG_IMMUTABLE); 513 } 514 515 /** 516 * @hide 517 */ createPermissionIntent(Context context, Uri sliceUri, String callingPackage)518 public static Intent createPermissionIntent(Context context, Uri sliceUri, 519 String callingPackage) { 520 Intent intent = new Intent(SliceManager.ACTION_REQUEST_SLICE_PERMISSION); 521 intent.setComponent(ComponentName.unflattenFromString(context.getResources().getString( 522 com.android.internal.R.string.config_slicePermissionComponent))); 523 intent.putExtra(EXTRA_BIND_URI, sliceUri); 524 intent.putExtra(EXTRA_PKG, callingPackage); 525 // Unique pending intent. 526 intent.setData(sliceUri.buildUpon().appendQueryParameter("package", callingPackage) 527 .build()); 528 return intent; 529 } 530 531 /** 532 * @hide 533 */ getPermissionString(Context context, String callingPackage)534 public static CharSequence getPermissionString(Context context, String callingPackage) { 535 PackageManager pm = context.getPackageManager(); 536 try { 537 return context.getString( 538 com.android.internal.R.string.slices_permission_request, 539 pm.getApplicationInfo(callingPackage, 0).loadLabel(pm), 540 context.getApplicationInfo().loadLabel(pm)); 541 } catch (NameNotFoundException e) { 542 // This shouldn't be possible since the caller is verified. 543 throw new RuntimeException("Unknown calling app", e); 544 } 545 } 546 onBindSliceStrict(Uri sliceUri, List<SliceSpec> supportedSpecs)547 private Slice onBindSliceStrict(Uri sliceUri, List<SliceSpec> supportedSpecs) { 548 ThreadPolicy oldPolicy = StrictMode.getThreadPolicy(); 549 try { 550 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 551 .detectAll() 552 .penaltyDeath() 553 .build()); 554 return onBindSlice(sliceUri, new ArraySet<>(supportedSpecs)); 555 } finally { 556 StrictMode.setThreadPolicy(oldPolicy); 557 } 558 } 559 560 private final Runnable mAnr = () -> { 561 Process.sendSignal(Process.myPid(), Process.SIGNAL_QUIT); 562 Log.wtf(TAG, "Timed out while handling slice callback " + mCallback); 563 }; 564 } 565