[NFC]NFC启动流程1

       前面介绍过NFC相关的Spec后,从本章节开始,将进入Android AOSP NFC Frameworks部分的学习。

 

      代码主要的路径存放在:

      Android5.0\packages\apps\Nfc 包含以下目录:

             Assets:含start.png

             etc:nfcee_access实例

             nci:nci规范中的接口和驱动

             nxp:nxp芯片对应的接口和驱动

             res:app用到的图片,字串资源等

             src:主要代码流程

             tests:Google提供的部分测试程序

      Android5.0\packages\apps\Settings\src\com\android\settings\nfc是Setting中关于NFC的代码。

 

      NFC Application的启动不同于Wi-Fi及Wi-Fi P2P等service。

 

      NFC Application对应的AndroidManifest.xml文件中含有关键信息如下:

    <applicationandroid:name=".NfcApplication"
                android:icon="@drawable/icon"
                android:label="@string/app_name"
                 android:theme="@android:style/Theme.Material.Light"
                android:persistent="true"
                android:backupAgent="com.android.nfc.NfcBackupAgent"
                android:killAfterRestore="false"
    >

      

      此处就涉及到android application启动的机制。此处不详细的分析,列举对应的代码,方便大家学习:

     Android5.0\frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.java

 public void systemReady(final RunnablegoingCallback) {
          ....
          if (mFactoryTest !=FactoryTest.FACTORY_TEST_LOW_LEVEL) {
                try {
                    List apps =AppGlobals.getPackageManager().
                       getPersistentApplications(STOCK_PM_FLAGS);
                    if (apps != null) {
                        int N = apps.size();
                        int i;
                        for (i=0; i<N; i++){
                            ApplicationInfoinfo
                                =(ApplicationInfo)apps.get(i);
                            if (info != null &&
                                   !info.packageName.equals("android")) {
                               addAppLocked(info, false, null /* ABI override */);
                            }
                        }
                    }
                } catch (RemoteException ex) {
                    // pm is in same process,this will never happen.
                }
         }
          ...
}

 

      继续分析addAppLocked()

final ProcessRecordaddAppLocked(ApplicationInfo info, boolean isolated,
            String abiOverride) {
          ...
        if (app.thread == null &&mPersistentStartingProcesses.indexOf(app) < 0) {
           mPersistentStartingProcesses.add(app);
            startProcessLocked(app, "addedapplication", app.processName, abiOverride,
                    null /* entryPoint */, null /*entryPointArgs */);
        }
          ...
}

 

      最终会call到NfcApplication.java中的NfcApplication()构造函数,其中的onCreate()会被调用到。   

@Override
    public void onCreate() {
        super.onCreate();
        boolean isMainProcess = false;
        // We start a service in a separateprocess to do
        // handover transfer. We don't want toinstantiate an NfcService
        // object in those cases, hence checkthe name of the process
        // to determine whether we're the mainNFC service, or the
        // handover process
        ActivityManager am =(ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
        List processes =am.getRunningAppProcesses();
        Iterator i = processes.iterator();
        while (i.hasNext()) {
            //检查当前运行的App名字是否为"com.android.nfc",如果名字相同,则表示当前启动的程序是主程序
            RunningAppProcessInfo appInfo =(RunningAppProcessInfo)(i.next());
            if (appInfo.pid == Process.myPid()){
               isMainProcess =  (NFC_PROCESS.equals(appInfo.processName));
                break;
            }
        }
        if (UserHandle.myUserId() == 0&& isMainProcess) {</span>
            //启动NfcService
           mNfcService = new NfcService(this);
            HardwareRenderer.enableForegroundTrimming();
        }
    }




      当调用了new NfcService()之后,后面会依次创建各个Service。

 

     在进行后续分析前,简单的列举一下NFC的类图,方便后续的理解:

     上层APP主要透过调用android.nfc.tech及android.nfc的接口来实现期望的功能;而android.nfc.tech和android.nfc透过AIDL的方式调用到NfcService中的接口。Framework中NfcService透过JNI与底层NFC Driver进行沟通,实现发送命令和接收event功能。

 

 

 

 

        在进入NFCService.java前,需要对整个系统的架构有个初步的了解。

 

        DeviceHost.java定义了目前几乎NFC需要的全部interface和API。不同的厂家依据DeviceHost.java提供的interface,实现对应的内容,就可以和上层app进行沟通了。

 

 

 

 

 

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章