diff --git a/README.md b/README.md new file mode 100644 index 0000000..c8d0a57 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +Cocoon Plugin for Android Application +====================================== + +This plugin adds support for application level life cycle events in Android it doesn't really adds any functionality by itself and is only intended to work as a dependency for other plugins like the multidex plugin or the Parse notifications plugin. \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..bc26c35 --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "cocoon-plugin-application-android", + "version": "1.0.2", + "description": "Android Application plugin", + "cordova": { + "id": "cocoon-plugin-application-android", + "platforms": [ + "android" + ] + }, + "repository": { + "type": "git", + "url": "https://github.com/ludei/cocoon-plugin-application-android" + }, + "keywords": [ + "cordova", + "application", + "android", + "cocoon", + "atomic", + "plugins", + "ecosystem:cordova", + "cordova-android" + ], + "author": "ludei", + "license": "MPL 2.0", + "bugs": { + "url": "https://github.com/ludei/cocoon-plugin-application-android/issues" + }, + "homepage": "https://github.com/ludei/cocoon-plugin-application-android#readme" +} diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..cdeea8b --- /dev/null +++ b/plugin.xml @@ -0,0 +1,26 @@ + + + + + Cocoon Android Application plugin + Cocoon plugin to add support for Android Application life cycle methods. + Copyright Ludei + cordova, ludei, cocoon, application + + + + + + + + + + + + + + + diff --git a/scripts/after_install.js b/scripts/after_install.js new file mode 100644 index 0000000..56a3402 --- /dev/null +++ b/scripts/after_install.js @@ -0,0 +1,22 @@ +#!/usr/bin/env node + +var fs = require('fs'); +var path = require('path'); + +module.exports = function(context) { + if (context.opts.cordova.platforms.indexOf('android') <= -1) + return; + + var manifest_xml = path.join(context.opts.projectRoot, 'platforms', 'android','AndroidManifest.xml'); + var et = context.requireCordovaModule('elementtree'); + + var data = fs.readFileSync(manifest_xml).toString(); + var etree = et.parse(data); + + // Add Multidex application + var applications = etree.findall('./application'); + applications[0].attrib['android:name'] = "com.ludei.CocoonApp"; + + data = etree.write({'indent': 4}); + fs.writeFileSync(manifest_xml, data); +} \ No newline at end of file diff --git a/src/CocoonApp.java b/src/CocoonApp.java new file mode 100644 index 0000000..2152309 --- /dev/null +++ b/src/CocoonApp.java @@ -0,0 +1,102 @@ +package com.ludei; + +import android.app.Application; +import android.content.Context; + +import org.apache.cordova.ConfigXmlParser; +import org.apache.cordova.PluginEntry; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; + +/** + * Created by imanolmartin on 22/12/15. + */ +public class CocoonApp extends Application { + + + @Override + public void onCreate() { + super.onCreate(); + + ConfigXmlParser parser = new ConfigXmlParser(); + parser.parse(this); + List pluginEntries = parser.getPluginEntries(); + for (PluginEntry entry : pluginEntries) { + try { + Class clazz = Class.forName(entry.pluginClass); + Method method = clazz.getMethod("onApplicationCreate", Application.class); + method.invoke(null, this); + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + + } catch (InvocationTargetException e) { + e.printStackTrace(); + + } catch (NoSuchMethodException e) { + //e.printStackTrace(); + + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + + @Override + protected void attachBaseContext(Context base) { + super.attachBaseContext(base); + + ConfigXmlParser parser = new ConfigXmlParser(); + parser.parse(this); + List pluginEntries = parser.getPluginEntries(); + for (PluginEntry entry : pluginEntries) { + try { + Class clazz = Class.forName(entry.pluginClass); + Method method = clazz.getMethod("onApplicationAttachBaseContext", Application.class); + method.invoke(null, this); + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + + } catch (InvocationTargetException e) { + e.printStackTrace(); + + } catch (NoSuchMethodException e) { + //e.printStackTrace(); + + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + + @Override + public void onTerminate() { + super.onTerminate(); + + ConfigXmlParser parser = new ConfigXmlParser(); + parser.parse(this); + List pluginEntries = parser.getPluginEntries(); + for (PluginEntry entry : pluginEntries) { + try { + Class clazz = Class.forName(entry.pluginClass); + Method method = clazz.getMethod("onApplicationTerminate", Application.class); + method.invoke(null, this); + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + + } catch (InvocationTargetException e) { + e.printStackTrace(); + + } catch (NoSuchMethodException e) { + //e.printStackTrace(); + + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } +} \ No newline at end of file