`

ContentProvider --Call方法

阅读更多
很早之前接触ContentProvider知道它提供的方法就是query,insert,update,delete这几个常用的方法,最近在解决Email的问题时发现ContentProvider提供了一个有意思的方法Call,通过这个方法可调用到ContentProvider自定义的方法。

 /**
     * Call a provider-defined method.  This can be used to implement
     * interfaces that are cheaper and/or unnatural for a table-like
     * model.
     *
     * <p class="note"><strong>WARNING:</strong> The framework does no permission checking
     * on this entry into the content provider besides the basic ability for the application
     * to get access to the provider at all.  For example, it has no idea whether the call
     * being executed may read or write data in the provider, so can't enforce those
     * individual permissions.  Any implementation of this method <strong>must</strong>
     * do its own permission checks on incoming calls to make sure they are allowed.</p>
     *
     * @param method method name to call.  Opaque to framework, but should not be {@code null}.
     * @param arg provider-defined String argument.  May be {@code null}.
     * @param extras provider-defined Bundle argument.  May be {@code null}.
     * @return provider-defined return value.  May be {@code null}, which is also
     *   the default for providers which don't implement any call methods.
     */
    public @Nullable Bundle call(@NonNull String method, @Nullable String arg,
            @Nullable Bundle extras) {
        return null;
    }


调用cr.call(Uri.parse(callbackUri), callbackMethod, callbackArg, statusExtras);

http://androidxref.com/8.0.0_r4/xref/packages/apps/Email/emailcommon/src/com/android/emailcommon/service/EmailServiceStatus.java
 private static void syncStatus(final ContentResolver cr, final Bundle syncExtras,
            final int statusType, final long id, final int statusCode, final int progress,
            int syncResult,
            final StatusWriter writer) {
        final String callbackUri = syncExtras.getString(SYNC_EXTRAS_CALLBACK_URI);
        final String callbackMethod = syncExtras.getString(SYNC_EXTRAS_CALLBACK_METHOD);
        if (callbackUri != null && callbackMethod != null) {
            final String callbackArg = syncExtras.getString(SYNC_EXTRAS_CALLBACK_ARG, "");
            final Bundle statusExtras = new Bundle(4);
            statusExtras.putInt(SYNC_STATUS_TYPE, statusType);
            statusExtras.putLong(SYNC_STATUS_ID, id);
            statusExtras.putInt(SYNC_STATUS_CODE, statusCode);
            if (statusCode != IN_PROGRESS) {
                statusExtras.putInt(SYNC_RESULT, syncResult);
            }
            statusExtras.putInt(SYNC_STATUS_PROGRESS, progress);
            if (writer != null) {
                writer.addToStatus(statusExtras);
            }
            cr.call(Uri.parse(callbackUri), callbackMethod, callbackArg, statusExtras);
        }
    }


EmailProvider中的call
http://androidxref.com/8.0.0_r4/xref/packages/apps/Email/provider_src/com/android/email/provider/EmailProvider.java
@Override
    public Bundle call(String method, String arg, Bundle extras) {
        LogUtils.d(TAG, "EmailProvider#call(%s, %s)", method, arg);

        // Handle queries for the device friendly name.
        // TODO: This should eventually be a device property, not defined by the app.
        if (TextUtils.equals(method, EmailContent.DEVICE_FRIENDLY_NAME)) {
            final Bundle bundle = new Bundle(1);
            // TODO: For now, just use the model name since we don't yet have a user-supplied name.
            bundle.putString(EmailContent.DEVICE_FRIENDLY_NAME, Build.MODEL);
            return bundle;
        }

        // Handle sync status callbacks.
        if (TextUtils.equals(method, SYNC_STATUS_CALLBACK_METHOD)) {
            updateSyncStatus(extras);
            return null;
        }
        if (TextUtils.equals(method, MailboxUtilities.FIX_PARENT_KEYS_METHOD)) {
            fixParentKeys(getDatabase(getContext()));
            return null;
        }

        // Handle send & save.
        final Uri accountUri = Uri.parse(arg);
        final long accountId = Long.parseLong(accountUri.getPathSegments().get(1));

        Uri messageUri = null;

        if (TextUtils.equals(method, UIProvider.AccountCallMethods.SEND_MESSAGE)) {
            messageUri = uiSendDraftMessage(accountId, extras);
            Preferences.getPreferences(getContext()).setLastUsedAccountId(accountId);
        } else if (TextUtils.equals(method, UIProvider.AccountCallMethods.SAVE_MESSAGE)) {
            messageUri = uiSaveDraftMessage(accountId, extras);
        } else if (TextUtils.equals(method, UIProvider.AccountCallMethods.SET_CURRENT_ACCOUNT)) {
            LogUtils.d(TAG, "Unhandled (but expected) Content provider method: %s", method);
        } else {
            LogUtils.wtf(TAG, "Unexpected Content provider method: %s", method);
        }

        final Bundle result;
        if (messageUri != null) {
            result = new Bundle(1);
            result.putParcelable(UIProvider.MessageColumns.URI, messageUri);
        } else {
            result = null;
        }

        return result;
    }


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics