blob: 67a72ec33fd82eafeee92d705e3999d99f7b4c55 [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 Monke2c64512017-12-11 15:14:54 -050019import android.annotation.NonNull;
Jason Monkb9e06a82018-01-16 15:32:53 -050020import android.annotation.Nullable;
Jason Monk5e676a22018-03-08 14:18:55 -050021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040023import android.annotation.SystemService;
Jason Monk632def12018-02-01 15:21:16 -050024import android.content.ContentProviderClient;
Jason Monkb9e06a82018-01-16 15:32:53 -050025import android.content.ContentResolver;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040026import android.content.Context;
Jason Monkb9e06a82018-01-16 15:32:53 -050027import android.content.Intent;
Jason Monkf2008872018-02-23 08:59:31 -050028import android.content.pm.PackageManager;
Jason Monkb9e06a82018-01-16 15:32:53 -050029import android.content.pm.ResolveInfo;
Jason Monk74f5e362017-12-06 08:56:33 -050030import android.net.Uri;
Jason Monk38df2802018-02-22 19:28:12 -050031import android.os.Binder;
Jason Monkb9e06a82018-01-16 15:32:53 -050032import android.os.Bundle;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040033import android.os.Handler;
Jason Monk38df2802018-02-22 19:28:12 -050034import android.os.IBinder;
Jason Monk74f5e362017-12-06 08:56:33 -050035import android.os.RemoteException;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040036import android.os.ServiceManager;
37import android.os.ServiceManager.ServiceNotFoundException;
Jason Monk5f8cc272018-01-16 17:57:20 -050038import android.util.Log;
Jason Monke2c64512017-12-11 15:14:54 -050039
Jason Monkb9e06a82018-01-16 15:32:53 -050040import com.android.internal.util.Preconditions;
41
42import java.util.ArrayList;
Jason Monke2c64512017-12-11 15:14:54 -050043import java.util.Arrays;
Jason Monk5f8cc272018-01-16 17:57:20 -050044import java.util.Collection;
45import java.util.Collections;
Jason Monke2c64512017-12-11 15:14:54 -050046import java.util.List;
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 /**
Jason Monk5e676a22018-03-08 14:18:55 -050065 * Category used to resolve intents that can be rendered as slices.
66 * <p>
67 * This category should be included on intent filters on providers that extend
68 * {@link SliceProvider}.
69 * @see SliceProvider
70 * @see SliceProvider#onMapIntentToUri(Intent)
71 * @see #mapIntentToUri(Intent)
72 */
73 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
74 public static final String CATEGORY_SLICE = "android.app.slice.category.SLICE";
75
76 /**
Mady Mellor3267ed82018-02-21 11:42:31 -080077 * The meta-data key that allows an activity to easily be linked directly to a slice.
78 * <p>
79 * An activity can be statically linked to a slice uri by including a meta-data item
80 * for this key that contains a valid slice uri for the same application declaring
81 * the activity.
82 */
83 public static final String SLICE_METADATA_KEY = "android.metadata.SLICE_URI";
84
Jason Monk8f5f7ff2017-10-17 14:12:42 -040085 private final ISliceManager mService;
86 private final Context mContext;
Jason Monk38df2802018-02-22 19:28:12 -050087 private final IBinder mToken = new Binder();
Jason Monk8f5f7ff2017-10-17 14:12:42 -040088
Jason Monke2c64512017-12-11 15:14:54 -050089 /**
Jason Monke8f8be72018-01-21 10:10:35 -050090 * Permission denied.
91 * @hide
92 */
93 public static final int PERMISSION_DENIED = -1;
94 /**
95 * Permission granted.
96 * @hide
97 */
98 public static final int PERMISSION_GRANTED = 0;
99 /**
100 * Permission just granted by the user, and should be granted uri permission as well.
101 * @hide
102 */
103 public static final int PERMISSION_USER_GRANTED = 1;
104
105 /**
Jason Monke2c64512017-12-11 15:14:54 -0500106 * @hide
107 */
Jason Monk8f5f7ff2017-10-17 14:12:42 -0400108 public SliceManager(Context context, Handler handler) throws ServiceNotFoundException {
109 mContext = context;
110 mService = ISliceManager.Stub.asInterface(
111 ServiceManager.getServiceOrThrow(Context.SLICE_SERVICE));
112 }
Jason Monk74f5e362017-12-06 08:56:33 -0500113
114 /**
Jason Monke2c64512017-12-11 15:14:54 -0500115 * Ensures that a slice is in a pinned state.
116 * <p>
117 * Pinned state is not persisted across reboots, so apps are expected to re-pin any slices
118 * they still care about after a reboot.
Jason Monk632def12018-02-01 15:21:16 -0500119 * <p>
120 * This may only be called by apps that are the default launcher for the device
121 * or the default voice interaction service. Otherwise will throw {@link SecurityException}.
Jason Monke2c64512017-12-11 15:14:54 -0500122 *
123 * @param uri The uri of the slice being pinned.
124 * @param specs The list of supported {@link SliceSpec}s of the callback.
125 * @see SliceProvider#onSlicePinned(Uri)
Jason Monk632def12018-02-01 15:21:16 -0500126 * @see Intent#ACTION_ASSIST
127 * @see Intent#CATEGORY_HOME
Jason Monk74f5e362017-12-06 08:56:33 -0500128 */
Jason Monke2c64512017-12-11 15:14:54 -0500129 public void pinSlice(@NonNull Uri uri, @NonNull List<SliceSpec> specs) {
Jason Monk74f5e362017-12-06 08:56:33 -0500130 try {
Jason Monke2c64512017-12-11 15:14:54 -0500131 mService.pinSlice(mContext.getPackageName(), uri,
Jason Monk38df2802018-02-22 19:28:12 -0500132 specs.toArray(new SliceSpec[specs.size()]), mToken);
Jason Monk74f5e362017-12-06 08:56:33 -0500133 } catch (RemoteException e) {
134 throw e.rethrowFromSystemServer();
135 }
136 }
137
138 /**
Jason Monke2c64512017-12-11 15:14:54 -0500139 * Remove a pin for a slice.
140 * <p>
141 * If the slice has no other pins/callbacks then the slice will be unpinned.
Jason Monk632def12018-02-01 15:21:16 -0500142 * <p>
143 * This may only be called by apps that are the default launcher for the device
144 * or the default voice interaction service. Otherwise will throw {@link SecurityException}.
Jason Monke2c64512017-12-11 15:14:54 -0500145 *
146 * @param uri The uri of the slice being unpinned.
147 * @see #pinSlice
148 * @see SliceProvider#onSliceUnpinned(Uri)
Jason Monk632def12018-02-01 15:21:16 -0500149 * @see Intent#ACTION_ASSIST
150 * @see Intent#CATEGORY_HOME
Jason Monk74f5e362017-12-06 08:56:33 -0500151 */
Jason Monke2c64512017-12-11 15:14:54 -0500152 public void unpinSlice(@NonNull Uri uri) {
Jason Monk74f5e362017-12-06 08:56:33 -0500153 try {
Jason Monk38df2802018-02-22 19:28:12 -0500154 mService.unpinSlice(mContext.getPackageName(), uri, mToken);
Jason Monk74f5e362017-12-06 08:56:33 -0500155 } catch (RemoteException e) {
156 throw e.rethrowFromSystemServer();
157 }
158 }
159
160 /**
Jason Monke2c64512017-12-11 15:14:54 -0500161 * @hide
Jason Monk74f5e362017-12-06 08:56:33 -0500162 */
163 public boolean hasSliceAccess() {
164 try {
165 return mService.hasSliceAccess(mContext.getPackageName());
166 } catch (RemoteException e) {
167 throw e.rethrowFromSystemServer();
168 }
169 }
170
171 /**
Jason Monke2c64512017-12-11 15:14:54 -0500172 * Get the current set of specs for a pinned slice.
173 * <p>
174 * This is the set of specs supported for a specific pinned slice. It will take
175 * into account all clients and returns only specs supported by all.
176 * @see SliceSpec
Jason Monk74f5e362017-12-06 08:56:33 -0500177 */
Jason Monke2c64512017-12-11 15:14:54 -0500178 public @NonNull List<SliceSpec> getPinnedSpecs(Uri uri) {
Jason Monk74f5e362017-12-06 08:56:33 -0500179 try {
Jason Monke2c64512017-12-11 15:14:54 -0500180 return Arrays.asList(mService.getPinnedSpecs(uri, mContext.getPackageName()));
Jason Monk74f5e362017-12-06 08:56:33 -0500181 } catch (RemoteException e) {
182 throw e.rethrowFromSystemServer();
183 }
184 }
185
186 /**
Jason Monkf88d25e2018-03-06 20:13:24 -0500187 * Get the list of currently pinned slices for this app.
188 * @see SliceProvider#onSlicePinned
189 */
190 public @NonNull List<Uri> getPinnedSlices() {
191 try {
192 return Arrays.asList(mService.getPinnedSlices(mContext.getPackageName()));
193 } catch (RemoteException e) {
194 throw e.rethrowFromSystemServer();
195 }
196 }
197
198 /**
Jason Monk5f8cc272018-01-16 17:57:20 -0500199 * Obtains a list of slices that are descendants of the specified Uri.
200 * <p>
201 * Not all slice providers will implement this functionality, in which case,
202 * an empty collection will be returned.
203 *
204 * @param uri The uri to look for descendants under.
205 * @return All slices within the space.
206 * @see SliceProvider#onGetSliceDescendants(Uri)
207 */
208 public @NonNull Collection<Uri> getSliceDescendants(@NonNull Uri uri) {
209 ContentResolver resolver = mContext.getContentResolver();
Jason Monk632def12018-02-01 15:21:16 -0500210 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
Jason Monk5f8cc272018-01-16 17:57:20 -0500211 Bundle extras = new Bundle();
212 extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri);
Jason Monk632def12018-02-01 15:21:16 -0500213 final Bundle res = provider.call(SliceProvider.METHOD_GET_DESCENDANTS, null, extras);
Jason Monk5f8cc272018-01-16 17:57:20 -0500214 return res.getParcelableArrayList(SliceProvider.EXTRA_SLICE_DESCENDANTS);
215 } catch (RemoteException e) {
216 Log.e(TAG, "Unable to get slice descendants", e);
Jason Monk5f8cc272018-01-16 17:57:20 -0500217 }
218 return Collections.emptyList();
219 }
220
221 /**
Jason Monkb9e06a82018-01-16 15:32:53 -0500222 * Turns a slice Uri into slice content.
223 *
224 * @param uri The URI to a slice provider
225 * @param supportedSpecs List of supported specs.
226 * @return The Slice provided by the app or null if none is given.
227 * @see Slice
228 */
229 public @Nullable Slice bindSlice(@NonNull Uri uri, @NonNull List<SliceSpec> supportedSpecs) {
230 Preconditions.checkNotNull(uri, "uri");
231 ContentResolver resolver = mContext.getContentResolver();
Jason Monk632def12018-02-01 15:21:16 -0500232 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
233 if (provider == null) {
234 throw new IllegalArgumentException("Unknown URI " + uri);
235 }
Jason Monkb9e06a82018-01-16 15:32:53 -0500236 Bundle extras = new Bundle();
237 extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri);
238 extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS,
239 new ArrayList<>(supportedSpecs));
Jason Monk632def12018-02-01 15:21:16 -0500240 final Bundle res = provider.call(SliceProvider.METHOD_SLICE, null, extras);
Jason Monkb9e06a82018-01-16 15:32:53 -0500241 Bundle.setDefusable(res, true);
242 if (res == null) {
243 return null;
244 }
245 return res.getParcelable(SliceProvider.EXTRA_SLICE);
246 } catch (RemoteException e) {
247 // Arbitrary and not worth documenting, as Activity
248 // Manager will kill this process shortly anyway.
249 return null;
Jason Monkb9e06a82018-01-16 15:32:53 -0500250 }
251 }
252
253 /**
Jason Monkf2008872018-02-23 08:59:31 -0500254 * Turns a slice intent into a slice uri. Expects an explicit intent.
Jason Monk5e676a22018-03-08 14:18:55 -0500255 * <p>
256 * This goes through a several stage resolution process to determine if any slice
257 * can represent this intent.
258 * - If the intent contains data that {@link ContentResolver#getType} is
259 * {@link SliceProvider#SLICE_TYPE} then the data will be returned.
260 * - If the intent with {@link #CATEGORY_SLICE} added resolves to a provider, then
261 * the provider will be asked to {@link SliceProvider#onMapIntentToUri} and that result
262 * will be returned.
263 * - Lastly, if the intent explicitly points at an activity, and that activity has
264 * meta-data for key {@link #SLICE_METADATA_KEY}, then the Uri specified there will be
265 * returned.
266 * - If no slice is found, then {@code null} is returned.
Jason Monk7b8fef22018-01-30 16:04:14 -0500267 *
268 * @param intent The intent associated with a slice.
Jason Monkf2008872018-02-23 08:59:31 -0500269 * @return The Slice Uri provided by the app or null if none exists.
Jason Monk7b8fef22018-01-30 16:04:14 -0500270 * @see Slice
271 * @see SliceProvider#onMapIntentToUri(Intent)
272 * @see Intent
273 */
274 public @Nullable Uri mapIntentToUri(@NonNull Intent intent) {
275 Preconditions.checkNotNull(intent, "intent");
Jason Monk135f4172018-03-15 17:48:47 -0400276 Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null
277 || intent.getData() != null,
Jason Monk7b8fef22018-01-30 16:04:14 -0500278 "Slice intent must be explicit %s", intent);
279 ContentResolver resolver = mContext.getContentResolver();
280
281 // Check if the intent has data for the slice uri on it and use that
282 final Uri intentData = intent.getData();
283 if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) {
284 return intentData;
285 }
286 // Otherwise ask the app
Jason Monk5e676a22018-03-08 14:18:55 -0500287 Intent queryIntent = new Intent(intent);
288 if (!queryIntent.hasCategory(CATEGORY_SLICE)) {
289 queryIntent.addCategory(CATEGORY_SLICE);
290 }
Jason Monk7b8fef22018-01-30 16:04:14 -0500291 List<ResolveInfo> providers =
Jason Monk5e676a22018-03-08 14:18:55 -0500292 mContext.getPackageManager().queryIntentContentProviders(queryIntent, 0);
Jason Monk7b8fef22018-01-30 16:04:14 -0500293 if (providers == null || providers.isEmpty()) {
Jason Monkf2008872018-02-23 08:59:31 -0500294 // There are no providers, see if this activity has a direct link.
295 ResolveInfo resolve = mContext.getPackageManager().resolveActivity(intent,
296 PackageManager.GET_META_DATA);
297 if (resolve != null && resolve.activityInfo != null
298 && resolve.activityInfo.metaData != null
299 && resolve.activityInfo.metaData.containsKey(SLICE_METADATA_KEY)) {
300 return Uri.parse(
301 resolve.activityInfo.metaData.getString(SLICE_METADATA_KEY));
302 }
303 return null;
Jason Monk7b8fef22018-01-30 16:04:14 -0500304 }
305 String authority = providers.get(0).providerInfo.authority;
306 Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
307 .authority(authority).build();
Jason Monk632def12018-02-01 15:21:16 -0500308 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
309 if (provider == null) {
310 throw new IllegalArgumentException("Unknown URI " + uri);
311 }
Jason Monk7b8fef22018-01-30 16:04:14 -0500312 Bundle extras = new Bundle();
313 extras.putParcelable(SliceProvider.EXTRA_INTENT, intent);
Jason Monk632def12018-02-01 15:21:16 -0500314 final Bundle res = provider.call(SliceProvider.METHOD_MAP_ONLY_INTENT, null, extras);
Jason Monk7b8fef22018-01-30 16:04:14 -0500315 if (res == null) {
316 return null;
317 }
318 return res.getParcelable(SliceProvider.EXTRA_SLICE);
319 } catch (RemoteException e) {
320 // Arbitrary and not worth documenting, as Activity
321 // Manager will kill this process shortly anyway.
322 return null;
Jason Monk7b8fef22018-01-30 16:04:14 -0500323 }
324 }
325
326 /**
Jason Monkb9e06a82018-01-16 15:32:53 -0500327 * Turns a slice intent into slice content. Expects an explicit intent. If there is no
328 * {@link android.content.ContentProvider} associated with the given intent this will throw
329 * {@link IllegalArgumentException}.
330 *
331 * @param intent The intent associated with a slice.
332 * @param supportedSpecs List of supported specs.
333 * @return The Slice provided by the app or null if none is given.
334 * @see Slice
335 * @see SliceProvider#onMapIntentToUri(Intent)
336 * @see Intent
337 */
338 public @Nullable Slice bindSlice(@NonNull Intent intent,
339 @NonNull List<SliceSpec> supportedSpecs) {
340 Preconditions.checkNotNull(intent, "intent");
Jason Monk135f4172018-03-15 17:48:47 -0400341 Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null
342 || intent.getData() != null,
Jason Monk7b8fef22018-01-30 16:04:14 -0500343 "Slice intent must be explicit %s", intent);
Jason Monkb9e06a82018-01-16 15:32:53 -0500344 ContentResolver resolver = mContext.getContentResolver();
345
346 // Check if the intent has data for the slice uri on it and use that
347 final Uri intentData = intent.getData();
348 if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) {
349 return bindSlice(intentData, supportedSpecs);
350 }
351 // Otherwise ask the app
352 List<ResolveInfo> providers =
353 mContext.getPackageManager().queryIntentContentProviders(intent, 0);
Jason Monk7b8fef22018-01-30 16:04:14 -0500354 if (providers == null || providers.isEmpty()) {
Jason Monkf2008872018-02-23 08:59:31 -0500355 // There are no providers, see if this activity has a direct link.
356 ResolveInfo resolve = mContext.getPackageManager().resolveActivity(intent,
357 PackageManager.GET_META_DATA);
358 if (resolve != null && resolve.activityInfo != null
359 && resolve.activityInfo.metaData != null
360 && resolve.activityInfo.metaData.containsKey(SLICE_METADATA_KEY)) {
361 return bindSlice(Uri.parse(resolve.activityInfo.metaData
362 .getString(SLICE_METADATA_KEY)), supportedSpecs);
363 }
364 return null;
Jason Monkb9e06a82018-01-16 15:32:53 -0500365 }
366 String authority = providers.get(0).providerInfo.authority;
367 Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
368 .authority(authority).build();
Jason Monk632def12018-02-01 15:21:16 -0500369 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
370 if (provider == null) {
371 throw new IllegalArgumentException("Unknown URI " + uri);
372 }
Jason Monkb9e06a82018-01-16 15:32:53 -0500373 Bundle extras = new Bundle();
374 extras.putParcelable(SliceProvider.EXTRA_INTENT, intent);
375 extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS,
376 new ArrayList<>(supportedSpecs));
Jason Monk632def12018-02-01 15:21:16 -0500377 final Bundle res = provider.call(SliceProvider.METHOD_MAP_INTENT, null, extras);
Jason Monkb9e06a82018-01-16 15:32:53 -0500378 if (res == null) {
379 return null;
380 }
381 return res.getParcelable(SliceProvider.EXTRA_SLICE);
382 } catch (RemoteException e) {
383 // Arbitrary and not worth documenting, as Activity
384 // Manager will kill this process shortly anyway.
385 return null;
Jason Monkb9e06a82018-01-16 15:32:53 -0500386 }
387 }
388
389 /**
Jason Monke8f8be72018-01-21 10:10:35 -0500390 * Does the permission check to see if a caller has access to a specific slice.
391 * @hide
392 */
393 public void enforceSlicePermission(Uri uri, String pkg, int pid, int uid) {
394 try {
395 if (pkg == null) {
396 throw new SecurityException("No pkg specified");
397 }
398 int result = mService.checkSlicePermission(uri, pkg, pid, uid);
399 if (result == PERMISSION_DENIED) {
400 throw new SecurityException("User " + uid + " does not have slice permission for "
401 + uri + ".");
402 }
403 if (result == PERMISSION_USER_GRANTED) {
404 // We just had a user grant of this permission and need to grant this to the app
405 // permanently.
406 mContext.grantUriPermission(pkg, uri.buildUpon().path("").build(),
407 Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
408 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
409 | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
410 }
411 } catch (RemoteException e) {
412 throw e.rethrowFromSystemServer();
413 }
414 }
415
416 /**
417 * Called by SystemUI to grant a slice permission after a dialog is shown.
418 * @hide
419 */
420 public void grantPermissionFromUser(Uri uri, String pkg, boolean allSlices) {
421 try {
422 mService.grantPermissionFromUser(uri, pkg, mContext.getPackageName(), allSlices);
423 } catch (RemoteException e) {
424 throw e.rethrowFromSystemServer();
425 }
426 }
Jason Monk8f5f7ff2017-10-17 14:12:42 -0400427}