Skip to content

Commit d18babf

Browse files
Added more handlers and added cordova-android 7 support
1 parent ecb936c commit d18babf

File tree

6 files changed

+143
-22
lines changed

6 files changed

+143
-22
lines changed

README.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ AndroidManifest.xml is automatically updated to use the new MainActivity.
2121
Based on cordova-android-fragments (https://github.com/rajivnarayana/CordovaFragments)
2222

2323
# History
24+
## 0.0.4
25+
- Experimental support for cordova-android 7
26+
- Increased dependency versions
27+
2428
## 0.0.3
2529
- Added missing method to activity to handle permission request results
2630

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
{
22
"name": "cordova-plugin-android-fragmentactivity",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"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.",
55
"cordova": {
66
"id": "cordova-plugin-android-fragmentactivity",
77
"platforms": [
88
"android"
99
]
1010
},
11-
"devDependencies": {
11+
"devDependencies": {},
12+
"dependencies": {
13+
"lodash": "^4.17.5",
14+
"xml2js": "^0.4.17"
1215
},
1316
"keywords": [
1417
"cordova",

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-android-fragmentactivity" version="0.0.3">
2+
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-android-fragmentactivity" version="0.0.4">
33

44
<name>Cordova Android FragmentActivity Plugin</name>
55
<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>

scripts/android/afterPluginInstall.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,43 @@
55
const fs = require('fs');
66
const _ = require('lodash');
77
const xml2js = require('xml2js');
8+
const utilities = require("../lib/utilities");
89

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

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

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

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

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

29-
manifestRoot = manifest['manifest'];
30-
packageName = manifestRoot['$']['package'];
28+
let manifestRoot, packageName, newActivity, newActivityPath;
3129

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

35-
console.log(newActivityPath);
34+
manifestRoot = manifest['manifest'];
35+
packageName = manifestRoot['$']['package'];
3636

37-
fs.writeFileSync(newActivityPath, newActivity);
38-
console.log("New MainActivity generated.");
39-
});
37+
newActivity = activity.replace('${mypackage}', packageName);
38+
newActivityPath = utilities.getAndroidSourcePath(context) + "/" + packageName.replaceAll("\\.", "/") + '/MainActivity.java';
39+
40+
console.log(newActivityPath);
41+
42+
fs.writeFileSync(newActivityPath, newActivity);
43+
console.log("New MainActivity generated.");
44+
});
45+
}
4046
}
4147
};

scripts/lib/utilities.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const fs = require('fs');
6+
7+
module.exports = {
8+
9+
getAndroidResPath: function(context) {
10+
11+
var platforms = context.opts.cordova.platforms;
12+
13+
if (platforms.indexOf("android") === -1) {
14+
return null;
15+
}
16+
17+
var androidPath = context.opts.projectRoot + '/platforms/android';
18+
19+
if (!fs.existsSync(androidPath)) {
20+
androidPath = context.opts.projectRoot + '/platforms/android/app/src/main';
21+
22+
if (!fs.existsSync(androidPath)) {
23+
console.log("Unable to detect type of cordova-android application structure");
24+
throw new Error("Unable to detect type of cordova-android application structure");
25+
} else {
26+
console.log("Detected cordova-android 7 application structure");
27+
}
28+
} else {
29+
console.log("Detected pre cordova-android 7 application structure");
30+
}
31+
32+
return androidPath;
33+
},
34+
35+
getAndroidManifestPath: function(context) {
36+
return this.getAndroidResPath(context);
37+
},
38+
39+
getAndroidSourcePath: function(context) {
40+
var platforms = context.opts.cordova.platforms;
41+
42+
if (platforms.indexOf("android") === -1) {
43+
return null;
44+
}
45+
46+
var androidPath = context.opts.projectRoot + '/platforms/android/src';
47+
48+
if (!fs.existsSync(androidPath)) {
49+
androidPath = context.opts.projectRoot + '/platforms/android/app/src/main/java';
50+
51+
if (!fs.existsSync(androidPath)) {
52+
console.log("Unable to detect type of cordova-android application structure");
53+
throw new Error("Unable to detect type of cordova-android application structure");
54+
} else {
55+
console.log("Detected cordova-android 7 application structure");
56+
}
57+
} else {
58+
console.log("Detected pre cordova-android 7 application structure");
59+
}
60+
61+
return androidPath;
62+
}
63+
};

src/android/MainActivity.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one
1717
under the License.
1818
*/
1919

20-
package ${mypackage};
20+
package uk.co.reallysmall.leaveyouregosatthedoor;
2121

2222
/** extends CordovaActivity */
2323

@@ -45,6 +45,51 @@ public void onCreate(Bundle savedInstanceState)
4545
ft.commit();
4646
}
4747

48+
/**
49+
* Called when the system is about to start resuming a previous activity.
50+
*/
51+
@Override
52+
protected void onPause() {
53+
super.onPause();
54+
currentFragment.onPause();
55+
}
56+
57+
/**
58+
* Called when the activity will start interacting with the user.
59+
*/
60+
@Override
61+
protected void onResume() {
62+
super.onResume();
63+
currentFragment.onResume();
64+
}
65+
66+
/**
67+
* Called when the activity is no longer visible to the user.
68+
*/
69+
@Override
70+
protected void onStop() {
71+
super.onStop();
72+
currentFragment.onStop();
73+
}
74+
75+
/**
76+
* Called when the activity is becoming visible to the user.
77+
*/
78+
@Override
79+
protected void onStart() {
80+
super.onStart();
81+
currentFragment.onStart();
82+
}
83+
84+
/**
85+
* The final call you receive before your activity is destroyed.
86+
*/
87+
@Override
88+
public void onDestroy() {
89+
super.onDestroy();
90+
currentFragment.onDestroy();
91+
}
92+
4893
@Override
4994
public void onRequestPermissionsResult(int requestCode, String permissions[],
5095
int[] grantResults) {

0 commit comments

Comments
 (0)