This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/swig/android/JavaYap.java

258 lines
7.2 KiB
Java
Raw Normal View History

2014-07-10 05:28:10 +01:00
package pt.up.fc.dcc.yap;
2015-02-10 00:03:02 +00:00
/**** using sqlite
For example,the following:
import android.database.sqlite.SQLiteDatabase;
should be replaced with:
import org.sqlite.database.sqlite.SQLiteDatabase;
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:
org.sqlite.database.SQLException
org.sqlite.database.DatabaseErrorHandler
instead of:
android.database.SQLException
android.database.DatabaseErrorHandler
Aside from namespace changes, there are other differences from the stock Android interface that applications need to be aware of:
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.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ScrollView;
import android.text.method.ScrollingMovementMethod;
2014-06-04 22:07:37 +01:00
import android.content.pm.PackageManager;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.content.res.AssetManager;
2014-06-11 19:31:24 +01:00
import android.widget.EditText;
2014-06-22 17:35:05 +01:00
import java.text.ParseException;
2014-07-16 00:57:48 +01:00
import org.sqlite.database.sqlite.SQLiteDatabase;
import org.sqlite.database.sqlite.SQLiteStatement;
import org.sqlite.database.sqlite.SQLiteDatabaseCorruptException;
import org.sqlite.database.sqlite.SQLiteOpenHelper;
2015-02-10 00:03:02 +00:00
import org.sqlite.database.SQLException;
2014-07-16 00:57:48 +01:00
import org.sqlite.database.DatabaseErrorHandler;
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());
}
}
2014-07-10 05:28:10 +01:00
public class JavaYap extends Activity
{
2015-03-11 22:18:00 +00:00
TextView outputText = null;
ScrollView scroller = null;
YAPEngine eng = null;
EditText text;
String str;
String buf;
void runQuery(String str)
{
try
{
YAPQuery q = eng.query( str );
if (BuildConfig.DEBUG) {
Log.i(TAG, "onQueryButtonClick called");
}
YAPListTerm vs0 = q.namedVars();
Boolean rc;
// text.setText("");
if (vs0.nil()) {
if (BuildConfig.DEBUG) {
Log.i(TAG, "q0=\n");
}
if (q.next()) {
outputText.append( "yes\n" );
} else {
outputText.append( "no\n" );
}
} else {
int i=1;
if (BuildConfig.DEBUG) {
//Log.i(TAG, "q1= "+vs0.text()+"\n");
}
while (rc = q.next()) {
YAPListTerm vs = vs0;
while(!vs.nil()){
YAPTerm eq = vs.car();
if (BuildConfig.DEBUG) {
Log.i(TAG, "q= "+vs0+"\n");
}
//outputText.append(Integer.toString(i) + ": " + eq.text() );
outputText.append(Integer.toString(i++));
Log.i(TAG, "q= "+ eq.getArg(1)+"\n");
Log.i(TAG, "q= "+ eq.getArg(2)+"\n");
Log.i(TAG, "q= "+ eq.getArg(1).text()+"\n");
Log.i(TAG, "q= "+ eq.getArg(2).text()+"\n");
outputText.append(":\t" + eq.getArg(1).text() + " = " + eq.getArg(2).text() +"\n" );
vs = vs.cdr();
}
}
2014-06-22 17:35:05 +01:00
}
2015-03-11 22:18:00 +00:00
q.close();
} catch(Exception e){
outputText.append("Exception thrown :" + e);
return;
}
}
2014-06-22 17:35:05 +01:00
2015-03-11 22:18:00 +00:00
/** 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;
AssetManager mgr = getResources().getAssets();
Log.i(TAG, "mgr=" +mgr + " " + s);
load(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 'Query' to start...\n");
outputText.setMovementMethod(new ScrollingMovementMethod());
scroller = (ScrollView)findViewById(R.id.Scroller);
if (BuildConfig.DEBUG) {
Log.i(TAG, "window making done");
}
eng = new YAPEngine( );
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");
}
}
2014-06-11 19:31:24 +01:00
2015-03-11 22:18:00 +00:00
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);
text.setText("");
2014-06-11 19:31:24 +01:00
}
2015-03-11 22:18:00 +00:00
});
}
2014-06-11 19:31:24 +01:00
2015-03-11 22:18:00 +00:00
public void onQueryButtonClick(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);
Log.i(TAG, "onQueryButtonClick "+str + "\n");
runQuery(str);
2014-06-11 19:31:24 +01:00
}
2015-03-11 22:18:00 +00:00
});
}
2014-06-11 19:31:24 +01:00
2015-03-11 22:18:00 +00:00
public void onQuerySelectionButtonClick(View view)
{
if (BuildConfig.DEBUG) {
Log.i(TAG, "onQuerySelectionButtonClick called");
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
int startSelection = text.getSelectionStart();
int endSelection = text.getSelectionEnd();
str = text.getText().toString().substring( startSelection, endSelection );
Log.i(TAG, "onQuerySelectionButtonClick "+str);
outputText.append("?- " + str + "\n");
runQuery(str);
2014-06-11 19:31:24 +01:00
}
2015-03-11 22:18:00 +00:00
});
}
2014-06-11 19:31:24 +01:00
2015-03-11 22:18:00 +00:00
/** static constructor */
static {
System.loadLibrary("android");
System.loadLibrary("log");
System.loadLibrary("gmp");
System.loadLibrary("sqliteX");
System.loadLibrary("example");
}
2014-06-11 19:31:24 +01:00
2015-03-11 22:18:00 +00:00
private static native void load(AssetManager mgr);
2014-06-11 19:31:24 +01:00
2015-03-11 22:18:00 +00:00
private AssetManager mgr;
2014-06-11 19:31:24 +01:00
2015-03-11 22:18:00 +00:00
private static final String TAG = "JavaYap";
2014-06-11 19:31:24 +01:00
}
class JavaCallback extends YAPCallback
{
2015-03-11 22:18:00 +00:00
TextView output;
2014-06-11 19:31:24 +01:00
public JavaCallback( TextView outputText )
{
super();
output = outputText;
Log.i(TAG, "java callback init");
}
public void run()
{
2015-03-11 22:18:00 +00:00
Log.i(TAG, "java callback ");
System.out.println("JavaCallback.run() ");
2014-06-19 15:10:11 +01:00
}
2014-06-11 19:31:24 +01:00
2014-06-19 15:10:11 +01:00
public void run(String s)
2014-06-11 19:31:24 +01:00
{
2014-06-19 15:10:11 +01:00
Log.i(TAG, "java callback ");
output.append(s);
2015-03-11 22:18:00 +00:00
}
2014-06-11 19:31:24 +01:00
private static final String TAG = "JavaCallback";
}