package com.pascal.lcltest; import android.app.*; import android.content.*; import android.os.*; import android.widget.*; import android.util.*; import android.graphics.*; import android.text.*; import android.view.*; import android.view.inputmethod.*; import android.view.MenuItem.*; import android.content.res.Configuration; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.telephony.SmsManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import java.util.*; public class LCLActivity extends Activity implements SensorEventListener, LocationListener { // ------------------------------------------- // Input connection to get character events // ------------------------------------------- private class LCLInputConnection extends BaseInputConnection { private SpannableStringBuilder _editable; View _lclView; public LCLInputConnection(View targetView, boolean fullEditor) { super(targetView, fullEditor); _lclView = (View) targetView; } /* public Editable getEditable() { if (_editable == null) { _editable = (SpannableStringBuilder) Editable.Factory.getInstance() .newEditable("Placeholder"); } return _editable; } This crashes in HTC */ // This method sends a text to be added at the current cursor position @Override public boolean commitText(CharSequence text, int newCursorPosition) { //if (_editable != null) _editable.append(text); Log.v("lclproject", "LCLInputConnection.commitText =" + text + " newCursorPosition=" + Integer.toString(newCursorPosition)); // Send each character of the string int eventResult, i; for (i = 0; i= 0) builder.setPositiveButton(lclbutton1str, dialogClickListener); if (lclbutton2 >= 0) builder.setNeutralButton(lclbutton2str, dialogClickListener); if (lclbutton3 >= 0) builder.setNegativeButton(lclbutton3str, dialogClickListener); builder.show().setOnCancelListener(dialogCancelListener); }; private Handler LocalHandler = new Handler(); private class LCLRunnable implements Runnable { public boolean Destroyed = false; public void run() { int eventResult = LCLOnTimer(this); ProcessEventResult(eventResult); if (this.Destroyed == false) LocalHandler.postDelayed(this, lcltimerinterval); } }; // input: int lcltimerinterval in milliseconds // output: Runnable lcltimerid public void LCLDoCreateTimer() { lcltimerid = new LCLRunnable(); LocalHandler.removeCallbacks(lcltimerid); LocalHandler.postDelayed(lcltimerid, lcltimerinterval); }; // input: Runnable lcltimerid public void LCLDoDestroyTimer() { LocalHandler.removeCallbacks(lcltimerid); ((LCLRunnable) lcltimerid).Destroyed = true; }; public void LCLDoHideVirtualKeyboard() { InputMethodManager localInputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); localInputManager.hideSoftInputFromWindow(lclsurface.getWindowToken(), 0); }; public void LCLDoShowVirtualKeyboard() { InputMethodManager localInputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); localInputManager.showSoftInput(lclsurface, 0); }; // SensorEventListener overrides @Override public void onSensorChanged(SensorEvent event) { double[] eventValues = convertFloatsToDoubles(event.values); int eventKind = event.sensor.getType(); int eventResult = LCLOnSensorChanged(eventKind, eventValues); if (((eventResult | 1) != 0) && (lclsurface != null)) lclsurface.postInvalidate(); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void LCLDoStartReadingAccelerometer() { localSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); localSensorManager.registerListener(this, localSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); }; public void LCLDoStopReadingAccelerometer() { localSensorManager.unregisterListener(this); }; // input: String lcldestination, String lcltext (Body) public void LCLDoSendMessage() { if (lclkind == 1) { PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0); //---when the SMS has been sent--- registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { double[] statusArray = new double[1]; int eventResult = 0; switch (getResultCode()) { case Activity.RESULT_OK: statusArray[0] = 1.0; eventResult = LCLOnSensorChanged(-11, statusArray); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: statusArray[0] = 2.0; eventResult = LCLOnSensorChanged(-11, statusArray); break; case SmsManager.RESULT_ERROR_NO_SERVICE: statusArray[0] = 3.0; eventResult = LCLOnSensorChanged(-11, statusArray); break; case SmsManager.RESULT_ERROR_NULL_PDU: statusArray[0] = 2.0; eventResult = LCLOnSensorChanged(-11, statusArray); break; case SmsManager.RESULT_ERROR_RADIO_OFF: statusArray[0] = 5.0; eventResult = LCLOnSensorChanged(-11, statusArray); break; } ProcessEventResult(eventResult); } }, new IntentFilter("SMS_SENT")); //---when the SMS has been delivered--- registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { double[] statusArray = new double[1]; int eventResult = 0; switch (getResultCode()) { case Activity.RESULT_OK: statusArray[0] = 10.0; eventResult = LCLOnSensorChanged(-11, statusArray); break; case Activity.RESULT_CANCELED: statusArray[0] = 11.0; eventResult = LCLOnSensorChanged(-11, statusArray); break; } ProcessEventResult(eventResult); } }, new IntentFilter("SMS_DELIVERED")); // SMS sending seams to cause an awful lot of exceptions // See: http://stackoverflow.com/questions/4580952/why-do-i-get-nullpointerexception-when-sending-an-sms-on-an-htc-desire-or-what // See: http://code.google.com/p/android/issues/detail?id=3718 try { SmsManager sms = SmsManager.getDefault(); Log.i("lclapp", "[LCLDoSendMessage] lcldestination="+lcldestination+" lcltext="+lcltext); ArrayList parts = sms.divideMessage(lcltext); //sms.sendMultipartTextMessage(lcldestination, null, parts, sentPI, deliveredPI); sms.sendTextMessage(lcldestination, null, lcltext, sentPI, deliveredPI); } catch (Exception e) { } } }; // LocationListener overrides @Override public void onLocationChanged(Location loc) { if (loc != null) { double[] positionArray = new double[6]; positionArray[0] = loc.getLatitude(); positionArray[1] = loc.getLongitude(); positionArray[2] = loc.getAltitude(); positionArray[3] = (double)loc.getAccuracy(); positionArray[4] = (double)loc.getSpeed(); positionArray[5] = (double)loc.getTime(); int eventResult = LCLOnSensorChanged(-10, positionArray); if (((eventResult | 1) != 0) && (lclsurface != null)) lclsurface.postInvalidate(); } } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } // input: int lclkind public void LCLDoRequestPositionInfo() { LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); switch (lclkind) { case 1: mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); case 2: mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); default: Log.i("lclapp", "[LCLDoRequestPositionInfo] Wrong lclkind parameter"); } } // Prepare dialog callbacks // for TCDComboBox public void LCLDoPrepareSelectItemDialog(CharSequence[] items, int selected, AlertDialog.Builder dialog) { //Log.i("lclapp", "LCLDoPrepareSelectItemDialog"); dialog.setSingleChoiceItems(items, selected, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Log.i("lclapp", "LCLDoPrepareSelectItemDialog.onClick"); LCLOnMessageBoxFinished(which, 1); dialog.dismiss(); } }); } // ------------------------------------------- // Fields exported to the Pascal side for easier data communication // ------------------------------------------- public String lcltext; public String lcltitle; public String lclbutton1str; public String lclbutton2str; public String lclbutton3str; // public int lclwidth; public int lclheight; public int lclbutton1; public int lclbutton2; public int lclbutton3; public Bitmap lclbitmap; // public int lcltextsize; public int lcltextascent; public int lcltextbottom; public int lcltextdescent; public int lcltextleading; public int lcltexttop; public int lclmaxwidth; public int lclmaxcount; public float[] lclpartialwidths; // public int lcltimerinterval; public Runnable lcltimerid; // public int lclxdpi; public int lclydpi; public int lclformwidth; public int lclformheight; public int lclscreenwidth; public int lclscreenheight; // for LazDeviceAPIs public String lcldestination; public int lclkind; // for the menus public String[] lclmenu_captions = new String[6]; public boolean flagIsMenuOpen = false; static { try { Log.i("lclapp", "Trying to load liblclapp.so"); System.loadLibrary("lclapp"); } catch(UnsatisfiedLinkError ule) { Log.e("lclapp", "WARNING: Could not load liblclapp.so"); ule.printStackTrace(); } } }