From c192ca5a54723247d2d2b449eeaaffb71c88b9de Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Wed, 8 Jan 2020 11:00:23 -0700 Subject: [PATCH] Respond to API council feedback. Adjust Exception to more general Throwable, and add docs for new ContentResolver and ContentProvider overloads. Also add docs for default state of strict query options. Bug: 131598520, 141227540, 147287177 Test: atest --test-mapping packages/providers/MediaProvider Change-Id: Icb0153b8c8a352db39de301074e948bfaa676ddb --- api/current.txt | 4 +- .../java/android/content/ContentProvider.java | 37 +++++++++++++------ .../content/ContentProviderResult.java | 8 ++-- .../java/android/content/ContentResolver.java | 32 +++++++++++++--- .../database/sqlite/SQLiteQueryBuilder.java | 30 +++++++++------ 5 files changed, 77 insertions(+), 34 deletions(-) diff --git a/api/current.txt b/api/current.txt index 64981ca7be2a9..7b1944ae0e740 100644 --- a/api/current.txt +++ b/api/current.txt @@ -9627,13 +9627,13 @@ package android.content { ctor public ContentProviderResult(@NonNull android.net.Uri); ctor public ContentProviderResult(int); ctor public ContentProviderResult(@NonNull android.os.Bundle); - ctor public ContentProviderResult(@NonNull Exception); + ctor public ContentProviderResult(@NonNull Throwable); ctor public ContentProviderResult(android.os.Parcel); method public int describeContents(); method public void writeToParcel(android.os.Parcel, int); field @NonNull public static final android.os.Parcelable.Creator CREATOR; field @Nullable public final Integer count; - field @Nullable public final Exception exception; + field @Nullable public final Throwable exception; field @Nullable public final android.os.Bundle extras; field @Nullable public final android.net.Uri uri; } diff --git a/core/java/android/content/ContentProvider.java b/core/java/android/content/ContentProvider.java index 3eb0e7c9b504b..85826fd4e6699 100644 --- a/core/java/android/content/ContentProvider.java +++ b/core/java/android/content/ContentProvider.java @@ -1409,8 +1409,11 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall * @param uri The URI to query. This will be the full URI sent by the client. * @param projection The list of columns to put into the cursor. * If {@code null} provide a default set of columns. - * @param queryArgs A Bundle containing all additional information necessary for the query. - * Values in the Bundle may include SQL style arguments. + * @param queryArgs A Bundle containing additional information necessary for + * the operation. Arguments may include SQL style arguments, such + * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that + * the documentation for each individual provider will indicate + * which arguments they support. * @param cancellationSignal A signal to cancel the operation in progress, * or {@code null}. * @return a Cursor or {@code null}. @@ -1609,9 +1612,14 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall * * @param uri The content:// URI of the insertion request. * @param values A set of column_name/value pairs to add to the database. - * @param extras A Bundle containing all additional information necessary - * for the insert. + * @param extras A Bundle containing additional information necessary for + * the operation. Arguments may include SQL style arguments, such + * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that + * the documentation for each individual provider will indicate + * which arguments they support. * @return The URI for the newly inserted item. + * @throws IllegalArgumentException if the provider doesn't support one of + * the requested Bundle arguments. */ @Override public @Nullable Uri insert(@NonNull Uri uri, @Nullable ContentValues values, @@ -1688,10 +1696,13 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall * * @param uri The full URI to query, including a row ID (if a specific * record is requested). - * @param extras A Bundle containing all additional information necessary - * for the delete. Values in the Bundle may include SQL style - * arguments. - * @return The number of rows affected. + * @param extras A Bundle containing additional information necessary for + * the operation. Arguments may include SQL style arguments, such + * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that + * the documentation for each individual provider will indicate + * which arguments they support. + * @throws IllegalArgumentException if the provider doesn't support one of + * the requested Bundle arguments. * @throws SQLException */ @Override @@ -1734,10 +1745,14 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall * @param uri The URI to query. This can potentially have a record ID if * this is an update request for a specific record. * @param values A set of column_name/value pairs to update in the database. - * @param extras A Bundle containing all additional information necessary - * for the update. Values in the Bundle may include SQL style - * arguments. + * @param extras A Bundle containing additional information necessary for + * the operation. Arguments may include SQL style arguments, such + * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that + * the documentation for each individual provider will indicate + * which arguments they support. * @return the number of rows affected. + * @throws IllegalArgumentException if the provider doesn't support one of + * the requested Bundle arguments. */ @Override public int update(@NonNull Uri uri, @Nullable ContentValues values, diff --git a/core/java/android/content/ContentProviderResult.java b/core/java/android/content/ContentProviderResult.java index 11dda83a6e6c2..4fb1ddb958d57 100644 --- a/core/java/android/content/ContentProviderResult.java +++ b/core/java/android/content/ContentProviderResult.java @@ -36,7 +36,7 @@ public class ContentProviderResult implements Parcelable { public final @Nullable Uri uri; public final @Nullable Integer count; public final @Nullable Bundle extras; - public final @Nullable Exception exception; + public final @Nullable Throwable exception; public ContentProviderResult(@NonNull Uri uri) { this(Objects.requireNonNull(uri), null, null, null); @@ -50,12 +50,12 @@ public class ContentProviderResult implements Parcelable { this(null, null, Objects.requireNonNull(extras), null); } - public ContentProviderResult(@NonNull Exception exception) { + public ContentProviderResult(@NonNull Throwable exception) { this(null, null, null, exception); } /** {@hide} */ - public ContentProviderResult(Uri uri, Integer count, Bundle extras, Exception exception) { + public ContentProviderResult(Uri uri, Integer count, Bundle extras, Throwable exception) { this.uri = uri; this.count = count; this.extras = extras; @@ -79,7 +79,7 @@ public class ContentProviderResult implements Parcelable { extras = null; } if (source.readInt() != 0) { - exception = (Exception) ParcelableException.readFromParcel(source); + exception = ParcelableException.readFromParcel(source); } else { exception = null; } diff --git a/core/java/android/content/ContentResolver.java b/core/java/android/content/ContentResolver.java index 592190e0ec115..6cd1cd3354c24 100644 --- a/core/java/android/content/ContentResolver.java +++ b/core/java/android/content/ContentResolver.java @@ -984,7 +984,11 @@ public abstract class ContentResolver implements ContentInterface { * retrieve. * @param projection A list of which columns to return. Passing null will * return all columns, which is inefficient. - * @param queryArgs A Bundle containing any arguments to the query. + * @param queryArgs A Bundle containing additional information necessary for + * the operation. Arguments may include SQL style arguments, such + * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that + * the documentation for each individual provider will indicate + * which arguments they support. * @param cancellationSignal A signal to cancel the operation in progress, or null if none. * If the operation is canceled, then {@link OperationCanceledException} will be thrown * when the query is executed. @@ -1925,9 +1929,15 @@ public abstract class ContentResolver implements ContentInterface { * @param url The URL of the table to insert into. * @param values The initial values for the newly inserted row. The key is the column name for * the field. Passing an empty ContentValues will create an empty row. - * @param extras A Bundle containing all additional information necessary for the insert. + * @param extras A Bundle containing additional information necessary for + * the operation. Arguments may include SQL style arguments, such + * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that + * the documentation for each individual provider will indicate + * which arguments they support. * @return the URL of the newly created row. May return null if the underlying * content provider returns null, or if it crashes. + * @throws IllegalArgumentException if the provider doesn't support one of + * the requested Bundle arguments. */ @Override public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url, @@ -2061,9 +2071,14 @@ public abstract class ContentResolver implements ContentInterface { * If the content provider supports transactions, the deletion will be atomic. * * @param url The URL of the row to delete. - * @param extras A Bundle containing all additional information necessary for the delete. - * Values in the Bundle may include SQL style arguments. + * @param extras A Bundle containing additional information necessary for + * the operation. Arguments may include SQL style arguments, such + * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that + * the documentation for each individual provider will indicate + * which arguments they support. * @return The number of rows deleted. + * @throws IllegalArgumentException if the provider doesn't support one of + * the requested Bundle arguments. */ @Override public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable Bundle extras) { @@ -2121,10 +2136,15 @@ public abstract class ContentResolver implements ContentInterface { * @param uri The URI to modify. * @param values The new field values. The key is the column name for the field. A null value will remove an existing field value. - * @param extras A Bundle containing all additional information necessary for the update. - * Values in the Bundle may include SQL style arguments. + * @param extras A Bundle containing additional information necessary for + * the operation. Arguments may include SQL style arguments, such + * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that + * the documentation for each individual provider will indicate + * which arguments they support. * @return the number of rows updated. * @throws NullPointerException if uri or values are null + * @throws IllegalArgumentException if the provider doesn't support one of + * the requested Bundle arguments. */ @Override public final int update(@RequiresPermission.Write @NonNull Uri uri, diff --git a/core/java/android/database/sqlite/SQLiteQueryBuilder.java b/core/java/android/database/sqlite/SQLiteQueryBuilder.java index 5e4a78439500b..36ec67ee1471c 100644 --- a/core/java/android/database/sqlite/SQLiteQueryBuilder.java +++ b/core/java/android/database/sqlite/SQLiteQueryBuilder.java @@ -240,25 +240,27 @@ public class SQLiteQueryBuilder { } /** - * When set, the selection is verified against malicious arguments. - * When using this class to create a statement using + * When set, the selection is verified against malicious arguments. When + * using this class to create a statement using * {@link #buildQueryString(boolean, String, String[], String, String, String, String, String)}, - * non-numeric limits will raise an exception. If a projection map is specified, fields - * not in that map will be ignored. - * If this class is used to execute the statement directly using + * non-numeric limits will raise an exception. If a projection map is + * specified, fields not in that map will be ignored. If this class is used + * to execute the statement directly using * {@link #query(SQLiteDatabase, String[], String, String[], String, String, String)} * or * {@link #query(SQLiteDatabase, String[], String, String[], String, String, String, String)}, - * additionally also parenthesis escaping selection are caught. - * - * To summarize: To get maximum protection against malicious third party apps (for example - * content provider consumers), make sure to do the following: + * additionally also parenthesis escaping selection are caught. To + * summarize: To get maximum protection against malicious third party apps + * (for example content provider consumers), make sure to do the following: * - * By default, this value is false. + *

+ * This feature is disabled by default on each newly constructed + * {@link SQLiteQueryBuilder} and needs to be manually enabled. */ public void setStrict(boolean strict) { if (strict) { @@ -283,6 +285,9 @@ public class SQLiteQueryBuilder { * This enforcement applies to {@link #insert}, {@link #query}, and * {@link #update} operations. Any enforcement failures will throw an * {@link IllegalArgumentException}. + *

+ * This feature is disabled by default on each newly constructed + * {@link SQLiteQueryBuilder} and needs to be manually enabled. */ public void setStrictColumns(boolean strictColumns) { if (strictColumns) { @@ -319,6 +324,9 @@ public class SQLiteQueryBuilder { * {@link #delete} operations. This enforcement does not apply to trusted * inputs, such as those provided by {@link #appendWhere}. Any enforcement * failures will throw an {@link IllegalArgumentException}. + *

+ * This feature is disabled by default on each newly constructed + * {@link SQLiteQueryBuilder} and needs to be manually enabled. */ public void setStrictGrammar(boolean strictGrammar) { if (strictGrammar) {