| 
									
										
										
										
											2014-05-28 00:07:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-10 00:03:02 +00:00
										 |  |  | /**** using sqlite
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | For example,the following:
 | 
					
						
							| 
									
										
										
										
											2015-02-10 00:03:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | import android.database.sqlite.SQLiteDatabase;
 | 
					
						
							| 
									
										
										
										
											2015-02-10 00:03:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | should be replaced with:
 | 
					
						
							| 
									
										
										
										
											2015-02-10 00:03:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | import org.sqlite.database.sqlite.SQLiteDatabase;
 | 
					
						
							| 
									
										
										
										
											2015-02-10 00:03:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | 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:
 | 
					
						
							| 
									
										
										
										
											2015-02-10 00:03:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | org.sqlite.database.SQLException
 | 
					
						
							|  |  |  | org.sqlite.database.DatabaseErrorHandler
 | 
					
						
							| 
									
										
										
										
											2015-02-10 00:03:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | instead of:
 | 
					
						
							| 
									
										
										
										
											2015-02-10 00:03:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | android.database.SQLException
 | 
					
						
							|  |  |  | android.database.DatabaseErrorHandler
 | 
					
						
							| 
									
										
										
										
											2015-02-10 00:03:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | Aside from namespace changes, there are other differences from the stock Android interface that applications need to be aware of:
 | 
					
						
							| 
									
										
										
										
											2015-02-10 00:03:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | 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.
 | 
					
						
							| 
									
										
										
										
											2015-02-10 00:03:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ****/
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 02:52:50 +01:00
										 |  |  | import pt.up.fc.dcc.Yap.*;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-28 00:07:50 +01:00
										 |  |  | 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;
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | import  android.content.pm.PackageManager.NameNotFoundException;
 | 
					
						
							| 
									
										
										
										
											2014-06-04 22:07:37 +01:00
										 |  |  | 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-15 18:57:48 -05: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-15 18:57:48 -05:00
										 |  |  | import org.sqlite.database.DatabaseErrorHandler;
 | 
					
						
							| 
									
										
										
										
											2016-04-05 02:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-15 18:57:48 -05:00
										 |  |  | class DoNotDeleteErrorHandler implements DatabaseErrorHandler {
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |     private static final String TAG = "DoNotDeleteErrorHandler";
 | 
					
						
							|  |  |  |     public void onCorruption(SQLiteDatabase dbObj) {
 | 
					
						
							|  |  |  |         Log.e(TAG, "Corruption reported by sqlite on database: " + dbObj.getPath());
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2014-07-15 18:57:48 -05:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-04-05 02:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-09 23:28:10 -05:00
										 |  |  | public class JavaYap extends Activity
 | 
					
						
							| 
									
										
										
										
											2014-05-28 00:07:50 +01:00
										 |  |  | {
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |     TextView outputText = null;
 | 
					
						
							|  |  |  |     ScrollView scroller = null;
 | 
					
						
							|  |  |  |     YAPEngine eng = null;
 | 
					
						
							|  |  |  |     EditText text;
 | 
					
						
							|  |  |  |     String str;
 | 
					
						
							|  |  |  |     String buf;
 | 
					
						
							|  |  |  |     YAPQuery q;
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     Boolean running = false, compute = true;
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |     int i=1;
 | 
					
						
							|  |  |  |     YAPListTerm vs0;
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     private AssetManager mgr;
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void runQuery(String str, Boolean more)
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         try
 | 
					
						
							|  |  |  |         {
 | 
					
						
							|  |  |  |             // check if at initial query
 | 
					
						
							|  |  |  |             if (!running) {
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                             if (BuildConfig.DEBUG) {
 | 
					
						
							|  |  |  |                                 Log.i(TAG, "query "+str);
 | 
					
						
							|  |  |  |                             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-23 07:27:10 +00:00
										 |  |  | 	      q = eng.query( str );
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |                 // get the uninstantiated query variables.
 | 
					
						
							|  |  |  |                 vs0 = q.namedVars();
 | 
					
						
							|  |  |  |                 running = true;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             // start computing
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |             compute = true;
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (BuildConfig.DEBUG) {
 | 
					
						
							|  |  |  |                 Log.i(TAG, "onQueryButtonClick called");
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Boolean rc = true;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // text.setText("");
 | 
					
						
							|  |  |  |             if (vs0.nil()) {
 | 
					
						
							|  |  |  |                 if (BuildConfig.DEBUG) {
 | 
					
						
							|  |  |  |                     Log.i(TAG, "q0=\n");
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |                 if (compute && (rc = q.next())) {
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |                     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()) ) {
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |                     YAPListTerm vs = q.namedVars();
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |                     while(!vs.nil()){
 | 
					
						
							|  |  |  |                         if (BuildConfig.DEBUG) {
 | 
					
						
							|  |  |  |                             Log.i(TAG, "q= "+vs0.text()+"\n");
 | 
					
						
							|  |  |  |                         }
 | 
					
						
							|  |  |  |                         YAPTerm eq = vs.car();
 | 
					
						
							|  |  |  |                         //outputText.append(Integer.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) {
 | 
					
						
							|  |  |  |                 q.close();
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |                 compute = true;
 | 
					
						
							|  |  |  |                 running = false;
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |             }
 | 
					
						
							|  |  |  |         } catch(Exception e){
 | 
					
						
							|  |  |  |             outputText.append("Exception thrown  :" + e);
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |             q.close();
 | 
					
						
							|  |  |  |             compute = true;
 | 
					
						
							|  |  |  |             running = false;
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +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;
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |             mgr = getResources().getAssets();
 | 
					
						
							|  |  |  |             Log.i(TAG, "mgr0=" +mgr + " " + s);
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |             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) {
 | 
					
						
							| 
									
										
										
										
											2015-03-23 07:27:10 +00:00
										 |  |  | 	  Log.i(TAG, "callback done");
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +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() {
 | 
					
						
							| 
									
										
										
										
											2015-03-23 07:27:10 +00:00
										 |  |  | 	      scroller.fullScroll(ScrollView.FOCUS_DOWN);
 | 
					
						
							|  |  |  | 	      if (running) {
 | 
					
						
							|  |  |  | 		q.close();
 | 
					
						
							|  |  |  | 		q = null;
 | 
					
						
							|  |  |  | 	      }
 | 
					
						
							|  |  |  | 	      running = false;
 | 
					
						
							|  |  |  | 	      text.setText("");
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |             }
 | 
					
						
							|  |  |  |         });
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         });
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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() {
 | 
					
						
							|  |  |  |                 scroller.fullScroll(ScrollView.FOCUS_DOWN);
 | 
					
						
							|  |  |  |                 str = text.getText().toString();
 | 
					
						
							| 
									
										
										
										
											2015-03-23 07:27:10 +00:00
										 |  |  |                 outputText.append("?- " + str + "\n\n");
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |                 Log.i(TAG, "onAllButtonClick "+str + "\n");
 | 
					
						
							|  |  |  |                 runQuery(str, true);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         });
 | 
					
						
							| 
									
										
										
										
											2015-03-11 22:18:00 +00:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** static constructor */
 | 
					
						
							|  |  |  |     static {
 | 
					
						
							|  |  |  |         System.loadLibrary("android");
 | 
					
						
							|  |  |  |         System.loadLibrary("log");
 | 
					
						
							|  |  |  |         System.loadLibrary("gmp");
 | 
					
						
							| 
									
										
										
										
											2016-04-05 23:56:25 +01:00
										 |  |  |         System.loadLibrary("sqlite3");
 | 
					
						
							|  |  |  |         System.loadLibrary("utf8proc");
 | 
					
						
							|  |  |  |         System.loadLibrary("Yap");
 | 
					
						
							|  |  |  |         System.loadLibrary("Yap++");
 | 
					
						
							| 
									
										
										
										
											2016-04-05 02:52:50 +01:00
										 |  |  |         System.loadLibrary("YapDroid");
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static native void load(AssetManager mgr);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static final String TAG = "JavaYap";
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class JavaCallback extends YAPCallback
 | 
					
						
							|  |  |  | {
 | 
					
						
							| 
									
										
										
										
											2015-03-16 17:25:09 +00:00
										 |  |  |     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);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static final String TAG = "JavaCallback";
 | 
					
						
							| 
									
										
										
										
											2014-05-28 00:07:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | }
 |