Added Android builder juicy example

git-svn-id: svn://ultimatepp.org/upp/trunk@8640 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
klugier 2015-07-07 21:52:43 +00:00
parent 20ee1a9a47
commit 99222001cf
15 changed files with 439 additions and 0 deletions

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.upp.AndroidMath">
<application
android:allowBackup="true">
<activity android:name=".AndroidMathActivity"
android:label="AndroidMath">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

View file

@ -0,0 +1,23 @@
#include <string.h>
#include <jni.h>
#include <AndroidMathUtility/AndroidMathUtility.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_upp_AndroidMath_AndroidMath
* Method: power
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_org_upp_AndroidMath_AndroidMath_power
(JNIEnv *, jclass, jint number, jint n)
{
return AndroidMathUtility::Power(number, n);
}
#ifdef __cplusplus
}
#endif

View file

@ -0,0 +1,15 @@
package org.upp.AndroidMath;
public class AndroidMath
{
private AndroidMath() {}
// Native stuff - C/C++
public static native int power(int number, int n);
static {
// In this place we are loading native libraries.
// Native library always has upp package name!
System.loadLibrary("AndroidMath");
}
}

View file

@ -0,0 +1,21 @@
description "Android builder juicy example\377";
uses
AndroidMathUtility;
file
MemoryManager readonly separator,
MemoryManager.h,
Native readonly separator,
AndroidMath.cpp,
Vector.cpp,
Math readonly separator,
Vector.java,
Runtime readonly separator,
AndroidManifest.xml,
AndroidMath.java,
AndroidMathActivity.java;
mainconfig
"" = "";

View file

@ -0,0 +1,48 @@
package org.upp.AndroidMath;
import android.app.Activity;
import android.widget.TextView;
import android.widget.ScrollView;
import android.os.Bundle;
public class AndroidMathActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ScrollView scroller = new ScrollView(this);
TextView tv = new TextView(this);
calculatePowerOperations(tv, 20);
calculateVectorOperations(tv);
scroller.addView(tv);
setContentView(scroller);
}
private void calculatePowerOperations(TextView tv, int maxPower)
{
String text = "Power:\n";
int maxNumber = maxPower;
for(int i = 1; i <= maxNumber; i++) {
int pow = AndroidMath.power(i, 2);
text += Integer.toString(i) + "^2 = " + Integer.toString(pow) + "\n";
}
tv.append(text);
}
private void calculateVectorOperations(TextView tv)
{
String text = "Vector operations:\n";
Vector vec1 = new Vector(3);
text += "Vec1 Size: " + Integer.toString(vec1.getSize()) + "\n";
text += "Vec1: " + vec1.toString() + "\n";
tv.append(text);
}
}

View file

@ -0,0 +1,71 @@
#ifndef _JniMath_MemoryManager_h_
#define _JniMath_MemoryManager_h_
#include <jni.h>
#include <string>
#include <sstream>
#include <vector>
/**
* Java "HashCode()" object memory manager.
*/
template <class T>
class MemoryManager {
public:
void Insert(JNIEnv *env, jobject jobj, const T& obj)
{
std::string hashCode = GetJobjectHasCode(env, jobj);
if(!hashCode.empty()) {
hashCodes.push_back(hashCode);
values.push_back(obj);
}
}
void Erase(JNIEnv *env, jobject jobj)
{
int idx = FindIdx(env, jobj);
if(idx >= 0) {
hashCodes.erase(hashCodes.begin() + idx);
values.erase(values.begin() + idx);
}
}
T* Get(JNIEnv *env, jobject jobj)
{
int idx = FindIdx(env, jobj);
return idx != -1 ? &values[idx] : NULL;
}
private:
int FindIdx(JNIEnv *env, jobject jobj)
{
std::string hashCode = GetJobjectHasCode(env, jobj);
for(int i = 0; i < hashCodes.size(); i++) {
if(hashCode == hashCodes[i]) {
return i;
}
}
return -1;
}
std::string GetJobjectHasCode(JNIEnv *env, jobject jobj)
{
jclass cls = env->GetObjectClass(jobj);
jmethodID mid = env->GetMethodID(cls, "hashCode", "()I");
if(mid == 0) {
return "";
}
int ret = env->CallIntMethod(jobj, mid);
std::stringstream ss;
ss << ret;
return ss.str();
}
private:
std::vector<std::string> hashCodes;
std::vector<T> values;
};
#endif

View file

@ -0,0 +1,96 @@
#ifndef _Included_org_upp_AndroidMath_Vector
#define _Included_org_upp_AndroidMath_Vector
#include <jni.h>
#include <map>
#include <vector>
#include <sstream>
#include <string>
#include <AndroidMathUtility/AndroidMathUtility.h>
#include "MemoryManager.h"
#ifdef __cplusplus
extern "C" {
#endif
using AndroidMathUtility::Vector;
MemoryManager<Vector> mm;
/*
* Class: org_upp_AndroidMath_Vector
* Method: construct
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_org_upp_AndroidMath_Vector_construct
(JNIEnv *env, jobject obj, jint size)
{
mm.Insert(env, obj, Vector(size));
}
/*
* Class: org_upp_AndroidMath_Vector
* Method: destruct
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_upp_AndroidMath_Vector_destroy
(JNIEnv *env, jobject obj)
{
mm.Erase(env, obj);
}
/*
* Class: org_upp_AndroidMath_Vector
* Method: getSize
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_org_upp_AndroidMath_Vector_getSize
(JNIEnv *env, jobject obj)
{
Vector* vec = mm.Get(env, obj);
return vec->GetSize();
}
/*
* Class: org_upp_AndroidMath_Vector
* Method: get
* Signature: (I)F
*/
JNIEXPORT jfloat JNICALL Java_org_upp_AndroidMath_Vector_get
(JNIEnv *, jobject, jint)
{
return 0.0f;
}
/*
* Class: org_upp_AndroidMath_Vector
* Method: set
* Signature: (IF)V
*/
JNIEXPORT void JNICALL Java_org_upp_AndroidMath_Vector_set
(JNIEnv *, jobject, jint, jfloat)
{
}
/*
* Class: org_upp_AndroidMath_Vector
* Method: toStirng
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_upp_AndroidMath_Vector_toString
(JNIEnv *env, jobject obj)
{
Vector* vec = mm.Get(env, obj);
return env->NewStringUTF(vec->ToString().c_str());
}
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,33 @@
package org.upp.AndroidMath;
/**
* Class witch whole implementaiton is native.
*/
public class Vector
{
public Vector(int size)
{
construct(size);
}
@Override
public void finalize()
{
destroy();
}
// Native stuff - C/C++
public native int getSize();
public native float get(int id);
public native void set(int id, float data);
public native String toString();
private native void construct(int size);
private native void destroy();
static {
System.loadLibrary("AndroidMath");
}
}

View file

@ -0,0 +1,4 @@
#ifndef _AndroidMath_icpp_init_stub
#define _AndroidMath_icpp_init_stub
#include "AndroidMathUtility/init"
#endif