Skip to content

Commit

Permalink
Added more handlers and added cordova-android 7 support
Browse files Browse the repository at this point in the history
  • Loading branch information
ReallySmallSoftware committed Mar 8, 2018
1 parent ecb936c commit d18babf
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 22 deletions.
4 changes: 4 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ AndroidManifest.xml is automatically updated to use the new MainActivity.
Based on cordova-android-fragments (https://github.com/rajivnarayana/CordovaFragments)

# History
## 0.0.4
- Experimental support for cordova-android 7
- Increased dependency versions

## 0.0.3
- Added missing method to activity to handle permission request results

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"name": "cordova-plugin-android-fragmentactivity",
"version": "0.0.3",
"version": "0.0.4",
"description": "An android plugin that provides a replacement activity to the default activity to start a cordova application with MainActivity as a Fragment Activity. Useful when you want to add native views on top of cordova webview.",
"cordova": {
"id": "cordova-plugin-android-fragmentactivity",
"platforms": [
"android"
]
},
"devDependencies": {
"devDependencies": {},
"dependencies": {
"lodash": "^4.17.5",
"xml2js": "^0.4.17"
},
"keywords": [
"cordova",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-android-fragmentactivity" version="0.0.3">
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-android-fragmentactivity" version="0.0.4">

<name>Cordova Android FragmentActivity Plugin</name>
<description>An android plugin that provides a replacement activity to the default activity to start a cordova application with MainActivity as a Fragment Activity. Useful when you want to add native views on top of cordova webview.</description>
Expand Down
42 changes: 24 additions & 18 deletions scripts/android/afterPluginInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,43 @@
const fs = require('fs');
const _ = require('lodash');
const xml2js = require('xml2js');
const utilities = require("../lib/utilities");

String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};

module.exports = function(context) {
const parseString = xml2js.parseString;
const builder = new xml2js.Builder();
const manifestPath = context.opts.projectRoot + '/platforms/android/AndroidManifest.xml';
const androidManifest = fs.readFileSync(manifestPath).toString();

const activityPath = context.opts.projectRoot + '/plugins/cordova-plugin-android-fragmentactivity/src/android/MainActivity.java';
const activity = fs.readFileSync(activityPath).toString();
var androidManifestPath = utilities.getAndroidManifestPath(context);

let manifestRoot, packageName, newActivity, newActivityPath;
if (androidManifestPath !== null) {
const parseString = xml2js.parseString;
const builder = new xml2js.Builder();
const manifestPath = androidManifestPath + '/AndroidManifest.xml';
const androidManifest = fs.readFileSync(manifestPath).toString();

if (androidManifest) {
parseString(androidManifest, (err, manifest) => {
if (err) return console.error(err);
const activityPath = context.opts.projectRoot + '/plugins/cordova-plugin-android-fragmentactivity/src/android/MainActivity.java';
const activity = fs.readFileSync(activityPath).toString();

manifestRoot = manifest['manifest'];
packageName = manifestRoot['$']['package'];
let manifestRoot, packageName, newActivity, newActivityPath;

newActivity = activity.replace('${mypackage}', packageName);
newActivityPath = 'platforms/android/src/' + packageName.replaceAll("\\.", "/") + '/MainActivity.java';
if (androidManifest) {
parseString(androidManifest, (err, manifest) => {
if (err) return console.error(err);

console.log(newActivityPath);
manifestRoot = manifest['manifest'];
packageName = manifestRoot['$']['package'];

fs.writeFileSync(newActivityPath, newActivity);
console.log("New MainActivity generated.");
});
newActivity = activity.replace('${mypackage}', packageName);
newActivityPath = utilities.getAndroidSourcePath(context) + "/" + packageName.replaceAll("\\.", "/") + '/MainActivity.java';

console.log(newActivityPath);

fs.writeFileSync(newActivityPath, newActivity);
console.log("New MainActivity generated.");
});
}
}
};
63 changes: 63 additions & 0 deletions scripts/lib/utilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env node

'use strict';

const fs = require('fs');

module.exports = {

getAndroidResPath: function(context) {

var platforms = context.opts.cordova.platforms;

if (platforms.indexOf("android") === -1) {
return null;
}

var androidPath = context.opts.projectRoot + '/platforms/android';

if (!fs.existsSync(androidPath)) {
androidPath = context.opts.projectRoot + '/platforms/android/app/src/main';

if (!fs.existsSync(androidPath)) {
console.log("Unable to detect type of cordova-android application structure");
throw new Error("Unable to detect type of cordova-android application structure");
} else {
console.log("Detected cordova-android 7 application structure");
}
} else {
console.log("Detected pre cordova-android 7 application structure");
}

return androidPath;
},

getAndroidManifestPath: function(context) {
return this.getAndroidResPath(context);
},

getAndroidSourcePath: function(context) {
var platforms = context.opts.cordova.platforms;

if (platforms.indexOf("android") === -1) {
return null;
}

var androidPath = context.opts.projectRoot + '/platforms/android/src';

if (!fs.existsSync(androidPath)) {
androidPath = context.opts.projectRoot + '/platforms/android/app/src/main/java';

if (!fs.existsSync(androidPath)) {
console.log("Unable to detect type of cordova-android application structure");
throw new Error("Unable to detect type of cordova-android application structure");
} else {
console.log("Detected cordova-android 7 application structure");
}
} else {
console.log("Detected pre cordova-android 7 application structure");
}

return androidPath;
}
};
47 changes: 46 additions & 1 deletion src/android/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one
under the License.
*/

package ${mypackage};
package uk.co.reallysmall.leaveyouregosatthedoor;

/** extends CordovaActivity */

Expand Down Expand Up @@ -45,6 +45,51 @@ public void onCreate(Bundle savedInstanceState)
ft.commit();
}

/**
* Called when the system is about to start resuming a previous activity.
*/
@Override
protected void onPause() {
super.onPause();
currentFragment.onPause();
}

/**
* Called when the activity will start interacting with the user.
*/
@Override
protected void onResume() {
super.onResume();
currentFragment.onResume();
}

/**
* Called when the activity is no longer visible to the user.
*/
@Override
protected void onStop() {
super.onStop();
currentFragment.onStop();
}

/**
* Called when the activity is becoming visible to the user.
*/
@Override
protected void onStart() {
super.onStart();
currentFragment.onStart();
}

/**
* The final call you receive before your activity is destroyed.
*/
@Override
public void onDestroy() {
super.onDestroy();
currentFragment.onDestroy();
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[],
int[] grantResults) {
Expand Down

0 comments on commit d18babf

Please sign in to comment.