Skip to content

Commit

Permalink
Adding android version
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonala committed Jun 4, 2019
1 parent 61f0bab commit 5998d0a
Show file tree
Hide file tree
Showing 61 changed files with 10,868 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# cordova-plugin-measure

Cordova plugin using ARKit(iOS) to calculate distance of real world objects
Cordova plugin using ARKit(iOS) and ARCore(Android) to calculate distance of real world objects

Swift original measure project: https://github.com/levantAJ/Measure
Swift(iOS) original measure project: https://github.com/levantAJ/Measure
Kotlin(Android) original measure project: https://github.com/Terran-Marine/ARCoreMeasuredDistance

## Supported Platforms

- iOS (Requires ARKit support iOS 11.3+)
- Android (See devices supported: https://developers.google.com/ar/discover/supported-devices)

## Installation
cordova plugin add https://github.com/robsonala/cordova-plugin-measure
cordova plugin add cordova-plugin-measure

## Methods
- cordova.plugins.measure.start
Expand Down Expand Up @@ -66,4 +68,4 @@ data = ["10.00cm", "20.14cm", ...]
```

## TODO
Android support
FIX `allowMultiplePoints` on Android, It's always FALSE at the moment
53 changes: 53 additions & 0 deletions hooks/on-uninstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const fs = require('fs');
const path = require('path');
const xml2js = require('xml2js');
const Q = require('q');

const PLUGIN_ID = "cordova-plugin-measure";
const androidPlatformRoot = "./platforms/android/";

const deferral = Q.defer();

function removeKotlinSourceFiles(){

const pluginXML = fs.readFileSync('./plugins/'+PLUGIN_ID+'/plugin.xml').toString();
const parser = new xml2js.Parser();
parser.parseString(pluginXML, (error, config) => {
if (error) return;
if (!config.plugin.hasOwnProperty('platform')) return;
for (let platform of config.plugin.platform)
if (platform['$'].name === 'android') {
if (platform.hasOwnProperty('source-file')){
let sourceFiles = platform['source-file'];
for(let sourceFile of sourceFiles){
if (sourceFile['$'].hasOwnProperty('src')){
let src = sourceFile['$']['src'];
if(src.match(/\.kt/)){
let srcParts = src.split('/');
let filename = srcParts[srcParts.length - 1];
let filepath = sourceFile['$']['target-dir'];
filepath = androidPlatformRoot+filepath+'/'+filename;
if(fs.existsSync(path.resolve(filepath))){
fs.unlinkSync(filepath);
console.log("Removed Kotlin source file: "+filepath);
}
}
}
}
}
break;
}
});

}
module.exports = function(ctx) {
try{
removeKotlinSourceFiles();
deferral.resolve();
}catch(e){
let msg = e.toString();
console.dir(e);
deferral.reject(msg);
return deferral.promise;
}
};
59 changes: 59 additions & 0 deletions hooks/support-kotlin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const fs = require('fs');
const xml2js = require('xml2js');
const Q = require('q');

const PLUGIN_ID = "cordova-plugin-measure";
const gradlePath = './platforms/android/app/build.gradle'; // cordova-android@7+ path

const deferral = Q.defer();

function addSupport(){
let defaultArgs = {
kotlin_version: '\text.kotlin_version = "latest.integration"\n\t',
kotlin_android: 'apply plugin: "kotlin-android"',
classpath: ' \t\tclasspath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"'
};
const pluginXML = fs.readFileSync('./plugins/'+PLUGIN_ID+'/plugin.xml').toString();
const gradle = fs.readFileSync(gradlePath).toString();
let text = gradle;
const parser = new xml2js.Parser();
parser.parseString(pluginXML, (error, config) => {
if (error) return;
if (!config.plugin.hasOwnProperty('platform')) return;
for (let x of config.plugin.platform)
if (x['$'].name === 'android') {
if (x['$'].hasOwnProperty('kotlin')) defaultArgs.kotlin_version = `\text.kotlin_version = "${x['$'].kotlin}"\n\t`;
if (x.hasOwnProperty('apply-plugin')) defaultArgs.apply_plugin = x['apply-plugin'];
break;
}
if (!gradle.match(/ext.kotlin_version/g)) append(defaultArgs.kotlin_version, /buildscript(\s*)\{\s*/g);
if (!gradle.match(/kotlin-gradle-plugin/g)) append(defaultArgs.classpath, /classpath\s+(['"])[\w.:]+(['"])/g);
if (!gradle.match(/apply\s+plugin(\s*:\s*)(['"])kotlin-android(['"])/g)) append(defaultArgs.kotlin_android);
if (defaultArgs.apply_plugin)
for (let x of defaultArgs.apply_plugin) {
const reg = new RegExp(`apply\\s+plugin(\\s*:\\s*)(['"])${x}(['"])`, 'g');
if (!gradle.match(reg)) append(`apply plugin: "${x}"`);
}
});

function append(edit, reg) {
if (reg === undefined) reg = /com.android.application['"]/g;
const pos = text.search(reg);
const len = text.match(reg)[0].length;
const header = text.substring(0, pos + len);
const footer = text.substring(pos + len);
text = header + '\n' + edit + footer;
}

fs.writeFileSync(gradlePath, text);
}
module.exports = function(ctx) {
try{
addSupport();
deferral.resolve();
}catch(e){
let msg = e.toString();
deferral.reject(msg);
return deferral.promise;
}
};
Loading

0 comments on commit 5998d0a

Please sign in to comment.