Phonegap/cordova is a professional mobile application development framework, a comprehensive web App development framework that provides web-based access to end-device API capabilities. This is a boon for developers who use web apps, which avoids some of the features of native development. Cordova is just a native shell, the app's kernel is a complete webapp, the need to invoke the native functionality will be implemented in the form of native plug-ins, to expose the way the JS interface calls.
The Cordova Android project is the Java code implementation of the native part of Cordova Android, which provides JavaScript communication interfaces for Android native code and upper-level Web pages. This series of articles focuses on the implementation of Cordova Android framework code. Through in-depth analysis of the implementation of Cordova Android source code, we in the development of Hybridapp can be more leisurely, know Cordova plug-in principle, the development of Cordova plug-in integration of their own function modules, Avoid some of the holes in JS and WebView when implementing Web applications, and develop high-quality hybrid applications.
Project source code can be found in https://git-wip-us.apache.org/repos/asf, which shows the git address of all Cordova sub-projects. After you create the Cordova app and add the Android platform via Cordova Platform Add Android command, in the app's platforms/android/cordovalib/src/org/apache/ The Cordova Android framework code can be found below the Cordova directory.
cordova3.5 version Android Core Framework a total of 27 Java files, the amount of code is not too large. From the cordova3.0 version, all the device capabilities API from the Cordova Core framework to separate out, into a plug-in, each platform for native implementation, such as access to device information devices plug-in, access network status of the Web information plug-in, At present, the official website contains a total of 250 plugins. Here is Cordova Android's overall UML class diagram, from which we see the relationship between the core framework and plug-ins, plug-ins need to implement the Cordovaplugin interface.
Cordova Frame class Diagram
- Cordovainterface Interface Analysis
Cordovainterface is the underlying interface for Cordova applications, and cordovaactivity needs to implement this interface. Used to isolate Cordova plug-in development and isolate plug-ins from direct reliance on the Cordova Core library.
The main methods are startactivityforresult,setactivityresultcallback,getactivity,onmessage,getthreadpool these interface methods. The concrete implementation is in the cordovaactivity.
/** * The Activity interface that's implemented by Cordovaactivity. * It is used to isolate plugin development, and remove dependency on entire Cordova library. */public interface Cordovainterface {/** * Launch an activity for which you would as a result when it finished. When this activity exits, * your Onactivityresult () method would be called. * * @param command the Command object * @param intent the intent to start * @param requestcode the Request code that's passed to callback to identify the activity */abstract public void Startactivityforresult (Cor Dovaplugin command, Intent Intent, int requestcode); /** * Set The plugin to is called when a sub-activity exits. * * @param plugin the plugin on which Onactivityresult are to be called */abstract public void setactivity Resultcallback (Cordovaplugin plugin); /** * Get the Android activity. * * @return The Activity * * Public abstract ActiviTy Getactivity (); /** * Called when a message was sent to plugin. * * @param ID the message ID * @param data the message data * @return Object or null */public Object onMessage (String ID, object data); /** * Returns A shared thread pool that can is used for background tasks. */Public Executorservice getthreadpool ();}
- Cordovaactivity Core class Analysis
Cordovaactivity is the entry class for the Cordova application, and the activity that the user uses to load the HTML page needs to inherit the activity. Cordovaactivity reads the configuration in the Cordova configuration file res/xml/config.xml.
Cordovaactivity inherits the Android Activity and implements the Cordovainterface interface.
The more important member variables are Cordovawebview appview,cordovawebviewclient webviewclient, with Executors.newcachedthreadpool () Initializes a thread pool ThreadPool, creates the return plugin Activityresultcallback when the activity returns, and some variables such as the splash screen splashscreen. Cordovaactivity inherits activity, so its life cycle and activity, we can start looking at the code in the order of activity life cycle.
First, look at the OnCreate method, first call the Config.init (this) method, To read the configuration of the CONFIG. css file initialization, see Config.java Source code can know, the method mainly initializes the URL whitelist, background color, whether full screen, load page timeout time (default 20s), Start screen delay (default 3s) and so on. Then is read intent extra medium Some parameters, set whether to display the title, whether full screen and so on. The next step is to read the screen width and height and create a linearlayoutsoftkeyboarddetect. Linearlayoutsoftkeyboarddetect This class is used to detect whether the soft keyboard pops up, mainly to rewrite the Onmeasure method, when the soft keyboard pops up, the height changes when sending App.appView.sendJavascript (" Cordova.firedocumentevent (' Hidekeyboard '); ") Event. Our own Cordova application calls the following method in turn when the method is re-oncreate.
Super.oncreate (savedinstancestate); Super.init (); Set by <content src= ' index.html '/> in config . Super.loadurl (Config.getstarturl ());
UML Diagram of Cordovaactivity class
The Onresume method first re-invokes the Config.init (this) method, then determines whether it is the first start, and if so, returns directly, Otherwise, the Appview.handleresume method is called, which triggers the JavaScript event cordova.firedocumentevent (' Resume '), notifies PlugInManager, and finally counts.
OnPause method is relatively simple, when Appview is not empty, call the Appview.handlepause method, and then remove the splash screen splashscreen.
The implementation of the Ondestroy,onnewintent,postmessage,sendjavascript,showwebpage method is similar to the OnPause method, which is called the Appview method.
Method AddService (String servicetype, String className) is used to add the service, this method has been degraded and should be added later in the Res/xml/plugins.xml file.
The Startactivityforresult method first assigns a value to the callback object Activityresultcallback, sets the activityresultkeeprunning, Finally, the Startactivityforresult method of the activity is called.
The Onactivityresult method is called when the activated activity returns results. First, the activity's Onactivityresult method is called to get the data, then call Muploadmessage.onreceivevalue (Result), Finally, the Cordova plugin is notified by calling Activityresultcallback's Onactivityresult method.
The Onreceivederror method is responsible for displaying a pre-defined error page or error message when an unrecoverable error occurs. Unrecoverable errors are those such as the unavailability of primary resource files. If the error page is configured, the UI thread stops the progress bar, calls the Appview.showwebpage method to display the error page, or calls the DisplayError method to eject the error description dialog box.
Oncreateoptionsmenu,onprepareoptionsmenu,onoptionsitemselected These methods are similar to the first call PostMessage send the corresponding event, The corresponding method of the parent activity is then called.
Both the onkeyup and OnKeyDown methods call the related methods of Appview first, and then invoke the related methods of the activity.
first article in this series this is it, tomorrow next article Analysis Cordovaresourceapi,cordovawebview , Cordovawebviewclient, and several other classes.