您的位置:首页 > 移动开发 > Android开发

Android应用程序组件Content Provider在应用程序之间共享数据的原理分析(3)

2011-12-05 01:00 811 查看
Step 11. CursorWindow.writeToParcel
这个函数定义在frameworks/base/core/java/android/database/CursorWindow.java文件中:

public class CursorWindow extends SQLiteClosable implements Parcelable {

......

public void writeToParcel(Parcel dest, int flags) {

......

dest.writeStrongBinder(native_getBinder());

......

}

......

}

这个函数最主要的操作就是往数据流dest写入一个Binder对象,这个Binder对象是通过调用本地方法native_getBinder来得到的。

Step 12. CursorWindow.native_getBinder
这个函数定义在frameworks/base/core/jni/android_database_CursorWindow.cpp文件中:

static jobject native_getBinder(JNIEnv * env, jobject object)

{

CursorWindow * window = GET_WINDOW(env, object);

if (window) {

sp<IMemory> memory = window->getMemory();

if (memory != NULL) {

sp<IBinder> binder = memory->asBinder();

return javaObjectForIBinder(env, binder);

}

}

return NULL;

}

在前面的Step 8中,我们在C++层创建了一个CursorWindow对象,这个对象保存在Java层创建的CursorWindow对象的成员变量nWindow中,这里通过GET_WINDOW宏来把这个在C++层创建的CurosrWindow对象返回来:

#define GET_WINDOW(env, object) ((CursorWindow *)env->GetIntField(object, gWindowField))

获得了这个CursorWindow对象以后,就调用它的getMemory函数来获得一个IMemory接口,这是一个Binder接口,具体可以参考前面一篇文章Android系统匿名共享内存(Anonymous Shared Memory)C++调用接口分析

Step 13. CursorWindow.getMemory
这个函数定义在frameworks/base/core/jni/CursorWindow.h文件中:

class CursorWindow

{

public:

......

sp<IMemory> getMemory() {return mMemory;}

......

}

这个CursorWindow对象的成员变量mMemory就是在前面Step 9中创建的了。

这样,在第三方应用程序这一侧创建的匿名共享存对象就可以传递给Content Provider来使用了。回到前面的Step 10中,所有的参数都就准备就绪以后,就通过Binder进程间通信机制把数据查询请求发送给相应的Content Proivder了。这个请求是在ContentProviderNative类的onTransact函数中响应的。
Step 14. ContentProviderNative.onTransact
这个函数定义在frameworks/base/core/java/android/content/ContentProviderNative.java文件中:
abstract public class ContentProviderNative extends Binder implements IContentProvider {
......

@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws RemoteException {
try {
switch (code) {
case QUERY_TRANSACTION:
{
data.enforceInterface(IContentProvider.descriptor);

Uri url = Uri.CREATOR.createFromParcel(data);

// String[] projection
int num = data.readInt();
String[] projection = null;
if (num > 0) {
projection = new String[num];
for (int i = 0; i < num; i++) {
projection[i] = data.readString();
}
}

// String selection, String[] selectionArgs...
String selection = data.readString();
num = data.readInt();
String[] selectionArgs = null;
if (num > 0) {
selectionArgs = new String[num];
for (int i = 0; i < num; i++) {
selectionArgs[i] = data.readString();
}
}

String sortOrder = data.readString();
IContentObserver observer = IContentObserver.Stub.
asInterface(data.readStrongBinder());
CursorWindow window = CursorWindow.CREATOR.createFromParcel(data);

// Flag for whether caller wants the number of
// rows in the cursor and the position of the
// "_id" column index (or -1 if non-existent)
// Only to be returned if binder != null.
boolean wantsCursorMetadata = data.readInt() != 0;

IBulkCursor bulkCursor = bulkQuery(url, projection, selection,
selectionArgs, sortOrder, observer, window);
reply.writeNoException();
if (bulkCursor != null) {
reply.writeStrongBinder(bulkCursor.asBinder());

if (wantsCursorMetadata) {
reply.writeInt(bulkCursor.count());
reply.writeInt(BulkCursorToCursorAdaptor.findRowIdColumnIndex(
bulkCursor.getColumnNames()));
}
} else {
reply.writeStrongBinder(null);
}

return true;
}
......
}
} catch (Exception e) {
DatabaseUtils.writeExceptionToParcel(reply, e);
return true;
}

return super.onTransact(code, data, reply, flags);
}

......
}

这一步其实就是前面Step 10的逆操作,把请求参数从数据流data中读取出来。这里我们同样是重点关注下面这两个参数读取的步骤:

CursorWindow window = CursorWindow.CREATOR.createFromParcel(data);

// Flag for whether caller wants the number of

// rows in the cursor and the position of the

// "_id" column index (or -1 if non-existent)

// Only to be returned if binder != null.

boolean wantsCursorMetadata = data.readInt() != 0;

通过调用CursorWindow.CREATOR.createFromParcel函数来从数据流data中重建一个本地的CursorWindow对象;接着又将数据流data的下一个整数值读取出来,如果这个整数值不为0,变量wantsCursorMetadata的值就为true,说明Content Provider在返回IBulkCursor接口给第三方应用程序之前,要先实际执行一把数据库查询操作,以便把结果数据的元信息返回给第三方应用程序。

通过下面的代码我们可以看到,调用bulkQuery函数之后,就得到了一个IBulkCursor接口,这表示要返回的数据准备就绪了,但是这时候实际上还没有把结果数据从数据库中提取出来,而只是准备好了一个SQL查询计划,等到真正要使用这些结果数据时,系统才会真正执行查询数据库的操作:

if (wantsCursorMetadata) {

reply.writeInt(bulkCursor.count());

......

}

在将这个IBulkCursor接口返回给第三方应用程序之前,如果发现wantsCursorMetadata的值就为true,就会调用它的count函数来获得结果数据的总行数,这样就会导致系统真正去执行数据库查询操作,并把结果数据保存到前面得到的CursorWindow对象中的匿名共享内存中去。

下面我们就重点关注CursorWindow.CREATOR.createFromParcel函数是如何从数据流data中在本地构造一个CursorWindow对象的。
Step 15. CursorWindow.CREATOR.createFromParcel
这个函数定义在frameworks/base/core/java/android/database/CursorWindow.java文件中:

public class CursorWindow extends SQLiteClosable implements Parcelable {

......

private CursorWindow(Parcel source) {

IBinder nativeBinder = source.readStrongBinder();

......

native_init(nativeBinder);

}

......

public static final Parcelable.Creator<CursorWindow> CREATOR

= new Parcelable.Creator<CursorWindow>() {

public CursorWindow createFromParcel(Parcel source) {

return new CursorWindow(source);

}

......

};

......

}

在创建CursorWindow对象的过程中,首先是从数据流source中将在前面Step 10中写入的Binder接口读取出来,然后使用这个Binder接口来初始化这个CursorWindow对象,通过前面的Step 13,我们知道,这个Binder接口的实际类型为IMemory,它封装了对匿名共享内存的访问操作。初始化这个匿名共享内存对象的操作是由本地方法native_init函数来实现的,下面我们就看看它的实现。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