SliceProvider.java revision 43ba1c3f098b58d605fa100ab977cfc5e299309a
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 */ 16package androidx.slice; 17 18import static android.app.slice.Slice.HINT_SHORTCUT; 19import static android.app.slice.Slice.HINT_TITLE; 20import static android.app.slice.SliceProvider.SLICE_TYPE; 21 22import static androidx.slice.compat.SliceProviderCompat.EXTRA_BIND_URI; 23import static androidx.slice.compat.SliceProviderCompat.EXTRA_PKG; 24import static androidx.slice.compat.SliceProviderCompat.EXTRA_PROVIDER_PKG; 25import static androidx.slice.core.SliceHints.HINT_PERMISSION_REQUEST; 26 27import android.app.PendingIntent; 28import android.content.ComponentName; 29import android.content.ContentProvider; 30import android.content.ContentResolver; 31import android.content.ContentValues; 32import android.content.Context; 33import android.content.Intent; 34import android.content.IntentFilter; 35import android.content.pm.PackageManager; 36import android.database.ContentObserver; 37import android.database.Cursor; 38import android.net.Uri; 39import android.os.Bundle; 40import android.os.CancellationSignal; 41import android.util.Log; 42 43import androidx.annotation.NonNull; 44import androidx.annotation.Nullable; 45import androidx.annotation.RequiresApi; 46import androidx.annotation.RestrictTo; 47import androidx.core.app.CoreComponentFactory; 48import androidx.core.os.BuildCompat; 49import androidx.slice.compat.SliceProviderCompat; 50import androidx.slice.compat.SliceProviderWrapperContainer; 51import androidx.slice.core.R; 52 53import java.util.Collection; 54import java.util.Collections; 55import java.util.Set; 56 57/** 58 * A SliceProvider allows an app to provide content to be displayed in system spaces. This content 59 * is templated and can contain actions, and the behavior of how it is surfaced is specific to the 60 * system surface. 61 * <p> 62 * Slices are not currently live content. They are bound once and shown to the user. If the content 63 * changes due to a callback from user interaction, then 64 * {@link ContentResolver#notifyChange(Uri, ContentObserver)} should be used to notify the system. 65 * </p> 66 * <p> 67 * The provider needs to be declared in the manifest to provide the authority for the app. The 68 * authority for most slices is expected to match the package of the application. 69 * </p> 70 * 71 * <pre class="prettyprint"> 72 * {@literal 73 * <provider 74 * android:name="com.android.mypkg.MySliceProvider" 75 * android:authorities="com.android.mypkg" />} 76 * </pre> 77 * <p> 78 * Slices can be identified by a Uri or by an Intent. To link an Intent with a slice, the provider 79 * must have an {@link IntentFilter} matching the slice intent. When a slice is being requested via 80 * an intent, {@link #onMapIntentToUri(Intent)} can be called and is expected to return an 81 * appropriate Uri representing the slice. 82 * 83 * <pre class="prettyprint"> 84 * {@literal 85 * <provider 86 * android:name="com.android.mypkg.MySliceProvider" 87 * android:authorities="com.android.mypkg"> 88 * <intent-filter> 89 * <action android:name="android.intent.action.MY_SLICE_INTENT" /> 90 * </intent-filter> 91 * </provider>} 92 * </pre> 93 * 94 * @see android.app.slice.Slice 95 */ 96public abstract class SliceProvider extends ContentProvider implements 97 CoreComponentFactory.CompatWrapped { 98 99 private static Set<SliceSpec> sSpecs; 100 101 private static final String TAG = "SliceProvider"; 102 103 private static final boolean DEBUG = false; 104 105 private SliceProviderCompat mCompat; 106 107 /** 108 * Implement this to initialize your slice provider on startup. 109 * This method is called for all registered slice providers on the 110 * application main thread at application launch time. It must not perform 111 * lengthy operations, or application startup will be delayed. 112 * 113 * <p>You should defer nontrivial initialization (such as opening, 114 * upgrading, and scanning databases) until the slice provider is used 115 * (via #onBindSlice, etc). Deferred initialization 116 * keeps application startup fast, avoids unnecessary work if the provider 117 * turns out not to be needed, and stops database errors (such as a full 118 * disk) from halting application launch. 119 * 120 * @return true if the provider was successfully loaded, false otherwise 121 */ 122 public abstract boolean onCreateSliceProvider(); 123 124 @Override 125 public Object getWrapper() { 126 if (BuildCompat.isAtLeastP()) { 127 return new SliceProviderWrapperContainer.SliceProviderWrapper(this); 128 } 129 return null; 130 } 131 132 @Override 133 public final boolean onCreate() { 134 if (!BuildCompat.isAtLeastP()) { 135 mCompat = new SliceProviderCompat(this); 136 } 137 return onCreateSliceProvider(); 138 } 139 140 @Override 141 public final String getType(Uri uri) { 142 if (DEBUG) Log.d(TAG, "getFormat " + uri); 143 return SLICE_TYPE; 144 } 145 146 @Override 147 public Bundle call(String method, String arg, Bundle extras) { 148 return mCompat != null ? mCompat.call(method, arg, extras) : null; 149 } 150 151 /** 152 * Generate a slice that contains a permission request. 153 * @hide 154 */ 155 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 156 public static Slice createPermissionSlice(Context context, Uri sliceUri, 157 String callingPackage) { 158 Slice.Builder parent = new Slice.Builder(sliceUri); 159 160 Slice.Builder action = new Slice.Builder(parent) 161 .addHints(HINT_TITLE, HINT_SHORTCUT) 162 .addAction(createPermissionIntent(context, sliceUri, callingPackage), 163 new Slice.Builder(parent).build(), null); 164 165 parent.addSubSlice(new Slice.Builder(sliceUri.buildUpon().appendPath("permission").build()) 166 .addText(getPermissionString(context, callingPackage), null) 167 .addSubSlice(action.build()) 168 .build()); 169 170 return parent.addHints(HINT_PERMISSION_REQUEST).build(); 171 } 172 173 /** 174 * Create a PendingIntent pointing at the permission dialog. 175 * @hide 176 */ 177 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 178 public static PendingIntent createPermissionIntent(Context context, Uri sliceUri, 179 String callingPackage) { 180 Intent intent = new Intent(); 181 intent.setComponent(new ComponentName(context.getPackageName(), 182 "androidx.slice.compat.SlicePermissionActivity")); 183 intent.putExtra(EXTRA_BIND_URI, sliceUri); 184 intent.putExtra(EXTRA_PKG, callingPackage); 185 intent.putExtra(EXTRA_PROVIDER_PKG, context.getPackageName()); 186 // Unique pending intent. 187 intent.setData(sliceUri.buildUpon().appendQueryParameter("package", callingPackage) 188 .build()); 189 190 return PendingIntent.getActivity(context, 0, intent, 0); 191 } 192 193 /** 194 * Get string describing permission request. 195 * @hide 196 */ 197 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 198 public static CharSequence getPermissionString(Context context, String callingPackage) { 199 PackageManager pm = context.getPackageManager(); 200 try { 201 return context.getString(R.string.abc_slices_permission_request, 202 pm.getApplicationInfo(callingPackage, 0).loadLabel(pm), 203 context.getApplicationInfo().loadLabel(pm)); 204 } catch (PackageManager.NameNotFoundException e) { 205 // This shouldn't be possible since the caller is verified. 206 throw new RuntimeException("Unknown calling app", e); 207 } 208 } 209 210 /** 211 * Implemented to create a slice. 212 * <p> 213 * onBindSlice should return as quickly as possible so that the UI tied 214 * to this slice can be responsive. No network or other IO will be allowed 215 * during onBindSlice. Any loading that needs to be done should happen 216 * in the background with a call to {@link ContentResolver#notifyChange(Uri, ContentObserver)} 217 * when the app is ready to provide the complete data in onBindSlice. 218 * <p> 219 * 220 * @see {@link Slice}. 221 * @see {@link android.app.slice.Slice#HINT_PARTIAL} 222 */ 223 // TODO: Provide alternate notifyChange that takes in the slice (i.e. notifyChange(Uri, Slice)). 224 public abstract Slice onBindSlice(Uri sliceUri); 225 226 /** 227 * Called to inform an app that a slice has been pinned. 228 * <p> 229 * Pinning is a way that slice hosts use to notify apps of which slices 230 * they care about updates for. When a slice is pinned the content is 231 * expected to be relatively fresh and kept up to date. 232 * <p> 233 * Being pinned does not provide any escalated privileges for the slice 234 * provider. So apps should do things such as turn on syncing or schedule 235 * a job in response to a onSlicePinned. 236 * <p> 237 * Pinned state is not persisted through a reboot, and apps can expect a 238 * new call to onSlicePinned for any slices that should remain pinned 239 * after a reboot occurs. 240 * 241 * @param sliceUri The uri of the slice being unpinned. 242 * @see #onSliceUnpinned(Uri) 243 */ 244 public void onSlicePinned(Uri sliceUri) { 245 } 246 247 /** 248 * Called to inform an app that a slices is no longer pinned. 249 * <p> 250 * This means that no other apps on the device care about updates to this 251 * slice anymore and therefore it is not important to be updated. Any syncs 252 * or jobs related to this slice should be cancelled. 253 * @see #onSlicePinned(Uri) 254 */ 255 public void onSliceUnpinned(Uri sliceUri) { 256 } 257 258 /** 259 * This method must be overridden if an {@link IntentFilter} is specified on the SliceProvider. 260 * In that case, this method can be called and is expected to return a non-null Uri representing 261 * a slice. Otherwise this will throw {@link UnsupportedOperationException}. 262 * 263 * @return Uri representing the slice associated with the provided intent. 264 * @see {@link android.app.slice.Slice} 265 */ 266 public @NonNull Uri onMapIntentToUri(Intent intent) { 267 throw new UnsupportedOperationException( 268 "This provider has not implemented intent to uri mapping"); 269 } 270 271 /** 272 * Obtains a list of slices that are descendants of the specified Uri. 273 * <p> 274 * Implementing this is optional for a SliceProvider, but does provide a good 275 * discovery mechanism for finding slice Uris. 276 * 277 * @param uri The uri to look for descendants under. 278 * @return All slices within the space. 279 * @see androidx.slice.SliceManager#getSliceDescendants(Uri) 280 */ 281 public Collection<Uri> onGetSliceDescendants(Uri uri) { 282 return Collections.emptyList(); 283 } 284 285 @Nullable 286 @Override 287 public final Cursor query(@NonNull Uri uri, @Nullable String[] projection, 288 @Nullable String selection, @Nullable String[] selectionArgs, 289 @Nullable String sortOrder) { 290 return null; 291 } 292 293 @Nullable 294 @Override 295 @RequiresApi(28) 296 public final Cursor query(@NonNull Uri uri, @Nullable String[] projection, 297 @Nullable Bundle queryArgs, @Nullable CancellationSignal cancellationSignal) { 298 return null; 299 } 300 301 @Nullable 302 @Override 303 @RequiresApi(16) 304 public final Cursor query(@NonNull Uri uri, @Nullable String[] projection, 305 @Nullable String selection, @Nullable String[] selectionArgs, 306 @Nullable String sortOrder, @Nullable CancellationSignal cancellationSignal) { 307 return null; 308 } 309 310 @Nullable 311 @Override 312 public final Uri insert(@NonNull Uri uri, @Nullable ContentValues values) { 313 return null; 314 } 315 316 @Override 317 public final int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) { 318 return 0; 319 } 320 321 @Override 322 public final int delete(@NonNull Uri uri, @Nullable String selection, 323 @Nullable String[] selectionArgs) { 324 return 0; 325 } 326 327 @Override 328 public final int update(@NonNull Uri uri, @Nullable ContentValues values, 329 @Nullable String selection, @Nullable String[] selectionArgs) { 330 return 0; 331 } 332 333 @Nullable 334 @Override 335 @RequiresApi(19) 336 public final Uri canonicalize(@NonNull Uri url) { 337 return null; 338 } 339 340 /** 341 * @hide 342 */ 343 @RestrictTo(RestrictTo.Scope.LIBRARY) 344 public static void setSpecs(Set<SliceSpec> specs) { 345 sSpecs = specs; 346 } 347 348 /** 349 * @hide 350 */ 351 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 352 public static Set<SliceSpec> getCurrentSpecs() { 353 return sSpecs; 354 } 355} 356