9
0
mirror of https://gitee.com/shuto/customCamera.git synced 2026-05-02 00:07:24 +08:00

Mise en place de la structure pour android + ajout de hook pour l'installation et le fonctionnement du plugin sous android.

This commit is contained in:
Christophe Boucaut
2014-11-14 16:05:50 +01:00
parent cfaf8434c2
commit f6c5a77186
32 changed files with 216 additions and 34 deletions
+4
View File
@@ -0,0 +1,4 @@
#!/usr/bin/env node
var exec = require('child_process').exec;
exec("npm install xml2js@0.4.x");
+181
View File
@@ -0,0 +1,181 @@
#!/usr/bin/env node
// Define required.
var fs = require("fs");
var xml2js = require('xml2js');
var parseString = xml2js.parseString;
var builder = new xml2js.Builder();
// Define differents paths.
var pathConfigXml = "config.xml";
var pathAndroidCordova = "platforms/android/";
var pathResAndroidCordova = "platforms/android/res/";
var pathAndroidPlugin = __dirname+"/../../src/android/";
var pathResPlugin = __dirname+"/../../res/";
/**
* Generate a path based on a package name.
*
* @param {string} packageName Package name, eg: com.example.
*
* @return {string} Return path generate.
*/
var generatePathFrompackageName = function(packageName) {
return packageName.split(".").join("/");
}
/**
* Create different java classes.
*
* @param {string} packageName Package name of the current application.
*/
var createClasses = function(packageName) {
var pathCameraView = pathAndroidPlugin+"customCamera/src/org/geneanet/customcamera/CameraView.java";
var pathCameraLauncher = pathAndroidPlugin+"CameraLauncher.java";
if (fs.existsSync(pathCameraView) && fs.existsSync(pathCameraLauncher)) {
// Rewrite package of CameraView class.
var contentCameraView = fs.readFileSync(pathCameraView, {encoding: "utf8"});
contentCameraView = contentCameraView.replace(/^package\s[^;]*/,"package "+packageName);
fs.writeFileSync(pathAndroidCordova+"src/"+generatePathFrompackageName(packageName)+"/CameraView.java", contentCameraView);
// Rewrite import of CameraLauncher class.
var contentCameraLauncher = fs.readFileSync(pathCameraLauncher, {encoding: "utf8"});
contentCameraLauncher = contentCameraLauncher.replace("XXX_NAME_CURRENT_PACKAGE_XXX", packageName);
fs.writeFileSync(
pathAndroidCordova+"src/"+generatePathFrompackageName(contentCameraLauncher.match(/package\s([^;]*)/)[1])+"/CameraLauncher.java",
contentCameraLauncher
);
} else {
console.error("File CameraView.java or/and CameraLauncher.java not found.");
process.exit(1);
}
}
/**
* Update AndroidManifest.xml of the current application.
*
* @param {string} packageName Package name of the current application.
*/
var updateAndroidManifest = function(packageName) {
var pathAndroidManifestCordova = pathAndroidCordova+"AndroidManifest.xml";
if (fs.existsSync(pathAndroidManifestCordova)) {
// get content AndroidManifest.
var contentAndroidManifest = fs.readFileSync(pathAndroidManifestCordova, {encoding: "utf8"});
parseString(contentAndroidManifest, function (err, result) {
contentAndroidManifest = result;
});
// add activity if needed.
var needAddActivity = true;
var currentActivities = contentAndroidManifest["manifest"]["application"][0]["activity"];
for (var i = currentActivities.length - 1; i >= 0; i--) {
if (currentActivities[i]["$"]["android:name"] == "CameraView") {
needAddActivity = false;
}
};
if (needAddActivity) {
contentAndroidManifest["manifest"]["application"][0]["activity"].push({
$: {
"android:name": "CameraView",
"android:label": "CameraView",
}
})
var newXmlAndroidManifest = builder.buildObject(contentAndroidManifest);
fs.writeFileSync(
pathAndroidManifestCordova,
newXmlAndroidManifest
);
}
} else {
console.error("File AndroidManifest.xml for cordova not found.");
process.exit(1);
}
}
/**
* Update differents config file (translate, res/layout, etc).
*/
var updateConfig = function() {
var pathLayoutCordova = pathResAndroidCordova+"layout/";
var pathLayoutPlugin = pathAndroidPlugin+"customCamera/res/layout/";
var pathLayoutCameraView = pathLayoutPlugin+"activity_camera_view.xml";
// create directory layout in cordova if it doesn't exist.
if (!fs.existsSync(pathLayoutCordova)) {
fs.mkdirSync(pathLayoutCordova);
}
// "copy" layout for camera.
if (fs.existsSync(pathLayoutCameraView)) {
var layoutCameraViewContent = fs.readFileSync(pathLayoutCameraView, {encoding: "utf8"});
fs.writeFileSync(pathLayoutCordova+"activity_camera_view.xml", layoutCameraViewContent);
} else {
console.error("File activity_camera_view.xml in plugin not found.");
process.exit(1);
}
// add translations.
var pathTranslations = pathResPlugin+"translations.json";
if (fs.existsSync(pathTranslations)) {
// get translations.
var translationsForApplications = fs.readFileSync(pathTranslations, {encoding: "utf8"});
translationsForApplications = JSON.parse(translationsForApplications);
for (lang in translationsForApplications) {
var pathFileTranslate = pathResAndroidCordova+"values-"+lang+"/";
var objToXml;
// already exist, get data.
if (fs.existsSync(pathFileTranslate+"strings.xml")) {
var objToXml = fs.readFileSync(pathFileTranslate+"strings.xml", {encoding: "utf8"});
parseString(objToXml, function (err, result) {
objToXml = result;
});
} else {
// generate minimal object.
objToXml = {
resources: {
string: []
}
};
}
// add message.
for (tag in translationsForApplications[lang]) {
objToXml["resources"]["string"].push({
_: translationsForApplications[lang][tag],
$: {
name: tag
}
});
}
var xmlBuild = builder.buildObject(objToXml);
if (!fs.existsSync(pathFileTranslate)) {
fs.mkdirSync(pathFileTranslate);
}
fs.writeFileSync(pathFileTranslate+"strings.xml", xmlBuild);
}
} else {
console.error("File translations.json in plugin not found.");
process.exit(1);
}
}
// Check if files required exist.
if (fs.existsSync(pathConfigXml)) {
// Get the name package of the current application.
var configContent = fs.readFileSync(pathConfigXml, {encoding: "utf8"});
parseString(configContent, function (err, result) {
configContent = result;
});
var packageName = configContent["widget"]["$"]["id"];
createClasses(packageName);
updateAndroidManifest(packageName);
updateConfig();
} else {
console.error("File config.xml for cordova not found.");
process.exit(1);
}
+8 -5
View File
@@ -12,6 +12,9 @@
<js-module src="www/customCamera.js" name="customCamera">
<clobbers target="customCamera" />
</js-module>
<hook type="after_plugin_install" src="hooks/after_plugin_install/npminstall.js" />
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="CustomCamera">
@@ -22,10 +25,10 @@
<uses-permission android:name="android.permission.CAMERA" />
</config-file>
<source-file src="src/android/CustomCamera.jar" target-dir="libs" framework="true" />
<source-file
src="src/android/CameraLauncher.java"
target-dir="src/org/geneanet/customcamera"
/>
<source-file src="src/android/CameraLauncher.java" target-dir="src/org/geneanet/customcamera" />
<source-file src="src/android/customCamera/src/org/geneanet/customcamera/CustomCamera.java" target-dir="src/org/geneanet/customcamera" />
<source-file src="src/android/customCamera/src/org/geneanet/customcamera/CameraPreview.java" target-dir="src/org/geneanet/customcamera" />
<hook type="before_build" src="hooks/before_build/generateActivity.js" />
</platform>
</plugin>
+10
View File
@@ -0,0 +1,10 @@
{
"fr": {
"sss": "",
"capture": "Prendre photo"
},
"en": {
"sss": "",
"capture": "Take picture"
}
}
+4 -8
View File
@@ -1,5 +1,6 @@
package org.geneanet.customcamera;
import XXX_NAME_CURRENT_PACKAGE_XXX.CameraView;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
@@ -9,14 +10,9 @@ import android.content.Intent;
public class CameraLauncher extends CordovaPlugin {
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
Intent intent = new Intent("org.geneanet.customcamera.CameraView");
cordova.startActivityForResult((CordovaPlugin) this, intent, 1111111);
Intent intent = new Intent(this.cordova.getActivity(), CameraView.class);
cordova.getActivity().startActivity(intent);
return true;
}
public void testAction()
{
}
}
Binary file not shown.
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.geneanet.testcustomcamera"
package="org.geneanet.customcamera"
android:versionCode="1"
android:versionName="1.0" >

