blob: 15ff03a219c390ba97f399e42158cf27d1529789 [file] [log] [blame]
Jason Monk8f5f7ff2017-10-17 14:12:42 -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 */
16
17package android.app.slice;
18
Jason Monk4ef50bc2018-01-22 10:52:23 -050019import android.annotation.CallbackExecutor;
Jason Monke2c64512017-12-11 15:14:54 -050020import android.annotation.NonNull;
Jason Monkb9e06a82018-01-16 15:32:53 -050021import android.annotation.Nullable;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040022import android.annotation.SystemService;
Jason Monk632def12018-02-01 15:21:16 -050023import android.content.ContentProviderClient;
Jason Monkb9e06a82018-01-16 15:32:53 -050024import android.content.ContentResolver;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040025import android.content.Context;
Jason Monkb9e06a82018-01-16 15:32:53 -050026import android.content.Intent;
Jason Monkf2008872018-02-23 08:59:31 -050027import android.content.pm.PackageManager;
Jason Monkb9e06a82018-01-16 15:32:53 -050028import android.content.pm.ResolveInfo;
Jason Monk74f5e362017-12-06 08:56:33 -050029import android.net.Uri;
Jason Monkb9e06a82018-01-16 15:32:53 -050030import android.os.Bundle;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040031import android.os.Handler;
Jason Monk74f5e362017-12-06 08:56:33 -050032import android.os.RemoteException;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040033import android.os.ServiceManager;
34import android.os.ServiceManager.ServiceNotFoundException;
Jason Monke2c64512017-12-11 15:14:54 -050035import android.util.ArrayMap;
Jason Monk5f8cc272018-01-16 17:57:20 -050036import android.util.Log;
Jason Monke2c64512017-12-11 15:14:54 -050037import android.util.Pair;
38
Jason Monkb9e06a82018-01-16 15:32:53 -050039import com.android.internal.util.Preconditions;
40
41import java.util.ArrayList;
Jason Monke2c64512017-12-11 15:14:54 -050042import java.util.Arrays;
Jason Monk5f8cc272018-01-16 17:57:20 -050043import java.util.Collection;
44import java.util.Collections;
Jason Monke2c64512017-12-11 15:14:54 -050045import java.util.List;
46import java.util.concurrent.Executor;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040047
48/**
Jason Monke2c64512017-12-11 15:14:54 -050049 * Class to handle interactions with {@link Slice}s.
50 * <p>
51 * The SliceManager manages permissions and pinned state for slices.
Jason Monk8f5f7ff2017-10-17 14:12:42 -040052 */
53@SystemService(Context.SLICE_SERVICE)
54public class SliceManager {
55
Jason Monk5f8cc272018-01-16 17:57:20 -050056 private static final String TAG = "SliceManager";
57
Jason Monke8f8be72018-01-21 10:10:35 -050058 /**
59 * @hide
60 */
61 public static final String ACTION_REQUEST_SLICE_PERMISSION =
62 "android.intent.action.REQUEST_SLICE_PERMISSION";
63
Mady Mellor3267ed82018-02-21 11:42:31 -080064 /**
65 * The meta-data key that allows an activity to easily be linked directly to a slice.
66 * <p>
67 * An activity can be statically linked to a slice uri by including a meta-data item
68 * for this key that contains a valid slice uri for the same application declaring
69 * the activity.
70 */
71 public static final String SLICE_METADATA_KEY = "android.metadata.SLICE_URI";
72
Jason Monk8f5f7ff2017-10-17 14:12:42 -040073 private final ISliceManager mService;
74 private final Context mContext;
Jason Monke2c64512017-12-11 15:14:54 -050075 private final ArrayMap<Pair<Uri, SliceCallback>, ISliceListener> mListenerLookup =
76 new ArrayMap<>();
Jason Monk8f5f7ff2017-10-17 14:12:42 -040077
Jason Monke2c64512017-12-11 15:14:54 -050078 /**
Jason Monke8f8be72018-01-21 10:10:35 -050079 * Permission denied.
80 * @hide
81 */
82 public static final int PERMISSION_DENIED = -1;
83 /**
84 * Permission granted.
85 * @hide
86 */
87 public static final int PERMISSION_GRANTED = 0;
88 /**
89 * Permission just granted by the user, and should be granted uri permission as well.
90 * @hide
91 */
92 public static final int PERMISSION_USER_GRANTED = 1;
93
94 /**
Jason Monke2c64512017-12-11 15:14:54 -050095 * @hide
96 */
Jason Monk8f5f7ff2017-10-17 14:12:42 -040097 public SliceManager(Context context, Handler handler) throws ServiceNotFoundException {
98 mContext = context;
99 mService = ISliceManager.Stub.asInterface(
100 ServiceManager.getServiceOrThrow(Context.SLICE_SERVICE));
101 }
Jason Monk74f5e362017-12-06 08:56:33 -0500102
103 /**
Jason Monk4ef50bc2018-01-22 10:52:23 -0500104 * @deprecated TO BE REMOVED.
Jason Monk74f5e362017-12-06 08:56:33 -0500105 */
Jason Monk4ef50bc2018-01-22 10:52:23 -0500106 @Deprecated
Jason Monke2c64512017-12-11 15:14:54 -0500107 public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback,
108 @NonNull List<SliceSpec> specs) {
Jason Monk4ef50bc2018-01-22 10:52:23 -0500109 registerSliceCallback(uri, specs, mContext.getMainExecutor(), callback);
Jason Monke2c64512017-12-11 15:14:54 -0500110 }
111
112 /**
Jason Monk4ef50bc2018-01-22 10:52:23 -0500113 * @deprecated TO BE REMOVED.
Jason Monke2c64512017-12-11 15:14:54 -0500114 */
Jason Monk4ef50bc2018-01-22 10:52:23 -0500115 @Deprecated
Jason Monke2c64512017-12-11 15:14:54 -0500116 public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback,
117 @NonNull List<SliceSpec> specs, Executor executor) {
Jason Monk4ef50bc2018-01-22 10:52:23 -0500118 registerSliceCallback(uri, specs, executor, callback);
119 }
120
121 /**
122 * Adds a callback to a specific slice uri.
123 * <p>
124 * This is a convenience that performs a few slice actions at once. It will put
125 * the slice in a pinned state since there is a callback attached. It will also
126 * listen for content changes, when a content change observes, the android system
127 * will bind the new slice and provide it to all registered {@link SliceCallback}s.
128 *
129 * @param uri The uri of the slice being listened to.
130 * @param callback The listener that should receive the callbacks.
131 * @param specs The list of supported {@link SliceSpec}s of the callback.
132 * @see SliceProvider#onSlicePinned(Uri)
133 */
134 public void registerSliceCallback(@NonNull Uri uri, @NonNull List<SliceSpec> specs,
135 @NonNull SliceCallback callback) {
136 registerSliceCallback(uri, specs, mContext.getMainExecutor(), callback);
137 }
138
139 /**
140 * Adds a callback to a specific slice uri.
141 * <p>
142 * This is a convenience that performs a few slice actions at once. It will put
143 * the slice in a pinned state since there is a callback attached. It will also
144 * listen for content changes, when a content change observes, the android system
145 * will bind the new slice and provide it to all registered {@link SliceCallback}s.
146 *
147 * @param uri The uri of the slice being listened to.
148 * @param callback The listener that should receive the callbacks.
149 * @param specs The list of supported {@link SliceSpec}s of the callback.
150 * @see SliceProvider#onSlicePinned(Uri)
151 */
152 public void registerSliceCallback(@NonNull Uri uri, @NonNull List<SliceSpec> specs,
153 @NonNull @CallbackExecutor Executor executor, @NonNull SliceCallback callback) {
Jason Monk74f5e362017-12-06 08:56:33 -0500154 try {
Jason Monke2c64512017-12-11 15:14:54 -0500155 mService.addSliceListener(uri, mContext.getPackageName(),
156 getListener(uri, callback, new ISliceListener.Stub() {
157 @Override
158 public void onSliceUpdated(Slice s) throws RemoteException {
159 executor.execute(() -> callback.onSliceUpdated(s));
160 }
161 }), specs.toArray(new SliceSpec[specs.size()]));
162 } catch (RemoteException e) {
163 throw e.rethrowFromSystemServer();
164 }
165 }
166
167 private ISliceListener getListener(Uri uri, SliceCallback callback,
168 ISliceListener listener) {
169 Pair<Uri, SliceCallback> key = new Pair<>(uri, callback);
170 if (mListenerLookup.containsKey(key)) {
171 try {
172 mService.removeSliceListener(uri, mContext.getPackageName(),
173 mListenerLookup.get(key));
174 } catch (RemoteException e) {
175 throw e.rethrowFromSystemServer();
176 }
177 }
178 mListenerLookup.put(key, listener);
179 return listener;
180 }
181
182 /**
183 * Removes a callback for a specific slice uri.
184 * <p>
185 * Removes the app from the pinned state (if there are no other apps/callbacks pinning it)
186 * in addition to removing the callback.
187 *
188 * @param uri The uri of the slice being listened to
189 * @param callback The listener that should no longer receive callbacks.
190 * @see #registerSliceCallback
191 */
192 public void unregisterSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback) {
193 try {
194 mService.removeSliceListener(uri, mContext.getPackageName(),
195 mListenerLookup.remove(new Pair<>(uri, callback)));
Jason Monk74f5e362017-12-06 08:56:33 -0500196 } catch (RemoteException e) {
197 throw e.rethrowFromSystemServer();
198 }
199 }
200
201 /**
Jason Monke2c64512017-12-11 15:14:54 -0500202 * Ensures that a slice is in a pinned state.
203 * <p>
204 * Pinned state is not persisted across reboots, so apps are expected to re-pin any slices
205 * they still care about after a reboot.
Jason Monk632def12018-02-01 15:21:16 -0500206 * <p>
207 * This may only be called by apps that are the default launcher for the device
208 * or the default voice interaction service. Otherwise will throw {@link SecurityException}.
Jason Monke2c64512017-12-11 15:14:54 -0500209 *
210 * @param uri The uri of the slice being pinned.
211 * @param specs The list of supported {@link SliceSpec}s of the callback.
212 * @see SliceProvider#onSlicePinned(Uri)
Jason Monk632def12018-02-01 15:21:16 -0500213 * @see Intent#ACTION_ASSIST
214 * @see Intent#CATEGORY_HOME
Jason Monk74f5e362017-12-06 08:56:33 -0500215 */
Jason Monke2c64512017-12-11 15:14:54 -0500216 public void pinSlice(@NonNull Uri uri, @NonNull List<SliceSpec> specs) {
Jason Monk74f5e362017-12-06 08:56:33 -0500217 try {
Jason Monke2c64512017-12-11 15:14:54 -0500218 mService.pinSlice(mContext.getPackageName(), uri,
219 specs.toArray(new SliceSpec[specs.size()]));
Jason Monk74f5e362017-12-06 08:56:33 -0500220 } catch (RemoteException e) {
221 throw e.rethrowFromSystemServer();
222 }
223 }
224
225 /**
Jason Monke2c64512017-12-11 15:14:54 -0500226 * Remove a pin for a slice.
227 * <p>
228 * If the slice has no other pins/callbacks then the slice will be unpinned.
Jason Monk632def12018-02-01 15:21:16 -0500229 * <p>
230 * This may only be called by apps that are the default launcher for the device
231 * or the default voice interaction service. Otherwise will throw {@link SecurityException}.
Jason Monke2c64512017-12-11 15:14:54 -0500232 *
233 * @param uri The uri of the slice being unpinned.
234 * @see #pinSlice
235 * @see SliceProvider#onSliceUnpinned(Uri)
Jason Monk632def12018-02-01 15:21:16 -0500236 * @see Intent#ACTION_ASSIST
237 * @see Intent#CATEGORY_HOME
Jason Monk74f5e362017-12-06 08:56:33 -0500238 */
Jason Monke2c64512017-12-11 15:14:54 -0500239 public void unpinSlice(@NonNull Uri uri) {
Jason Monk74f5e362017-12-06 08:56:33 -0500240 try {
241 mService.unpinSlice(mContext.getPackageName(), uri);
242 } catch (RemoteException e) {
243 throw e.rethrowFromSystemServer();
244 }
245 }
246
247 /**
Jason Monke2c64512017-12-11 15:14:54 -0500248 * @hide
Jason Monk74f5e362017-12-06 08:56:33 -0500249 */
250 public boolean hasSliceAccess() {
251 try {
252 return mService.hasSliceAccess(mContext.getPackageName());
253 } catch (RemoteException e) {
254 throw e.rethrowFromSystemServer();
255 }
256 }
257
258 /**
Jason Monke2c64512017-12-11 15:14:54 -0500259 * Get the current set of specs for a pinned slice.
260 * <p>
261 * This is the set of specs supported for a specific pinned slice. It will take
262 * into account all clients and returns only specs supported by all.
263 * @see SliceSpec
Jason Monk74f5e362017-12-06 08:56:33 -0500264 */
Jason Monke2c64512017-12-11 15:14:54 -0500265 public @NonNull List<SliceSpec> getPinnedSpecs(Uri uri) {
Jason Monk74f5e362017-12-06 08:56:33 -0500266 try {
Jason Monke2c64512017-12-11 15:14:54 -0500267 return Arrays.asList(mService.getPinnedSpecs(uri, mContext.getPackageName()));
Jason Monk74f5e362017-12-06 08:56:33 -0500268 } catch (RemoteException e) {
269 throw e.rethrowFromSystemServer();
270 }
271 }
272
273 /**
Jason Monk5f8cc272018-01-16 17:57:20 -0500274 * Obtains a list of slices that are descendants of the specified Uri.
275 * <p>
276 * Not all slice providers will implement this functionality, in which case,
277 * an empty collection will be returned.
278 *
279 * @param uri The uri to look for descendants under.
280 * @return All slices within the space.
281 * @see SliceProvider#onGetSliceDescendants(Uri)
282 */
283 public @NonNull Collection<Uri> getSliceDescendants(@NonNull Uri uri) {
284 ContentResolver resolver = mContext.getContentResolver();
Jason Monk632def12018-02-01 15:21:16 -0500285 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
Jason Monk5f8cc272018-01-16 17:57:20 -0500286 Bundle extras = new Bundle();
287 extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri);
Jason Monk632def12018-02-01 15:21:16 -0500288 final Bundle res = provider.call(SliceProvider.METHOD_GET_DESCENDANTS, null, extras);
Jason Monk5f8cc272018-01-16 17:57:20 -0500289 return res.getParcelableArrayList(SliceProvider.EXTRA_SLICE_DESCENDANTS);
290 } catch (RemoteException e) {
291 Log.e(TAG, "Unable to get slice descendants", e);
Jason Monk5f8cc272018-01-16 17:57:20 -0500292 }
293 return Collections.emptyList();
294 }
295
296 /**
Jason Monkb9e06a82018-01-16 15:32:53 -0500297 * Turns a slice Uri into slice content.
298 *
299 * @param uri The URI to a slice provider
300 * @param supportedSpecs List of supported specs.
301 * @return The Slice provided by the app or null if none is given.
302 * @see Slice
303 */
304 public @Nullable Slice bindSlice(@NonNull Uri uri, @NonNull List<SliceSpec> supportedSpecs) {
305 Preconditions.checkNotNull(uri, "uri");
306 ContentResolver resolver = mContext.getContentResolver();
Jason Monk632def12018-02-01 15:21:16 -0500307 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
308 if (provider == null) {
309 throw new IllegalArgumentException("Unknown URI " + uri);
310 }
Jason Monkb9e06a82018-01-16 15:32:53 -0500311 Bundle extras = new Bundle();
312 extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri);
313 extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS,
314 new ArrayList<>(supportedSpecs));
Jason Monk632def12018-02-01 15:21:16 -0500315 final Bundle res = provider.call(SliceProvider.METHOD_SLICE, null, extras);
Jason Monkb9e06a82018-01-16 15:32:53 -0500316 Bundle.setDefusable(res, true);
317 if (res == null) {
318 return null;
319 }
320 return res.getParcelable(SliceProvider.EXTRA_SLICE);
321 } catch (RemoteException e) {
322 // Arbitrary and not worth documenting, as Activity
323 // Manager will kill this process shortly anyway.
324 return null;
Jason Monkb9e06a82018-01-16 15:32:53 -0500325 }
326 }
327
328 /**
Jason Monkf2008872018-02-23 08:59:31 -0500329 * Turns a slice intent into a slice uri. Expects an explicit intent.
Jason Monk7b8fef22018-01-30 16:04:14 -0500330 *
331 * @param intent The intent associated with a slice.
Jason Monkf2008872018-02-23 08:59:31 -0500332 * @return The Slice Uri provided by the app or null if none exists.
Jason Monk7b8fef22018-01-30 16:04:14 -0500333 * @see Slice
334 * @see SliceProvider#onMapIntentToUri(Intent)
335 * @see Intent
336 */
337 public @Nullable Uri mapIntentToUri(@NonNull Intent intent) {
338 Preconditions.checkNotNull(intent, "intent");
339 Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null,
340 "Slice intent must be explicit %s", intent);
341 ContentResolver resolver = mContext.getContentResolver();
342
343 // Check if the intent has data for the slice uri on it and use that
344 final Uri intentData = intent.getData();
345 if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) {
346 return intentData;
347 }
348 // Otherwise ask the app
349 List<ResolveInfo> providers =
350 mContext.getPackageManager().queryIntentContentProviders(intent, 0);
351 if (providers == null || providers.isEmpty()) {
Jason Monkf2008872018-02-23 08:59:31 -0500352 // There are no providers, see if this activity has a direct link.
353 ResolveInfo resolve = mContext.getPackageManager().resolveActivity(intent,
354 PackageManager.GET_META_DATA);
355 if (resolve != null && resolve.activityInfo != null
356 && resolve.activityInfo.metaData != null
357 && resolve.activityInfo.metaData.containsKey(SLICE_METADATA_KEY)) {
358 return Uri.parse(
359 resolve.activityInfo.metaData.getString(SLICE_METADATA_KEY));
360 }
361 return null;
Jason Monk7b8fef22018-01-30 16:04:14 -0500362 }
363 String authority = providers.get(0).providerInfo.authority;
364 Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
365 .authority(authority).build();
Jason Monk632def12018-02-01 15:21:16 -0500366 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
367 if (provider == null) {
368 throw new IllegalArgumentException("Unknown URI " + uri);
369 }
Jason Monk7b8fef22018-01-30 16:04:14 -0500370 Bundle extras = new Bundle();
371 extras.putParcelable(SliceProvider.EXTRA_INTENT, intent);
Jason Monk632def12018-02-01 15:21:16 -0500372 final Bundle res = provider.call(SliceProvider.METHOD_MAP_ONLY_INTENT, null, extras);
Jason Monk7b8fef22018-01-30 16:04:14 -0500373 if (res == null) {
374 return null;
375 }
376 return res.getParcelable(SliceProvider.EXTRA_SLICE);
377 } catch (RemoteException e) {
378 // Arbitrary and not worth documenting, as Activity
379 // Manager will kill this process shortly anyway.
380 return null;
Jason Monk7b8fef22018-01-30 16:04:14 -0500381 }
382 }
383
384 /**
Jason Monkb9e06a82018-01-16 15:32:53 -0500385 * Turns a slice intent into slice content. Expects an explicit intent. If there is no
386 * {@link android.content.ContentProvider} associated with the given intent this will throw
387 * {@link IllegalArgumentException}.
388 *
389 * @param intent The intent associated with a slice.
390 * @param supportedSpecs List of supported specs.
391 * @return The Slice provided by the app or null if none is given.
392 * @see Slice
393 * @see SliceProvider#onMapIntentToUri(Intent)
394 * @see Intent
395 */
396 public @Nullable Slice bindSlice(@NonNull Intent intent,
397 @NonNull List<SliceSpec> supportedSpecs) {
398 Preconditions.checkNotNull(intent, "intent");
399 Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null,
Jason Monk7b8fef22018-01-30 16:04:14 -0500400 "Slice intent must be explicit %s", intent);
Jason Monkb9e06a82018-01-16 15:32:53 -0500401 ContentResolver resolver = mContext.getContentResolver();
402
403 // Check if the intent has data for the slice uri on it and use that
404 final Uri intentData = intent.getData();
405 if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) {
406 return bindSlice(intentData, supportedSpecs);
407 }
408 // Otherwise ask the app
409 List<ResolveInfo> providers =
410 mContext.getPackageManager().queryIntentContentProviders(intent, 0);
Jason Monk7b8fef22018-01-30 16:04:14 -0500411 if (providers == null || providers.isEmpty()) {
Jason Monkf2008872018-02-23 08:59:31 -0500412 // There are no providers, see if this activity has a direct link.
413 ResolveInfo resolve = mContext.getPackageManager().resolveActivity(intent,
414 PackageManager.GET_META_DATA);
415 if (resolve != null && resolve.activityInfo != null
416 && resolve.activityInfo.metaData != null
417 && resolve.activityInfo.metaData.containsKey(SLICE_METADATA_KEY)) {
418 return bindSlice(Uri.parse(resolve.activityInfo.metaData
419 .getString(SLICE_METADATA_KEY)), supportedSpecs);
420 }
421 return null;
Jason Monkb9e06a82018-01-16 15:32:53 -0500422 }
423 String authority = providers.get(0).providerInfo.authority;
424 Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
425 .authority(authority).build();
Jason Monk632def12018-02-01 15:21:16 -0500426 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
427 if (provider == null) {
428 throw new IllegalArgumentException("Unknown URI " + uri);
429 }
Jason Monkb9e06a82018-01-16 15:32:53 -0500430 Bundle extras = new Bundle();
431 extras.putParcelable(SliceProvider.EXTRA_INTENT, intent);
432 extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS,
433 new ArrayList<>(supportedSpecs));
Jason Monk632def12018-02-01 15:21:16 -0500434 final Bundle res = provider.call(SliceProvider.METHOD_MAP_INTENT, null, extras);
Jason Monkb9e06a82018-01-16 15:32:53 -0500435 if (res == null) {
436 return null;
437 }
438 return res.getParcelable(SliceProvider.EXTRA_SLICE);
439 } catch (RemoteException e) {
440 // Arbitrary and not worth documenting, as Activity
441 // Manager will kill this process shortly anyway.
442 return null;
Jason Monkb9e06a82018-01-16 15:32:53 -0500443 }
444 }
445
446 /**
Jason Monke8f8be72018-01-21 10:10:35 -0500447 * Does the permission check to see if a caller has access to a specific slice.
448 * @hide
449 */
450 public void enforceSlicePermission(Uri uri, String pkg, int pid, int uid) {
451 try {
452 if (pkg == null) {
453 throw new SecurityException("No pkg specified");
454 }
455 int result = mService.checkSlicePermission(uri, pkg, pid, uid);
456 if (result == PERMISSION_DENIED) {
457 throw new SecurityException("User " + uid + " does not have slice permission for "
458 + uri + ".");
459 }
460 if (result == PERMISSION_USER_GRANTED) {
461 // We just had a user grant of this permission and need to grant this to the app
462 // permanently.
463 mContext.grantUriPermission(pkg, uri.buildUpon().path("").build(),
464 Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
465 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
466 | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
467 }
468 } catch (RemoteException e) {
469 throw e.rethrowFromSystemServer();
470 }
471 }
472
473 /**
474 * Called by SystemUI to grant a slice permission after a dialog is shown.
475 * @hide
476 */
477 public void grantPermissionFromUser(Uri uri, String pkg, boolean allSlices) {
478 try {
479 mService.grantPermissionFromUser(uri, pkg, mContext.getPackageName(), allSlices);
480 } catch (RemoteException e) {
481 throw e.rethrowFromSystemServer();
482 }
483 }
484
485 /**
Jason Monke2c64512017-12-11 15:14:54 -0500486 * Class that listens to changes in {@link Slice}s.
Jason Monk74f5e362017-12-06 08:56:33 -0500487 */
Jason Monke2c64512017-12-11 15:14:54 -0500488 public interface SliceCallback {
Jason Monk74f5e362017-12-06 08:56:33 -0500489
490 /**
Jason Monke2c64512017-12-11 15:14:54 -0500491 * Called when slice is updated.
492 *
493 * @param s The updated slice.
494 * @see #registerSliceCallback
Jason Monk74f5e362017-12-06 08:56:33 -0500495 */
Jason Monke2c64512017-12-11 15:14:54 -0500496 void onSliceUpdated(Slice s);
Jason Monk74f5e362017-12-06 08:56:33 -0500497 }
Jason Monk8f5f7ff2017-10-17 14:12:42 -0400498}