【私人备忘录】Android P ActivityManagerService(五)ContentProvider的工作过程

一、Activity中调用getContentResolver()获取ContentResolver

ContextWrapper.java
getContentResolver();Activity.java调用的是爷爷类ContextWrapper.java的getContentResolver()
ContextImpl.java
getContentResolver()

二、调用ContentResolver中的query方法启动ContentProvider(insert、update、delete等类似)

ContentResolver.java
query() → acquireUnstableProvider(uri) || acquireProvider(uri)
IContentProvider.java
acquireUnstableProvider(uri) || acquireProvider(uri)
ContextImpl.java
acquireUnstableProvider(uri) || acquireProvider(uri) → mMainThread.acquireProvider()
ActivityThread.java
acquireProvider()
ActivityManagerService.java
getContentProvider() → getContentProviderImpl() → startProcessLocked()


.....(Activity启动代码流程中介绍过此流程,不再详述)


ActivityThread.java
main(thread.attach(false, startSeq)) → attach(mgr.attachApplication(mAppThread, startSeq))
ActivityManagerService.java
attachApplication() → attachApplicationLocked(thread.bindApplication())
ActivityThread.java
ApplicationThread.bindApplication(sendMessage(H.BIND_APPLICATION, data)) → handleBindApplication() → installContentProviders() → installProvider(localProvider.attachInfo(c, info))
ContentProvider.java
attachInfo() → ContentProvider.this.onCreate()

三、调用query方法

ContentResolver.java
query()
IContentProvider.java
query()
ContentProvider.java
ContentProvider.Transport.query() → ContentProvider.this.query()

备注:
1、ContextImpl.getContentResolver()返回的是ApplicationContentResolver
2、ActivityThread.acquireProvider()首先检查是否已经存在目标ContentProvider,如果有就直接返回它,如果没有则请求AMS启动。
3、ActivityManagerService的getContentProviderImpl方法干了很多事情,我们看重点,如果process已经启动了则proc.thread.scheduleInstallProvider(cpi),否则proc = startProcessLocked(),这里我们看后面一种情况。
4、ActivityManagerService.startProcessLocked方法之前已经介绍过了可查看Activity启动代码流程第三部分,主要是完成新进程的启动。然后走到ActivityThread.main()
5、ActivityThread.handleBindApplication()主要工作:
    5.1、创建ContentImpl
    final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
    5.2、创建Instrumentation
    mInstrumentation = (Instrumentation) cl.loadClass(data.instrumentationName.getClassName()).newInstance();
    mInstrumentation.init(this, instrContext, appContext, component, data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
    5.3、创建Application对象
    app = data.info.makeApplication(data.restrictedBackupMode, null);
    5.4、启动当前进程的ContentProvider
    installContentProviders(app, data.providers);
    5.5、调用Application的onCreate方法
    mInstrumentation.callApplicationOnCreate(app);
6、总结一下就是其他应用通过AMS获取ContentProvider的Binder对象即IContentProvider,IContentProvider的具体实现是ContentProviderNative和ContentProvider.Transport,调用IContentProvider的query方法最终会以进程间通信的方式调用到ContentProvider.Transport的query方法,最终调用了ContentProvider的query方法,结果再通过Binder返回给调用者。

你可能感兴趣的:(Android源码学习)