YAPDroid project: use the latest Android SDK

This commit is contained in:
Vitor Santos Costa 2016-06-17 15:52:07 +01:00
parent a1a4ac9d0b
commit a7720d42ed
8 changed files with 602 additions and 0 deletions

37
YAPDroid/app/build.gradle Normal file
View File

@ -0,0 +1,37 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "pt.up.yap.app"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
project.gradle.taskGraph.whenReady {
connectedDebugAndroidTest {
ignoreFailures = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support:support-annotations:23.4.0'
compile 'com.google.android.gms:play-services-appindexing:8.4.0'
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pt.up.yap.app"
android:versionCode="1"
android:versionName="1.0">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme"
>
<activity android:name=".YAPDroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>

View File

@ -0,0 +1,86 @@
package pt.up.yap.app;
import android.content.Context;
import android.content.res.AssetManager;
import android.support.v4.content.res.ResourcesCompat;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by vsc on 30/05/16.
*/
public class CreateFiles {
public static void setupfiles(Context context, AssetManager assets){
try {
context.getExternalFilesDir("Yap").mkdirs();
context.getExternalFilesDir("Yap/pl").mkdirs();
context.getExternalFilesDir("Yap/os").mkdirs();
String list[] = {};
list= assets.list("Yap");
for (int i = 0; i < list.length; i++) {
copy(context, "Yap/"+ list[i]);
}
list = null;
list= assets.list("Yap/pl");
for (int i = 0; i < list.length; i++) {
copy(context, "Yap/pl/"+ list[i]);
}
list =null;
list= assets.list("Yap/os");
for (int i = 0; i < list.length; i++) {
copy(context, "Yap/os/"+ list[i]);
}
Log.d("CreateFiles", context.getFilesDir().getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
private static void copy(Context context, String originalName) {
try {
if (originalName.contains(".")) {
File outFile;
outFile = new File(context.getExternalFilesDir(null), originalName );
InputStream in = context.getAssets().open(originalName);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
byte data[] = new byte[1024];
int count;
while ((count = in.read(data)) != -1) {
out.write(data, 0, count);
}
out.flush();
out.close();
in.close();
in = null;
out = null;
//Log.d("Copied: ", originalName + " to " + destinationName);
//System.out.println("Copied: "+ originalName + " to " + destinationName);
//System.out.println();
}
}catch(IOException e){
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,330 @@
package pt.up.yap.app;
/****
* using sqlite
* For example,the following:
* <p/>
* import android.database.sqlite.SQLiteDatabase;
* <p/>
* should be replaced with:
* <p/>
* import org.sqlite.database.sqlite.SQLiteDatabase;
* <p/>
* As well as replacing all uses of the classes in the android.database.sqlite.* namespace, the application must also be sure to use the following two:
* <p/>
* org.sqlite.database.SQLException
* org.sqlite.database.DatabaseErrorHandler
* <p/>
* instead of:
* <p/>
* android.database.SQLException
* android.database.DatabaseErrorHandler
* <p/>
* Aside from namespace changes, there are other differences from the stock Android interface that applications need to be aware of:
* <p/>
* The SQLiteStatement.simpleQueryForBlobFileDescriptor() API is not available. The collation sequence "UNICODE" is not available. The collation sequence "LOCALIZED", which normally changes with the system's current locale, is always equivalent to SQLite's built in collation BINARY.
****/
import android.content.Context;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import org.sqlite.database.DatabaseErrorHandler;
import org.sqlite.database.sqlite.SQLiteDatabase;
import pt.up.yap.lib.*;
class DoNotDeleteErrorHandler implements DatabaseErrorHandler {
private static final String TAG = "DoNotDeleteErrorHandler";
public void onCorruption(SQLiteDatabase dbObj) {
Log.e(TAG, "Corruption reported by sqlite on database: " + dbObj.getPath());
}
}
public class YAPDroid extends Activity {
private static final String TAG = "YAPDroid";
TextView outputText = null;
ScrollView scroller = null;
YAPEngine eng = null;
EditText text;
String str;
String buf;
YAPQuery q;
Boolean running = false, compute = true;
int i = 1;
YAPListTerm vs0;
private AssetManager mgr;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
// private static native void load(AssetManager mgr);
void runQuery(String str, Boolean more) {
try {
// check if at initial query
if (running) {
if (q != null) q.close();
}
if (BuildConfig.DEBUG) {
Log.i(TAG, "query " + str);
}
q = eng.query(str);
// get the uninstantiated query variables.
vs0 = q.namedVars();
running = true;
// start computing
compute = true;
if (BuildConfig.DEBUG) {
Log.i(TAG, "onQueryButtonClick called");
}
Boolean rc = true;
text.setText("?- ");
if (vs0.nil()) {
if (BuildConfig.DEBUG) {
Log.i(TAG, "q0=\n");
}
if (compute && (rc = q.next())) {
outputText.append("yes\n");
running = compute = more;
} else {
outputText.append("no\n");
running = false;
compute = false;
}
} else {
// if (BuildConfig.DEBUG) {
// Log.i(TAG, "q1= " + vs0.text() + "\n");
// }
while (compute && (rc = q.next())) {
YAPListTerm vs = q.namedVars();
while (!vs.nil()) {
if (BuildConfig.DEBUG) {
Log.i(TAG, "q= " + vs.text() + "\n");
}
YAPTerm eq = vs.car();
//outputText.append(Integr.toString(i) + ": " + eq.text() );
outputText.append(Integer.toString(i));
outputText.append(":\t" + eq.getArg(1).text() + " = " + eq.getArg(2).text() + "\n");
vs = vs.cdr();
}
compute = more;
}
}
if (!rc) {
outputText.append("no\n");
if (q != null)
q.close();
q = null;
compute = false;
running = false;
}
} catch (Exception e) {
outputText.append("Exception thrown :" + e);
if (q != null)
q.close();
compute = true;
running = false;
}
}
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
String s = null;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
PackageManager
m = getPackageManager();
s = getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
//s = p.applicationInfo.dataDir;
mgr = this.getAssets();
/** static constructor */
// follow this order carefully.
System.loadLibrary("gmp");
System.loadLibrary("Yap");
System.loadLibrary("Yapsqlite3");
System.loadLibrary("Yap++");
System.loadLibrary("YAPDroid");
CreateFiles.setupfiles(this, mgr);
} catch (NameNotFoundException e) {
Log.e(TAG, "Couldn't find package information in PackageManager", e);
}
Log.i(TAG, "mgr=" + mgr);
text = (EditText) findViewById(R.id.EditText01);
outputText = (TextView) findViewById(R.id.OutputText);
outputText.setText("Application " + s + "\nPress 'First' or 'All' to query...\n");
outputText.setMovementMethod(new ScrollingMovementMethod());
scroller = (ScrollView) findViewById(R.id.Scroller);
if (BuildConfig.DEBUG) {
Log.i(TAG, "window making done");
}
eng = new YAPEngine(null, this.getExternalFilesDir("/Yap/pl/boot.yap").getAbsolutePath());
if (BuildConfig.DEBUG) {
Log.i(TAG, "engine done");
}
if (BuildConfig.DEBUG) {
Log.i(TAG, "onClearButtonClick called");
}
JavaCallback callback = new JavaCallback(outputText);
// set the Java Callback
if (BuildConfig.DEBUG) {
Log.i(TAG, "before setting callback");
}
eng.setYAPCallback(callback);
if (BuildConfig.DEBUG) {
Log.i(TAG, "callback done");
}
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void onClearButtonClick(View view) {
if (BuildConfig.DEBUG) {
Log.i(TAG, "onClearButtonClick called");
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
if (running) {
if (q != null)
q.close();
q = null;
}
running = false;
text.setText("");
}
});
}
public void onFirstButtonClick(View view) {
if (BuildConfig.DEBUG) {
Log.i(TAG, "onQueryButtonClick called");
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
str = text.getText().toString();
//outputText.append("?- " + str+"\n\n");
Log.i(TAG, "onQueryAnyButtonClick " + str + "\n");
runQuery(str, false);
scroller.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
public void onAllButtonClick(View view) {
if (BuildConfig.DEBUG) {
Log.i(TAG, "onQueryButtonClick called");
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
str = text.getText().toString();
scroller.fullScroll(ScrollView.FOCUS_DOWN);
outputText.append("?- " + str + "\n\n");
Log.i(TAG, "onAllButtonClick " + str + "\n");
runQuery(str, true);
scroller.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("YAPDroid Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}
class JavaCallback extends YAPCallback {
private static final String TAG = "JavaCallback";
TextView output;
public JavaCallback(TextView outputText) {
super();
output = outputText;
Log.i(TAG, "java callback init");
}
public void run(String s) {
Log.i(TAG, "java callback ");
output.append(s);
}
}

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".YAPDroid">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:lines="6" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/FirstButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onFirstButtonClick"
android:text="First..." />
<Button
android:id="@+id/AllButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onAllButtonClick"
android:text="All..." />
<Button
android:id="@+id/ClearButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClearButtonClick"
android:text="Reset" />
</LinearLayout>
</LinearLayout>
<ScrollView
android:id="@+id/Scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/OutputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false" />
<!-- Set MaxLegth EditText -->
</ScrollView>
</LinearLayout>

23
YAPDroid/build.gradle Normal file
View File

@ -0,0 +1,23 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-alpha3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

42
YAPDroid/lib/build.gradle Normal file
View File

@ -0,0 +1,42 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "pt.up.yap.app"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
cmake {
// cFlags "-DTEST_C_FLAG1" "-DTEST_C_FLAG2"
// cppFlags "-DTEST_CPP_FLAG2" "-DTEST_CPP_FLAG2"
abiFilters "armeabi-v7a", "x86"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// externalNativeBuild {
// cmake {
// path '../../CMakeLists.txt'
// }
// }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support:support-annotations:23.4.0'
}

1
YAPDroid/settings.gradle Normal file
View File

@ -0,0 +1 @@
include ':app', ':lib'