From 2ae6c90244ccda245d3a2afc8bbb03df67d7afd0 Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Thu, 10 May 2012 16:18:54 +0000 Subject: [PATCH] LCL-CustomDrawn-Android: Further improves the improved timer. Starts adding a list to make sure nothing is GCed, uses global reference and adds a new parameter to in the future use only the list ID instead of the raw reference git-svn-id: trunk@37246 - --- .../src/com/pascal/lcltest/LCLActivity.java | 9 +++++++-- lcl/interfaces/customdrawn/customdrawnint.pas | 2 +- .../customdrawn/customdrawnobject_android.inc | 18 ++++++++++-------- lcl/interfaces/customdrawn/customdrawnproc.pas | 1 + 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/examples/androidlcl/android/src/com/pascal/lcltest/LCLActivity.java b/examples/androidlcl/android/src/com/pascal/lcltest/LCLActivity.java index 402dfbe085..f84857ad0f 100755 --- a/examples/androidlcl/android/src/com/pascal/lcltest/LCLActivity.java +++ b/examples/androidlcl/android/src/com/pascal/lcltest/LCLActivity.java @@ -315,7 +315,7 @@ public class LCLActivity extends Activity implements SensorEventListener, Locati public native int LCLOnCreate(Activity lclactivity); public native int LCLOnMessageBoxFinished(int Result, int DialogType); public native int LCLOnKey(int kind, int keyCode, KeyEvent event, int AChar); - public native int LCLOnTimer(Runnable timerid); + public native int LCLOnTimer(Runnable timerid, int timeridindex); public native int LCLOnConfigurationChanged(int ANewDPI, int ANewWidth); public native int LCLOnSensorChanged(int ASensorKind, double[] AValues); public native int LCLOnMenuAction(int kind, int itemIndex); @@ -447,7 +447,8 @@ public class LCLActivity extends Activity implements SensorEventListener, Locati public void run() { - int eventResult = LCLOnTimer(this); + int lcltimeridindex = lcltimerids.indexOf(this); + int eventResult = LCLOnTimer(this, lcltimeridindex); ProcessEventResult(eventResult); if (this.Destroyed == false) LocalHandler.postDelayed(this, lcltimerinterval); } @@ -461,6 +462,8 @@ public class LCLActivity extends Activity implements SensorEventListener, Locati LocalHandler.removeCallbacks(lcltimerid); LocalHandler.postDelayed(lcltimerid, lcltimerinterval); + + lcltimerids.add(lcltimerid); }; // input: Runnable lcltimerid @@ -468,6 +471,7 @@ public class LCLActivity extends Activity implements SensorEventListener, Locati { LocalHandler.removeCallbacks(lcltimerid); ((LCLRunnable) lcltimerid).Destroyed = true; + lcltimerids.remove(lcltimerids.indexOf(lcltimerid)); }; public void LCLDoHideVirtualKeyboard() @@ -679,6 +683,7 @@ public class LCLActivity extends Activity implements SensorEventListener, Locati // public int lcltimerinterval; public Runnable lcltimerid; + public List lcltimerids = new ArrayList(); // To keep the references alive, avoids a wrong GC // public int lclxdpi; public int lclydpi; diff --git a/lcl/interfaces/customdrawn/customdrawnint.pas b/lcl/interfaces/customdrawn/customdrawnint.pas index 2748f98b9d..485806248d 100644 --- a/lcl/interfaces/customdrawn/customdrawnint.pas +++ b/lcl/interfaces/customdrawn/customdrawnint.pas @@ -351,7 +351,7 @@ function Java_com_pascal_lclproject_LCLActivity_LCLOnKey( env:PJNIEnv; this:jobject; AKind: jint; AKeyCode: jint; AEvent: jobject; AChar: jint): jint; cdecl; function Java_com_pascal_lclproject_LCLActivity_LCLOnTimer( - env:PJNIEnv; this:jobject; ATimer: jobject): jint; cdecl; + env:PJNIEnv; this:jobject; ATimer: jobject; ATimerIDIndex: jint): jint; cdecl; function Java_com_pascal_lclproject_LCLActivity_LCLOnConfigurationChanged( env:PJNIEnv; this:jobject; ANewDPI, ANewWidth: jint): jint; cdecl; function Java_com_pascal_lclproject_LCLActivity_LCLOnSensorChanged( diff --git a/lcl/interfaces/customdrawn/customdrawnobject_android.inc b/lcl/interfaces/customdrawn/customdrawnobject_android.inc index 1df996a56f..4e69d49cd4 100644 --- a/lcl/interfaces/customdrawn/customdrawnobject_android.inc +++ b/lcl/interfaces/customdrawn/customdrawnobject_android.inc @@ -227,7 +227,7 @@ begin end; function Java_com_pascal_lclproject_LCLActivity_LCLOnTimer( - env:PJNIEnv; this:jobject; ATimer: jobject): jint; cdecl; + env:PJNIEnv; this:jobject; ATimer: jobject; ATimerIDIndex: jint): jint; cdecl; var lTimer: TCDTimer; begin @@ -458,7 +458,7 @@ const NativeMethods: array[0..8] of JNINativeMethod= signature:'(IILandroid/view/KeyEvent;I)I'; fnPtr:@Java_com_pascal_lclproject_LCLActivity_LCLOnKey;), (name:'LCLOnTimer'; - signature:'(Ljava/lang/Runnable;)I'; + signature:'(Ljava/lang/Runnable;I)I'; fnPtr:@Java_com_pascal_lclproject_LCLActivity_LCLOnTimer;), (name:'LCLOnConfigurationChanged'; signature:'(II)I'; @@ -1385,7 +1385,8 @@ begin // Read the output lTimerObject := javaEnvRef^^.GetObjectField(javaEnvRef, javaActivityObject, javaField_lcltimerid); lGlobalTimerObject := javaEnvRef^^.NewGlobalRef(javaEnvRef, lTimerObject); - lTimer.NativeHandle := PtrInt(lGlobalTimerObject); + lTimer.NativeHandle := PtrInt(lTimerObject); + lTimer.NativeGlobalReference := PtrInt(lGlobalTimerObject); // Add it to our list AddTimer(lTimer); @@ -1401,19 +1402,20 @@ function TCDWidgetSet.DestroyTimer(TimerHandle: THandle) : boolean; var lTimer: TCDTimer; begin - DebugLn(Format('[TCDWidgetSet.DestroyTimer] TimerHandle=%x', [PtrInt(TimerHandle)])); - Result := False; - lTimer := TCDTimer(TimerHandle); + DebugLn(Format('[TCDWidgetSet.DestroyTimer] TimerHandle=%x lTimer.NativeHandle=%x lTimer.NativeGlobalReference=%x', + [PtrInt(TimerHandle), PtrInt(lTimer.NativeHandle), PtrInt(lTimer.NativeGlobalReference)])); + Result := False; + if lTimer = nil then Exit; // Prepare the input - javaEnvRef^^.SetObjectField(javaEnvRef, javaActivityObject, javaField_lcltimerid, jobject(lTimer.NativeHandle)); + javaEnvRef^^.SetObjectField(javaEnvRef, javaActivityObject, javaField_lcltimerid, jobject(lTimer.NativeGlobalReference)); // Call the method javaEnvRef^^.CallVoidMethod(javaEnvRef, javaActivityObject, javaMethod_LCLDoDestroyTimer); - javaEnvRef^^.DeleteGlobalRef(javaEnvRef, JObject(lTimer.NativeHandle)); + javaEnvRef^^.DeleteGlobalRef(javaEnvRef, JObject(lTimer.NativeGlobalReference)); // Remove from the list RemoveTimer(lTimer); diff --git a/lcl/interfaces/customdrawn/customdrawnproc.pas b/lcl/interfaces/customdrawn/customdrawnproc.pas index 77dfc9a3b8..0f7fb7c4bd 100644 --- a/lcl/interfaces/customdrawn/customdrawnproc.pas +++ b/lcl/interfaces/customdrawn/customdrawnproc.pas @@ -111,6 +111,7 @@ type TCDTimer = class public NativeHandle: PtrInt; // The X11 timer uses this to store the current time which is summed up to the next interval + NativeGlobalReference: PtrInt; // Utilized in Android to store the global JNI reference Interval: integer; TimerFunc: TWSTimerProc; end;