Before

Width:  |  Height:  |  Size: 50 KiB

After

Width:  |  Height:  |  Size: 50 KiB

@@ -11,4 +11,4 @@
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-14
target=android-16

Before

Width:  |  Height:  |  Size: 7.5 KiB

After

Width:  |  Height:  |  Size: 7.5 KiB

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

@@ -15,8 +15,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5"
android:contentDescription="@string/sss"
android:src="@drawable/labs" />
android:contentDescription="@string/sss" />
<Button
android:id="@+id/button_capture"
@@ -4,7 +4,7 @@
<string name="app_name">TestCustomCamera</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_camera_view">CameraView</string>
<string name="title_activity_camera_view">CameraViewNative</string>
<string name="start_custom_camera">Start Custom Camera</string>
<string name="capture">Capture</string>
<string name="todo">TODO</string>
@@ -1,4 +1,4 @@
package org.geneanet.customcamera.utils;
package org.geneanet.customcamera;
import java.io.IOException;
@@ -1,23 +1,15 @@
package org.geneanet.customcamera;
import org.geneanet.customcamera.R;
import org.geneanet.customcamera.utils.CameraPreview;
import org.geneanet.customcamera.utils.CustomCamera;
import org.geneanet.customcamera.CameraPreview;
import org.geneanet.customcamera.CustomCamera;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Matrix;
import android.graphics.Point;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class CameraView extends Activity {
@@ -32,7 +24,7 @@ public class CameraView extends Activity {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_camera_view);
// Récupère les infos sur le device.
@@ -1,4 +1,4 @@
package org.geneanet.customcamera.utils;
package org.geneanet.customcamera;
import android.hardware.Camera;
@@ -6,13 +6,10 @@ import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {