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 | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame^] | 29 | import android.os.Binder; |
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 | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame^] | 32 | import android.os.IBinder; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 33 | import android.os.RemoteException; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 34 | import android.os.ServiceManager; |
| 35 | import android.os.ServiceManager.ServiceNotFoundException; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 36 | import android.util.ArrayMap; |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 37 | import android.util.Log; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 38 | import android.util.Pair; |
| 39 | |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 40 | import com.android.internal.util.Preconditions; |
| 41 | |
| 42 | import java.util.ArrayList; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 43 | import java.util.Arrays; |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 44 | import java.util.Collection; |
| 45 | import java.util.Collections; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 46 | import java.util.List; |
| 47 | import java.util.concurrent.Executor; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 48 | |
| 49 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 50 | * Class to handle interactions with {@link Slice}s. |
| 51 | * <p> |
| 52 | * The SliceManager manages permissions and pinned state for slices. |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 53 | */ |
| 54 | @SystemService(Context.SLICE_SERVICE) |
| 55 | public class SliceManager { |
| 56 | |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 57 | private static final String TAG = "SliceManager"; |
| 58 | |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 59 | /** |
| 60 | * @hide |
| 61 | */ |
| 62 | public static final String ACTION_REQUEST_SLICE_PERMISSION = |
| 63 | "android.intent.action.REQUEST_SLICE_PERMISSION"; |
| 64 | |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 65 | private final ISliceManager mService; |
| 66 | private final Context mContext; |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 67 | private final ArrayMap<Pair<Uri, SliceCallback>, ISliceListener> mListenerLookup = |
| 68 | new ArrayMap<>(); |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame^] | 69 | private final IBinder mToken = new Binder(); |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 70 | |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 71 | /** |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 72 | * 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 Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 88 | * @hide |
| 89 | */ |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 90 | public SliceManager(Context context, Handler handler) throws ServiceNotFoundException { |
| 91 | mContext = context; |
| 92 | mService = ISliceManager.Stub.asInterface( |
| 93 | ServiceManager.getServiceOrThrow(Context.SLICE_SERVICE)); |
| 94 | } |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 95 | |
| 96 | /** |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 97 | * @deprecated TO BE REMOVED. |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 98 | */ |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 99 | @Deprecated |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 100 | public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback, |
| 101 | @NonNull List<SliceSpec> specs) { |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 105 | * @deprecated TO BE REMOVED. |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 106 | */ |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 107 | @Deprecated |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 108 | public void registerSliceCallback(@NonNull Uri uri, @NonNull SliceCallback callback, |
| 109 | @NonNull List<SliceSpec> specs, Executor executor) { |
Jason Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 110 | } |
| 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 Monk | 4ef50bc | 2018-01-22 10:52:23 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Adds a callback to a specific slice uri. |
| 131 | * <p> |
| 132 | * This is a convenience that performs a few slice actions at once. It will put |
| 133 | * the slice in a pinned state since there is a callback attached. It will also |
| 134 | * listen for content changes, when a content change observes, the android system |
| 135 | * will bind the new slice and provide it to all registered {@link SliceCallback}s. |
| 136 | * |
| 137 | * @param uri The uri of the slice being listened to. |
| 138 | * @param callback The listener that should receive the callbacks. |
| 139 | * @param specs The list of supported {@link SliceSpec}s of the callback. |
| 140 | * @see SliceProvider#onSlicePinned(Uri) |
| 141 | */ |
| 142 | public void registerSliceCallback(@NonNull Uri uri, @NonNull List<SliceSpec> specs, |
| 143 | @NonNull @CallbackExecutor Executor executor, @NonNull SliceCallback callback) { |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 144 | |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 145 | } |
| 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 Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame^] | 158 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 162 | * 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 Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 166 | * <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 Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 169 | * |
| 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 Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 173 | * @see Intent#ACTION_ASSIST |
| 174 | * @see Intent#CATEGORY_HOME |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 175 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 176 | public void pinSlice(@NonNull Uri uri, @NonNull List<SliceSpec> specs) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 177 | try { |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 178 | mService.pinSlice(mContext.getPackageName(), uri, |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame^] | 179 | specs.toArray(new SliceSpec[specs.size()]), mToken); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 180 | } catch (RemoteException e) { |
| 181 | throw e.rethrowFromSystemServer(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 186 | * Remove a pin for a slice. |
| 187 | * <p> |
| 188 | * 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] | 189 | * <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 Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 192 | * |
| 193 | * @param uri The uri of the slice being unpinned. |
| 194 | * @see #pinSlice |
| 195 | * @see SliceProvider#onSliceUnpinned(Uri) |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 196 | * @see Intent#ACTION_ASSIST |
| 197 | * @see Intent#CATEGORY_HOME |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 198 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 199 | public void unpinSlice(@NonNull Uri uri) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 200 | try { |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame^] | 201 | mService.unpinSlice(mContext.getPackageName(), uri, mToken); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 202 | } catch (RemoteException e) { |
| 203 | throw e.rethrowFromSystemServer(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 208 | * @hide |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 209 | */ |
| 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 Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 219 | * 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 Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 224 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 225 | public @NonNull List<SliceSpec> getPinnedSpecs(Uri uri) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 226 | try { |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 227 | return Arrays.asList(mService.getPinnedSpecs(uri, mContext.getPackageName())); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 228 | } catch (RemoteException e) { |
| 229 | throw e.rethrowFromSystemServer(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 234 | * 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 Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 245 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 246 | Bundle extras = new Bundle(); |
| 247 | extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 248 | final Bundle res = provider.call(SliceProvider.METHOD_GET_DESCENDANTS, null, extras); |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 249 | return res.getParcelableArrayList(SliceProvider.EXTRA_SLICE_DESCENDANTS); |
| 250 | } catch (RemoteException e) { |
| 251 | Log.e(TAG, "Unable to get slice descendants", e); |
Jason Monk | 5f8cc27 | 2018-01-16 17:57:20 -0500 | [diff] [blame] | 252 | } |
| 253 | return Collections.emptyList(); |
| 254 | } |
| 255 | |
| 256 | /** |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 257 | * 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 Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 267 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
| 268 | if (provider == null) { |
| 269 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 270 | } |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 271 | Bundle extras = new Bundle(); |
| 272 | extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri); |
| 273 | extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS, |
| 274 | new ArrayList<>(supportedSpecs)); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 275 | final Bundle res = provider.call(SliceProvider.METHOD_SLICE, null, extras); |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 276 | 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 Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 289 | * 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 Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 319 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
| 320 | if (provider == null) { |
| 321 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 322 | } |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 323 | Bundle extras = new Bundle(); |
| 324 | extras.putParcelable(SliceProvider.EXTRA_INTENT, intent); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 325 | final Bundle res = provider.call(SliceProvider.METHOD_MAP_ONLY_INTENT, null, extras); |
Jason Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 326 | 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 Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 338 | * 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 Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 353 | "Slice intent must be explicit %s", intent); |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 354 | 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 Monk | 7b8fef2 | 2018-01-30 16:04:14 -0500 | [diff] [blame] | 364 | if (providers == null || providers.isEmpty()) { |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 365 | 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 Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 370 | try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { |
| 371 | if (provider == null) { |
| 372 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 373 | } |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 374 | Bundle extras = new Bundle(); |
| 375 | extras.putParcelable(SliceProvider.EXTRA_INTENT, intent); |
| 376 | extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS, |
| 377 | new ArrayList<>(supportedSpecs)); |
Jason Monk | 632def1 | 2018-02-01 15:21:16 -0500 | [diff] [blame] | 378 | final Bundle res = provider.call(SliceProvider.METHOD_MAP_INTENT, null, extras); |
Jason Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 379 | 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 Monk | b9e06a8 | 2018-01-16 15:32:53 -0500 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 391 | * 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 Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 430 | * Class that listens to changes in {@link Slice}s. |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 431 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 432 | public interface SliceCallback { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 433 | |
| 434 | /** |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 435 | * Called when slice is updated. |
| 436 | * |
| 437 | * @param s The updated slice. |
| 438 | * @see #registerSliceCallback |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 439 | */ |
Jason Monk | e2c6451 | 2017-12-11 15:14:54 -0500 | [diff] [blame] | 440 | void onSliceUpdated(Slice s); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 441 | } |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 442 | } |