| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.slice; |
| 18 |
|
| 19 |
import static android.content.pm.PackageManager.PERMISSION_DENIED; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.SdkConstant; |
| 24 |
import android.annotation.SdkConstant.SdkConstantType; |
| 25 |
import android.annotation.SystemService; |
| 26 |
import android.annotation.WorkerThread; |
| 27 |
import android.content.ContentProviderClient; |
| 28 |
import android.content.ContentResolver; |
| 29 |
import android.content.Context; |
| 30 |
import android.content.Intent; |
| 31 |
import android.content.pm.PackageManager; |
| 32 |
import android.content.pm.PackageManager.PermissionResult; |
| 33 |
import android.content.pm.ResolveInfo; |
| 34 |
import android.net.Uri; |
| 35 |
import android.os.Binder; |
| 36 |
import android.os.Bundle; |
| 37 |
import android.os.Handler; |
| 38 |
import android.os.IBinder; |
| 39 |
import android.os.Process; |
| 40 |
import android.os.RemoteException; |
| 41 |
import android.os.ServiceManager; |
| 42 |
import android.os.ServiceManager.ServiceNotFoundException; |
| 43 |
import android.os.UserHandle; |
| 44 |
import android.text.TextUtils; |
| 45 |
import android.util.ArraySet; |
| 46 |
import android.util.Log; |
| 47 |
|
| 48 |
import com.android.internal.util.Preconditions; |
| 49 |
|
| 50 |
import java.util.ArrayList; |
| 51 |
import java.util.Arrays; |
| 52 |
import java.util.Collection; |
| 53 |
import java.util.Collections; |
| 54 |
import java.util.List; |
| 55 |
import java.util.Objects; |
| 56 |
import java.util.Set; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
@SystemService(Context.SLICE_SERVICE) |
| 64 |
public class SliceManager { |
| 65 |
|
| 66 |
private static final String TAG = "SliceManager"; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static final String ACTION_REQUEST_SLICE_PERMISSION = |
| 72 |
"com.android.intent.action.REQUEST_SLICE_PERMISSION"; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
@SdkConstant(SdkConstantType.INTENT_CATEGORY) |
| 84 |
public static final String CATEGORY_SLICE = "android.app.slice.category.SLICE"; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public static final String SLICE_METADATA_KEY = "android.metadata.SLICE_URI"; |
| 105 |
|
| 106 |
private final ISliceManager mService; |
| 107 |
private final Context mContext; |
| 108 |
private final IBinder mToken = new Binder(); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public SliceManager(Context context, Handler handler) throws ServiceNotFoundException { |
| 114 |
mContext = context; |
| 115 |
mService = ISliceManager.Stub.asInterface( |
| 116 |
ServiceManager.getServiceOrThrow(Context.SLICE_SERVICE)); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public void pinSlice(@NonNull Uri uri, @NonNull Set<SliceSpec> specs) { |
| 135 |
try { |
| 136 |
mService.pinSlice(mContext.getPackageName(), uri, |
| 137 |
specs.toArray(new SliceSpec[specs.size()]), mToken); |
| 138 |
} catch (RemoteException e) { |
| 139 |
throw e.rethrowFromSystemServer(); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
@Deprecated |
| 148 |
public void pinSlice(@NonNull Uri uri, @NonNull List<SliceSpec> specs) { |
| 149 |
pinSlice(uri, new ArraySet<>(specs)); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public void unpinSlice(@NonNull Uri uri) { |
| 167 |
try { |
| 168 |
mService.unpinSlice(mContext.getPackageName(), uri, mToken); |
| 169 |
} catch (RemoteException e) { |
| 170 |
throw e.rethrowFromSystemServer(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public boolean hasSliceAccess() { |
| 178 |
try { |
| 179 |
return mService.hasSliceAccess(mContext.getPackageName()); |
| 180 |
} catch (RemoteException e) { |
| 181 |
throw e.rethrowFromSystemServer(); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public @NonNull Set<SliceSpec> getPinnedSpecs(Uri uri) { |
| 193 |
try { |
| 194 |
return new ArraySet<>(Arrays.asList(mService.getPinnedSpecs(uri, |
| 195 |
mContext.getPackageName()))); |
| 196 |
} catch (RemoteException e) { |
| 197 |
throw e.rethrowFromSystemServer(); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public @NonNull List<Uri> getPinnedSlices() { |
| 206 |
try { |
| 207 |
return Arrays.asList(mService.getPinnedSlices(mContext.getPackageName())); |
| 208 |
} catch (RemoteException e) { |
| 209 |
throw e.rethrowFromSystemServer(); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
@WorkerThread |
| 224 |
public @NonNull Collection<Uri> getSliceDescendants(@NonNull Uri uri) { |
| 225 |
ContentResolver resolver = mContext.getContentResolver(); |
| 226 |
try (ContentProviderClient provider = resolver.acquireUnstableContentProviderClient(uri)) { |
| 227 |
if (provider == null) { |
| 228 |
Log.w(TAG, TextUtils.formatSimple("Unknown URI: %s", uri)); |
| 229 |
} else { |
| 230 |
Bundle extras = new Bundle(); |
| 231 |
extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri); |
| 232 |
final Bundle res = provider.call( |
| 233 |
SliceProvider.METHOD_GET_DESCENDANTS, null, extras); |
| 234 |
return res.getParcelableArrayList(SliceProvider.EXTRA_SLICE_DESCENDANTS); |
| 235 |
} |
| 236 |
} catch (RemoteException e) { |
| 237 |
Log.e(TAG, "Unable to get slice descendants", e); |
| 238 |
} |
| 239 |
return Collections.emptyList(); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public @Nullable Slice bindSlice(@NonNull Uri uri, @NonNull Set<SliceSpec> supportedSpecs) { |
| 251 |
Objects.requireNonNull(uri, "uri"); |
| 252 |
ContentResolver resolver = mContext.getContentResolver(); |
| 253 |
try (ContentProviderClient provider = resolver.acquireUnstableContentProviderClient(uri)) { |
| 254 |
if (provider == null) { |
| 255 |
Log.w(TAG, String.format("Unknown URI: %s", uri)); |
| 256 |
return null; |
| 257 |
} |
| 258 |
Bundle extras = new Bundle(); |
| 259 |
extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri); |
| 260 |
extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS, |
| 261 |
new ArrayList<>(supportedSpecs)); |
| 262 |
final Bundle res = provider.call(SliceProvider.METHOD_SLICE, null, extras); |
| 263 |
Bundle.setDefusable(res, true); |
| 264 |
if (res == null) { |
| 265 |
return null; |
| 266 |
} |
| 267 |
return res.getParcelable(SliceProvider.EXTRA_SLICE); |
| 268 |
} catch (RemoteException e) { |
| 269 |
|
| 270 |
|
| 271 |
return null; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
@Deprecated |
| 280 |
public @Nullable Slice bindSlice(@NonNull Uri uri, @NonNull List<SliceSpec> supportedSpecs) { |
| 281 |
return bindSlice(uri, new ArraySet<>(supportedSpecs)); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public @Nullable Uri mapIntentToUri(@NonNull Intent intent) { |
| 307 |
ContentResolver resolver = mContext.getContentResolver(); |
| 308 |
final Uri staticUri = resolveStatic(intent, resolver); |
| 309 |
if (staticUri != null) return staticUri; |
| 310 |
|
| 311 |
String authority = getAuthority(intent); |
| 312 |
if (authority == null) return null; |
| 313 |
Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 314 |
.authority(authority).build(); |
| 315 |
try (ContentProviderClient provider = resolver.acquireUnstableContentProviderClient(uri)) { |
| 316 |
if (provider == null) { |
| 317 |
Log.w(TAG, String.format("Unknown URI: %s", uri)); |
| 318 |
return null; |
| 319 |
} |
| 320 |
Bundle extras = new Bundle(); |
| 321 |
extras.putParcelable(SliceProvider.EXTRA_INTENT, intent); |
| 322 |
final Bundle res = provider.call(SliceProvider.METHOD_MAP_ONLY_INTENT, null, extras); |
| 323 |
if (res == null) { |
| 324 |
return null; |
| 325 |
} |
| 326 |
return res.getParcelable(SliceProvider.EXTRA_SLICE); |
| 327 |
} catch (RemoteException e) { |
| 328 |
|
| 329 |
|
| 330 |
return null; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
private String getAuthority(Intent intent) { |
| 335 |
Intent queryIntent = new Intent(intent); |
| 336 |
if (!queryIntent.hasCategory(CATEGORY_SLICE)) { |
| 337 |
queryIntent.addCategory(CATEGORY_SLICE); |
| 338 |
} |
| 339 |
List<ResolveInfo> providers = |
| 340 |
mContext.getPackageManager().queryIntentContentProviders(queryIntent, 0); |
| 341 |
return providers != null && !providers.isEmpty() ? providers.get(0).providerInfo.authority |
| 342 |
: null; |
| 343 |
} |
| 344 |
|
| 345 |
private Uri resolveStatic(@NonNull Intent intent, ContentResolver resolver) { |
| 346 |
Objects.requireNonNull(intent, "intent"); |
| 347 |
Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null |
| 348 |
|| intent.getData() != null, |
| 349 |
"Slice intent must be explicit %s", intent); |
| 350 |
|
| 351 |
|
| 352 |
final Uri intentData = intent.getData(); |
| 353 |
if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) { |
| 354 |
return intentData; |
| 355 |
} |
| 356 |
|
| 357 |
ResolveInfo resolve = mContext.getPackageManager().resolveActivity(intent, |
| 358 |
PackageManager.GET_META_DATA); |
| 359 |
if (resolve != null && resolve.activityInfo != null |
| 360 |
&& resolve.activityInfo.metaData != null |
| 361 |
&& resolve.activityInfo.metaData.containsKey(SLICE_METADATA_KEY)) { |
| 362 |
return Uri.parse( |
| 363 |
resolve.activityInfo.metaData.getString(SLICE_METADATA_KEY)); |
| 364 |
} |
| 365 |
return null; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
public @Nullable Slice bindSlice(@NonNull Intent intent, |
| 380 |
@NonNull Set<SliceSpec> supportedSpecs) { |
| 381 |
Objects.requireNonNull(intent, "intent"); |
| 382 |
Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null |
| 383 |
|| intent.getData() != null, |
| 384 |
"Slice intent must be explicit %s", intent); |
| 385 |
ContentResolver resolver = mContext.getContentResolver(); |
| 386 |
final Uri staticUri = resolveStatic(intent, resolver); |
| 387 |
if (staticUri != null) return bindSlice(staticUri, supportedSpecs); |
| 388 |
|
| 389 |
String authority = getAuthority(intent); |
| 390 |
if (authority == null) return null; |
| 391 |
Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 392 |
.authority(authority).build(); |
| 393 |
try (ContentProviderClient provider = resolver.acquireUnstableContentProviderClient(uri)) { |
| 394 |
if (provider == null) { |
| 395 |
Log.w(TAG, String.format("Unknown URI: %s", uri)); |
| 396 |
return null; |
| 397 |
} |
| 398 |
Bundle extras = new Bundle(); |
| 399 |
extras.putParcelable(SliceProvider.EXTRA_INTENT, intent); |
| 400 |
extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS, |
| 401 |
new ArrayList<>(supportedSpecs)); |
| 402 |
final Bundle res = provider.call(SliceProvider.METHOD_MAP_INTENT, null, extras); |
| 403 |
if (res == null) { |
| 404 |
return null; |
| 405 |
} |
| 406 |
return res.getParcelable(SliceProvider.EXTRA_SLICE); |
| 407 |
} catch (RemoteException e) { |
| 408 |
|
| 409 |
|
| 410 |
return null; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
@Deprecated |
| 419 |
@Nullable |
| 420 |
public Slice bindSlice(@NonNull Intent intent, |
| 421 |
@NonNull List<SliceSpec> supportedSpecs) { |
| 422 |
return bindSlice(intent, new ArraySet<>(supportedSpecs)); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
public @PermissionResult int checkSlicePermission(@NonNull Uri uri, int pid, int uid) { |
| 441 |
try { |
| 442 |
return mService.checkSlicePermission(uri, mContext.getPackageName(), null, pid, uid, |
| 443 |
null); |
| 444 |
} catch (RemoteException e) { |
| 445 |
throw e.rethrowFromSystemServer(); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
public void grantSlicePermission(@NonNull String toPackage, @NonNull Uri uri) { |
| 458 |
try { |
| 459 |
mService.grantSlicePermission(mContext.getPackageName(), toPackage, uri); |
| 460 |
} catch (RemoteException e) { |
| 461 |
throw e.rethrowFromSystemServer(); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
public void revokeSlicePermission(@NonNull String toPackage, @NonNull Uri uri) { |
| 480 |
try { |
| 481 |
mService.revokeSlicePermission(mContext.getPackageName(), toPackage, uri); |
| 482 |
} catch (RemoteException e) { |
| 483 |
throw e.rethrowFromSystemServer(); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
public void enforceSlicePermission(Uri uri, String pkg, int pid, int uid, |
| 492 |
String[] autoGrantPermissions) { |
| 493 |
try { |
| 494 |
if (UserHandle.isSameApp(uid, Process.myUid())) { |
| 495 |
return; |
| 496 |
} |
| 497 |
if (pkg == null) { |
| 498 |
throw new SecurityException("No pkg specified"); |
| 499 |
} |
| 500 |
int result = mService.checkSlicePermission(uri, mContext.getPackageName(), pkg, pid, |
| 501 |
uid, autoGrantPermissions); |
| 502 |
if (result == PERMISSION_DENIED) { |
| 503 |
throw new SecurityException("User " + uid + " does not have slice permission for " |
| 504 |
+ uri + "."); |
| 505 |
} |
| 506 |
} catch (RemoteException e) { |
| 507 |
throw e.rethrowFromSystemServer(); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
public void grantPermissionFromUser(Uri uri, String pkg, boolean allSlices) { |
| 516 |
try { |
| 517 |
mService.grantPermissionFromUser(uri, pkg, mContext.getPackageName(), allSlices); |
| 518 |
} catch (RemoteException e) { |
| 519 |
throw e.rethrowFromSystemServer(); |
| 520 |
} |
| 521 |
} |
| 522 |
} |