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);

[url]http://androidxref.com/8.0.0_r4/xref/packages/apps/Email/emailcommon/src/com/android/emailcommon/service/EmailServiceStatus.java[/url]
 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
[url]http://androidxref.com/8.0.0_r4/xref/packages/apps/Email/provider_src/com/android/email/provider/EmailProvider.java[/url]

@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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值