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; |
Jason Monk | f200887 | 2018-02-23 08:59:31 -0500 | [diff] [blame^] | 27 | import android.content.pm.PackageManager; |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 28 | import android.content.pm.ResolveInfo; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 29 | import android.net.Uri; |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 30 | import android.os.Bundle; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 31 | import android.os.Handler; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 32 | import android.os.RemoteException; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 33 | import android.os.ServiceManager; |
| 34 | import android.os.ServiceManager.ServiceNotFoundException; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 35 | import android.util.ArrayMap; |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 36 | import android.util.Log; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 37 | import android.util.Pair; |
| 38 | |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 39 | import com.android.internal.util.Preconditions; |
| 40 | |
| 41 | import java.util.ArrayList; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 42 | import java.util.Arrays; |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 43 | import java.util.Collection; |
| 44 | import java.util.Collections; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 45 | import java.util.List; |
| 46 | import java.util.concurrent.Executor; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 47 | |
| 48 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 49 | * Class to handle interactions with {@link Slice}s. |
| 50 | * <p> |
| 51 | * The SliceManager manages permissions and pinned state for slices. |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 52 | */ |
| 53 | @SystemService(Context.SLICE_SERVICE) |
| 54 | public class SliceManager { |
| 55 | |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 56 | private static final String TAG = "SliceManager"; |
| 57 | |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 58 | /** |
| 59 | * @hide |
| 60 | */ |
| 61 | public static final String ACTION_REQUEST_SLICE_PERMISSION = |
| 62 | "android.intent.action.REQUEST_SLICE_PERMISSION"; |
| 63 | |
Mady Mellor | 3267ed8 | 2018-02-21 11:42:31 -0800 | [diff] [blame] | 64 | /** |
| 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 Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 73 | private final ISliceManager mService; |
| 74 | private final Context mContext; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 75 | private final ArrayMap<Pair<Uri, SliceCallback>, ISliceListener> mListenerLookup = |
| 76 | new ArrayMap<>(); |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 77 | |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 78 | /** |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 79 | * 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 Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 95 | * @hide |
| 96 | */ |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 97 | public SliceManager(Context context, Handler handler) throws ServiceNotFoundException { |
| 98 | mContext = context; |
| 99 | mService = ISliceManager.Stub.asInterface( |
| 100 | ServiceManager.getServiceOrThrow(Context.SLICE_SERVICE)); |
| 101 | } |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 102 | |
| 103 | /** |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 104 | * @deprecated TO BE REMOVED. |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 105 | */ |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 106 | @Deprecated |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 107 | public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback, |
| 108 | @NonNull List<SliceSpec> specs) { |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 109 | registerSliceCallback(uri, specs, mContext.getMainExecutor(), callback); |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 113 | * @deprecated TO BE REMOVED. |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 114 | */ |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 115 | @Deprecated |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 116 | public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback, |
| 117 | @NonNull List<SliceSpec> specs, Executor executor) { |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 118 | 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 Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 154 | try { |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 155 | 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 Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 196 | } catch (RemoteException e) { |
| 197 | throw e.rethrowFromSystemServer(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 202 | * 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 Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 206 | * <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 Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 209 | * |
| 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 Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 213 | * @see Intent#ACTION_ASSIST |
| 214 | * @see Intent#CATEGORY_HOME |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 215 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 216 | public void pinSlice(@NonNull Uri uri, @NonNull List<SliceSpec> specs) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 217 | try { |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 218 | mService.pinSlice(mContext.getPackageName(), uri, |
| 219 | specs.toArray(new SliceSpec[specs.size()])); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 220 | } catch (RemoteException e) { |
| 221 | throw e.rethrowFromSystemServer(); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 226 | * Remove a pin for a slice. |
| 227 | * <p> |
| 228 | * 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] | 229 | * <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 Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 232 | * |
| 233 | * @param uri The uri of the slice being unpinned. |
| 234 | * @see #pinSlice |
| 235 | * @see SliceProvider#onSliceUnpinned(Uri) |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 236 | * @see Intent#ACTION_ASSIST |
| 237 | * @see Intent#CATEGORY_HOME |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 238 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 239 | public void unpinSlice(@NonNull Uri uri) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 240 | try { |
| 241 | mService.unpinSlice(mContext.getPackageName(), uri); |
| 242 | } catch (RemoteException e) { |
| 243 | throw e.rethrowFromSystemServer(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 248 | * @hide |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 249 | */ |
| 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 Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 259 | * 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 Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 264 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 265 | public @NonNull List<SliceSpec> getPinnedSpecs(Uri uri) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 266 | try { |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 267 | return Arrays.asList(mService.getPinnedSpecs(uri, mContext.getPackageName())); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 268 | } catch (RemoteException e) { |
| 269 | throw e.rethrowFromSystemServer(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 274 | * 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 Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 285 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 286 | Bundle extras = new Bundle(); |
| 287 | extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 288 | final Bundle res = provider.call(SliceProvider.METHOD_GET_DESCENDANTS, null, extras); |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 289 | return res.getParcelableArrayList(SliceProvider.EXTRA_SLICE_DESCENDANTS); |
| 290 | } catch (RemoteException e) { |
| 291 | Log.e(TAG, "Unable to get slice descendants", e); |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 292 | } |
| 293 | return Collections.emptyList(); |
| 294 | } |
| 295 | |
| 296 | /** |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 297 | * 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 Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 307 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
| 308 | if (provider == null) { |
| 309 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 310 | } |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 311 | Bundle extras = new Bundle(); |
| 312 | extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri); |
| 313 | extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS, |
| 314 | new ArrayList<>(supportedSpecs)); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 315 | final Bundle res = provider.call(SliceProvider.METHOD_SLICE, null, extras); |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 316 | 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 Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
Jason Monk | f200887 | 2018-02-23 08:59:31 -0500 | [diff] [blame^] | 329 | * Turns a slice intent into a slice uri. Expects an explicit intent. |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 330 | * |
| 331 | * @param intent The intent associated with a slice. |
Jason Monk | f200887 | 2018-02-23 08:59:31 -0500 | [diff] [blame^] | 332 | * @return The Slice Uri provided by the app or null if none exists. |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 333 | * @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 Monk | f200887 | 2018-02-23 08:59:31 -0500 | [diff] [blame^] | 352 | // 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 Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 362 | } |
| 363 | String authority = providers.get(0).providerInfo.authority; |
| 364 | Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 365 | .authority(authority).build(); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 366 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
| 367 | if (provider == null) { |
| 368 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 369 | } |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 370 | Bundle extras = new Bundle(); |
| 371 | extras.putParcelable(SliceProvider.EXTRA_INTENT, intent); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 372 | final Bundle res = provider.call(SliceProvider.METHOD_MAP_ONLY_INTENT, null, extras); |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 373 | 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 Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | /** |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 385 | * 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 Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 400 | "Slice intent must be explicit %s", intent); |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 401 | 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 Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 411 | if (providers == null || providers.isEmpty()) { |
Jason Monk | f200887 | 2018-02-23 08:59:31 -0500 | [diff] [blame^] | 412 | // 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 Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 422 | } |
| 423 | String authority = providers.get(0).providerInfo.authority; |
| 424 | Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 425 | .authority(authority).build(); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 426 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
| 427 | if (provider == null) { |
| 428 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 429 | } |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 430 | Bundle extras = new Bundle(); |
| 431 | extras.putParcelable(SliceProvider.EXTRA_INTENT, intent); |
| 432 | extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS, |
| 433 | new ArrayList<>(supportedSpecs)); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 434 | final Bundle res = provider.call(SliceProvider.METHOD_MAP_INTENT, null, extras); |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 435 | 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 Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | /** |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 447 | * 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 Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 486 | * Class that listens to changes in {@link Slice}s. |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 487 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 488 | public interface SliceCallback { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 489 | |
| 490 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 491 | * Called when slice is updated. |
| 492 | * |
| 493 | * @param s The updated slice. |
| 494 | * @see #registerSliceCallback |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 495 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 496 | void onSliceUpdated(Slice s); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 497 | } |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 498 | } |