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 -
This commit is contained in:
sekelsenmat 2012-05-10 16:18:54 +00:00
parent 0c28a14046
commit 2ae6c90244
4 changed files with 19 additions and 11 deletions

View File

@ -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;

View File

@ -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(

View File

@ -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);

View File

@ -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;