blob: 5d40ffd6e55338496ccf6dc93defe5ae2c0334d4 [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;
27import android.content.pm.ResolveInfo;
Jason Monk74f5e362017-12-06 08:56:33 -050028import android.net.Uri;
Jason Monk38df2802018-02-22 19:28:12 -050029import android.os.Binder;
Jason Monkb9e06a82018-01-16 15:32:53 -050030import android.os.Bundle;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040031import android.os.Handler;
Jason Monk38df2802018-02-22 19:28:12 -050032import android.os.IBinder;
Jason Monk74f5e362017-12-06 08:56:33 -050033import android.os.RemoteException;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040034import android.os.ServiceManager;
35import android.os.ServiceManager.ServiceNotFoundException;
Jason Monke2c64512017-12-11 15:14:54 -050036import android.util.ArrayMap;
Jason Monk5f8cc272018-01-16 17:57:20 -050037import android.util.Log;
Jason Monke2c64512017-12-11 15:14:54 -050038import android.util.Pair;
39
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;
47import java.util.concurrent.Executor;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040048
49/**
Jason Monke2c64512017-12-11 15:14:54 -050050 * Class to handle interactions with {@link Slice}s.
51 * <p>
52 * The SliceManager manages permissions and pinned state for slices.
Jason Monk8f5f7ff2017-10-17 14:12:42 -040053 */
54@SystemService(Context.SLICE_SERVICE)
55public class SliceManager {
56
Jason Monk5f8cc272018-01-16 17:57:20 -050057 private static final String TAG = "SliceManager";
58
Jason Monke8f8be72018-01-21 10:10:35 -050059 /**
60 * @hide
61 */
62 public static final String ACTION_REQUEST_SLICE_PERMISSION =
63 "android.intent.action.REQUEST_SLICE_PERMISSION";
64
Jason Monk8f5f7ff2017-10-17 14:12:42 -040065 private final ISliceManager mService;
66 private final Context mContext;
Jason Monke2c64512017-12-11 15:14:54 -050067 private final ArrayMap<Pair<Uri, SliceCallback>, ISliceListener> mListenerLookup =
68 new ArrayMap<>();
Jason Monk38df2802018-02-22 19:28:12 -050069 private final IBinder mToken = new Binder();
Jason Monk8f5f7ff2017-10-17 14:12:42 -040070
Jason Monke2c64512017-12-11 15:14:54 -050071 /**
Jason Monke8f8be72018-01-21 10:10:35 -050072 * Permission denied.
73 * @hide
74 */
75 public static final int PERMISSION_DENIED = -1;
76 /**
77 * Permission granted.
78 * @hide
79 */
80 public static final int PERMISSION_GRANTED = 0;
81 /**
82 * Permission just granted by the user, and should be granted uri permission as well.
83 * @hide
84 */
85 public static final int PERMISSION_USER_GRANTED = 1;
86
87 /**
Jason Monke2c64512017-12-11 15:14:54 -050088 * @hide
89 */
Jason Monk8f5f7ff2017-10-17 14:12:42 -040090 public SliceManager(Context context, Handler handler) throws ServiceNotFoundException {
91 mContext = context;
92 mService = ISliceManager.Stub.asInterface(
93 ServiceManager.getServiceOrThrow(Context.SLICE_SERVICE));
94 }
Jason Monk74f5e362017-12-06 08:56:33 -050095
96 /**
Jason Monk4ef50bc2018-01-22 10:52:23 -050097 * @deprecated TO BE REMOVED.
Jason Monk74f5e362017-12-06 08:56:33 -050098 */
Jason Monk4ef50bc2018-01-22 10:52:23 -050099 @Deprecated
Jason Monke2c64512017-12-11 15:14:54 -0500100 public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback,
101 @NonNull List<SliceSpec> specs) {
Jason Monke2c64512017-12-11 15:14:54 -0500102 }
103
104 /**
Jason Monk4ef50bc2018-01-22 10:52:23 -0500105 * @deprecated TO BE REMOVED.
Jason Monke2c64512017-12-11 15:14:54 -0500106 */
Jason Monk4ef50bc2018-01-22 10:52:23 -0500107 @Deprecated
Jason Monke2c64512017-12-11 15:14:54 -0500108 public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback,
109 @NonNull List<SliceSpec> specs, Executor executor) {
Jason Monk4ef50bc2018-01-22 10:52:23 -0500110 }
111
112 /**
113 * Adds a callback to a specific slice uri.
114 * <p>
115 * This is a convenience that performs a few slice actions at once. It will put
116 * the slice in a pinned state since there is a callback attached. It will also
117 * listen for content changes, when a content change observes, the android system
118 * will bind the new slice and provide it to all registered {@link SliceCallback}s.
119 *
120 * @param uri The uri of the slice being listened to.
121 * @param callback The listener that should receive the callbacks.
122 * @param specs The list of supported {@link SliceSpec}s of the callback.
123 * @see SliceProvider#onSlicePinned(Uri)
124 */
125 public void registerSliceCallback(@NonNull Uri uri, @NonNull List<SliceSpec> specs,
126 @NonNull SliceCallback callback) {
Jason Monk4ef50bc2018-01-22 10:52:23 -0500127 }
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 Monke2c64512017-12-11 15:14:54 -0500144
Jason Monke2c64512017-12-11 15:14:54 -0500145 }
146
147 /**
148 * Removes a callback for a specific slice uri.
149 * <p>
150 * Removes the app from the pinned state (if there are no other apps/callbacks pinning it)
151 * in addition to removing the callback.
152 *
153 * @param uri The uri of the slice being listened to
154 * @param callback The listener that should no longer receive callbacks.
155 * @see #registerSliceCallback
156 */
157 public void unregisterSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback) {
Jason Monk38df2802018-02-22 19:28:12 -0500158
Jason Monk74f5e362017-12-06 08:56:33 -0500159 }
160
161 /**
Jason Monke2c64512017-12-11 15:14:54 -0500162 * Ensures that a slice is in a pinned state.
163 * <p>
164 * Pinned state is not persisted across reboots, so apps are expected to re-pin any slices
165 * they still care about after a reboot.
Jason Monk632def12018-02-01 15:21:16 -0500166 * <p>
167 * This may only be called by apps that are the default launcher for the device
168 * or the default voice interaction service. Otherwise will throw {@link SecurityException}.
Jason Monke2c64512017-12-11 15:14:54 -0500169 *
170 * @param uri The uri of the slice being pinned.
171 * @param specs The list of supported {@link SliceSpec}s of the callback.
172 * @see SliceProvider#onSlicePinned(Uri)
Jason Monk632def12018-02-01 15:21:16 -0500173 * @see Intent#ACTION_ASSIST
174 * @see Intent#CATEGORY_HOME
Jason Monk74f5e362017-12-06 08:56:33 -0500175 */
Jason Monke2c64512017-12-11 15:14:54 -0500176 public void pinSlice(@NonNull Uri uri, @NonNull List<SliceSpec> specs) {
Jason Monk74f5e362017-12-06 08:56:33 -0500177 try {
Jason Monke2c64512017-12-11 15:14:54 -0500178 mService.pinSlice(mContext.getPackageName(), uri,
Jason Monk38df2802018-02-22 19:28:12 -0500179 specs.toArray(new SliceSpec[specs.size()]), mToken);
Jason Monk74f5e362017-12-06 08:56:33 -0500180 } catch (RemoteException e) {
181 throw e.rethrowFromSystemServer();
182 }
183 }
184
185 /**
Jason Monke2c64512017-12-11 15:14:54 -0500186 * Remove a pin for a slice.
187 * <p>
188 * If the slice has no other pins/callbacks then the slice will be unpinned.
Jason Monk632def12018-02-01 15:21:16 -0500189 * <p>
190 * This may only be called by apps that are the default launcher for the device
191 * or the default voice interaction service. Otherwise will throw {@link SecurityException}.
Jason Monke2c64512017-12-11 15:14:54 -0500192 *
193 * @param uri The uri of the slice being unpinned.
194 * @see #pinSlice
195 * @see SliceProvider#onSliceUnpinned(Uri)
Jason Monk632def12018-02-01 15:21:16 -0500196 * @see Intent#ACTION_ASSIST
197 * @see Intent#CATEGORY_HOME
Jason Monk74f5e362017-12-06 08:56:33 -0500198 */
Jason Monke2c64512017-12-11 15:14:54 -0500199 public void unpinSlice(@NonNull Uri uri) {
Jason Monk74f5e362017-12-06 08:56:33 -0500200 try {
Jason Monk38df2802018-02-22 19:28:12 -0500201 mService.unpinSlice(mContext.getPackageName(), uri, mToken);
Jason Monk74f5e362017-12-06 08:56:33 -0500202 } catch (RemoteException e) {
203 throw e.rethrowFromSystemServer();
204 }
205 }
206
207 /**
Jason Monke2c64512017-12-11 15:14:54 -0500208 * @hide
Jason Monk74f5e362017-12-06 08:56:33 -0500209 */
210 public boolean hasSliceAccess() {
211 try {
212 return mService.hasSliceAccess(mContext.getPackageName());
213 } catch (RemoteException e) {
214 throw e.rethrowFromSystemServer();
215 }
216 }
217
218 /**
Jason Monke2c64512017-12-11 15:14:54 -0500219 * Get the current set of specs for a pinned slice.
220 * <p>
221 * This is the set of specs supported for a specific pinned slice. It will take
222 * into account all clients and returns only specs supported by all.
223 * @see SliceSpec
Jason Monk74f5e362017-12-06 08:56:33 -0500224 */
Jason Monke2c64512017-12-11 15:14:54 -0500225 public @NonNull List<SliceSpec> getPinnedSpecs(Uri uri) {
Jason Monk74f5e362017-12-06 08:56:33 -0500226 try {
Jason Monke2c64512017-12-11 15:14:54 -0500227 return Arrays.asList(mService.getPinnedSpecs(uri, mContext.getPackageName()));
Jason Monk74f5e362017-12-06 08:56:33 -0500228 } catch (RemoteException e) {
229 throw e.rethrowFromSystemServer();
230 }
231 }
232
233 /**
Jason Monk5f8cc272018-01-16 17:57:20 -0500234 * Obtains a list of slices that are descendants of the specified Uri.
235 * <p>
236 * Not all slice providers will implement this functionality, in which case,
237 * an empty collection will be returned.
238 *
239 * @param uri The uri to look for descendants under.
240 * @return All slices within the space.
241 * @see SliceProvider#onGetSliceDescendants(Uri)
242 */
243 public @NonNull Collection<Uri> getSliceDescendants(@NonNull Uri uri) {
244 ContentResolver resolver = mContext.getContentResolver();
Jason Monk632def12018-02-01 15:21:16 -0500245 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
Jason Monk5f8cc272018-01-16 17:57:20 -0500246 Bundle extras = new Bundle();
247 extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri);
Jason Monk632def12018-02-01 15:21:16 -0500248 final Bundle res = provider.call(SliceProvider.METHOD_GET_DESCENDANTS, null, extras);
Jason Monk5f8cc272018-01-16 17:57:20 -0500249 return res.getParcelableArrayList(SliceProvider.EXTRA_SLICE_DESCENDANTS);
250 } catch (RemoteException e) {
251 Log.e(TAG, "Unable to get slice descendants", e);
Jason Monk5f8cc272018-01-16 17:57:20 -0500252 }
253 return Collections.emptyList();
254 }
255
256 /**
Jason Monkb9e06a82018-01-16 15:32:53 -0500257 * Turns a slice Uri into slice content.
258 *
259 * @param uri The URI to a slice provider
260 * @param supportedSpecs List of supported specs.
261 * @return The Slice provided by the app or null if none is given.
262 * @see Slice
263 */
264 public @Nullable Slice bindSlice(@NonNull Uri uri, @NonNull List<SliceSpec> supportedSpecs) {
265 Preconditions.checkNotNull(uri, "uri");
266 ContentResolver resolver = mContext.getContentResolver();
Jason Monk632def12018-02-01 15:21:16 -0500267 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
268 if (provider == null) {
269 throw new IllegalArgumentException("Unknown URI " + uri);
270 }
Jason Monkb9e06a82018-01-16 15:32:53 -0500271 Bundle extras = new Bundle();
272 extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri);
273 extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS,
274 new ArrayList<>(supportedSpecs));
Jason Monk632def12018-02-01 15:21:16 -0500275 final Bundle res = provider.call(SliceProvider.METHOD_SLICE, null, extras);
Jason Monkb9e06a82018-01-16 15:32:53 -0500276 Bundle.setDefusable(res, true);
277 if (res == null) {
278 return null;
279 }
280 return res.getParcelable(SliceProvider.EXTRA_SLICE);
281 } catch (RemoteException e) {
282 // Arbitrary and not worth documenting, as Activity
283 // Manager will kill this process shortly anyway.
284 return null;
Jason Monkb9e06a82018-01-16 15:32:53 -0500285 }
286 }
287
288 /**
Jason Monk7b8fef22018-01-30 16:04:14 -0500289 * Turns a slice intent into a slice uri. Expects an explicit intent. If there is no
290 * {@link android.content.ContentProvider} associated with the given intent this will throw
291 * {@link IllegalArgumentException}.
292 *
293 * @param intent The intent associated with a slice.
294 * @return The Slice Uri provided by the app or null if none is given.
295 * @see Slice
296 * @see SliceProvider#onMapIntentToUri(Intent)
297 * @see Intent
298 */
299 public @Nullable Uri mapIntentToUri(@NonNull Intent intent) {
300 Preconditions.checkNotNull(intent, "intent");
301 Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null,
302 "Slice intent must be explicit %s", intent);
303 ContentResolver resolver = mContext.getContentResolver();
304
305 // Check if the intent has data for the slice uri on it and use that
306 final Uri intentData = intent.getData();
307 if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) {
308 return intentData;
309 }
310 // Otherwise ask the app
311 List<ResolveInfo> providers =
312 mContext.getPackageManager().queryIntentContentProviders(intent, 0);
313 if (providers == null || providers.isEmpty()) {
314 throw new IllegalArgumentException("Unable to resolve intent " + intent);
315 }
316 String authority = providers.get(0).providerInfo.authority;
317 Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
318 .authority(authority).build();
Jason Monk632def12018-02-01 15:21:16 -0500319 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
320 if (provider == null) {
321 throw new IllegalArgumentException("Unknown URI " + uri);
322 }
Jason Monk7b8fef22018-01-30 16:04:14 -0500323 Bundle extras = new Bundle();
324 extras.putParcelable(SliceProvider.EXTRA_INTENT, intent);
Jason Monk632def12018-02-01 15:21:16 -0500325 final Bundle res = provider.call(SliceProvider.METHOD_MAP_ONLY_INTENT, null, extras);
Jason Monk7b8fef22018-01-30 16:04:14 -0500326 if (res == null) {
327 return null;
328 }
329 return res.getParcelable(SliceProvider.EXTRA_SLICE);
330 } catch (RemoteException e) {
331 // Arbitrary and not worth documenting, as Activity
332 // Manager will kill this process shortly anyway.
333 return null;
Jason Monk7b8fef22018-01-30 16:04:14 -0500334 }
335 }
336
337 /**
Jason Monkb9e06a82018-01-16 15:32:53 -0500338 * Turns a slice intent into slice content. Expects an explicit intent. If there is no
339 * {@link android.content.ContentProvider} associated with the given intent this will throw
340 * {@link IllegalArgumentException}.
341 *
342 * @param intent The intent associated with a slice.
343 * @param supportedSpecs List of supported specs.
344 * @return The Slice provided by the app or null if none is given.
345 * @see Slice
346 * @see SliceProvider#onMapIntentToUri(Intent)
347 * @see Intent
348 */
349 public @Nullable Slice bindSlice(@NonNull Intent intent,
350 @NonNull List<SliceSpec> supportedSpecs) {
351 Preconditions.checkNotNull(intent, "intent");
352 Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null,
Jason Monk7b8fef22018-01-30 16:04:14 -0500353 "Slice intent must be explicit %s", intent);
Jason Monkb9e06a82018-01-16 15:32:53 -0500354 ContentResolver resolver = mContext.getContentResolver();
355
356 // Check if the intent has data for the slice uri on it and use that
357 final Uri intentData = intent.getData();
358 if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) {
359 return bindSlice(intentData, supportedSpecs);
360 }
361 // Otherwise ask the app
362 List<ResolveInfo> providers =
363 mContext.getPackageManager().queryIntentContentProviders(intent, 0);
Jason Monk7b8fef22018-01-30 16:04:14 -0500364 if (providers == null || providers.isEmpty()) {
Jason Monkb9e06a82018-01-16 15:32:53 -0500365 throw new IllegalArgumentException("Unable to resolve intent " + intent);
366 }
367 String authority = providers.get(0).providerInfo.authority;
368 Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
369 .authority(authority).build();
Jason Monk632def12018-02-01 15:21:16 -0500370 try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
371 if (provider == null) {
372 throw new IllegalArgumentException("Unknown URI " + uri);
373 }
Jason Monkb9e06a82018-01-16 15:32:53 -0500374 Bundle extras = new Bundle();
375 extras.putParcelable(SliceProvider.EXTRA_INTENT, intent);
376 extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS,
377 new ArrayList<>(supportedSpecs));
Jason Monk632def12018-02-01 15:21:16 -0500378 final Bundle res = provider.call(SliceProvider.METHOD_MAP_INTENT, null, extras);
Jason Monkb9e06a82018-01-16 15:32:53 -0500379 if (res == null) {
380 return null;
381 }
382 return res.getParcelable(SliceProvider.EXTRA_SLICE);
383 } catch (RemoteException e) {
384 // Arbitrary and not worth documenting, as Activity
385 // Manager will kill this process shortly anyway.
386 return null;
Jason Monkb9e06a82018-01-16 15:32:53 -0500387 }
388 }
389
390 /**
Jason Monke8f8be72018-01-21 10:10:35 -0500391 * Does the permission check to see if a caller has access to a specific slice.
392 * @hide
393 */
394 public void enforceSlicePermission(Uri uri, String pkg, int pid, int uid) {
395 try {
396 if (pkg == null) {
397 throw new SecurityException("No pkg specified");
398 }
399 int result = mService.checkSlicePermission(uri, pkg, pid, uid);
400 if (result == PERMISSION_DENIED) {
401 throw new SecurityException("User " + uid + " does not have slice permission for "
402 + uri + ".");
403 }
404 if (result == PERMISSION_USER_GRANTED) {
405 // We just had a user grant of this permission and need to grant this to the app
406 // permanently.
407 mContext.grantUriPermission(pkg, uri.buildUpon().path("").build(),
408 Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
409 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
410 | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
411 }
412 } catch (RemoteException e) {
413 throw e.rethrowFromSystemServer();
414 }
415 }
416
417 /**
418 * Called by SystemUI to grant a slice permission after a dialog is shown.
419 * @hide
420 */
421 public void grantPermissionFromUser(Uri uri, String pkg, boolean allSlices) {
422 try {
423 mService.grantPermissionFromUser(uri, pkg, mContext.getPackageName(), allSlices);
424 } catch (RemoteException e) {
425 throw e.rethrowFromSystemServer();
426 }
427 }
428
429 /**
Jason Monke2c64512017-12-11 15:14:54 -0500430 * Class that listens to changes in {@link Slice}s.
Jason Monk74f5e362017-12-06 08:56:33 -0500431 */
Jason Monke2c64512017-12-11 15:14:54 -0500432 public interface SliceCallback {
Jason Monk74f5e362017-12-06 08:56:33 -0500433
434 /**
Jason Monke2c64512017-12-11 15:14:54 -0500435 * Called when slice is updated.
436 *
437 * @param s The updated slice.
438 * @see #registerSliceCallback
Jason Monk74f5e362017-12-06 08:56:33 -0500439 */
Jason Monke2c64512017-12-11 15:14:54 -0500440 void onSliceUpdated(Slice s);
Jason Monk74f5e362017-12-06 08:56:33 -0500441 }
Jason Monk8f5f7ff2017-10-17 14:12:42 -0400442}