Adds Accelerometer stopping in Android (and to the general structure) and starts implementing SMS sending

git-svn-id: trunk@34486 -
This commit is contained in:
sekelsenmat 2011-12-29 00:24:15 +00:00
parent 97fac1bc4c
commit fdc8606135
15 changed files with 210 additions and 32 deletions

View File

@ -4,6 +4,7 @@
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<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"

View File

@ -13,6 +13,7 @@ import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.telephony.SmsManager;
public class LCLActivity extends Activity implements SensorEventListener
{
@ -342,6 +343,22 @@ public class LCLActivity extends Activity implements SensorEventListener
localSensorManager.unregisterListener(this);
};
// input: String lcldestination, String lcltext (Body)
public void LCLDoSendMessage()
{
if (lclkind == 1)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, Object.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(lcldestination, null, lcltext, pi, null);
}
};
public void LCLDoRequestPositionInfo()
{
}
// -------------------------------------------
// Fields exported to the Pascal side for easier data communication
// -------------------------------------------
@ -377,6 +394,9 @@ public class LCLActivity extends Activity implements SensorEventListener
public int lclformheight;
public int lclscreenwidth;
public int lclscreenheight;
// for LazDeviceAPIs
public String lcldestination;
public int lclkind;
static
{

View File

@ -16,20 +16,20 @@ object Form2: TForm2
OnClick = Button1Click
TabOrder = 0
end
object Edit1: TEdit
Left = 12
object textDest: TEdit
Left = 8
Height = 25
Top = 32
Top = 56
Width = 80
OnExit = Edit1Exit
OnKeyDown = Edit1KeyDown
OnKeyUp = Edit1KeyUp
OnUTF8KeyPress = Edit1UTF8KeyPress
OnExit = textDestExit
OnKeyDown = textDestKeyDown
OnKeyUp = textDestKeyUp
OnUTF8KeyPress = textDestUTF8KeyPress
TabOrder = 1
Text = 'Edit1'
Text = 'textDest'
end
object Image1: TImage
Left = 96
Left = 102
Height = 138
Top = 32
Width = 210
@ -1783,13 +1783,13 @@ object Form2: TForm2
2E202E202E202E202E202E20227D3B0A
}
end
object Button2: TButton
object btnStartAccel: TButton
Left = 8
Height = 25
Top = 194
Width = 304
Caption = 'Read Accelerometer'
OnClick = Button2Click
Width = 112
Caption = 'Read Accel.'
OnClick = btnStartAccelClick
TabOrder = 2
end
object labelSensorData: TLabel
@ -1800,4 +1800,55 @@ object Form2: TForm2
Caption = 'Sensor Data'
ParentColor = False
end
object btnStopAccel: TButton
Left = 128
Height = 25
Top = 194
Width = 91
Caption = 'Stop Accel.'
OnClick = btnStopAccelClick
TabOrder = 3
end
object btnGetPos: TButton
Left = 224
Height = 25
Top = 194
Width = 88
Caption = 'Get Position'
OnClick = btnGetPosClick
TabOrder = 4
end
object Label1: TLabel
Left = 10
Height = 18
Top = 88
Width = 63
Caption = 'SMS Text:'
ParentColor = False
end
object textBody: TEdit
Left = 8
Height = 25
Top = 104
Width = 80
TabOrder = 5
Text = 'textBody'
end
object btnSendSMS: TButton
Left = 8
Height = 25
Top = 136
Width = 75
Caption = 'Send SMS'
OnClick = btnSendSMSClick
TabOrder = 6
end
object Label2: TLabel
Left = 8
Height = 18
Top = 32
Width = 53
Caption = 'Number'
ParentColor = False
end
end

View File

