Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app.slice; |
| 18 | |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 19 | import android.annotation.CallbackExecutor; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 22 | import android.annotation.SystemService; |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 23 | import android.content.ContentProviderClient; |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 25 | import android.content.Context; |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 26 | import android.content.Intent; |
| 27 | import android.content.pm.ResolveInfo; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 28 | import android.net.Uri; |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 29 | import android.os.Bundle; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 30 | import android.os.Handler; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 31 | import android.os.RemoteException; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 32 | import android.os.ServiceManager; |
| 33 | import android.os.ServiceManager.ServiceNotFoundException; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 34 | import android.util.ArrayMap; |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 35 | import android.util.Log; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 36 | import android.util.Pair; |
| 37 | |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 38 | import com.android.internal.util.Preconditions; |
| 39 | |
| 40 | import java.util.ArrayList; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 41 | import java.util.Arrays; |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 42 | import java.util.Collection; |
| 43 | import java.util.Collections; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 44 | import java.util.List; |
| 45 | import java.util.concurrent.Executor; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 46 | |
| 47 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 48 | * Class to handle interactions with {@link Slice}s. |
| 49 | * <p> |
| 50 | * The SliceManager manages permissions and pinned state for slices. |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 51 | */ |
| 52 | @SystemService(Context.SLICE_SERVICE) |
| 53 | public class SliceManager { |
| 54 | |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 55 | private static final String TAG = "SliceManager"; |
| 56 | |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 57 | /** |
| 58 | * @hide |
| 59 | */ |
| 60 | public static final String ACTION_REQUEST_SLICE_PERMISSION = |
| 61 | "android.intent.action.REQUEST_SLICE_PERMISSION"; |
| 62 | |
Mady Mellor | 3267ed8 | 2018-02-21 11:42:31 -0800 | [diff] [blame^] | 63 | /** |
| 64 | * The meta-data key that allows an activity to easily be linked directly to a slice. |
| 65 | * <p> |
| 66 | * An activity can be statically linked to a slice uri by including a meta-data item |
| 67 | * for this key that contains a valid slice uri for the same application declaring |
| 68 | * the activity. |
| 69 | */ |
| 70 | public static final String SLICE_METADATA_KEY = "android.metadata.SLICE_URI"; |
| 71 | |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 72 | private final ISliceManager mService; |
| 73 | private final Context mContext; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 74 | private final ArrayMap<Pair<Uri, SliceCallback>, ISliceListener> mListenerLookup = |
| 75 | new ArrayMap<>(); |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 76 | |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 77 | /** |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 78 | * Permission denied. |
| 79 | * @hide |
| 80 | */ |
| 81 | public static final int PERMISSION_DENIED = -1; |
| 82 | /** |
| 83 | * Permission granted. |
| 84 | * @hide |
| 85 | */ |
| 86 | public static final int PERMISSION_GRANTED = 0; |
| 87 | /** |
| 88 | * Permission just granted by the user, and should be granted uri permission as well. |
| 89 | * @hide |
| 90 | */ |
| 91 | public static final int PERMISSION_USER_GRANTED = 1; |
| 92 | |
| 93 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 94 | * @hide |
| 95 | */ |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 96 | public SliceManager(Context context, Handler handler) throws ServiceNotFoundException { |
| 97 | mContext = context; |
| 98 | mService = ISliceManager.Stub.asInterface( |
| 99 | ServiceManager.getServiceOrThrow(Context.SLICE_SERVICE)); |
| 100 | } |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 101 | |
| 102 | /** |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 103 | * @deprecated TO BE REMOVED. |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 104 | */ |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 105 | @Deprecated |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 106 | public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback, |
| 107 | @NonNull List<SliceSpec> specs) { |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 108 | registerSliceCallback(uri, specs, mContext.getMainExecutor(), callback); |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 112 | * @deprecated TO BE REMOVED. |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 113 | */ |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 114 | @Deprecated |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 115 | public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback, |
| 116 | @NonNull List<SliceSpec> specs, Executor executor) { |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 117 | registerSliceCallback(uri, specs, executor, callback); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Adds a callback to a specific slice uri. |
| 122 | * <p> |
| 123 | * This is a convenience that performs a few slice actions at once. It will put |
| 124 | * the slice in a pinned state since there is a callback attached. It will also |
| 125 | * listen for content changes, when a content change observes, the android system |
| 126 | * will bind the new slice and provide it to all registered {@link SliceCallback}s. |
| 127 | * |
| 128 | * @param uri The uri of the slice being listened to. |
| 129 | * @param callback The listener that should receive the callbacks. |
| 130 | * @param specs The list of supported {@link SliceSpec}s of the callback. |
| 131 | * @see SliceProvider#onSlicePinned(Uri) |
| 132 | */ |
| 133 | public void registerSliceCallback(@NonNull Uri uri, @NonNull List<SliceSpec> specs, |
| 134 | @NonNull SliceCallback callback) { |
| 135 | registerSliceCallback(uri, specs, mContext.getMainExecutor(), callback); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Adds a callback to a specific slice uri. |
| 140 | * <p> |
| 141 | * This is a convenience that performs a few slice actions at once. It will put |
| 142 | * the slice in a pinned state since there is a callback attached. It will also |
| 143 | * listen for content changes, when a content change observes, the android system |
| 144 | * will bind the new slice and provide it to all registered {@link SliceCallback}s. |
| 145 | * |
| 146 | * @param uri The uri of the slice being listened to. |
| 147 | * @param callback The listener that should receive the callbacks. |
| 148 | * @param specs The list of supported {@link SliceSpec}s of the callback. |
| 149 | * @see SliceProvider#onSlicePinned(Uri) |
| 150 | */ |
| 151 | public void registerSliceCallback(@NonNull Uri uri, @NonNull List<SliceSpec> specs, |
| 152 | @NonNull @CallbackExecutor Executor executor, @NonNull SliceCallback callback) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 153 | try { |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 154 | mService.addSliceListener(uri, mContext.getPackageName(), |
| 155 | getListener(uri, callback, new ISliceListener.Stub() { |
| 156 | @Override |
| 157 | public void onSliceUpdated(Slice s) throws RemoteException { |
| 158 | executor.execute(() -> callback.onSliceUpdated(s)); |
| 159 | } |
| 160 | }), specs.toArray(new SliceSpec[specs.size()])); |
| 161 | } catch (RemoteException e) { |
| 162 | throw e.rethrowFromSystemServer(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private ISliceListener getListener(Uri uri, SliceCallback callback, |
| 167 | ISliceListener listener) { |
| 168 | Pair<Uri, SliceCallback> key = new Pair<>(uri, callback); |
| 169 | if (mListenerLookup.containsKey(key)) { |
| 170 | try { |
| 171 | mService.removeSliceListener(uri, mContext.getPackageName(), |
| 172 | mListenerLookup.get(key)); |
| 173 | } catch (RemoteException e) { |
| 174 | throw e.rethrowFromSystemServer(); |
| 175 | } |
| 176 | } |
| 177 | mListenerLookup.put(key, listener); |
| 178 | return listener; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Removes a callback for a specific slice uri. |
| 183 | * <p> |
| 184 | * Removes the app from the pinned state (if there are no other apps/callbacks pinning it) |
| 185 | * in addition to removing the callback. |
| 186 | * |
| 187 | * @param uri The uri of the slice being listened to |
| 188 | * @param callback The listener that should no longer receive callbacks. |
| 189 | * @see #registerSliceCallback |
| 190 | */ |
| 191 | public void unregisterSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback) { |
| 192 | try { |
| 193 | mService.removeSliceListener(uri, mContext.getPackageName(), |
| 194 | mListenerLookup.remove(new Pair<>(uri, callback))); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 195 | } catch (RemoteException e) { |
| 196 | throw e.rethrowFromSystemServer(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 201 | * Ensures that a slice is in a pinned state. |
| 202 | * <p> |
| 203 | * Pinned state is not persisted across reboots, so apps are expected to re-pin any slices |
| 204 | * they still care about after a reboot. |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 205 | * <p> |
| 206 | * This may only be called by apps that are the default launcher for the device |
| 207 | * or the default voice interaction service. Otherwise will throw {@link SecurityException}. |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 208 | * |
| 209 | * @param uri The uri of the slice being pinned. |
| 210 | * @param specs The list of supported {@link SliceSpec}s of the callback. |
| 211 | * @see SliceProvider#onSlicePinned(Uri) |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 212 | * @see Intent#ACTION_ASSIST |
| 213 | * @see Intent#CATEGORY_HOME |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 214 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 215 | public void pinSlice(@NonNull Uri uri, @NonNull List<SliceSpec> specs) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 216 | try { |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 217 | mService.pinSlice(mContext.getPackageName(), uri, |
| 218 | specs.toArray(new SliceSpec[specs.size()])); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 219 | } catch (RemoteException e) { |
| 220 | throw e.rethrowFromSystemServer(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 225 | * Remove a pin for a slice. |
| 226 | * <p> |
| 227 | * If the slice has no other pins/callbacks then the slice will be unpinned. |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 228 | * <p> |
| 229 | * This may only be called by apps that are the default launcher for the device |
| 230 | * or the default voice interaction service. Otherwise will throw {@link SecurityException}. |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 231 | * |
| 232 | * @param uri The uri of the slice being unpinned. |
| 233 | * @see #pinSlice |
| 234 | * @see SliceProvider#onSliceUnpinned(Uri) |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 235 | * @see Intent#ACTION_ASSIST |
| 236 | * @see Intent#CATEGORY_HOME |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 237 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 238 | public void unpinSlice(@NonNull Uri uri) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 239 | try { |
| 240 | mService.unpinSlice(mContext.getPackageName(), uri); |
| 241 | } catch (RemoteException e) { |
| 242 | throw e.rethrowFromSystemServer(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 247 | * @hide |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 248 | */ |
| 249 | public boolean hasSliceAccess() { |
| 250 | try { |
| 251 | return mService.hasSliceAccess(mContext.getPackageName()); |
| 252 | } catch (RemoteException e) { |
| 253 | throw e.rethrowFromSystemServer(); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 258 | * Get the current set of specs for a pinned slice. |
| 259 | * <p> |
| 260 | * This is the set of specs supported for a specific pinned slice. It will take |
| 261 | * into account all clients and returns only specs supported by all. |
| 262 | * @see SliceSpec |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 263 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 264 | public @NonNull List<SliceSpec> getPinnedSpecs(Uri uri) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 265 | try { |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 266 | return Arrays.asList(mService.getPinnedSpecs(uri, mContext.getPackageName())); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 267 | } catch (RemoteException e) { |
| 268 | throw e.rethrowFromSystemServer(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 273 | * Obtains a list of slices that are descendants of the specified Uri. |
| 274 | * <p> |
| 275 | * Not all slice providers will implement this functionality, in which case, |
| 276 | * an empty collection will be returned. |
| 277 | * |
| 278 | * @param uri The uri to look for descendants under. |
| 279 | * @return All slices within the space. |
| 280 | * @see SliceProvider#onGetSliceDescendants(Uri) |
| 281 | */ |
| 282 | public @NonNull Collection<Uri> getSliceDescendants(@NonNull Uri uri) { |
| 283 | ContentResolver resolver = mContext.getContentResolver(); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 284 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 285 | Bundle extras = new Bundle(); |
| 286 | extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 287 | final Bundle res = provider.call(SliceProvider.METHOD_GET_DESCENDANTS, null, extras); |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 288 | return res.getParcelableArrayList(SliceProvider.EXTRA_SLICE_DESCENDANTS); |
| 289 | } catch (RemoteException e) { |
| 290 | Log.e(TAG, "Unable to get slice descendants", e); |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 291 | } |
| 292 | return Collections.emptyList(); |
| 293 | } |
| 294 | |
| 295 | /** |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 296 | * Turns a slice Uri into slice content. |
| 297 | * |
| 298 | * @param uri The URI to a slice provider |
| 299 | * @param supportedSpecs List of supported specs. |
| 300 | * @return The Slice provided by the app or null if none is given. |
| 301 | * @see Slice |
| 302 | */ |
| 303 | public @Nullable Slice bindSlice(@NonNull Uri uri, @NonNull List<SliceSpec> supportedSpecs) { |
| 304 | Preconditions.checkNotNull(uri, "uri"); |
| 305 | ContentResolver resolver = mContext.getContentResolver(); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 306 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
| 307 | if (provider == null) { |
| 308 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 309 | } |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 310 | Bundle extras = new Bundle(); |
| 311 | extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri); |
| 312 | extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS, |
| 313 | new ArrayList<>(supportedSpecs)); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 314 | final Bundle res = provider.call(SliceProvider.METHOD_SLICE, null, extras); |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 315 | Bundle.setDefusable(res, true); |
| 316 | if (res == null) { |
| 317 | return null; |
| 318 | } |
| 319 | return res.getParcelable(SliceProvider.EXTRA_SLICE); |
| 320 | } catch (RemoteException e) { |
| 321 | // Arbitrary and not worth documenting, as Activity |
| 322 | // Manager will kill this process shortly anyway. |
| 323 | return null; |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 328 | * Turns a slice intent into a slice uri. Expects an explicit intent. If there is no |
| 329 | * {@link android.content.ContentProvider} associated with the given intent this will throw |
| 330 | * {@link IllegalArgumentException}. |
| 331 | * |
| 332 | * @param intent The intent associated with a slice. |
| 333 | * @return The Slice Uri 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 Uri mapIntentToUri(@NonNull Intent intent) { |
| 339 | Preconditions.checkNotNull(intent, "intent"); |
| 340 | Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null, |
| 341 | "Slice intent must be explicit %s", intent); |
| 342 | ContentResolver resolver = mContext.getContentResolver(); |
| 343 | |
| 344 | // Check if the intent has data for the slice uri on it and use that |
| 345 | final Uri intentData = intent.getData(); |
| 346 | if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) { |
| 347 | return intentData; |
| 348 | } |
| 349 | // Otherwise ask the app |
| 350 | List<ResolveInfo> providers = |
| 351 | mContext.getPackageManager().queryIntentContentProviders(intent, 0); |
| 352 | if (providers == null || providers.isEmpty()) { |
| 353 | throw new IllegalArgumentException("Unable to resolve intent " + intent); |
| 354 | } |
| 355 | String authority = providers.get(0).providerInfo.authority; |
| 356 | Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 357 | .authority(authority).build(); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 358 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
| 359 | if (provider == null) { |
| 360 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 361 | } |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 362 | Bundle extras = new Bundle(); |
| 363 | extras.putParcelable(SliceProvider.EXTRA_INTENT, intent); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 364 | final Bundle res = provider.call(SliceProvider.METHOD_MAP_ONLY_INTENT, null, extras); |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 365 | if (res == null) { |
| 366 | return null; |
| 367 | } |
| 368 | return res.getParcelable(SliceProvider.EXTRA_SLICE); |
| 369 | } catch (RemoteException e) { |
| 370 | // Arbitrary and not worth documenting, as Activity |
| 371 | // Manager will kill this process shortly anyway. |
| 372 | return null; |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 377 | * Turns a slice intent into slice content. Expects an explicit intent. If there is no |
| 378 | * {@link android.content.ContentProvider} associated with the given intent this will throw |
| 379 | * {@link IllegalArgumentException}. |
| 380 | * |
| 381 | * @param intent The intent associated with a slice. |
| 382 | * @param supportedSpecs List of supported specs. |
| 383 | * @return The Slice provided by the app or null if none is given. |
| 384 | * @see Slice |
| 385 | * @see SliceProvider#onMapIntentToUri(Intent) |
| 386 | * @see Intent |
| 387 | */ |
| 388 | public @Nullable Slice bindSlice(@NonNull Intent intent, |
| 389 | @NonNull List<SliceSpec> supportedSpecs) { |
| 390 | Preconditions.checkNotNull(intent, "intent"); |
| 391 | Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null, |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 392 | "Slice intent must be explicit %s", intent); |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 393 | ContentResolver resolver = mContext.getContentResolver(); |
| 394 | |
| 395 | // Check if the intent has data for the slice uri on it and use that |
| 396 | final Uri intentData = intent.getData(); |
| 397 | if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) { |
| 398 | return bindSlice(intentData, supportedSpecs); |
| 399 | } |
| 400 | // Otherwise ask the app |
| 401 | List<ResolveInfo> providers = |
| 402 | mContext.getPackageManager().queryIntentContentProviders(intent, 0); |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 403 | if (providers == null || providers.isEmpty()) { |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 404 | throw new IllegalArgumentException("Unable to resolve intent " + intent); |
| 405 | } |
| 406 | String authority = providers.get(0).providerInfo.authority; |
| 407 | Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 408 | .authority(authority).build(); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 409 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
| 410 | if (provider == null) { |
| 411 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 412 | } |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 413 | Bundle extras = new Bundle(); |
| 414 | extras.putParcelable(SliceProvider.EXTRA_INTENT, intent); |
| 415 | extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS, |
| 416 | new ArrayList<>(supportedSpecs)); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 417 | final Bundle res = provider.call(SliceProvider.METHOD_MAP_INTENT, null, extras); |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 418 | if (res == null) { |
| 419 | return null; |
| 420 | } |
| 421 | return res.getParcelable(SliceProvider.EXTRA_SLICE); |
| 422 | } catch (RemoteException e) { |
| 423 | // Arbitrary and not worth documenting, as Activity |
| 424 | // Manager will kill this process shortly anyway. |
| 425 | return null; |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | /** |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 430 | * Does the permission check to see if a caller has access to a specific slice. |
| 431 | * @hide |
| 432 | */ |
| 433 | public void enforceSlicePermission(Uri uri, String pkg, int pid, int uid) { |
| 434 | try { |
| 435 | if (pkg == null) { |
| 436 | throw new SecurityException("No pkg specified"); |
| 437 | } |
| 438 | int result = mService.checkSlicePermission(uri, pkg, pid, uid); |
| 439 | if (result == PERMISSION_DENIED) { |
| 440 | throw new SecurityException("User " + uid + " does not have slice permission for " |
| 441 | + uri + "."); |
| 442 | } |
| 443 | if (result == PERMISSION_USER_GRANTED) { |
| 444 | // We just had a user grant of this permission and need to grant this to the app |
| 445 | // permanently. |
| 446 | mContext.grantUriPermission(pkg, uri.buildUpon().path("").build(), |
| 447 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| 448 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 449 | | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION); |
| 450 | } |
| 451 | } catch (RemoteException e) { |
| 452 | throw e.rethrowFromSystemServer(); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Called by SystemUI to grant a slice permission after a dialog is shown. |
| 458 | * @hide |
| 459 | */ |
| 460 | public void grantPermissionFromUser(Uri uri, String pkg, boolean allSlices) { |
| 461 | try { |
| 462 | mService.grantPermissionFromUser(uri, pkg, mContext.getPackageName(), allSlices); |
| 463 | } catch (RemoteException e) { |
| 464 | throw e.rethrowFromSystemServer(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 469 | * Class that listens to changes in {@link Slice}s. |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 470 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 471 | public interface SliceCallback { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 472 | |
| 473 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 474 | * Called when slice is updated. |
| 475 | * |
| 476 | * @param s The updated slice. |
| 477 | * @see #registerSliceCallback |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 478 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 479 | void onSliceUpdated(Slice s); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 480 | } |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 481 | } |