mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-04-13 05:29:26 +02:00
Android: Fixes a bug in gettextextentexpoint which improves TEdit a lot. Starts adding Positioning support in Android
git-svn-id: trunk@34494 -
This commit is contained in:
parent
672a0f0798
commit
7dc377cd18
@ -5,6 +5,7 @@
|
||||
android:versionName="1.0">
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-permission android:name="android.permission.SEND_SMS" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" />
|
||||
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
|
||||
<application android:label="@string/app_name"
|
||||
|
@ -14,11 +14,12 @@ 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;
|
||||
|
||||
public class LCLActivity extends Activity implements SensorEventListener
|
||||
public class LCLActivity extends Activity implements SensorEventListener, LocationListener
|
||||
{
|
||||
private SensorManager localSensorManager;
|
||||
|
||||
// -------------------------------------------
|
||||
// Our drawing surface
|
||||
// -------------------------------------------
|
||||
@ -100,14 +101,24 @@ public class LCLActivity extends Activity implements SensorEventListener
|
||||
|
||||
// Global objects
|
||||
LCLSurface lclsurface;
|
||||
SensorManager localSensorManager;
|
||||
|
||||
// Utility routines
|
||||
public static double[] convertFloatsToDoubles(float[] input)
|
||||
{
|
||||
if (input == null) return null;
|
||||
double[] output = new double[input.length];
|
||||
for (int i = 0; i < input.length; i++)
|
||||
{ output[i] = input[i]; }
|
||||
return output;
|
||||
}
|
||||
|
||||
// -------------------------------------------
|
||||
// Activity Events
|
||||
// -------------------------------------------
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState)
|
||||
@Override public void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
@ -154,7 +165,7 @@ public class LCLActivity extends Activity implements SensorEventListener
|
||||
public native int LCLOnKey(int kind, int keyCode, KeyEvent event, char AChar);
|
||||
public native int LCLOnTimer(Runnable timerid);
|
||||
public native int LCLOnConfigurationChanged(int ANewDPI, int ANewWidth);
|
||||
public native int LCLOnSensorChanged(int ASensorKind, float[] AValues);
|
||||
public native int LCLOnSensorChanged(int ASensorKind, double[] AValues);
|
||||
|
||||
// -------------------------------------------
|
||||
// Functions exported to the Pascal side
|
||||
@ -193,6 +204,7 @@ public class LCLActivity extends Activity implements SensorEventListener
|
||||
localpaint.setTextSize(lcltextsize);
|
||||
|
||||
float localmaxwidth = (float) lclmaxwidth;
|
||||
//Log.i("lclapp", "[LCLDoGetTextPartialWidths] lcltext="+lcltext+" localmaxwidth="+Float.toString(localmaxwidth));
|
||||
lclmaxcount = localpaint.breakText(lcltext, true, localmaxwidth, lclpartialwidths);
|
||||
}
|
||||
|
||||
@ -317,16 +329,17 @@ public class LCLActivity extends Activity implements SensorEventListener
|
||||
localInputManager.showSoftInput(lclsurface, 0);
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event)
|
||||
// SensorEventListener overrides
|
||||
|
||||
@Override public void onSensorChanged(SensorEvent event)
|
||||
{
|
||||
double[] eventValues = convertFloatsToDoubles(event.values);
|
||||
int eventKind = event.sensor.getType();
|
||||
int eventResult = LCLOnSensorChanged(eventKind, event.values);
|
||||
int eventResult = LCLOnSensorChanged(eventKind, eventValues);
|
||||
if (((eventResult | 1) != 0) && (lclsurface != null)) lclsurface.postInvalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy)
|
||||
@Override public void onAccuracyChanged(Sensor sensor, int accuracy)
|
||||
{
|
||||
}
|
||||
|
||||
@ -355,8 +368,36 @@ public class LCLActivity extends Activity implements SensorEventListener
|
||||
}
|
||||
};
|
||||
|
||||
// LocationListener overrides
|
||||
|
||||
@Override public void onLocationChanged(Location loc)
|
||||
{
|
||||
if (loc != null)
|
||||
{
|
||||
double[] positionArray = new double[2];
|
||||
positionArray[0] = loc.getLatitude();
|
||||
positionArray[1] = loc.getLongitude();
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
public void LCLDoRequestPositionInfo()
|
||||
{
|
||||
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
|
||||
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
|
||||
}
|
||||
|
||||
// -------------------------------------------
|
||||
|
@ -253,7 +253,7 @@ function Java_com_pascal_lclproject_LCLActivity_LCLOnTimer(
|
||||
function Java_com_pascal_lclproject_LCLActivity_LCLOnConfigurationChanged(
|
||||
env:PJNIEnv; this:jobject; ANewDPI, ANewWidth: jint): jint; cdecl;
|
||||
function Java_com_pascal_lclproject_LCLActivity_LCLOnSensorChanged(
|
||||
env:PJNIEnv; this:jobject; ASensorKind: jint; AValues: JFloatArray): jint; cdecl;
|
||||
env:PJNIEnv; this:jobject; ASensorKind: jint; AValues: JDoubleArray): jint; cdecl;
|
||||
function JNI_OnLoad(vm:PJavaVM;reserved:pointer):jint; cdecl;
|
||||
procedure JNI_OnUnload(vm:PJavaVM;reserved:pointer); cdecl;
|
||||
|
||||
|
@ -165,6 +165,8 @@ end;
|
||||
|
||||
procedure TCDWidgetSet.LazDeviceAPIs_RequestPositionInfo(AMethod: TLazPositionMethod);
|
||||
begin
|
||||
// Call the method
|
||||
javaEnvRef^^.CallVoidMethod(javaEnvRef, javaActivityObject, javaMethod_LCLDoRequestPositionInfo);
|
||||
end;
|
||||
|
||||
procedure TCDWidgetSet.LazDeviceAPIs_SendMessage(AMsg: TLazDeviceMessage);
|
||||
|
@ -251,9 +251,9 @@ begin
|
||||
end;
|
||||
|
||||
function Java_com_pascal_lclproject_LCLActivity_LCLOnSensorChanged(
|
||||
env:PJNIEnv; this:jobject; ASensorKind: jint; AValues: JFloatArray): jint; cdecl;
|
||||
env:PJNIEnv; this:jobject; ASensorKind: jint; AValues: JDoubleArray): jint; cdecl;
|
||||
var
|
||||
arraydata: PSingle;
|
||||
arraydata: PDouble;
|
||||
arraylen: jsize;
|
||||
lIsCopy: jboolean;
|
||||
begin
|
||||
@ -264,20 +264,32 @@ begin
|
||||
// Get the elements and length
|
||||
lIsCopy := 0;
|
||||
arraylen := javaEnvRef^^.GetArrayLength(javaEnvRef, AValues);
|
||||
arraydata := javaEnvRef^^.GetFloatArrayElements(javaEnvRef, AValues, lIsCopy);
|
||||
arraydata := javaEnvRef^^.GetDoubleArrayElements(javaEnvRef, AValues, lIsCopy);
|
||||
|
||||
// Send the data to the LCL
|
||||
if ASensorKind = 1 then
|
||||
begin
|
||||
Accelerometer.xaxis := arraydata[0];
|
||||
Accelerometer.yaxis := arraydata[1];
|
||||
Accelerometer.zaxis := arraydata[2];
|
||||
if Assigned(Accelerometer.OnSensorChanged) then
|
||||
Accelerometer.OnSensorChanged(nil);
|
||||
case ASensorKind of
|
||||
-10: // Defined by ourselves for PositionInfo
|
||||
begin
|
||||
PositionInfo.latitude := arraydata[0];
|
||||
PositionInfo.longitude := arraydata[1];
|
||||
if Assigned(PositionInfo.OnPositionRetrieved) then
|
||||
PositionInfo.OnPositionRetrieved(PositionInfo);
|
||||
end;
|
||||
1: // ACCELEROMETER
|
||||
begin
|
||||
Accelerometer.xaxis := arraydata[0];
|
||||
Accelerometer.yaxis := arraydata[1];
|
||||
Accelerometer.zaxis := arraydata[2];
|
||||
if Assigned(Accelerometer.OnSensorChanged) then
|
||||
Accelerometer.OnSensorChanged(Accelerometer);
|
||||
end;
|
||||
end;
|
||||
|
||||
// Don't forget to release it
|
||||
javaEnvRef^^.ReleaseFloatArrayElements(javaEnvRef, AValues, arraydata, 0);
|
||||
javaEnvRef^^.ReleaseDoubleArrayElements(javaEnvRef, AValues, arraydata, 0);
|
||||
|
||||
// This sends messages like Invalidate requests
|
||||
Result := eventResult;
|
||||
end;
|
||||
|
||||
const NativeMethods: array[0..7] of JNINativeMethod=
|
||||
@ -303,7 +315,7 @@ const NativeMethods: array[0..7] of JNINativeMethod=
|
||||
signature:'(II)I';
|
||||
fnPtr:@Java_com_pascal_lclproject_LCLActivity_LCLOnConfigurationChanged;),
|
||||
(name:'LCLOnSensorChanged';
|
||||
signature:'(I[F)I';
|
||||
signature:'(I[D)I';
|
||||
fnPtr:@Java_com_pascal_lclproject_LCLActivity_LCLOnSensorChanged;)
|
||||
);
|
||||
|
||||
|
@ -4077,10 +4077,10 @@ var
|
||||
lJavaString: jstring;
|
||||
lIsCopy: jboolean;
|
||||
begin
|
||||
{$ifdef VerboseCDText}
|
||||
DebugLn(Format('[WinAPI GetTextExtentExPoint] DC=%x javaEnvRef=%x Str=%s',
|
||||
[DC, PtrInt(javaEnvRef), StrPas(Str)]));
|
||||
{$endif}
|
||||
{.$ifdef VerboseCDText}
|
||||
DebugLn(Format('[WinAPI GetTextExtentExPoint] DC=%x javaEnvRef=%x Str=%s MaxWidth=%d',
|
||||
[DC, PtrInt(javaEnvRef), StrPas(Str), MaxWidth]));
|
||||
{.$endif}
|
||||
// Result := inherited GetTextExtentExPoint(DC, Str, Count, MaxWidth, MaxCount, PartialWidths, Size);
|
||||
|
||||
Result := False;
|
||||
@ -4102,10 +4102,13 @@ begin
|
||||
javaEnvRef^^.SetIntField(javaEnvRef, javaActivityObject, javaField_lclmaxwidth, MaxWidth);
|
||||
|
||||
// Call the method
|
||||
javaEnvRef^^.CallVoidMethod(javaEnvRef, javaActivityObject, javaMethod_LCLDoGetTextBounds);
|
||||
javaEnvRef^^.CallVoidMethod(javaEnvRef, javaActivityObject, javaMethod_LCLDoGetTextPartialWidths);
|
||||
|
||||
// Read the output
|
||||
lMaxCount := javaEnvRef^^.GetIntField(javaEnvRef, javaActivityObject, javaField_lclmaxcount);
|
||||
{.$ifdef VerboseCDText}
|
||||
DebugLn(Format(':[WinAPI GetTextExtentExPoint] MaxCount=%d', [lMaxCount]));
|
||||
{.$endif}
|
||||
|
||||
if MaxCount <> nil then MaxCount^ := lMaxCount;
|
||||
|
||||
@ -4118,7 +4121,13 @@ begin
|
||||
arraydata := javaEnvRef^^.GetFloatArrayElements(javaEnvRef, arraydata_obj, lIsCopy);
|
||||
|
||||
for i := 0 to lMaxCount-1 do
|
||||
begin
|
||||
PartialWidths[i] := Round(arraydata[i]);
|
||||
{.$ifdef VerboseCDText}
|
||||
DebugLn(Format(':[WinAPI GetTextExtentExPoint] i=%d PartialWidth=%d',
|
||||
[i, PartialWidths[i]]));
|
||||
{.$endif}
|
||||
end;
|
||||
|
||||
// Don't forget to release it
|
||||
javaEnvRef^^.ReleaseFloatArrayElements(javaEnvRef, arraydata_obj, arraydata, 0);
|
||||
|
Loading…
Reference in New Issue
Block a user