@ -14,21 +14,31 @@ type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
btnStartAccel: TButton;
btnStopAccel: TButton;
btnGetPos: TButton;
btnSendSMS: TButton;
textDest: TEdit;
textBody: TEdit;
Image1: TImage;
Label1: TLabel;
Label2: TLabel;
labelSensorData: TLabel;
procedure btnSendSMSClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Edit1Exit(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure Edit1UTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
procedure btnStartAccelClick(Sender: TObject);
procedure btnStopAccelClick(Sender: TObject);
procedure btnGetPosClick(Sender: TObject);
procedure textDestExit(Sender: TObject);
procedure textDestKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure textDestKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure textDestUTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
private
{ private declarations }
public
{ public declarations }
procedure HandleAccelerometerChanged(Sender: TObject);
procedure HandlePositionRetrieved(Sender: TObject);
end;
var
@ -45,29 +55,50 @@ begin
Hide;
end;
procedure TForm2.Button2Click(Sender: TObject);
procedure TForm2.btnSendSMSClick(Sender: TObject);
var
lMessage: TLazDeviceMessage;
begin
lMessage := Messaging.CreateMessage();
lMessage.Body := textBody.Text;
lMessage.destinationAddress.Text := textDest.Text;
Messaging.SendMessage(lMessage);
end;
procedure TForm2.btnStartAccelClick(Sender: TObject);
begin
Accelerometer.OnSensorChanged := @HandleAccelerometerChanged;
Accelerometer.StartReadingAccelerometerData();
end;
procedure TForm2.Edit1Exit(Sender: TObject);
procedure TForm2.btnStopAccelClick(Sender: TObject);
begin
Accelerometer.StopReadingAccelerometerData();
end;
procedure TForm2.btnGetPosClick(Sender: TObject);
begin
PositionInfo.RequestPositionInfo([]);
PositionInfo.OnPositionRetrieved := @HandlePositionRetrieved;
end;
procedure TForm2.textDestExit(Sender: TObject);
begin
DebugLn('[Edit1Exit]');
end;
procedure TForm2.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
procedure TForm2.textDestKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
);
begin
DebugLn('[Edit1KeyDown] Key=' + DbgsVKCode(Key));
end;
procedure TForm2.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure TForm2.textDestKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
DebugLn('[Edit1KeyUp] Key=' + DbgsVKCode(Key));
end;
procedure TForm2.Edit1UTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
procedure TForm2.textDestUTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
begin
DebugLn('[Edit1UTF8KeyPress] Char=' + UTF8Key);
end;
@ -79,5 +110,12 @@ begin
DebugLn(labelSensorData.Caption);
end;
procedure TForm2.HandlePositionRetrieved(Sender: TObject);
begin
labelSensorData.Caption := Format('latitude=%f longitude=%f',
[PositionInfo.latitude, PositionInfo.longitude]);
DebugLn(labelSensorData.Caption);
end;
end.

View File

@ -532,6 +532,11 @@ begin
end;
procedure TWidgetSet.LazDeviceAPIs_StopReadingAccelerometerData();
begin
end;
function TWidgetSet.MoveWindowOrgEx(dc: hdc; dX,dY: Integer): boolean;
var
P: TPoint;

View File

@ -365,6 +365,11 @@ begin
Widgetset.LazDeviceAPIs_StartReadingAccelerometerData();
end;
procedure LazDeviceAPIs_StopReadingAccelerometerData();
begin
Widgetset.LazDeviceAPIs_StopReadingAccelerometerData();
end;
function MoveWindowOrgEx(dc: hdc; dX,dY: Integer): boolean;
begin
Result := WidgetSet.MoveWindowOrgEx(DC, dX, dY);

View File

