blob: e5f3eae3ab5a3252038306c9955c790b2cbbf346 [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 Monkb9e06a82018-01-16 15:32:53 -050023import android.content.ContentResolver;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040024import android.content.Context;
Jason Monkb9e06a82018-01-16 15:32:53 -050025import android.content.IContentProvider;
26import android.content.Intent;
27import android.content.pm.ResolveInfo;
Jason Monk74f5e362017-12-06 08:56:33 -050028import android.net.Uri;
Jason Monkb9e06a82018-01-16 15:32:53 -050029import android.os.Bundle;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040030import android.os.Handler;
Jason Monk74f5e362017-12-06 08:56:33 -050031import android.os.RemoteException;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040032import android.os.ServiceManager;
33import android.os.ServiceManager.ServiceNotFoundException;
Jason Monke2c64512017-12-11 15:14:54 -050034import android.util.ArrayMap;
Jason Monk5f8cc272018-01-16 17:57:20 -050035import android.util.Log;
Jason Monke2c64512017-12-11 15:14:54 -050036import android.util.Pair;
37
Jason Monkb9e06a82018-01-16 15:32:53 -050038import com.android.internal.util.Preconditions;
39
40import java.util.ArrayList;
Jason Monke2c64512017-12-11 15:14:54 -050041import java.util.Arrays;
Jason Monk5f8cc272018-01-16 17:57:20 -050042import java.util.Collection;
43import java.util.Collections;
Jason Monke2c64512017-12-11 15:14:54 -050044import java.util.List;
45import java.util.concurrent.Executor;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040046
47/**
Jason Monke2c64512017-12-11 15:14:54 -050048 * Class to handle interactions with {@link Slice}s.
49 * <p>
50 * The SliceManager manages permissions and pinned state for slices.
Jason Monk8f5f7ff2017-10-17 14:12:42 -040051 */
52@SystemService(Context.SLICE_SERVICE)
53public class SliceManager {
54
Jason Monk5f8cc272018-01-16 17:57:20 -050055 private static final String TAG = "SliceManager";
56
Jason Monke8f8be72018-01-21 10:10:35 -050057 /**
58 * @hide
59 */
60 public static final String ACTION_REQUEST_SLICE_PERMISSION =
61 "android.intent.action.REQUEST_SLICE_PERMISSION";
62
Jason Monk8f5f7ff2017-10-17 14:12:42 -040063 private final ISliceManager mService;
64 private final Context mContext;
Jason Monke2c64512017-12-11 15:14:54 -050065 private final ArrayMap<Pair<Uri, SliceCallback>, ISliceListener> mListenerLookup =
66 new ArrayMap<>();
Jason Monk8f5f7ff2017-10-17 14:12:42 -040067
Jason Monke2c64512017-12-11 15:14:54 -050068 /**
Jason Monke8f8be72018-01-21 10:10:35 -050069 * Permission denied.
70 * @hide
71 */
72 public static final int PERMISSION_DENIED = -1;
73 /**
74 * Permission granted.
75 * @hide
76 */
77 public static final int PERMISSION_GRANTED = 0;
78 /**
79 * Permission just granted by the user, and should be granted uri permission as well.
80 * @hide
81 */
82 public static final int PERMISSION_USER_GRANTED = 1;
83
84 /**
Jason Monke2c64512017-12-11 15:14:54 -050085 * @hide
86 */
Jason Monk8f5f7ff2017-10-17 14:12:42 -040087 public SliceManager(Context context, Handler handler) throws ServiceNotFoundException {
88 mContext = context;
89 mService = ISliceManager.Stub.asInterface(
90 ServiceManager.getServiceOrThrow(Context.SLICE_SERVICE));
91 }
Jason Monk74f5e362017-12-06 08:56:33 -050092
93 /**
Jason Monk4ef50bc2018-01-22 10:52:23 -050094 * @deprecated TO BE REMOVED.
Jason Monk74f5e362017-12-06 08:56:33 -050095 */
Jason Monk4ef50bc2018-01-22 10:52:23 -050096 @Deprecated
Jason Monke2c64512017-12-11 15:14:54 -050097 public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback,
98 @NonNull List<SliceSpec> specs) {
Jason Monk4ef50bc2018-01-22 10:52:23 -050099 registerSliceCallback(uri, specs, mContext.getMainExecutor(), callback);
Jason Monke2c64512017-12-11 15:14:54 -0500100 }
101
102 /**
Jason Monk4ef50bc2018-01-22 10:52:23 -0500103 * @deprecated TO BE REMOVED.
Jason Monke2c64512017-12-11 15:14:54 -0500104 */
Jason Monk4ef50bc2018-01-22 10:52:23 -0500105 @Deprecated
Jason Monke2c64512017-12-11 15:14:54 -0500106 public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback,
107 @NonNull List<SliceSpec> specs, Executor executor) {
Jason Monk4ef50bc2018-01-22 10:52:23 -0500108 registerSliceCallback(uri, specs, executor, callback);
109 }
110
111 /**
112 * Adds a callback to a specific slice uri.
113 * <p>
114 * This is a convenience that performs a few slice actions at once. It will put
115 * the slice in a pinned state since there is a callback attached. It will also
116 * listen for content changes, when a content change observes, the android system
117 * will bind the new slice and provide it to all registered {@link SliceCallback}s.
118 *
119 * @param uri The uri of the slice being listened to.
120 * @param callback The listener that should receive the callbacks.
121 * @param specs The list of supported {@link SliceSpec}s of the callback.
122 * @see SliceProvider#onSlicePinned(Uri)
123 */
124 public void registerSliceCallback(@NonNull Uri uri, @NonNull List<SliceSpec> specs,
125 @NonNull SliceCallback callback) {
126 registerSliceCallback(uri, specs, mContext.getMainExecutor(), callback);
127 }
128
129 /**
130 * Adds a callback to a specific slice uri.
131 * <p>
132 * This is a convenience that performs a few slice actions at once. It will put
133 * the slice in a pinned state since there is a callback attached. It will also
134 * listen for content changes, when a content change observes, the android system
135 * will bind the new slice and provide it to all registered {@link SliceCallback}s.
136 *
137 * @param uri The uri of the slice being listened to.
138 * @param callback The listener that should receive the callbacks.
139 * @param specs The list of supported {@link SliceSpec}s of the callback.
140 * @see SliceProvider#onSlicePinned(Uri)
141 */
142 public void registerSliceCallback(@NonNull Uri uri, @NonNull List<SliceSpec> specs,
143 @NonNull @CallbackExecutor Executor executor, @NonNull SliceCallback callback) {
Jason Monk74f5e362017-12-06 08:56:33 -0500144 try {
Jason Monke2c64512017-12-11 15:14:54 -0500145 mService.addSliceListener(uri, mContext.getPackageName(),
146 getListener(uri, callback, new ISliceListener.Stub() {
147 @Override
148 public void onSliceUpdated(Slice s) throws RemoteException {
149 executor.execute(() -> callback.onSliceUpdated(s));
150 }
151 }), specs.toArray(new SliceSpec[specs.size()]));
152 } catch (RemoteException e) {
153 throw e.rethrowFromSystemServer();
154 }
155 }
156
157 private ISliceListener getListener(Uri uri, SliceCallback callback,
158 ISliceListener listener) {
159 Pair<Uri, SliceCallback> key = new Pair<>(uri, callback);
160 if (mListenerLookup.containsKey(key)) {
161 try {
162 mService.removeSliceListener(uri, mContext.getPackageName(),
163 mListenerLookup.get(key));
164 } catch (RemoteException e) {
165 throw e.rethrowFromSystemServer();
166 }
167 }
168 mListenerLookup.put(key, listener);
169 return listener;
170 }
171
172 /**
173 * Removes a callback for a specific slice uri.
174 * <p>
175 * Removes the app from the pinned state (if there are no other apps/callbacks pinning it)
176 * in addition to removing the callback.
177 *
178 * @param uri The uri of the slice being listened to
179 * @param callback The listener that should no longer receive callbacks.
180 * @see #registerSliceCallback
181 */
182 public void unregisterSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback) {
183 try {
184 mService.removeSliceListener(uri, mContext.getPackageName(),
185 mListenerLookup.remove(new Pair<>(uri, callback)));
Jason Monk74f5e362017-12-06 08:56:33 -0500186 } catch (RemoteException e) {
187 throw e.rethrowFromSystemServer();
188 }
189 }
190
191 /**
Jason Monke2c64512017-12-11 15:14:54 -0500192 * Ensures that a slice is in a pinned state.
193 * <p>
194 * Pinned state is not persisted across reboots, so apps are expected to re-pin any slices
195 * they still care about after a reboot.
196 *
197 * @param uri The uri of the slice being pinned.
198 * @param specs The list of supported {@link SliceSpec}s of the callback.
199 * @see SliceProvider#onSlicePinned(Uri)
Jason Monk74f5e362017-12-06 08:56:33 -0500200 */
Jason Monke2c64512017-12-11 15:14:54 -0500201 public void pinSlice(@NonNull Uri uri, @NonNull List<SliceSpec> specs) {
Jason Monk74f5e362017-12-06 08:56:33 -0500202 try {
Jason Monke2c64512017-12-11 15:14:54 -0500203 mService.pinSlice(mContext.getPackageName(), uri,
204 specs.toArray(new SliceSpec[specs.size()]));
Jason Monk74f5e362017-12-06 08:56:33 -0500205 } catch (RemoteException e) {
206 throw e.rethrowFromSystemServer();
207 }
208 }
209
210 /**
Jason Monke2c64512017-12-11 15:14:54 -0500211 * Remove a pin for a slice.
212 * <p>
213 * If the slice has no other pins/callbacks then the slice will be unpinned.
214 *
215 * @param uri The uri of the slice being unpinned.
216 * @see #pinSlice
217 * @see SliceProvider#onSliceUnpinned(Uri)
Jason Monk74f5e362017-12-06 08:56:33 -0500218 */
Jason Monke2c64512017-12-11 15:14:54 -0500219 public void unpinSlice(@NonNull Uri uri) {
Jason Monk74f5e362017-12-06 08:56:33 -0500220 try {
221 mService.unpinSlice(mContext.getPackageName(), uri);
222 } catch (RemoteException e) {
223 throw e.rethrowFromSystemServer();
224 }
225 }
226
227 /**
Jason Monke2c64512017-12-11 15:14:54 -0500228 * @hide
Jason Monk74f5e362017-12-06 08:56:33 -0500229 */
230 public boolean hasSliceAccess() {
231 try {
232 return mService.hasSliceAccess(mContext.getPackageName());
233 } catch (RemoteException e) {
234 throw e.rethrowFromSystemServer();
235 }
236 }
237
238 /**
Jason Monke2c64512017-12-11 15:14:54 -0500239 * Get the current set of specs for a pinned slice.
240 * <p>
241 * This is the set of specs supported for a specific pinned slice. It will take
242 * into account all clients and returns only specs supported by all.
243 * @see SliceSpec
Jason Monk74f5e362017-12-06 08:56:33 -0500244 */
Jason Monke2c64512017-12-11 15:14:54 -0500245 public @NonNull List<SliceSpec> getPinnedSpecs(Uri uri) {
Jason Monk74f5e362017-12-06 08:56:33 -0500246 try {
Jason Monke2c64512017-12-11 15:14:54 -0500247 return Arrays.asList(mService.getPinnedSpecs(uri, mContext.getPackageName()));
Jason Monk74f5e362017-12-06 08:56:33 -0500248 } catch (RemoteException e) {
249 throw e.rethrowFromSystemServer();
250 }
251 }
252
253 /**
Jason Monk5f8cc272018-01-16 17:57:20 -0500254 * Obtains a list of slices that are descendants of the specified Uri.
255 * <p>
256 * Not all slice providers will implement this functionality, in which case,
257 * an empty collection will be returned.
258 *
259 * @param uri The uri to look for descendants under.
260 * @return All slices within the space.
261 * @see SliceProvider#onGetSliceDescendants(Uri)
262 */
263 public @NonNull Collection<Uri> getSliceDescendants(@NonNull Uri uri) {
264 ContentResolver resolver = mContext.getContentResolver();
265 IContentProvider provider = resolver.acquireProvider(uri);
266 try {
267 Bundle extras = new Bundle();
268 extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri);
269 final Bundle res = provider.call(resolver.getPackageName(),
270 SliceProvider.METHOD_GET_DESCENDANTS, null, extras);
271 return res.getParcelableArrayList(SliceProvider.EXTRA_SLICE_DESCENDANTS);
272 } catch (RemoteException e) {
273 Log.e(TAG, "Unable to get slice descendants", e);
274 } finally {
275 resolver.releaseProvider(provider);
276 }
277 return Collections.emptyList();
278 }
279
280 /**
Jason Monkb9e06a82018-01-16 15:32:53 -0500281 * Turns a slice Uri into slice content.
282 *
283 * @param uri The URI to a slice provider
284 * @param supportedSpecs List of supported specs.
285 * @return The Slice provided by the app or null if none is given.
286 * @see Slice
287 */
288 public @Nullable Slice bindSlice(@NonNull Uri uri, @NonNull List<SliceSpec> supportedSpecs) {
289 Preconditions.checkNotNull(uri, "uri");
290 ContentResolver resolver = mContext.getContentResolver();
291 IContentProvider provider = resolver.acquireProvider(uri);
292 if (provider == null) {
293 throw new IllegalArgumentException("Unknown URI " + uri);
294 }
295 try {
296 Bundle extras = new Bundle();
297 extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri);
298 extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS,
299 new ArrayList<>(supportedSpecs));
Jason Monke8f8be72018-01-21 10:10:35 -0500300 final Bundle res = provider.call(mContext.getPackageName(), SliceProvider.METHOD_SLICE,
Jason Monkb9e06a82018-01-16 15:32:53 -0500301 null, extras);
302 Bundle.setDefusable(res, true);
303 if (res == null) {
304 return null;
305 }
306 return res.getParcelable(SliceProvider.EXTRA_SLICE);
307 } catch (RemoteException e) {
308 // Arbitrary and not worth documenting, as Activity
309 // Manager will kill this process shortly anyway.
310 return null;
311 } finally {
312 resolver.releaseProvider(provider);
313 }
314 }
315
316 /**
Jason Monk7b8fef22018-01-30 16:04:14 -0500317 * Turns a slice intent into a slice uri. Expects an explicit intent. If there is no
318 * {@link android.content.ContentProvider} associated with the given intent this will throw
319 * {@link IllegalArgumentException}.
320 *
321 * @param intent The intent associated with a slice.
322 * @return The Slice Uri provided by the app or null if none is given.
323 * @see Slice
324 * @see SliceProvider#onMapIntentToUri(Intent)
325 * @see Intent
326 */
327 public @Nullable Uri mapIntentToUri(@NonNull Intent intent) {
328 Preconditions.checkNotNull(intent, "intent");
329 Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null,
330 "Slice intent must be explicit %s", intent);
331 ContentResolver resolver = mContext.getContentResolver();
332
333 // Check if the intent has data for the slice uri on it and use that
334 final Uri intentData = intent.getData();
335 if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) {
336 return intentData;
337 }
338 // Otherwise ask the app
339 List<ResolveInfo> providers =
340 mContext.getPackageManager().queryIntentContentProviders(intent, 0);
341 if (providers == null || providers.isEmpty()) {
342 throw new IllegalArgumentException("Unable to resolve intent " + intent);
343 }
344 String authority = providers.get(0).providerInfo.authority;
345 Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
346 .authority(authority).build();
347 IContentProvider provider = resolver.acquireProvider(uri);
348 if (provider == null) {
349 throw new IllegalArgumentException("Unknown URI " + uri);
350 }
351 try {
352 Bundle extras = new Bundle();
353 extras.putParcelable(SliceProvider.EXTRA_INTENT, intent);
354 final Bundle res = provider.call(mContext.getPackageName(),
355 SliceProvider.METHOD_MAP_ONLY_INTENT, null, extras);
356 if (res == null) {
357 return null;
358 }
359 return res.getParcelable(SliceProvider.EXTRA_SLICE);
360 } catch (RemoteException e) {
361 // Arbitrary and not worth documenting, as Activity
362 // Manager will kill this process shortly anyway.
363 return null;
364 } finally {
365 resolver.releaseProvider(provider);
366 }
367 }
368
369 /**
Jason Monkb9e06a82018-01-16 15:32:53 -0500370 * Turns a slice intent into slice content. Expects an explicit intent. If there is no
371 * {@link android.content.ContentProvider} associated with the given intent this will throw
372 * {@link IllegalArgumentException}.
373 *
374 * @param intent The intent associated with a slice.
375 * @param supportedSpecs List of supported specs.
376 * @return The Slice provided by the app or null if none is given.
377 * @see Slice
378 * @see SliceProvider#onMapIntentToUri(Intent)
379 * @see Intent
380 */
381 public @Nullable Slice bindSlice(@NonNull Intent intent,
382 @NonNull List<SliceSpec> supportedSpecs) {
383 Preconditions.checkNotNull(intent, "intent");
384 Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null,
Jason Monk7b8fef22018-01-30 16:04:14 -0500385 "Slice intent must be explicit %s", intent);
Jason Monkb9e06a82018-01-16 15:32:53 -0500386 ContentResolver resolver = mContext.getContentResolver();
387
388 // Check if the intent has data for the slice uri on it and use that
389 final Uri intentData = intent.getData();
390 if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) {
391 return bindSlice(intentData, supportedSpecs);
392 }
393 // Otherwise ask the app
394 List<ResolveInfo> providers =
395 mContext.getPackageManager().queryIntentContentProviders(intent, 0);
Jason Monk7b8fef22018-01-30 16:04:14 -0500396 if (providers == null || providers.isEmpty()) {
Jason Monkb9e06a82018-01-16 15:32:53 -0500397 throw new IllegalArgumentException("Unable to resolve intent " + intent);
398 }
399 String authority = providers.get(0).providerInfo.authority;
400 Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
401 .authority(authority).build();
402 IContentProvider provider = resolver.acquireProvider(uri);
403 if (provider == null) {
404 throw new IllegalArgumentException("Unknown URI " + uri);
405 }
406 try {
407 Bundle extras = new Bundle();
408 extras.putParcelable(SliceProvider.EXTRA_INTENT, intent);
409 extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS,
410 new ArrayList<>(supportedSpecs));
Jason Monke8f8be72018-01-21 10:10:35 -0500411 final Bundle res = provider.call(mContext.getPackageName(),
Jason Monkb9e06a82018-01-16 15:32:53 -0500412 SliceProvider.METHOD_MAP_INTENT, null, extras);
413 if (res == null) {
414 return null;
415 }
416 return res.getParcelable(SliceProvider.EXTRA_SLICE);
417 } catch (RemoteException e) {
418 // Arbitrary and not worth documenting, as Activity
419 // Manager will kill this process shortly anyway.
420 return null;
421 } finally {
422 resolver.releaseProvider(provider);
423 }
424 }
425
426 /**
Jason Monke8f8be72018-01-21 10:10:35 -0500427 * Does the permission check to see if a caller has access to a specific slice.
428 * @hide
429 */
430 public void enforceSlicePermission(Uri uri, String pkg, int pid, int uid) {
431 try {
432 if (pkg == null) {
433 throw new SecurityException("No pkg specified");
434 }
435 int result = mService.checkSlicePermission(uri, pkg, pid, uid);
436 if (result == PERMISSION_DENIED) {
437 throw new SecurityException("User " + uid + " does not have slice permission for "
438 + uri + ".");
439 }
440 if (result == PERMISSION_USER_GRANTED) {
441 // We just had a user grant of this permission and need to grant this to the app
442 // permanently.
443 mContext.grantUriPermission(pkg, uri.buildUpon().path("").build(),
444 Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
445 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
446 | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
447 }
448 } catch (RemoteException e) {
449 throw e.rethrowFromSystemServer();
450 }
451 }
452
453 /**
454 * Called by SystemUI to grant a slice permission after a dialog is shown.
455 * @hide
456 */
457 public void grantPermissionFromUser(Uri uri, String pkg, boolean allSlices) {
458 try {
459 mService.grantPermissionFromUser(uri, pkg, mContext.getPackageName(), allSlices);
460 } catch (RemoteException e) {
461 throw e.rethrowFromSystemServer();
462 }
463 }
464
465 /**
Jason Monke2c64512017-12-11 15:14:54 -0500466 * Class that listens to changes in {@link Slice}s.
Jason Monk74f5e362017-12-06 08:56:33 -0500467 */
Jason Monke2c64512017-12-11 15:14:54 -0500468 public interface SliceCallback {
Jason Monk74f5e362017-12-06 08:56:33 -0500469
470 /**
Jason Monke2c64512017-12-11 15:14:54 -0500471 * Called when slice is updated.
472 *
473 * @param s The updated slice.
474 * @see #registerSliceCallback
Jason Monk74f5e362017-12-06 08:56:33 -0500475 */
Jason Monke2c64512017-12-11 15:14:54 -0500476 void onSliceUpdated(Slice s);
Jason Monk74f5e362017-12-06 08:56:33 -0500477 }
Jason Monk8f5f7ff2017-10-17 14:12:42 -0400478}