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

190 lines
4.5 KiB
Java
Raw Normal View History

2014-07-10 05:28:10 +01:00
package pt.up.fc.dcc.yap;
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-10 05:28:10 +01:00
public class JavaYap extends Activity
{
2014-07-10 05:28:10 +01:00
TextView outputText = null;
2014-06-11 19:31:24 +01:00
ScrollView scroller = null;
2014-06-04 22:07:37 +01:00
YAPEngine eng = null;
2014-06-11 19:31:24 +01:00
EditText text;
String str;
String buf;
2014-06-22 17:35:05 +01:00
void runQuery(String str)
{
try
{
YAPQuery q = eng.query( str );
YAPListTerm vs0 = q.namedVars();
Boolean rc;
2014-07-14 06:03:08 +01:00
2014-06-22 17:35:05 +01:00
// text.setText("");
if (vs0.nil()) {
if (q.next()) {
outputText.append( "yes\n" );
} else {
outputText.append( "no\n" );
}
} else {
int i=1;
2014-07-14 06:03:08 +01:00
while (rc = q.next()) {
2014-06-22 17:35:05 +01:00
YAPListTerm vs = vs0;
while(!vs.nil()){
YAPTerm eq = vs.car();
//outputText.append(Integer.toString(i) + ": " + eq.text() );
outputText.append(Integer.toString(i++) + ":\t" + eq.getArg(1).text() + " = " + eq.getArg(2).text() +"\n" );
vs = vs.cdr();
}
}
}
q.close();
} catch(Exception e){
outputText.append("Exception thrown :" + e);
return;
}
}
2014-06-11 19:31:24 +01: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();
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);
2014-06-22 17:35:05 +01:00
outputText.setText("Application " + s + "\nPress 'Query' to start...\n");
2014-06-11 19:31:24 +01:00
outputText.setMovementMethod(new ScrollingMovementMethod());
scroller = (ScrollView)findViewById(R.id.Scroller);
eng = new YAPEngine( );
Log.i(TAG, "engine done");
JavaCallback callback = new JavaCallback( outputText );
// set the Java Callback
eng.setYAPCallback(callback);
if (BuildConfig.DEBUG) {
Log.i(TAG, "callback done");
}
}
2014-06-22 17:35:05 +01:00
public void onClearButtonClick(View view)
2014-06-11 19:31:24 +01:00
{
if (BuildConfig.DEBUG) {
2014-06-22 17:35:05 +01:00
Log.i(TAG, "onClearButtonClick called");
2014-06-11 19:31:24 +01:00
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
2014-06-22 17:35:05 +01:00
text.setText("");
2014-06-11 19:31:24 +01:00
}
});
}
2014-06-22 17:35:05 +01:00
public void onQueryButtonClick(View view)
2014-06-11 19:31:24 +01:00
{
if (BuildConfig.DEBUG) {
2014-06-22 17:35:05 +01:00
Log.i(TAG, "onQueryButtonClick called");
2014-06-11 19:31:24 +01:00
}
// 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);
2014-06-22 17:35:05 +01:00
Log.i(TAG, "onQueryButtonClick "+str + "\n");
runQuery(str);
2014-06-11 19:31:24 +01:00
}
});
}
2014-06-22 17:35:05 +01:00
public void onQuerySelectionButtonClick(View view)
2014-06-11 19:31:24 +01:00
{
if (BuildConfig.DEBUG) {
2014-06-22 17:35:05 +01:00
Log.i(TAG, "onQuerySelectionButtonClick called");
2014-06-11 19:31:24 +01:00
}
// 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 );
2014-06-22 17:35:05 +01:00
Log.i(TAG, "onQuerySelectionButtonClick "+str);
outputText.append("?- " + str + "\n");
runQuery(str);
2014-06-11 19:31:24 +01:00
}
});
}
/** static constructor */
static {
System.loadLibrary("android");
System.loadLibrary("log");
System.loadLibrary("gmp");
System.loadLibrary("example");
}
private static native void load(AssetManager mgr);
private AssetManager mgr;
2014-07-10 05:28:10 +01:00
private static final String TAG = "JavaYap";
2014-06-11 19:31:24 +01:00
}
class JavaCallback extends YAPCallback
{
TextView output;
public JavaCallback( TextView outputText )
{
super();
output = outputText;
Log.i(TAG, "java callback init");
}
public void run()
{
2014-06-19 15:10:11 +01:00
Log.i(TAG, "java callback ");
System.out.println("JavaCallback.run() ");
}
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);
2014-06-11 19:31:24 +01:00
}
private static final String TAG = "JavaCallback";
}