@ -96,6 +96,7 @@ function IsDesignerDC(WindowHandle: HWND; DC: HDC): Boolean; {$IFDEF IF_BASE_MEM
procedure LazDeviceAPIs_RequestPositionInfo(AMethod: TLazPositionMethod); {$IFDEF IF_BASE_MEMBER}virtual;{$ENDIF}
procedure LazDeviceAPIs_SendMessage(AMsg: TLazDeviceMessage); {$IFDEF IF_BASE_MEMBER}virtual;{$ENDIF}
procedure LazDeviceAPIs_StartReadingAccelerometerData(); {$IFDEF IF_BASE_MEMBER}virtual;{$ENDIF}
procedure LazDeviceAPIs_StopReadingAccelerometerData(); {$IFDEF IF_BASE_MEMBER}virtual;{$ENDIF}
function MoveWindowOrgEx(dc : hdc; dX,dY : Integer): boolean; {$IFDEF IF_BASE_MEMBER}virtual;{$ENDIF}

View File

@ -297,6 +297,9 @@ var
javaField_lclformheight: JfieldID=nil;
javaField_lclscreenwidth: JfieldID=nil;
javaField_lclscreenheight: JfieldID=nil;
// For LazDeviceAPIs
javaField_lcldestination: JfieldID=nil;
javaField_lclkind: JfieldID=nil;
// Methods of our Activity
javaMethod_LCLDoGetTextBounds: jmethodid = nil;
@ -308,6 +311,9 @@ var
javaMethod_LCLDoHideVirtualKeyboard: jmethodid = nil;
javaMethod_LCLDoShowVirtualKeyboard: jmethodid = nil;
javaMethod_LCLDoStartReadingAccelerometer: jmethodid = nil;
javaMethod_LCLDoStopReadingAccelerometer: jmethodid = nil;
javaMethod_LCLDoSendMessage: jmethodid = nil;
javaMethod_LCLDoRequestPositionInfo: jmethodid = nil;
// This is utilized to store the information such as invalidate requests in events
eventResult: jint;

View File

@ -165,12 +165,25 @@ end;
procedure TCDWidgetSet.LazDeviceAPIs_RequestPositionInfo(AMethod: TLazPositionMethod);
begin
end;
procedure TCDWidgetSet.LazDeviceAPIs_SendMessage(AMsg: TLazDeviceMessage);
var
lJavaString: jstring;
lStr: String;
begin
// Prepare the input
// String fields
lStr := AMsg.Body;
lJavaString :=javaEnvRef^^.NewStringUTF(javaEnvRef, PChar(lStr));
javaEnvRef^^.SetObjectField(javaEnvRef, javaActivityObject, JavaField_lcltext, lJavaString);
lStr := AMsg.destinationAddress.Text;
lJavaString :=javaEnvRef^^.NewStringUTF(javaEnvRef, PChar(lStr));
javaEnvRef^^.SetObjectField(javaEnvRef, javaActivityObject, JavaField_lcldestination, lJavaString);
// Message type
javaEnvRef^^.SetIntField(javaEnvRef, javaActivityObject, JavaField_lclkind, 1);
// Call the method
javaEnvRef^^.CallVoidMethod(javaEnvRef, javaActivityObject, javaMethod_LCLDoSendMessage);
end;
procedure TCDWidgetSet.LazDeviceAPIs_StartReadingAccelerometerData();
@ -179,6 +192,12 @@ begin
javaEnvRef^^.CallVoidMethod(javaEnvRef, javaActivityObject, javaMethod_LCLDoStartReadingAccelerometer);
end;
procedure TCDWidgetSet.LazDeviceAPIs_StopReadingAccelerometerData();
begin
// Call the method
javaEnvRef^^.CallVoidMethod(javaEnvRef, javaActivityObject, javaMethod_LCLDoStopReadingAccelerometer);
end;
function TCDWidgetSet.PromptUser(const DialogCaption : string;
const DialogMessage : string;
DialogType : LongInt;

View File

@ -128,6 +128,11 @@ begin
end;
procedure TCDWidgetSet.LazDeviceAPIs_StopReadingAccelerometerData();
begin
end;
{------------------------------------------------------------------------------
Function: PromptUser
Params:

View File

@ -129,6 +129,11 @@ begin
end;
procedure TCDWidgetSet.LazDeviceAPIs_StopReadingAccelerometerData();
begin
end;
{------------------------------------------------------------------------------
Function: PromptUser
Params:

View File

@ -129,6 +129,11 @@ begin
end;
procedure TCDWidgetSet.LazDeviceAPIs_StopReadingAccelerometerData();
begin
end;
{------------------------------------------------------------------------------
Function: PromptUser
Params:

View File

@ -59,6 +59,7 @@ function IsDesignerDC(WindowHandle: HWND; DC: HDC): Boolean; override;*)
procedure LazDeviceAPIs_RequestPositionInfo(AMethod: TLazPositionMethod); override;
procedure LazDeviceAPIs_SendMessage(AMsg: TLazDeviceMessage); override;
procedure LazDeviceAPIs_StartReadingAccelerometerData(); override;
procedure LazDeviceAPIs_StopReadingAccelerometerData(); override;
function PromptUser(const DialogCaption : string;
const DialogMessage : string;

View File

@ -375,6 +375,9 @@ begin
javaField_lclformheight := javaEnvRef^^.GetFieldID(javaEnvRef, javaActivityClass, 'lclformheight', 'I');
javaField_lclscreenwidth := javaEnvRef^^.GetFieldID(javaEnvRef, javaActivityClass, 'lclscreenwidth', 'I');
javaField_lclscreenheight := javaEnvRef^^.GetFieldID(javaEnvRef, javaActivityClass, 'lclscreenheight', 'I');
// For LazDeviceAPIs
javaField_lcldestination := javaEnvRef^^.GetFieldID(javaEnvRef, javaActivityClass, 'lcldestination', 'Ljava/lang/String;');
javaField_lclkind := javaEnvRef^^.GetFieldID(javaEnvRef, javaActivityClass, 'lclkind', 'I');
//
if not assigned(JavaField_lcltext) then
begin
@ -392,6 +395,9 @@ begin
javaMethod_LCLDoHideVirtualKeyboard := javaEnvRef^^.GetMethodID(javaEnvRef, javaActivityClass, 'LCLDoHideVirtualKeyboard', '()V');
javaMethod_LCLDoShowVirtualKeyboard := javaEnvRef^^.GetMethodID(javaEnvRef, javaActivityClass, 'LCLDoShowVirtualKeyboard', '()V');
javaMethod_LCLDoStartReadingAccelerometer := javaEnvRef^^.GetMethodID(javaEnvRef, javaActivityClass, 'LCLDoStartReadingAccelerometer', '()V');
javaMethod_LCLDoStopReadingAccelerometer := javaEnvRef^^.GetMethodID(javaEnvRef, javaActivityClass, 'LCLDoStopReadingAccelerometer', '()V');
javaMethod_LCLDoSendMessage := javaEnvRef^^.GetMethodID(javaEnvRef, javaActivityClass, 'LCLDoSendMessage', '()V');
javaMethod_LCLDoRequestPositionInfo := javaEnvRef^^.GetMethodID(javaEnvRef, javaActivityClass, 'LCLDoRequestPositionInfo', '()V');
__android_log_write(ANDROID_LOG_INFO, 'lclapp', 'JNI_OnLoad finished');
result:=JNI_VERSION_1_4;// 1_6 is another option

View File

@ -40,6 +40,7 @@ type
TLazAccelerometer = class
private
FOnSensorChanged: TNotifyEvent;
FReadingStarted: Boolean;
public
// These fields store the last data read, to get fresh data use UpdateAccelerometerData;
xaxis, yaxis, zaxis: Double; // in m/s^2
@ -63,6 +64,8 @@ type
public
// Attempt to send the specified message.
procedure SendMessage(AMsg: TLazDeviceMessage);
//
function CreateMessage: TLazDeviceMessage;
// Called asynchronously when the message sending is finished
property OnMessageSendingFinished: TOnMessageSendingFinished read FTOnMessageSendingFinished
write FTOnMessageSendingFinished;
@ -70,11 +73,9 @@ type
// TLazPositionInfo
TOnPositionRetrieved = procedure of object;
TLazPositionInfo = class
private
FOnPositionRetrieved: TOnPositionRetrieved;
FOnPositionRetrieved: TNotifyEvent;
public
IsPositionDataAvailable: Boolean; // Indicates if position info was read in the life of this program
// These fields hold the last position information read
@ -87,7 +88,7 @@ type
timeStamp: TDateTime; // The time when the location was established.
procedure RequestPositionInfo(AMethod: TLazPositionMethod);
// Called asynchronously when the position is read
property OnPositionRetrieved: TOnPositionRetrieved read FOnPositionRetrieved write FOnPositionRetrieved;
property OnPositionRetrieved: TNotifyEvent read FOnPositionRetrieved write FOnPositionRetrieved;
end;
var
@ -101,12 +102,16 @@ implementation
procedure TLazAccelerometer.StartReadingAccelerometerData;
begin
if FReadingStarted then Exit;
LCLIntf.LazDeviceAPIs_StartReadingAccelerometerData();
FReadingStarted := True;
end;
procedure TLazAccelerometer.StopReadingAccelerometerData;
begin
if not FReadingStarted then Exit;
LCLIntf.LazDeviceAPIs_StopReadingAccelerometerData();
FReadingStarted := False;
end;
{ TLazPositionInfo }
@ -123,6 +128,11 @@ begin
LCLIntf.LazDeviceAPIs_SendMessage(AMsg);
end;
function TLazMessaging.CreateMessage: TLazDeviceMessage;
begin
Result := TLazDeviceMessage.Create;
end;
initialization
Accelerometer := TLazAccelerometer.Create;
Messaging := TLazMessaging.Create;