Content Provider��Ӧ�ó������Activity��Serviceһ������Ҫ��AndroidManifest.xml�ļ�������֮�����ʹ�á�ϵͳ�ڰ�װ����Content Provider��Ӧ�ó����ʱ�����ЩContent Provider��������Ϣ������������������Ҫ�ľ���Content Provider��Authority��Ϣ��AndroidӦ�ó���İ�װ���̾�����Բο�AndroidӦ�ó���װ����Դ�������һ�ġ�ע�⣬��װӦ�ó����ʱ���������Ӧ��Content Provider���ص��ڴ�������ϵͳ��ȡ���������صĻ��ƣ��ȵ���һ��Ҫʹ�����Content Provider��ʱ��ϵͳ�Ż�������ص��ڴ��������´���Ҫʹ�����Content Provider��ʱ�Ϳ���ֱ�ӷ����ˡ�
������ǰ��һƪ����AndroidӦ�ó������Content ProviderӦ��ʵ���е���������ϸ����Content Provider���������̡���AndroidӦ�ó������Content ProviderӦ��ʵ����ƪ���½��ܵ�Ӧ�ó���Article�У���һ��ʹ��ArticlesProvider���Content Provider�ĵط���ArticlesAdapter���getArticleCount��������ΪMainActivityҪ��ListView����ʾ������Ϣ�б�ʱ�� ����Ҫ֪��ArticlesProvider�е�������Ϣ����������ArticlesAdapter���getArticleCount�������ÿ�ʼ��һֱ��ArticlesProvider���onCreate���������ã�����ArticlesProvider�������������̣��������Ǿ��ȿ���������̵�����ͼ��Ȼ������ϸ����ÿһ�����裺
Step 1. ArticlesAdapter.getArticleCount
�������������ǰ��һƪ����AndroidӦ�ó������Content ProviderӦ��ʵ�����ܵ�Ӧ�ó���ArtilceԴ���빤��Ŀ¼�£����ļ�Ϊpackages/experimental/Article/src/shy/luo/article/ArticlesAdapter.java�У�
- public class ArticlesAdapter {
- ......
- private ContentResolver resolver = null;
- public ArticlesAdapter(Context context) {
- resolver = context.getContentResolver();
- }
- ......
- public int getArticleCount() {
- int count = 0;
- try {
- IContentProvider provider = resolver.acquireProvider(Articles.CONTENT_URI);
- Bundle bundle = provider.call(Articles.METHOD_GET_ITEM_COUNT, null, null);
- count = bundle.getInt(Articles.KEY_ITEM_COUNT, 0);
- } catch(RemoteException e) {
- e.printStackTrace();
- }
- return count;
- }
- ......
- }
�������ͨ��Ӧ�ó��������ĵ�ContentResolver�ӿ�resolver��acquireProvider�����������Articles.CONTENT_URI��Ӧ��Content Provider�����IContentProvider�ӿڡ�����Articles.CONTENT_URI����Ӧ�ó���ArticlesProvider�ж���ģ�����ֵΪ��content://shy.luo.providers.articles/item������Ӧ��Content Provider����ArticlesProvider�ˡ�
Step 2. ContentResolver.acqireProvider
�������������frameworks/base/core/java/android/content/ContentResolver.java�ļ��У�
- public abstract class ContentResolver {
- ......
- public final IContentProvider acquireProvider(Uri uri) {
- if (!SCHEME_CONTENT.equals(uri.getScheme())) {
- return null;
- }
- String auth = uri.getAuthority();
- if (auth != null) {
- return acquireProvider(mContext, uri.getAuthority());
- }
- return null;
- }
- ......
- }
����������֤����uri��scheme�Ƿ���ȷ�����Ƿ�����content://��ͷ��Ȼ��ȡ������authority���֣�����������һ����Ա����acquireProviderִ�л�ȡContentProvider�ӿڵIJ���������������龰�У�����uri��authority�����ݱ��ǡ�shy.luo.providers.articles���ˡ�
��ContentResolver��Ķ������ǿ��Կ���������һ�������࣬���������汾��acquireProvider������������������ʵ�ֵġ��ص�Step 1�У����ContentResolver�ӿ���ͨ��Ӧ�ó���������Context�����getContentResolver��������õģ���Ӧ�ó���������Context����ContextImpl����ʵ�ֵģ���������frameworks/base/core/java/android/app/ContextImpl.java�ļ��У�
- class ContextImpl extends Context {
- ......
- private ApplicationContentResolver mContentResolver;
- ......
- final void init(LoadedApk packageInfo,
- IBinder activityToken, ActivityThread mainThread,
- Resources container) {
- ......
- mContentResolver = new ApplicationContentResolver(this, mainThread);
- ......
- }
- ......
- @Override
- public ContentResolver getContentResolver() {
- return mContentResolver;
- }
- ......
- }
ContextImpl���init��������Ӧ�ó���������ʱ����õģ�������Բο�AndroidӦ�ó�����������Դ�������һ���е�Step 34��
��ˣ��������ContentResolver���acquireProvider�������������Ҫ���õ�ApplicationContentResolver���acquireProvider������
Step 3. ApplicationContentResolve.acquireProvider
�������������frameworks/base/core/java/android/app/ContextImpl.java�ļ��У�
- class ContextImpl extends Context {
- ......
- private static final class ApplicationContentResolver extends ContentResolver {
- ......
- @Override
- protected IContentProvider acquireProvider(Context context, String name) {
- return mMainThread.acquireProvider(context, name);
- }
- ......
- private final ActivityThread mMainThread;
- }
- ......
- }
������ActivityThread���acquireProvider������һ��ִ�л�ȡContent Provider�ӿڵIJ�����
Step 4. ActivityThread.acquireProvider
�������������frameworks/base/core/java/android/app/ActivityThread.java�ļ��У�
- public final class ActivityThread {
- ......
- public final IContentProvider acquireProvider(Context c, String name) {
- IContentProvider provider = getProvider(c, name);
- if(provider == null)
- return null;
- ......
- return provider;
- }
- ......
- }
�����ǵ���������һ����Ա����getProvider����һ��ִ�л�ȡContent Provider�ӿڵIJ�����
Step 5. ActivityThread.getProvider
�������������frameworks/base/core/java/android/app/ActivityThread.java�ļ��У�
- public final class ActivityThread {
- ......
- private final IContentProvider getExistingProvider(Context context, String name) {
- synchronized(mProviderMap) {
- final ProviderClientRecord pr = mProviderMap.get(name);
- if (pr != null) {
- return pr.mProvider;
- }
- return null;
- }
- }
- ......
- private final IContentProvider getProvider(Context context, String name) {
- IContentProvider existing = getExistingProvider(context, name);
- if (existing != null) {
- return existing;
- }
- IActivityManager.ContentProviderHolder holder = null;
- try {
- holder = ActivityManagerNative.getDefault().getContentProvider(
- getApplicationThread(), name);
- } catch (RemoteException ex) {
- }
- IContentProvider prov = installProvider(context, holder.provider,
- holder.info, true);
- ......
- return prov;
- }
- ......
- }
����������Ȼ�ͨ��getExistingProvider��������鱾���Ƿ��Ѿ��������Ҫ��ȡ��ContentProvider�ӿڣ�������ڣ���ֱ�ӷ����ˡ������Ѿ����ڵ�ContextProvider�ӿڱ�����ActivityThread���mProviderMap��Ա�����У���ContentProvider��Ӧ��URI��authorityΪ��ֵ���档����������龰�У���Ϊ�ǵ�һ�ε���ArticlesProvider�ӿڣ���ˣ���ʱ��ͨ��getExistingProvider�����õ���IContentProvider�ӿ�Ϊnull����������ͻ����ActivityManagerService�����getContentProvider�ӿ�����ȡһ��ContentProviderHolder����holder���������Ͱ�����������Ҫ��ȡ��ArticlesProvider�ӿڣ��ڽ�����ӿڷ��ظ�������֮�������installProvider������������ӿڱ����ڱ����У��Ա��´�Ҫʹ�����ContentProvider�ӿ�ʱ��ֱ�ӾͿ���ͨ��getExistingProvider������ȡ�ˡ�
�����Ƚ��뵽ActivityManagerService�����getContentProvider�����п���������λ�ȡ��������Ҫ��ArticlesProvider�ӿڵģ�Ȼ���ٷ���������installProvider������ʵ�֡�
Step 6. ActivityManagerService.getContentProvider
�������������frameworks/base/services/java/com/android/server/am/ActivityManagerService.java�ļ��У�
- public final class ActivityManagerService extends ActivityManagerNative
- implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
- ......
- public final ContentProviderHolder getContentProvider(
- IApplicationThread caller, String name) {
- ......
- return getContentProviderImpl(caller, name);
- }
- ......
- }
������getContentProviderImpl��������һ��ִ�в�����
Step 7. ActivityManagerService.getContentProviderImpl
�������������frameworks/base/services/java/com/android/server/am/ActivityManagerService.java�ļ��У�
- public final class ActivityManagerService extends ActivityManagerNative
- implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
- ......
- private final ContentProviderHolder getContentProviderImpl(
- IApplicationThread caller, String name) {
- ContentProviderRecord cpr;
- ProviderInfo cpi = null;
- synchronized(this) {
- ProcessRecord r = null;
- if (caller != null) {
- r = getRecordForAppLocked(caller);
- ......
- }
- // First check if this content provider has been published...
- cpr = mProvidersByName.get(name);
- if (cpr != null) {
- ......
- } else {
- try {
- cpi = AppGlobals.getPackageManager().
- resolveContentProvider(name,
- STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
- } catch (RemoteException ex) {
- }
- ......
- }
- cpr = mProvidersByClass.get(cpi.name);
- final boolean firstClass = cpr == null;
- if (firstClass) {
- try {
- ApplicationInfo ai =
- AppGlobals.getPackageManager().
- getApplicationInfo(
- cpi.applicationInfo.packageName,
- STOCK_PM_FLAGS);
- ......
- cpr = new ContentProviderRecord(cpi, ai);
- } catch (RemoteException ex) {
- // pm is in same process, this will never happen.
- }
- }
- if (r != null && cpr.canRunHere(r)) {
- // If this is a multiprocess provider, then just return its
- // info and allow the caller to instantiate it. Only do
- // this if the provider is the same user as the caller's
- // process, or can run as root (so can be in any process).
- return cpr;
- }
- ......
- // This is single process, and our app is now connecting to it.
- // See if we are already in the process of launching this
- // provider.
- final int N = mLaunchingProviders.size();
- int i;
- for (i=0; i<N; i++) {
- if (mLaunchingProviders.get(i) == cpr) {
- break;
- }
- }
- // If the provider is not already being launched, then get it
- // started.
- if (i >= N) {
- final long origId = Binder.clearCallingIdentity();
- ProcessRecord proc = startProcessLocked(cpi.processName,
- cpr.appInfo, false, 0, "content provider",
- new ComponentName(cpi.applicationInfo.packageName,
- cpi.name), false);
- ......
- mLaunchingProviders.add(cpr);
- ......
- }
- // Make sure the provider is published (the same provider class
- // may be published under multiple names).
- if (firstClass) {
- mProvidersByClass.put(cpi.name, cpr);
- }
- cpr.launchingApp = proc;
- mProvidersByName.put(name, cpr);
- ......
- }
- // Wait for the provider to be published...
- synchronized (cpr) {
- while (cpr.provider == null) {
- ......
- try {
- cpr.wait();
- } catch (InterruptedException ex) {
- }
- }
- }
- return cpr;
- }
- ......
- }
��������Ƚϳ�������һ��һ���ط�����
���������ǻ�ȡ�����ߵĽ��̼�¼����Ϣ��
- ProcessRecord r = null;
- if (caller != null) {
- r = getRecordForAppLocked(caller);
- ......
- }
����������龰�У�Ҫ��ȡ�ľ���Ӧ�ó���Article�Ľ��̼�¼����Ϣ�ˣ�������õ���
��ActivityManagerService�У���������Ա��������������ϵͳ�е�Content Provider��Ϣ�ģ�һ����mProvidersByName��һ����mProvidersByClass��ǰ������Content Provider��authoriryֵΪ��ֵ������ģ���������Content Provider������Ϊ��ֵ������ġ�һ��Content Provider�����ж��authority����ֻ��һ������������Ӧ����ˣ�����Ҫ������Map�����棬����Ϊ�˷�����ݲ�ͬ���������ٲ��Ҷ���Ƶġ�����Ĵ�������������Ҫ��ȡ��Content Provider�Ƿ��Ѿ��Ӵ��ڵ��ˣ�
- // First check if this content provider has been published...
- cpr = mProvidersByName.get(name);
- if (cpr != null) {
- ......
- } else {
- try {
- cpi = AppGlobals.getPackageManager().
- resolveContentProvider(name,
- STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
- } catch (RemoteException ex) {
- }
- ......
- }
- cpr = mProvidersByClass.get(cpi.name);
- final boolean firstClass = cpr == null;
- if (firstClass) {
- try {
- ApplicationInfo ai =
- AppGlobals.getPackageManager().
- getApplicationInfo(
- cpi.applicationInfo.packageName,
- STOCK_PM_FLAGS);
- ......
- cpr = new ContentProviderRecord(cpi, ai);
- } catch (RemoteException ex) {
- // pm is in same process, this will never happen.
- }
- }
����������龰�У������ǵ�һ�ε���ArticlesProvider�ӿڣ���ˣ���mProvidersByName��mProvidersByClass����Map�ж�������ArticlesProvider�������Ϣ����ˣ������ͨ��AppGlobals.getPackageManager���������PackageManagerService����ӿڣ�Ȼ��ֱ�ͨ������resolveContentProvider��getApplicationInfo�������ֱ��ȡArticlesProviderӦ�ó���������Ϣ���ֱ𱣴���cpi��cpr���������ر����С���Щ��Ϣ�����ڰ�װӦ�ó���Ĺ����б��������ģ�������Բο�AndroidӦ�ó���װ����Դ�������һ�ġ�
����ȥ��������жϵ�ǰҪ��ȡ��Content Provider�Ƿ������ڿͻ������м��أ����鿴һ�����Content Provider��������multiprocess����Ϊtrue����������ڿͻ������м��أ���ֱ�ӷ��������Content Provider����Ϣ�ˣ�
- if (r != null && cpr.canRunHere(r)) {
- // If this is a multiprocess provider, then just return its
- // info and allow the caller to instantiate it. Only do
- // this if the provider is the same user as the caller's
- // process, or can run as root (so can be in any process).
- return cpr;
- }
����������龰�У�Ҫ��ȡ��ArticlesProvider������Ҫ�ڶ����Ľ��������У���ˣ���������ִ�У�
- // This is single process, and our app is now connecting to it.
- // See if we are already in the process of launching this
- // provider.
- final int N = mLaunchingProviders.size();
- int i;
- for (i=0; i<N; i++) {
- if (mLaunchingProviders.get(i) == cpr) {
- break;
- }
- }
ϵͳ���������ڼ��ص�Content Provider��������mLaunchingProviders��Ա�����С��ڼ�����Ӧ��Content Provider֮ǰ������Ҫ�ж�һ�����ǿɷ����ڱ�����Ӧ�ó�����أ�����ǵĻ����Ͳ����ظ������ˡ�����������龰�У�û������Ӧ�ó���Ҳ���ڼ���ArticlesProvider���Content Provider��������ǰִ�У�
- // If the provider is not already being launched, then get it
- // started.
- if (i >= N) {
- final long origId = Binder.clearCallingIdentity();
- ProcessRecord proc = startProcessLocked(cpi.processName,
- cpr.appInfo, false, 0, "content provider",
- new ComponentName(cpi.applicationInfo.packageName,
- cpi.name), false);
- ......
- mLaunchingProviders.add(cpr);
- ......
- }
���������i >= NΪtrue���ͱ���û������Ӧ�ó������ڼ������Content Provider����ˣ���Ҫ����startProcessLocked����������һ���µĽ������������Content Provider��Ӧ�����ˣ�Ȼ���������ڼ��ص���Ϣ���ӵ�mLaunchingProviders��ȥ�������Ƚ��ŷ������������Ȼ�����������½����м���Content Provider�Ĺ��̣���������ִ�У�
- // Make sure the provider is published (the same provider class
- // may be published under multiple names).
- if (firstClass) {
- mProvidersByClass.put(cpi.name, cpr);
- }
- cpr.launchingApp = proc;
- mProvidersByName.put(name, cpr);
��δ�������Content Provider����Ϣ�ֱ𱣴浽mProvidersByName��mProviderByCalss����Map��ȥ���Է��������ѯ��
��Ϊ������Ҫ��ȡ��Content Provider�����µĽ����м��صģ���getContentProviderImpl�����������ϵͳ������ִ�еģ�������Ҫ�ȵ�Ҫ��ȡ��Content Provider�����µĽ����м�����ɺ���ܷ��أ��������漰������ͬ���������ˡ�����ʹ�õ�ͬ�������Dz��ϵ�ȥ������cpr��provider���Ƿ������ˡ���Ҫ��ȡ��Content Provider���µĽ��̼������֮������ͨ��Binder���̼�ͨ�Ż��Ƶ��õ�ϵͳ�����У������cpr������provider������Ϊ�Ѿ����غõ�Content Provider�ӿڣ���ʱ����getContentProviderImpl�Ϳ��Է����ˡ�����Ĵ�����������ȴ�Ҫ��ȡ��Content Provider�����µĽ����м�����ɵģ�
- // Wait for the provider to be published...
- synchronized (cpr) {
- while (cpr.provider == null) {
- ......
- try {
- cpr.wait();
- } catch (InterruptedException ex) {
- }
- }
- }
���������ٷ������½����м���ArticlesProvider���Content Provider�Ĺ��̡�
Step 8. ActivityManagerService.startProcessLocked
Step 9. Process.start
Step 10. ActivityThread.main
Step 11. ActivityThread.attach
Step 12. ActivityManagerService.attachApplication
���岽�DZ���AndroidӦ�ó����������裬������Բο�AndroidӦ�ó�����������Դ�������һ���е�Step 23��Step 27������Androidϵͳ���½����������Զ��������̣�startService����ԭ������һ���е�Step 4��Step 9������Ͳ�����ϸ�����ˡ�
Step 13. ActivityManagerService.attachApplicationLocked
�������������frameworks/base/services/java/com/android/server/am/ActivityManagerService.java�ļ��У�
- public final class ActivityManagerService extends ActivityManagerNative
- implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
- ......
- private final boolean attachApplicationLocked(IApplicationThread thread,
- int pid) {
- // Find the application record that is being attached... either via
- // the pid if we are running in multiple processes, or just pull the
- // next app record if we are emulating process with anonymous threads.
- ProcessRecord app;
- if (pid != MY_PID && pid >= 0) {
- synchronized (mPidsSelfLocked) {
- app = mPidsSelfLocked.get(pid);
- }
- } else if (mStartingProcesses.size() > 0) {
- ......
- } else {
- ......
- }
- ......
- app.thread = thread;
- app.curAdj = app.setAdj = -100;
- app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
- app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
- app.forcingToForeground = null;
- app.foregroundServices = false;
- app.debugging = false;
- ......
- boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info);
- List providers = normalMode ? generateApplicationProvidersLocked(app) : null;
- try {
- ......
- thread.bindApplication(processName, app.instrumentationInfo != null
- ? app.instrumentationInfo : app.info, providers,
- app.instrumentationClass, app.instrumentationProfileFile,
- app.instrumentationArguments, app.instrumentationWatcher, testMode,
- isRestrictedBackupMode || !normalMode,
- mConfiguration, getCommonServicesLocked());
- ......
- } catch (Exception e) {
- ......
- }
- ......
- return true;
- }
- ......
- private final List generateApplicationProvidersLocked(ProcessRecord app) {
- List providers = null;
- try {
- providers = AppGlobals.getPackageManager().
- queryContentProviders(app.processName, app.info.uid,
- STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
- } catch (RemoteException ex) {
- }
- if (providers != null) {
- final int N = providers.size();
- for (int i=0; i<N; i++) {
- ProviderInfo cpi =
- (ProviderInfo)providers.get(i);
- ContentProviderRecord cpr = mProvidersByClass.get(cpi.name);
- if (cpr == null) {
- cpr = new ContentProviderRecord(cpi, app.info);
- mProvidersByClass.put(cpi.name, cpr);
- }
- app.pubProviders.put(cpi.name, cpr);
- app.addPackage(cpi.applicationInfo.packageName);
- ensurePackageDexOpt(cpi.applicationInfo.packageName);
- }
- }
- return providers;
- }
- ......
- }
������������Ǹ��ݴ������Ľ���ID�ҵ���Ӧ�Ľ��̼�¼�飬ע�⣬�������ID��Ӧ�ó���ArticlesProvider��ID��Ȼ���������̼�¼����һЩ����ʼ���Ĺ������ٽ�����ͨ������generateApplicationProvidersLocked�����Ҫ����������м��ص�Content Provider�б�������������龰�У���ֻ��ArticlesProvider���Content Provider�ˡ������ôӲ�����������IApplicationThread����thread��bindApplication������ִ��һЩӦ�ó����ʼ����������AndroidӦ�ó�����������Դ�������һ��������֪������Androidϵͳ�У�ÿһ��Ӧ�ó�����̶�������һ��ActivityThreadʵ���������ActivityThreadʵ�����棬��һ����Ա����mAppThread������һ��Binder��������ΪApplicationThread��ʵ����IApplicationThread�ӿڣ�����ר��������ActivityManagerService�������ͨ�ŵġ���ˣ�����������䣺
- thread.bindApplication(processName, app.instrumentationInfo != null
- ? app.instrumentationInfo : app.info, providers,
- app.instrumentationClass, app.instrumentationProfileFile,
- app.instrumentationArguments, app.instrumentationWatcher, testMode,
- isRestrictedBackupMode || !normalMode,
- mConfiguration, getCommonServicesLocked());
�ͻ���뵽Ӧ�ó���ArticlesProvider�����е�ApplicationThread�����bindApplication������ȥ������������龰���������������������Ҫ�IJ������ǵ���������providers�ˣ���������Ҫ�����Ķ���
Step 14. ApplicationThread.bindApplication
�������������frameworks/base/core/java/android/app/ActivityThread.java�ļ��У�
- public final class ActivityThread {
- ......
- private final class ApplicationThread extends ApplicationThreadNative {
- ......
- public final void bindApplication(String processName,
- ApplicationInfo appInfo, List<ProviderInfo> providers,
- ComponentName instrumentationName, String profileFile,
- Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
- int debugMode, boolean isRestrictedBackupMode, Configuration config,
- Map<String, IBinder> services) {
- if (services != null) {
- // Setup the service cache in the ServiceManager
- ServiceManager.initServiceCache(services);
- }
- AppBindData data = new AppBindData();
- data.processName = processName;
- data.appInfo = appInfo;
- data.providers = providers;
- data.instrumentationName = instrumentationName;
- data.profileFile = profileFile;
- data.instrumentationArgs = instrumentationArgs;
- data.instrumentationWatcher = instrumentationWatcher;
- data.debugMode = debugMode;
- data.restrictedBackupMode = isRestrictedBackupMode;
- data.config = config;
- queueOrSendMessage(H.BIND_APPLICATION, data);
- }
- ......
- }
- ......
- }
�����������ص���Ϣ����װ��һ��AppBindData����Ȼ����һ����Ϣ����ʽ���͵����̵߳���Ϣ������ȥ�ȵȴ������������Ϣ����������ActivityThread���handleBindApplication�����н��д����ġ�
Step 15. ActivityThread.handleBindApplication
�������������frameworks/base/core/java/android/app/ActivityThread.java�ļ��У�
- public final class ActivityThread {
- ......
- private final void handleBindApplication(AppBindData data) {
- ......
- List<ProviderInfo> providers = data.providers;
- if (providers != null) {
- installContentProviders(app, providers);
- ......
- }
- ......
- }
- ......
- }
������������ݱȽ϶࣬���Ǻ����������صIJ��֣�ֻ��ע��Content Provider�йص�����������Ҫ���ǵ���installContentProviders�������ڱ��ذ�װContent Providers��Ϣ��
Step 16. ActivityThread.installContentProviders
�������������frameworks/base/core/java/android/app/ActivityThread.java�ļ��У�
- public final class ActivityThread {
- ......
- private final void installContentProviders(
- Context context, List<ProviderInfo> providers) {
- final ArrayList<IActivityManager.ContentProviderHolder> results =
- new ArrayList<IActivityManager.ContentProviderHolder>();
- Iterator<ProviderInfo> i = providers.iterator();
- while (i.hasNext()) {
- ProviderInfo cpi = i.next();
- StringBuilder buf = new StringBuilder(128);
- buf.append("Pub ");
- buf.append(cpi.authority);
- buf.append(": ");
- buf.append(cpi.name);
- Log.i(TAG, buf.toString());
- IContentProvider cp = installProvider(context, null, cpi, false);
- if (cp != null) {
- IActivityManager.ContentProviderHolder cph =
- new IActivityManager.ContentProviderHolder(cpi);
- cph.provider = cp;
- results.add(cph);
- // Don't ever unload this provider from the process.
- synchronized(mProviderMap) {
- mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
- }
- }
- }
- try {
- ActivityManagerNative.getDefault().publishContentProviders(
- getApplicationThread(), results);
- } catch (RemoteException ex) {
- }
- }
- ......
- }
���������Ҫ�������������飬һ�ǵ���installProvider���ڱ��ذ�װÿһ��Content Proivder����Ϣ������Ϊÿһ��Content Provider����һ��ContentProviderHolder������������ص���Ϣ��ContentProviderHolder������һ��Binder������������Content Provider����Ϣ���ݸ�ActivityManagerService����ġ�����ЩContent Provider�����������Ժ�Ҫ����ActivityManagerService�����publishContentProviders������֪ͨActivityManagerService���������������Ҫ���ص�Content Provider�����Ѿ�������ˣ���ActivityManagerService�����publishContentProviders���������þ�������������ǰ��Step 7�ȴ����̵߳��ˡ�����������installProvider��ʵ�֣�Ȼ��������ActivityManagerService�����publishContentProviders������ʵ�֡�
Step 17. ActivityThread.installProvider
�������������frameworks/base/core/java/android/app/ActivityThread.java�ļ��У�
- public final class ActivityThread {
- ......
- private final IContentProvider installProvider(Context context,
- IContentProvider provider, ProviderInfo info, boolean noisy) {
- ContentProvider localProvider = null;
- if (provider == null) {
- ......
- Context c = null;
- ApplicationInfo ai = info.applicationInfo;
- if (context.getPackageName().equals(ai.packageName)) {
- c = context;
- } else if (mInitialApplication != null &&
- mInitialApplication.getPackageName().equals(ai.packageName)) {
- c = mInitialApplication;
- } else {
- try {
- c = context.createPackageContext(ai.packageName,
- Context.CONTEXT_INCLUDE_CODE);
- } catch (PackageManager.NameNotFoundException e) {
- }
- }
- ......
- try {
- final java.lang.ClassLoader cl = c.getClassLoader();
- localProvider = (ContentProvider)cl.
- loadClass(info.name).newInstance();
- provider = localProvider.getIContentProvider();
- ......
- // XXX Need to create the correct context for this provider.
- localProvider.attachInfo(c, info);
- } catch (java.lang.Exception e) {
- ......
- }
- } else if (localLOGV) {
- ......
- }
- synchronized (mProviderMap) {
- // Cache the pointer for the remote provider.
- String names[] = PATTERN_SEMICOLON.split(info.authority);
- for (int i=0; i<names.length; i++) {
- ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
- localProvider);
- try {
- provider.asBinder().linkToDeath(pr, 0);
- mProviderMap.put(names[i], pr);
- } catch (RemoteException e) {
- return null;
- }
- }
- if (localProvider != null) {
- mLocalProviders.put(provider.asBinder(),
- new ProviderClientRecord(null, provider, localProvider));
- }
- }
- return provider;
- }
- ......
- }
���������������Ҫ������Ӧ�ó�������а���Ӧ��Content Provider����ؽ����ˣ�������������龰�У�����Ҫ��ArticlesProvider���Ӧ�ó����а�ArticlesProvider���Content Provider����ص��ڴ������ˣ�
- final java.lang.ClassLoader cl = c.getClassLoader();
- localProvider = (ContentProvider)cl.
- loadClass(info.name).newInstance();
����ͨ������localProvider��getIContentProvider���������һ��Binder�������Binder���ظ�installContentProviders����֮�ͻᴫ��ActivityManagerService��ȥ����������Ӧ�ó������ͨ��������Binder����������Ӧ��Content Provider����ͨ�ŵ��ˡ������ȿ�һ�����������ʵ�֣�Ȼ���ٻص�installProvider�����м���������
Step 18. ContentProvider.getIContentProvider
�������������frameworks/base/core/java/android/content/ContentProvider.java�ļ��У�
- public abstract class ContentProvider implements ComponentCallbacks {
- ......
- private Transport mTransport = new Transport();
- ......
- class Transport extends ContentProviderNative {
- ......
- }
- public IContentProvider getIContentProvider() {
- return mTransport;
- }
- ......
- }
���������ǿ��Կ�����ContentProvider���Transport��Ĺ�ϵ��������ActivityThread��ApplicationThread�Ĺ�ϵ������Ӧ�ó�����ֱ�ӵ���ContentProvider�ӿ��������������ݣ�����ͨ�����������ڲ�����mTransport����ӵ���ContentProvider�Ľӿڣ���һ����������һƪ�����з�������Content Provider�ӿ�����ȡ��������ʱ���ῴ����
�ص�ǰ���installProvider�����У�����������������ӿ�����ʼ���ոռ��غõ�Content Provider��
- // XXX Need to create the correct context for this provider.
- localProvider.attachInfo(c, info);
ͬ���������Ƚ��뵽ContentProvider���attachInfo����ȥ��������ʵ�֣�Ȼ���ٻص�installProvider��������
Step 19. ContentProvider.attachInfo
�������������frameworks/base/core/java/android/content/ContentProvider.java�ļ��У�
- public abstract class ContentProvider implements ComponentCallbacks {
- ......
- public void attachInfo(Context context, ProviderInfo info) {
- /*
- * Only allow it to be set once, so after the content service gives
- * this to us clients can't change it.
- */
- if (mContext == null) {
- mContext = context;
- mMyUid = Process.myUid();
- if (info != null) {
- setReadPermission(info.readPermission);
- setWritePermission(info.writePermission);
- setPathPermissions(info.pathPermissions);
- mExported = info.exported;
- }
- ContentProvider.this.onCreate();
- }
- }
- ......
- }
��������ܼ���Ҫ���Ǹ������Content Provider����Ϣinfo��������Ӧ�Ķ�дȨ�ޣ�Ȼ��������������onCreate������������ִ��һЩ��ʼ���Ĺ���������������龰�У�����������ArticlesProvideӦ�ó����е�ArticlesProvider���ˡ�
Step 20. ArticlesProvider.onCreate
�������������ǰ��һƪ����AndroidӦ�ó������Content ProviderӦ��ʵ�����ܵ�Ӧ�ó���ArtilcesProviderԴ���빤��Ŀ¼�£����ļ�Ϊpackages/experimental/ArticlesProvider/src/shy/luo/providers/articles/ArticlesProvider.java�У�
- public class ArticlesProvider extends ContentProvider {
- ......
- @Override
- public boolean onCreate() {
- Context context = getContext();
- resolver = context.getContentResolver();
- dbHelper = new DBHelper(context, DB_NAME, null, DB_VERSION);
- return true;
- }
- ......
- }
���������Ҫִ��һЩ�Ĺ��������磬���Ӧ�ó��������ĵ�ContentResolver�ӿںʹ������ݿ����������������Բο�ǰ��һƪ����AndroidӦ�ó������Content ProviderӦ��ʵ����
�ص�ǰ��Step 17�е�installProvider�����У������������ǰ���Щ�ڱ����м��ص�Content Provider��Ϣ���������ˣ��Է�������ѯ��ʹ�ã�
- synchronized (mProviderMap) {
- // Cache the pointer for the remote provider.
- String names[] = PATTERN_SEMICOLON.split(info.authority);
- for (int i=0; i<names.length; i++) {
- ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
- localProvider);
- try {
- provider.asBinder().linkToDeath(pr, 0);
- mProviderMap.put(names[i], pr);
- } catch (RemoteException e) {
- return null;
- }
- }
- if (localProvider != null) {
- mLocalProviders.put(provider.asBinder(),
- new ProviderClientRecord(null, provider, localProvider));
- }
- }
��ActivityMangerService���ƣ���ActivityThread�У���Content Provider��authorityΪ��ֵ�������Content Provider����Ϣ������mProviderMap��Ա�����У���Ϊһ��Content Provider���Զ�Ӧ���authority�����������һ��forѭ����������ͬʱ�������Content Provider��Ӧ��Binder����provider����ֵ�������Content Provider����Ϣ������mLocalProviders��Ա�����У���������һ���ڱ��ؼ��ص�Content Provider��
����installProviderִ������Ժ��ص�Step 16�е�instalContentProviders�����У�ִ��������䣺
- try {
- ActivityManagerNative.getDefault().publishContentProviders(
- getApplicationThread(), results);
- } catch (RemoteException ex) {
- }
ǰ���Ѿ��ᵽ������������õ����þ���֪ͨActivityMangerService����Ҫ����������м��ص�Content Provider�Ѿ����������ˣ�����results�Ͱ�������Щ�Ѿ����غõ�Content Provider�ӿڡ�
Step 21. ActivityMangerService.publishContentProviders
�������������frameworks/base/services/java/com/android/server/am/ActivityManagerService.java�ļ��У�
- public final class ActivityManagerService extends ActivityManagerNative
- implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
- ......
- public final void publishContentProviders(IApplicationThread caller,
- List<ContentProviderHolder> providers) {
- ......
- synchronized(this) {
- final ProcessRecord r = getRecordForAppLocked(caller);
- ......
- final int N = providers.size();
- for (int i=0; i<N; i++) {
- ContentProviderHolder src = providers.get(i);
- if (src == null || src.info == null || src.provider == null) {
- continue;
- }
- ContentProviderRecord dst = r.pubProviders.get(src.info.name);
- if (dst != null) {
- mProvidersByClass.put(dst.info.name, dst);
- String names[] = dst.info.authority.split(";");
- for (int j = 0; j < names.length; j++) {
- mProvidersByName.put(names[j], dst);
- }
- int NL = mLaunchingProviders.size();
- int j;
- for (j=0; j<NL; j++) {
- if (mLaunchingProviders.get(j) == dst) {
- mLaunchingProviders.remove(j);
- j--;
- NL--;
- }
- }
- synchronized (dst) {
- dst.provider = src.provider;
- dst.app = r;
- dst.notifyAll();
- }
- ......
- }
- }
- }
- }
- ......
- }
����������龰�У�ֻ��һ��Content Provider����ˣ������N�ȴ�1�����м��forѭ�����棬����Ҫ�������������䣺
- ContentProviderRecord dst = r.pubProviders.get(src.info.name);
������õ���ContentProviderRecord����dst��������ǰ��Step 7�д�����ContentProviderRecord����cpr�ˡ���forѭ���У������ǰ����Content Provider��Ϣ�������mProvidersByClass��mProvidersByName�У�
- mProvidersByClass.put(dst.info.name, dst);
- String names[] = dst.info.authority.split(";");
- for (int j = 0; j < names.length; j++) {
- mProvidersByName.put(names[j], dst);
- }
ǰ���Ѿ�˵����������Map�У�һ����������Ϊ��ֵ����Content Provider��Ϣ��һ������authorityΪ��ֵ����Content Provider��Ϣ��
��Ϊ���Content Provider�Ѿ����غ��ˣ���ˣ�������mLaunchingProviders�б���ɾ����
- int NL = mLaunchingProviders.size();
- int j;
- for (j=0; j<NL; j++) {
- if (mLaunchingProviders.get(j) == dst) {
- mLaunchingProviders.remove(j);
- j--;
- NL--;
- }
- }
����������ContentProviderRecord����dst��provider��Ϊ�Ӳ�����������Content ProviderԶ�̽ӿڣ�
- synchronized (dst) {
- dst.provider = src.provider;
- dst.app = r;
- dst.notifyAll();
- }
ִ����dst.notiryAll������Step 7�еȴ�Ҫ��ȡ��Content Provider�ӿڼ�����ϵ��߳̾ͱ������ˡ�����֮������鱾��ContentProviderRecord����cpr��provider��Ϊnull�����Ǿͷ����ˡ������շ��ص�Step 5�е�ActivityThread���getProvider�����У���������ִ�У�
- IContentProvider prov = installProvider(context, holder.provider,
- holder.info, true);
ע�⣬��������ArticleӦ�ó����н�����ִ��installProvider�����ģ���ǰ���Step 17��installProvider��������ArticlesProviderӦ�ó��������ִ�еġ�
Step 22. ActivityThread.installProvider
�������������frameworks/base/core/java/android/app/ActivityThread.java�ļ��У�
- public final class ActivityThread {
- ......
- private final IContentProvider installProvider(Context context,
- IContentProvider provider, ProviderInfo info, boolean noisy) {
- ......
- if (provider == null) {
- ......
- } else if (localLOGV) {
- ......
- }
- synchronized (mProviderMap) {
- // Cache the pointer for the remote provider.
- String names[] = PATTERN_SEMICOLON.split(info.authority);
- for (int i=0; i<names.length; i++) {
- ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
- localProvider);
- try {
- provider.asBinder().linkToDeath(pr, 0);
- mProviderMap.put(names[i], pr);
- } catch (RemoteException e) {
- return null;
- }
- }
- ......
- }
- return provider;
- }
- ......
- }
ͬ����ִ��installProvider��������Step 17��ͬ�����ﴫ�����IJ���provider�Dz�Ϊnull�ģ���ˣ�������Ҫִ���ڱ��ؼ���Content Provider�Ĺ�����ֻ��Ҫ�Ѵ�ActivityMangerService�л�õ�Content Provider�ӿڱ����ڳ�Ա����mProviderMap�оͿ����ˡ�
��������ȡ��"shy.luo.providers.artilces"���uri��Ӧ��Content Provider��shy.luo.providers.articles.ArticlesProvider��������ˣ���ͬʱҲ������Content Provider���������̡�������Ӧ�ó����������Content Provider�Ľӿ�֮�Ϳ��Է���������Ĺ��������ˡ�������һƪ�����У����ǽ��ص����AndroidӦ�ó������Content Provider�ڲ�ͬ�����д������ݵĹ��̣���Content Provider�ڲ�ͬӦ�ó����й������ݵ�ԭ���������ע��
����ת�ԣ�http://blog.csdn.net/luoshengyang/article/details/6963418
- �������
- Android����������ndk-stack���ndk�����ж�̬��.so���������� (43�����)
- Android��������θı�RadioButton����ͼƬ�����ֵ����λ�� (179�����)
- android��������֮ADT��װ��ж�أ����� (13�����)
- Android����֮ListViewҳüҳ��Ч��VS android�������� (32�����)
- Android Ӧ�ó���֮�����ݹ�����ContentProvider (6�����)