Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -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 | */ |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 16 | package android.app.slice; |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 17 | |
| 18 | import android.Manifest.permission; |
Mady Mellor | 3b0a72f | 2017-10-19 10:12:09 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 20 | import android.content.ContentProvider; |
| 21 | import android.content.ContentResolver; |
| 22 | import android.content.ContentValues; |
Jason Monk | b40dad5 | 2017-11-01 16:01:40 -0400 | [diff] [blame] | 23 | import android.content.Intent; |
Mady Mellor | 3b0a72f | 2017-10-19 10:12:09 -0700 | [diff] [blame] | 24 | import android.content.IntentFilter; |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 25 | import android.database.ContentObserver; |
| 26 | import android.database.Cursor; |
| 27 | import android.net.Uri; |
Jason Monk | b40dad5 | 2017-11-01 16:01:40 -0400 | [diff] [blame] | 28 | import android.os.Binder; |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 29 | import android.os.Bundle; |
| 30 | import android.os.CancellationSignal; |
| 31 | import android.os.Handler; |
| 32 | import android.os.Looper; |
Jason Monk | b40dad5 | 2017-11-01 16:01:40 -0400 | [diff] [blame] | 33 | import android.os.Process; |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 34 | import android.os.StrictMode; |
| 35 | import android.os.StrictMode.ThreadPolicy; |
Jason Monk | b40dad5 | 2017-11-01 16:01:40 -0400 | [diff] [blame] | 36 | import android.os.UserHandle; |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 39 | import java.util.List; |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 40 | import java.util.concurrent.CountDownLatch; |
| 41 | |
| 42 | /** |
Mady Mellor | 3b0a72f | 2017-10-19 10:12:09 -0700 | [diff] [blame] | 43 | * A SliceProvider allows an app to provide content to be displayed in system spaces. This content |
| 44 | * is templated and can contain actions, and the behavior of how it is surfaced is specific to the |
| 45 | * system surface. |
| 46 | * <p> |
| 47 | * Slices are not currently live content. They are bound once and shown to the user. If the content |
| 48 | * changes due to a callback from user interaction, then |
| 49 | * {@link ContentResolver#notifyChange(Uri, ContentObserver)} should be used to notify the system. |
| 50 | * </p> |
| 51 | * <p> |
| 52 | * The provider needs to be declared in the manifest to provide the authority for the app. The |
| 53 | * authority for most slices is expected to match the package of the application. |
| 54 | * </p> |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 55 | * |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 56 | * <pre class="prettyprint"> |
| 57 | * {@literal |
| 58 | * <provider |
| 59 | * android:name="com.android.mypkg.MySliceProvider" |
| 60 | * android:authorities="com.android.mypkg" />} |
| 61 | * </pre> |
Mady Mellor | 3b0a72f | 2017-10-19 10:12:09 -0700 | [diff] [blame] | 62 | * <p> |
| 63 | * Slices can be identified by a Uri or by an Intent. To link an Intent with a slice, the provider |
| 64 | * must have an {@link IntentFilter} matching the slice intent. When a slice is being requested via |
| 65 | * an intent, {@link #onMapIntentToUri(Intent)} can be called and is expected to return an |
| 66 | * appropriate Uri representing the slice. |
| 67 | * |
| 68 | * <pre class="prettyprint"> |
| 69 | * {@literal |
| 70 | * <provider |
| 71 | * android:name="com.android.mypkg.MySliceProvider" |
| 72 | * android:authorities="com.android.mypkg"> |
| 73 | * <intent-filter> |
| 74 | * <action android:name="android.intent.action.MY_SLICE_INTENT" /> |
| 75 | * </intent-filter> |
| 76 | * </provider>} |
| 77 | * </pre> |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 78 | * |
| 79 | * @see Slice |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 80 | */ |
| 81 | public abstract class SliceProvider extends ContentProvider { |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 82 | /** |
| 83 | * This is the Android platform's MIME type for a slice: URI |
| 84 | * containing a slice implemented through {@link SliceProvider}. |
| 85 | */ |
| 86 | public static final String SLICE_TYPE = "vnd.android.slice"; |
| 87 | |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 88 | private static final String TAG = "SliceProvider"; |
| 89 | /** |
| 90 | * @hide |
| 91 | */ |
| 92 | public static final String EXTRA_BIND_URI = "slice_uri"; |
| 93 | /** |
| 94 | * @hide |
| 95 | */ |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 96 | public static final String EXTRA_SUPPORTED_SPECS = "supported_specs"; |
| 97 | /** |
| 98 | * @hide |
| 99 | */ |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 100 | public static final String METHOD_SLICE = "bind_slice"; |
| 101 | /** |
| 102 | * @hide |
| 103 | */ |
Mady Mellor | 3b0a72f | 2017-10-19 10:12:09 -0700 | [diff] [blame] | 104 | public static final String METHOD_MAP_INTENT = "map_slice"; |
| 105 | /** |
| 106 | * @hide |
| 107 | */ |
| 108 | public static final String EXTRA_INTENT = "slice_intent"; |
| 109 | /** |
| 110 | * @hide |
| 111 | */ |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 112 | public static final String EXTRA_SLICE = "slice"; |
| 113 | |
| 114 | private static final boolean DEBUG = false; |
| 115 | |
| 116 | /** |
| 117 | * Implemented to create a slice. Will be called on the main thread. |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 118 | * <p> |
| 119 | * onBindSlice should return as quickly as possible so that the UI tied |
| 120 | * to this slice can be responsive. No network or other IO will be allowed |
| 121 | * during onBindSlice. Any loading that needs to be done should happen |
| 122 | * off the main thread with a call to {@link ContentResolver#notifyChange(Uri, ContentObserver)} |
| 123 | * when the app is ready to provide the complete data in onBindSlice. |
| 124 | * <p> |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 125 | * The slice returned should have a spec that is compatible with one of |
| 126 | * the supported specs. |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 127 | * |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 128 | * @param sliceUri Uri to bind. |
| 129 | * @param supportedSpecs List of supported specs. |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 130 | * @see {@link Slice}. |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 131 | * @see {@link Slice#HINT_PARTIAL} |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 132 | */ |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 133 | public Slice onBindSlice(Uri sliceUri, List<SliceSpec> supportedSpecs) { |
| 134 | return onBindSlice(sliceUri); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @deprecated migrating to {@link #onBindSlice(Uri, List)} |
| 139 | */ |
| 140 | @Deprecated |
| 141 | public Slice onBindSlice(Uri sliceUri) { |
| 142 | return null; |
| 143 | } |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 144 | |
Mady Mellor | 3b0a72f | 2017-10-19 10:12:09 -0700 | [diff] [blame] | 145 | /** |
| 146 | * This method must be overridden if an {@link IntentFilter} is specified on the SliceProvider. |
| 147 | * In that case, this method can be called and is expected to return a non-null Uri representing |
| 148 | * a slice. Otherwise this will throw {@link UnsupportedOperationException}. |
| 149 | * |
| 150 | * @return Uri representing the slice associated with the provided intent. |
| 151 | * @see {@link Slice} |
Mady Mellor | 3b0a72f | 2017-10-19 10:12:09 -0700 | [diff] [blame] | 152 | */ |
| 153 | public @NonNull Uri onMapIntentToUri(Intent intent) { |
| 154 | throw new UnsupportedOperationException( |
| 155 | "This provider has not implemented intent to uri mapping"); |
| 156 | } |
| 157 | |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 158 | @Override |
| 159 | public final int update(Uri uri, ContentValues values, String selection, |
| 160 | String[] selectionArgs) { |
| 161 | if (DEBUG) Log.d(TAG, "update " + uri); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public final int delete(Uri uri, String selection, String[] selectionArgs) { |
| 167 | if (DEBUG) Log.d(TAG, "delete " + uri); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public final Cursor query(Uri uri, String[] projection, String selection, |
| 173 | String[] selectionArgs, String sortOrder) { |
| 174 | if (DEBUG) Log.d(TAG, "query " + uri); |
| 175 | return null; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public final Cursor query(Uri uri, String[] projection, String selection, String[] |
| 180 | selectionArgs, String sortOrder, CancellationSignal cancellationSignal) { |
| 181 | if (DEBUG) Log.d(TAG, "query " + uri); |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public final Cursor query(Uri uri, String[] projection, Bundle queryArgs, |
| 187 | CancellationSignal cancellationSignal) { |
| 188 | if (DEBUG) Log.d(TAG, "query " + uri); |
| 189 | return null; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public final Uri insert(Uri uri, ContentValues values) { |
| 194 | if (DEBUG) Log.d(TAG, "insert " + uri); |
| 195 | return null; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public final String getType(Uri uri) { |
| 200 | if (DEBUG) Log.d(TAG, "getType " + uri); |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 201 | return SLICE_TYPE; |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 205 | public Bundle call(String method, String arg, Bundle extras) { |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 206 | if (method.equals(METHOD_SLICE)) { |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 207 | Uri uri = extras.getParcelable(EXTRA_BIND_URI); |
Jason Monk | b40dad5 | 2017-11-01 16:01:40 -0400 | [diff] [blame] | 208 | if (!UserHandle.isSameApp(Binder.getCallingUid(), Process.myUid())) { |
| 209 | getContext().enforceUriPermission(uri, permission.BIND_SLICE, |
| 210 | permission.BIND_SLICE, Binder.getCallingPid(), Binder.getCallingUid(), |
| 211 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION, |
| 212 | "Slice binding requires the permission BIND_SLICE"); |
| 213 | } |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 214 | List<SliceSpec> supportedSpecs = extras.getParcelableArrayList(EXTRA_SUPPORTED_SPECS); |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 215 | |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 216 | Slice s = handleBindSlice(uri, supportedSpecs); |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 217 | Bundle b = new Bundle(); |
| 218 | b.putParcelable(EXTRA_SLICE, s); |
| 219 | return b; |
Mady Mellor | 3b0a72f | 2017-10-19 10:12:09 -0700 | [diff] [blame] | 220 | } else if (method.equals(METHOD_MAP_INTENT)) { |
| 221 | getContext().enforceCallingPermission(permission.BIND_SLICE, |
| 222 | "Slice binding requires the permission BIND_SLICE"); |
| 223 | Intent intent = extras.getParcelable(EXTRA_INTENT); |
| 224 | Uri uri = onMapIntentToUri(intent); |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 225 | List<SliceSpec> supportedSpecs = extras.getParcelableArrayList(EXTRA_SUPPORTED_SPECS); |
Mady Mellor | 3b0a72f | 2017-10-19 10:12:09 -0700 | [diff] [blame] | 226 | Bundle b = new Bundle(); |
| 227 | if (uri != null) { |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 228 | Slice s = handleBindSlice(uri, supportedSpecs); |
Mady Mellor | 3b0a72f | 2017-10-19 10:12:09 -0700 | [diff] [blame] | 229 | b.putParcelable(EXTRA_SLICE, s); |
| 230 | } else { |
| 231 | b.putParcelable(EXTRA_SLICE, null); |
| 232 | } |
| 233 | return b; |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 234 | } |
| 235 | return super.call(method, arg, extras); |
| 236 | } |
| 237 | |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 238 | private Slice handleBindSlice(Uri sliceUri, List<SliceSpec> supportedSpecs) { |
Lucas Dupin | e0653fa | 2017-10-23 14:31:45 -0700 | [diff] [blame] | 239 | if (Looper.myLooper() == Looper.getMainLooper()) { |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 240 | return onBindSliceStrict(sliceUri, supportedSpecs); |
Lucas Dupin | e0653fa | 2017-10-23 14:31:45 -0700 | [diff] [blame] | 241 | } else { |
| 242 | CountDownLatch latch = new CountDownLatch(1); |
| 243 | Slice[] output = new Slice[1]; |
| 244 | Handler.getMain().post(() -> { |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 245 | output[0] = onBindSliceStrict(sliceUri, supportedSpecs); |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 246 | latch.countDown(); |
Lucas Dupin | e0653fa | 2017-10-23 14:31:45 -0700 | [diff] [blame] | 247 | }); |
| 248 | try { |
| 249 | latch.await(); |
| 250 | return output[0]; |
| 251 | } catch (InterruptedException e) { |
| 252 | throw new RuntimeException(e); |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 253 | } |
Lucas Dupin | e0653fa | 2017-10-23 14:31:45 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 257 | private Slice onBindSliceStrict(Uri sliceUri, List<SliceSpec> supportedSpecs) { |
Lucas Dupin | e0653fa | 2017-10-23 14:31:45 -0700 | [diff] [blame] | 258 | ThreadPolicy oldPolicy = StrictMode.getThreadPolicy(); |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 259 | try { |
Lucas Dupin | e0653fa | 2017-10-23 14:31:45 -0700 | [diff] [blame] | 260 | StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() |
| 261 | .detectAll() |
| 262 | .penaltyDeath() |
| 263 | .build()); |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 264 | return onBindSlice(sliceUri, supportedSpecs); |
Lucas Dupin | e0653fa | 2017-10-23 14:31:45 -0700 | [diff] [blame] | 265 | } finally { |
| 266 | StrictMode.setThreadPolicy(oldPolicy); |
Jason Monk | d9edfa94 | 2017-09-25 12:38:53 -0400 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | } |