Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix on run clean command and after plugin install into cordova-android 7 #137

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 78 additions & 12 deletions bin/lib/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,42 @@ module.exports = function (context) {
cordova_lib = cordova.cordova_lib,
ConfigParser = cordova_lib.configparser,
cordova_util = req('cordova-lib/src/cordova/util'),
ofs = req("fs"),
fs = require("./filesystem")(Q, req('fs'), path),
platforms = {};

// fs, path, ET, cordova_util, ConfigParser

// Check the currente platform version and map the path of resources
function getResPath(){
return cordova_util
.getInstalledPlatformsWithVersions(context.opts.projectRoot)
.then(function(platformMap){
if ( typeof platformMap == 'object' && platformMap.android ){
var majorVersion = parseInt( platformMap.android[0] );
if ( majorVersion != NaN && majorVersion >= 7 ){
return path.join('platforms','android','app','src','main','res');
}
}
return path.join('platforms','android','res');
});
}

// Check the currente platform version and map the path of Java
function getJavaPath(){
return cordova_util
.getInstalledPlatformsWithVersions(context.opts.projectRoot)
.then(function(platformMap){
if ( typeof platformMap == 'object' && platformMap.android ){
var majorVersion = parseInt( platformMap.android[0] );
if ( majorVersion != NaN && majorVersion >= 7 ){
return path.join('platforms','android','app','src','main','java');
}
}
return path.join('platforms','android','src');
});
}

function mapConfig(config) {
var element = {
attrs: {},
Expand Down Expand Up @@ -153,15 +184,24 @@ module.exports = function (context) {
preferencesDocument = settingsDocuments.preferencesDocument,
preferencesStringDocument = settingsDocuments.preferencesStringDocument;


var pathXml = null;
var pathValues = null;
return fs.exists('platforms/android')
// Check version Platfom installed
.then(function () {
return getResPath();
})
// Write preferences xml file
.then(function () { return fs.mkdir('platforms/android/res/xml'); })
.then(function () { return fs.writeFile('platforms/android/res/xml/apppreferences.xml', preferencesDocument.write()); })
.then(function (pathRes) {
pathXml = path.join(pathRes, 'xml');
pathValues = path.join(pathRes, 'values');
return fs.mkdir(pathXml);
})
.then(function () { return fs.writeFile( path.join(pathXml,'apppreferences.xml'), preferencesDocument.write()); })

// Write localization resource file
.then(function () { return fs.mkdir('platforms/android/res/values'); })
.then(function (prefs) { return fs.writeFile('platforms/android/res/values/apppreferences.xml', preferencesStringDocument.write()); })
.then(function () { return fs.mkdir(pathValues); })
.then(function (prefs) { return fs.writeFile( path.join(pathValues,'apppreferences.xml'), preferencesStringDocument.write()); })

.then(function () { console.log('android preferences file was successfully generated'); })
.catch(function (err) {
Expand All @@ -175,9 +215,17 @@ module.exports = function (context) {
}

function afterPluginInstall () {
var pathJava = null;
return fs.exists('platforms/android')
// Check version Platfom installed
.then(function () {
return getJavaPath();
})
// Import preferences into native android project
.then(function () { return fs.readFile(path.resolve(__dirname, '../../src/android/AppPreferencesActivity.template')); })
.then(function (pathJ) {
pathJava = pathJ;
return fs.readFile(path.resolve(__dirname, '../../src/android/AppPreferencesActivity.template'));
})
.then(function (tmpl) {
var projectRoot = cordova_lib.cordova.findProjectRoot(process.cwd()),
projectXml = cordova_util.projectConfig(projectRoot),
Expand All @@ -193,8 +241,7 @@ module.exports = function (context) {
})
.then(function (data) {
var androidPackagePath = "me.apla.cordova".replace (/\./g, '/');
var activityFileName= path.join ('platforms/android/src', androidPackagePath, 'AppPreferencesActivity.java');

var activityFileName= path.join (pathJava, androidPackagePath, 'AppPreferencesActivity.java');
return fs.writeFile(activityFileName, data);
})

Expand All @@ -212,17 +259,36 @@ module.exports = function (context) {
function clean(config) {

var androidPackagePath = "me.apla.cordova".replace (/\./g, '/');
var activityFileName = path.join ('platforms/android/src', androidPackagePath, 'AppPreferencesActivity.java');
var activityFileName = null;

var pathXml = null;
var pathValues = null;
return fs.exists('platforms/android')
// Check version Platfom installed
.then(function () {
return getResPath();
})

// Remove preferences xml file
.then(function () { return fs.unlink('platforms/android/res/xml/apppreferences.xml'); })
.then(function (pathRes) {
pathXml = path.join(pathRes, 'xml');
pathValues = path.join(pathRes, 'values');
return fs.unlink( path.join(pathXml,'apppreferences.xml') );
})

// Remove localization resource file
.then(function (prefs) { return fs.unlink('platforms/android/res/values/apppreferences.xml'); })
.then(function (prefs) {
return fs.unlink( path.join(pathValues,'apppreferences.xml') );
})

// Check version Platfom installed
.then(function () {
return getJavaPath();
})

// Remove preferences from native android project
.then(function (data) {
.then(function (pathJava) {
activityFileName = path.join (pathJava, androidPackagePath, 'AppPreferencesActivity.java');
return fs.unlink(activityFileName);
})

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-app-preferences",
"version": "0.99.3",
"version": "0.99.4",
"description": "Application preferences plugin and preference pane generator",
"cordova": {
"id": "cordova-plugin-app-preferences",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-app-preferences"
version="0.99.3">
version="0.99.4">

<name>AppPreferences</name>
<description>Application preferences plugin and preference pane generator</description>
Expand Down