| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public abstract class SliceProvider extends ContentProvider { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static final String SLICE_TYPE = "vnd.android.slice"; |
| 101 |
|
| 102 |
private static final String TAG = "SliceProvider"; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public static final String EXTRA_BIND_URI = "slice_uri"; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public static final String EXTRA_SUPPORTED_SPECS = "supported_specs"; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static final String METHOD_SLICE = "bind_slice"; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public static final String METHOD_MAP_INTENT = "map_slice"; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static final String METHOD_MAP_ONLY_INTENT = "map_only"; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public static final String METHOD_PIN = "pin"; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static final String METHOD_UNPIN = "unpin"; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static final String METHOD_GET_DESCENDANTS = "get_descendants"; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public static final String METHOD_GET_PERMISSIONS = "get_permissions"; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public static final String EXTRA_INTENT = "slice_intent"; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public static final String EXTRA_SLICE = "slice"; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public static final String EXTRA_SLICE_DESCENDANTS = "slice_descendants"; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public static final String EXTRA_PKG = "pkg"; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public static final String EXTRA_RESULT = "result"; |
| 159 |
|
| 160 |
private static final boolean DEBUG = false; |
| 161 |
|
| 162 |
private static final long SLICE_BIND_ANR = 2000; |
| 163 |
private final String[] mAutoGrantPermissions; |
| 164 |
|
| 165 |
private String mCallback; |
| 166 |
private SliceManager mSliceManager; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public SliceProvider(@NonNull String... autoGrantPermissions) { |
| 181 |
mAutoGrantPermissions = autoGrantPermissions; |
| 182 |
} |
| 183 |
|
| 184 |
public SliceProvider() { |
| 185 |
mAutoGrantPermissions = new String[0]; |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public void attachInfo(Context context, ProviderInfo info) { |
| 190 |
super.attachInfo(context, info); |
| 191 |
mSliceManager = context.getSystemService(SliceManager.class); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public Slice onBindSlice(Uri sliceUri, Set<SliceSpec> supportedSpecs) { |
| 212 |
return onBindSlice(sliceUri, new ArrayList<>(supportedSpecs)); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
@Deprecated |
| 220 |
public Slice onBindSlice(Uri sliceUri, List<SliceSpec> supportedSpecs) { |
| 221 |
return null; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public void onSlicePinned(Uri sliceUri) { |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public void onSliceUnpinned(Uri sliceUri) { |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public @NonNull Collection<Uri> onGetSliceDescendants(@NonNull Uri uri) { |
| 267 |
return Collections.emptyList(); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public @NonNull Uri onMapIntentToUri(Intent intent) { |
| 284 |
throw new UnsupportedOperationException( |
| 285 |
"This provider has not implemented intent to uri mapping"); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public @NonNull PendingIntent onCreatePermissionRequest(Uri sliceUri) { |
| 302 |
return createPermissionPendingIntent(getContext(), sliceUri, getCallingPackage()); |
| 303 |
} |
| 304 |
|
| 305 |
@Override |
| 306 |
public final int update(Uri uri, ContentValues values, String selection, |
| 307 |
String[] selectionArgs) { |
| 308 |
if (DEBUG) Log.d(TAG, "update " + uri); |
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
@Override |
| 313 |
public final int delete(Uri uri, String selection, String[] selectionArgs) { |
| 314 |
if (DEBUG) Log.d(TAG, "delete " + uri); |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
@Override |
| 319 |
public final Cursor query(Uri uri, String[] projection, String selection, |
| 320 |
String[] selectionArgs, String sortOrder) { |
| 321 |
if (DEBUG) Log.d(TAG, "query " + uri); |
| 322 |
return null; |
| 323 |
} |
| 324 |
|
| 325 |
@Override |
| 326 |
public final Cursor query(Uri uri, String[] projection, String selection, String[] |
| 327 |
selectionArgs, String sortOrder, CancellationSignal cancellationSignal) { |
| 328 |
if (DEBUG) Log.d(TAG, "query " + uri); |
| 329 |
return null; |
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
public final Cursor query(Uri uri, String[] projection, Bundle queryArgs, |
| 334 |
CancellationSignal cancellationSignal) { |
| 335 |
if (DEBUG) Log.d(TAG, "query " + uri); |
| 336 |
return null; |
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
public final Uri insert(Uri uri, ContentValues values) { |
| 341 |
if (DEBUG) Log.d(TAG, "insert " + uri); |
| 342 |
return null; |
| 343 |
} |
| 344 |
|
| 345 |
@Override |
| 346 |
public final String getType(Uri uri) { |
| 347 |
if (DEBUG) Log.d(TAG, "getType " + uri); |
| 348 |
return SLICE_TYPE; |
| 349 |
} |
| 350 |
|
| 351 |
@Override |
| 352 |
public Bundle call(String method, String arg, Bundle extras) { |
| 353 |
if (method.equals(METHOD_SLICE)) { |
| 354 |
Uri uri = getUriWithoutUserId(validateIncomingUriOrNull( |
| 355 |
extras.getParcelable(EXTRA_BIND_URI))); |
| 356 |
List<SliceSpec> supportedSpecs = extras.getParcelableArrayList(EXTRA_SUPPORTED_SPECS); |
| 357 |
|
| 358 |
String callingPackage = getCallingPackage(); |
| 359 |
int callingUid = Binder.getCallingUid(); |
| 360 |
int callingPid = Binder.getCallingPid(); |
| 361 |
|
| 362 |
Slice s = handleBindSlice(uri, supportedSpecs, callingPackage, callingUid, callingPid); |
| 363 |
Bundle b = new Bundle(); |
| 364 |
b.putParcelable(EXTRA_SLICE, s); |
| 365 |
return b; |
| 366 |
} else if (method.equals(METHOD_MAP_INTENT)) { |
| 367 |
Intent intent = extras.getParcelable(EXTRA_INTENT); |
| 368 |
if (intent == null) return null; |
| 369 |
Uri uri = validateIncomingUriOrNull(onMapIntentToUri(intent)); |
| 370 |
List<SliceSpec> supportedSpecs = extras.getParcelableArrayList(EXTRA_SUPPORTED_SPECS); |
| 371 |
Bundle b = new Bundle(); |
| 372 |
if (uri != null) { |
| 373 |
Slice s = handleBindSlice(uri, supportedSpecs, getCallingPackage(), |
| 374 |
Binder.getCallingUid(), Binder.getCallingPid()); |
| 375 |
b.putParcelable(EXTRA_SLICE, s); |
| 376 |
} else { |
| 377 |
b.putParcelable(EXTRA_SLICE, null); |
| 378 |
} |
| 379 |
return b; |
| 380 |
} else if (method.equals(METHOD_MAP_ONLY_INTENT)) { |
| 381 |
Intent intent = extras.getParcelable(EXTRA_INTENT); |
| 382 |
if (intent == null) return null; |
| 383 |
Uri uri = validateIncomingUriOrNull(onMapIntentToUri(intent)); |
| 384 |
Bundle b = new Bundle(); |
| 385 |
b.putParcelable(EXTRA_SLICE, uri); |
| 386 |
return b; |
| 387 |
} else if (method.equals(METHOD_PIN)) { |
| 388 |
Uri uri = getUriWithoutUserId(validateIncomingUriOrNull( |
| 389 |
extras.getParcelable(EXTRA_BIND_URI))); |
| 390 |
if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 391 |
throw new SecurityException("Only the system can pin/unpin slices"); |
| 392 |
} |
| 393 |
handlePinSlice(uri); |
| 394 |
} else if (method.equals(METHOD_UNPIN)) { |
| 395 |
Uri uri = getUriWithoutUserId(validateIncomingUriOrNull( |
| 396 |
extras.getParcelable(EXTRA_BIND_URI))); |
| 397 |
if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 398 |
throw new SecurityException("Only the system can pin/unpin slices"); |
| 399 |
} |
| 400 |
handleUnpinSlice(uri); |
| 401 |
} else if (method.equals(METHOD_GET_DESCENDANTS)) { |
| 402 |
Uri uri = getUriWithoutUserId( |
| 403 |
validateIncomingUriOrNull(extras.getParcelable(EXTRA_BIND_URI))); |
| 404 |
Bundle b = new Bundle(); |
| 405 |
b.putParcelableArrayList(EXTRA_SLICE_DESCENDANTS, |
| 406 |
new ArrayList<>(handleGetDescendants(uri))); |
| 407 |
return b; |
| 408 |
} else if (method.equals(METHOD_GET_PERMISSIONS)) { |
| 409 |
if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 410 |
throw new SecurityException("Only the system can get permissions"); |
| 411 |
} |
| 412 |
Bundle b = new Bundle(); |
| 413 |
b.putStringArray(EXTRA_RESULT, mAutoGrantPermissions); |
| 414 |
return b; |
| 415 |
} |
| 416 |
return super.call(method, arg, extras); |
| 417 |
} |
| 418 |
|
| 419 |
private Uri validateIncomingUriOrNull(Uri uri) { |
| 420 |
return uri == null ? null : validateIncomingUri(uri); |
| 421 |
} |
| 422 |
|
| 423 |
private Collection<Uri> handleGetDescendants(Uri uri) { |
| 424 |
mCallback = "onGetSliceDescendants"; |
| 425 |
return onGetSliceDescendants(uri); |
| 426 |
} |
| 427 |
|
| 428 |
private void handlePinSlice(Uri sliceUri) { |
| 429 |
mCallback = "onSlicePinned"; |
| 430 |
Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR); |
| 431 |
try { |
| 432 |
onSlicePinned(sliceUri); |
| 433 |
} finally { |
| 434 |
Handler.getMain().removeCallbacks(mAnr); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
private void handleUnpinSlice(Uri sliceUri) { |
| 439 |
mCallback = "onSliceUnpinned"; |
| 440 |
Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR); |
| 441 |
try { |
| 442 |
onSliceUnpinned(sliceUri); |
| 443 |
} finally { |
| 444 |
Handler.getMain().removeCallbacks(mAnr); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
private Slice handleBindSlice(Uri sliceUri, List<SliceSpec> supportedSpecs, |
| 449 |
String callingPkg, int callingUid, int callingPid) { |
| 450 |
|
| 451 |
|
| 452 |
String pkg = callingPkg != null ? callingPkg |
| 453 |
: getContext().getPackageManager().getNameForUid(callingUid); |
| 454 |
try { |
| 455 |
mSliceManager.enforceSlicePermission(sliceUri, pkg, |
| 456 |
callingPid, callingUid, mAutoGrantPermissions); |
| 457 |
} catch (SecurityException e) { |
| 458 |
return createPermissionSlice(getContext(), sliceUri, pkg); |
| 459 |
} |
| 460 |
mCallback = "onBindSlice"; |
| 461 |
Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR); |
| 462 |
try { |
| 463 |
return onBindSliceStrict(sliceUri, supportedSpecs); |
| 464 |
} finally { |
| 465 |
Handler.getMain().removeCallbacks(mAnr); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
public Slice createPermissionSlice(Context context, Uri sliceUri, |
| 473 |
String callingPackage) { |
| 474 |
PendingIntent action; |
| 475 |
mCallback = "onCreatePermissionRequest"; |
| 476 |
Handler.getMain().postDelayed(mAnr, SLICE_BIND_ANR); |
| 477 |
try { |
| 478 |
action = onCreatePermissionRequest(sliceUri); |
| 479 |
} finally { |
| 480 |
Handler.getMain().removeCallbacks(mAnr); |
| 481 |
} |
| 482 |
Slice.Builder parent = new Slice.Builder(sliceUri); |
| 483 |
Slice.Builder childAction = new Slice.Builder(parent) |
| 484 |
.addIcon(Icon.createWithResource(context, |
| 485 |
com.android.internal.R.drawable.ic_permission), null, |
| 486 |
Collections.emptyList()) |
| 487 |
.addHints(Arrays.asList(Slice.HINT_TITLE, Slice.HINT_SHORTCUT)) |
| 488 |
.addAction(action, new Slice.Builder(parent).build(), null); |
| 489 |
|
| 490 |
TypedValue tv = new TypedValue(); |
| 491 |
new ContextThemeWrapper(context, android.R.style.Theme_DeviceDefault_Light) |
| 492 |
.getTheme().resolveAttribute(android.R.attr.colorAccent, tv, true); |
| 493 |
int deviceDefaultAccent = tv.data; |
| 494 |
|
| 495 |
parent.addSubSlice(new Slice.Builder(sliceUri.buildUpon().appendPath("permission").build()) |
| 496 |
.addIcon(Icon.createWithResource(context, |
| 497 |
com.android.internal.R.drawable.ic_arrow_forward), null, |
| 498 |
Collections.emptyList()) |
| 499 |
.addText(getPermissionString(context, callingPackage), null, |
| 500 |
Collections.emptyList()) |
| 501 |
.addInt(deviceDefaultAccent, SUBTYPE_COLOR, |
| 502 |
Collections.emptyList()) |
| 503 |
.addSubSlice(childAction.build(), null) |
| 504 |
.build(), null); |
| 505 |
return parent.addHints(Arrays.asList(Slice.HINT_PERMISSION_REQUEST)).build(); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public static PendingIntent createPermissionPendingIntent(Context context, Uri sliceUri, |
| 512 |
String callingPackage) { |
| 513 |
return PendingIntent.getActivity(context, 0, |
| 514 |
createPermissionIntent(context, sliceUri, callingPackage), |
| 515 |
PendingIntent.FLAG_IMMUTABLE); |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public static Intent createPermissionIntent(Context context, Uri sliceUri, |
| 522 |
String callingPackage) { |
| 523 |
Intent intent = new Intent(SliceManager.ACTION_REQUEST_SLICE_PERMISSION); |
| 524 |
intent.setComponent(ComponentName.unflattenFromString(context.getResources().getString( |
| 525 |
com.android.internal.R.string.config_slicePermissionComponent))); |
| 526 |
intent.putExtra(EXTRA_BIND_URI, sliceUri); |
| 527 |
intent.putExtra(EXTRA_PKG, callingPackage); |
| 528 |
|
| 529 |
intent.setData(sliceUri.buildUpon().appendQueryParameter("package", callingPackage) |
| 530 |
.build()); |
| 531 |
return intent; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
public static CharSequence getPermissionString(Context context, String callingPackage) { |
| 538 |
PackageManager pm = context.getPackageManager(); |
| 539 |
try { |
| 540 |
return context.getString( |
| 541 |
com.android.internal.R.string.slices_permission_request, |
| 542 |
pm.getApplicationInfo(callingPackage, 0).loadLabel(pm), |
| 543 |
context.getApplicationInfo().loadLabel(pm)); |
| 544 |
} catch (NameNotFoundException e) { |
| 545 |
|
| 546 |
throw new RuntimeException("Unknown calling app", e); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
private Slice onBindSliceStrict(Uri sliceUri, List<SliceSpec> supportedSpecs) { |
| 551 |
ThreadPolicy oldPolicy = StrictMode.getThreadPolicy(); |
| 552 |
try { |
| 553 |
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() |
| 554 |
.detectAll() |
| 555 |
.penaltyDeath() |
| 556 |
.build()); |
| 557 |
return onBindSlice(sliceUri, new ArraySet<>(supportedSpecs)); |
| 558 |
} finally { |
| 559 |
StrictMode.setThreadPolicy(oldPolicy); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
private final Runnable mAnr = () -> { |
| 564 |
Process.sendSignal(Process.myPid(), Process.SIGNAL_QUIT); |
| 565 |
Log.wtf(TAG, "Timed out while handling slice callback " + mCallback); |
| 566 |
}; |
| 567 |
} |