I am porting an older project (app for iOS ans Android) for a customer from an older AppMethod version to the newest RAD Studio version (10.0 Berlin). MessageDlg and similar were used often in the project but this does not work anymore. The apps shows a message, that modal dialogs are not supported on Android. I know and understand why this is so for the Android platform (so please don't mark this question as a dublicate by referencing to an explanation - I'm asking for something else!).
Firemonkey allows to set an anonymous function to be executed asynchronously after the user taps on a button in the dialog box or when it is closed. However, there is often code which depends on users decision or code which must be executed after the dialog box, regardless of which button the user taps on. For example, a dialog box must ask the user for his decision in the middle of an ongoing operation. Further operations then depend on the users decision (which might also include to stop further code execution in the current application!). Since RAD Studio does not support blocking dialogs on Android, I seem to be forced to break up my code - from one function to multiple functions (worse readability...). This gets even more complicated when there are nested function calls with possible modal dialog boxes, which require user interaction.
Is there still a way to emulate blocking dialogs somehow? It doesn't has to be perfect. I just don't want to rewrite a lot of code and outsource even small code fragments into many separate functions, everwhere where user interaction is required.
OK, I think the following "dirty" solution might be sufficient for me:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.DialogService,
FMX.Controls.Presentation, FMX.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
blockDlg: array[0..99] of Boolean;
blockDlgIdx: Integer = 0;
implementation
{$R *.fmx}
procedure NShowMessageAsync(AMessage: String);
begin
TDialogService.ShowMessage(AMessage);
end;
procedure NShowMessageSync(AMessage: String);
var
locBlockIdx: Integer;
begin
if blockDlgIdx = Length(blockDlg)-1 then blockDlgIdx := 0 else Inc(blockDlgIdx);
locBlockIdx := blockDlgIdx;
blockDlg[locBlockIdx] := true;
TDialogService.ShowMessage(AMessage,
procedure(const AResult: TModalResult)
begin
blockDlg[locBlockIdx] := false;
end);
while blockDlg[locBlockIdx] do begin
Sleep(40);
Application.ProcessMessages();
end;
end;
procedure NMessageDialogAsync(const AMessage: string; const ADialogType: TMsgDlgType;
const AButtons: TMsgDlgButtons; const ADefaultButton: TMsgDlgBtn;
const AHelpCtx: LongInt);
begin
TDialogService.MessageDialog(AMessage, ADialogType, AButtons, ADefaultButton,
AHelpCtx, procedure(const AResult: TModalResult) begin end);
end;
function NMessageDialogSync(const AMessage: string; const ADialogType: TMsgDlgType;
const AButtons: TMsgDlgButtons; const ADefaultButton: TMsgDlgBtn;
const AHelpCtx: LongInt): TModalResult;
var
locBlockIdx: Integer;
LModalResult: TModalResult;
begin
Result := -1;
if blockDlgIdx = Length(blockDlg)-1 then blockDlgIdx := 0 else Inc(blockDlgIdx);
locBlockIdx := blockDlgIdx;
blockDlg[locBlockIdx] := true;
TDialogService.MessageDialog(AMessage, ADialogType, AButtons, ADefaultButton, AHelpCtx,
procedure(const AResult: TModalResult)
begin
LModalResult := AResult;
blockDlg[locBlockIdx] := false;
end);
while blockDlg[locBlockIdx] do begin
Sleep(40);
Application.ProcessMessages();
end;
Result := LModalResult;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
mr: TModalResult;
begin
mr := NMessageDialogSync('1',
System.UITypes.TMsgDlgType.mtInformation,
[System.UITypes.TMsgDlgBtn.mbYes, System.UITypes.TMsgDlgBtn.mbNo, System.UITypes.TMsgDlgBtn.mbCancel],
System.UITypes.TMsgDlgBtn.mbYes,
0);
NShowMessageSync(IntToStr(mr));
mr := NMessageDialogSync('2',
System.UITypes.TMsgDlgType.mtWarning,
[System.UITypes.TMsgDlgBtn.mbYesToAll, System.UITypes.TMsgDlgBtn.mbAbort],
System.UITypes.TMsgDlgBtn.mbAbort,
0);
NShowMessageSync(IntToStr(mr));
mr := NMessageDialogSync('3',
System.UITypes.TMsgDlgType.mtInformation,
[System.UITypes.TMsgDlgBtn.mbIgnore, System.UITypes.TMsgDlgBtn.mbAll, System.UITypes.TMsgDlgBtn.mbHelp, System.UITypes.TMsgDlgBtn.mbClose],
System.UITypes.TMsgDlgBtn.mbClose,
0);
NShowMessageSync(IntToStr(mr));
Form1.Fill.Kind := TBrushKind.Solid;
if Form1.Fill.Color = TAlphaColors.Red then
Form1.Fill.Color := TAlphaColors.Blue
else
Form1.Fill.Color := TAlphaColors.Red;
end;
end.
Not very clean, but at least I can use this for a while.
Related
I need get telephone number of device on which my application runing. If has device two SIM cards ideal to get both numbers or if SIM card is not inserted (tablet device) can detect this.
I found some JAVA code but I have no idea how translate it to Delphi
TelephonyManager phneMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String phneNmbr = phneMgr.getLine1Number();
I try write something but it not working ....
USES Androidapi.Helpers, Androidapi.JNI.JavaTypes, Androidapi.JNI.Telephony;
procedure TForm1.Button1Click(Sender: TObject);
var
num: JString;
tman: Androidapi.JNI.Telephony.JTelephonyManager;
begin
tman:=TJtelephonyManager.Create;
num := tman.getLine1Number;
edit1.Text:=Jstringtostring(num);
end;
Something like this should do it, based on experience with other system services. This translates what you have suggested is viable Java code.
I'll edit this to make it compile correctly (if there are any issues with it) when I have a copy of Delphi to hand later, but this is roughly what is required.
Note that quick look at the telephony manager documentation doesn't readily say how one would get the phone number for a second SIM, but it does translate what you were trying to translate.
uses
System.SysUtils,
Androidapi.Helpers,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Telephony;
function DeviceTelephoneNumber: string;
var
TelephonyManagerObj: JObject;
TelephonyManager: JTelephonyManager;
begin
TelephonyManagerObj:= TAndroidHelper.Context.getSystemService(
TJContext.JavaClass.TELEPHONY_SERVICE);
if TelephonyManagerObj <> nil then
begin
TelephonyManager := TJTelephonyManager.Wrap(TelephonyManagerObj);
if TelephonyManager <> nil then
Result := JStringToString(TelephonyManager.getLine1Number);
end;
end;
This code is also a possibility, which works in Android 5.1 and later.
function DeviceTelephoneNumbers: TArray<string>;
var
SubscriptionManager: JSubscriptionManager;
I, SubscriptionInfoCount: Integer;
SubscriptionInfoList: JList;
SubscriptionInfo: JSubscriptionInfo;
begin
// Subscription manager is only available in Android 5.1 and later
if TOSVersion.Check(5, 1) then
begin
SubscriptionManager := TJSubscriptionManager.JavaClass.from(
TAndroidHelper.Context);
SubscriptionInfoCount := SubscriptionManager.getActiveSubscriptionInfoCount;
SubscriptionInfoList := SubscriptionManager.getActiveSubscriptionInfoList;
SetLength(Result, SubscriptionInfoCount);
for I := 0 to Pred(SubscriptionInfoCount) do
begin
SubscriptionInfo := TJSubscriptionInfo.Wrap(SubscriptionInfoList.get(I));
if SubscriptionInfo <> nil then
Result[I] := JStringToString(SubscriptionInfo.getNumber);
end;
end
else
begin
// If running on older OS, use older API
SetLength(Result, SubscriptionInfoCount);
Result[0] := DeviceTelephoneNumber
end;
end;
Does anyone know how to dim the screen on Delphi Firemonkey for Android? I've been googling around and searching but could only find how to remove the auto dim for android by acquiring a Wakelock (Delphi XE5 Android. How to use PowerManager.WakeLock?).
At best I'd like to achieve two states:
Evening = 20% brightness
Daytime = 100% brightness
I am using Delphi 10.1 Berlin.
Thank you for your help.
Taking this Stack Overflow question, which has the Java solution, as a guide I rustled up this helper unit that should work in Delphi versions from around XE8 to 10.1 Berlin, which seems to do the trick:
unit ScreenBrightnessU;
interface
function GetScreenBrightness: Byte;
procedure SetScreenBrightness(Brightness: Byte);
implementation
uses
MiscU,
FMX.Helpers.Android,
{$IF RTLVersion >= 31}
FMX.DialogService,
{$ELSE}
FMX.Dialogs,
{$ENDIF}
System.UITypes,
System.SysUtils,
Androidapi.Helpers,
Androidapi.JNI.App,
Androidapi.JNI.Provider,
Androidapi.JNI.GraphicsContentViewText;
function GetScreenBrightness: Byte;
var
Resolver: JContentResolver;
begin
Resolver :=
{$IF RTLVersion >= 30}
TAndroidHelper.ContentResolver;
{$ELSE}
SharedActivityContext.getContentResolver;
{$ENDIF}
Result := TJSettings_System.JavaClass.getInt(
Resolver,
TJSettings_System.JavaClass.SCREEN_BRIGHTNESS);
end;
procedure SetScreenBrightness(Brightness: Byte);
var
Resolver: JContentResolver;
AttainedBrightness: Single;
LayoutParams: JWindowManager_LayoutParams;
Window: JWindow;
begin
if not HasPermission('android.permission.WRITE_SETTINGS') then
{$IF RTLVersion >= 31}
TDialogService.MessageDialog('App does not have the WRITE_SETTINGS permission', TMsgDlgType.mtError, [TMsgDlgBtn.mbCancel], TMsgDlgBtn.mbCancel, 0, nil)
{$ELSE}
MessageDlg('App does not have the WRITE_SETTINGS permission', TMsgDlgType.mtError, [TMsgDlgBtn.mbCancel], 0)
{$ENDIF}
else
begin
{$IF RTLVersion >= 30}
Resolver := TAndroidHelper.ContentResolver;
{$ELSE}
Resolver := SharedActivityContext.getContentResolver;
{$ENDIF}
// This will set the manual mode (set the automatic mode off)
TJSettings_System.JavaClass.putInt(
Resolver,
TJSettings_System.JavaClass.SCREEN_BRIGHTNESS_MODE,
TJSettings_System.JavaClass.SCREEN_BRIGHTNESS_MODE_MANUAL);
// This will set the required brightness
TJSettings_System.JavaClass.putInt(
Resolver,
TJSettings_System.JavaClass.SCREEN_BRIGHTNESS,
Brightness);
try
AttainedBrightness := GetScreenBrightness;
CallInUIThread(
procedure
begin
{$IF RTLVersion >= 30}
Window := TAndroidHelper.Activity.getWindow;
{$ELSE}
Window := SharedActivity.getWindow;
{$ENDIF}
LayoutParams := Window.getAttributes;
LayoutParams.screenBrightness := AttainedBrightness / 255;
Window.setAttributes(LayoutParams);
end);
except
// ONOES!!!!
// <sweeps issue under the carpet>
end;
end;
end;
end.
You'll note that the code does permission checking via the helper unit below. This is not vital so long as you ensure you have the WRITE_SETTINGS permission set in your project for all the Android build configurations.
unit MiscU;
interface
function HasPermission(const Permission: string): Boolean;
implementation
uses
FMX.Helpers.Android,
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText;
function HasPermission(const Permission: string): Boolean;
begin
//Permissions listed at http://d.android.com/reference/android/Manifest.permission.html
{$IF RTLVersion >= 30}
Result := TAndroidHelper.Context.checkCallingOrSelfPermission(
{$ELSE}
Result := SharedActivityContext.checkCallingOrSelfPermission(
{$ENDIF}
StringToJString(Permission)) =
TJPackageManager.JavaClass.PERMISSION_GRANTED
end;
end.
I have reduced the code to a simple sample.
IdTCPClient reads a message and should display it in a Memo.
The attached code works fine on Windows, but the DoNotify is not executed on android if PostLog is called from ThC_Receive. If I call PostLog via Button.Click from MainForm it is executed.
Any suggestions?
TIdSync works, but is it recommended?
unit HeaderFooterTemplate;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls, IdContext, IdSync,
FMX.Layouts, FMX.Memo, IdThreadComponent, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdGlobal;
type
// ---------------------------------------------------------------------------
TLog = class(TIdNotify)
protected
fMsg: String;
procedure DoNotify; override;
//procedure DoSynchronize; override;
public
class procedure PostLog(const S: String);
end;
// ---------------------------------------------------------------------------
THeaderFooterForm = class(TForm)
Header: TToolBar;
Footer: TToolBar;
HeaderLabel: TLabel;
M_Log: TMemo;
IdTCPClient1: TIdTCPClient;
ThC_Receive: TIdThreadComponent;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure IdTCPClient1Connected(Sender: TObject);
procedure ThC_ReceiveRun(Sender: TIdThreadComponent);
private
{ Private declarations }
public
{ Public declarations }
end;
var
HeaderFooterForm: THeaderFooterForm;
implementation
{$R *.fmx}
// -----------------------------------------------------------------------------
{ TLog }
// -----------------------------------------------------------------------------
procedure TLog.DoNotify;
begin
HeaderFooterForm.M_Log.Lines.Append(fMsg);
end;
// -----------------------------------------------------------------------------
class procedure TLog.PostLog(const S: String);
begin
with Create do
begin
try
fMsg := S;
Notify;
except
Free;
Raise;
end;
end;
end;
// -----------------------------------------------------------------------------
procedure THeaderFooterForm.Button2Click(Sender: TObject);
begin
try
IdTCPClient1.Host :='192.168.1.12';
IdTCPClient1.Port := 1001;
IdTCPClient1.Connect;
except
on E: Exception do
begin
TLog.PostLog(trim(e.Message));
Raise;
end;
end;
end;
// -----------------------------------------------------------------------------
procedure THeaderFooterForm.IdTCPClient1Connected(Sender: TObject);
begin
ThC_Receive.Start;
end;
// -----------------------------------------------------------------------------
procedure THeaderFooterForm.ThC_ReceiveRun(Sender: TIdThreadComponent);
var
s: string;
begin
try
s:= (IdTCPClient1.IOHandler.ReadLn(TransmissionSeparator, IndyTextEncoding(IdTextEncodingType.encUTF16LE)));
TLog.PostLog(trim(s));
except
on E: Exception do
begin
TLog.PostLog(trim(e.Message));
Raise;
end;
end;
end;
// -----------------------------------------------------------------------------
end.
Thank you,
Spectro
When called in a worker thread, TIdNotify uses TThread.Queue() and TIdSync uses TThread.Synchronize(), both of which go through the same RTL queue, so it is unlikely that TThread.Synchronize() would work but TThread.Queue() would not. On the other hand, when called in the main thread, TThread is not used (unless TIdNotify.MainThreadUsesNotify is set to true), TIdNotify.DoNotify() and TIdSync.DoSynchronize() are called directly instead. If TIdNotify is not working in a thread, then TThread.Queue() has to be broken, which would be an Embarcadero issue, not an Indy issue.
That being said, TIdNotify does have logic to use TThread.Synchronize() on Delphi versions that do not have TThread.Queue() available. You could try making a copy of IdSync.pas and modify it to undefine the HAS_STATIC_TThread_Queue define on Android, and then add the modified file to your project. That only works when Runtime Packages are disabled, though. I am not an Android developer, so I do not know how Delphi uses Packages on mobile platforms.
BTW, IndyTextEncoding(IdTextEncodingType.encUTF16LE) can be replaced with IndyTextEncoding_UTF16LE.
Update: a bug has been identified in FireMonkey itself (see QC #123579), going all the way back to when FireMonkey was first introduced, and still exists in XE5 Update 2. In short, FMX.TApplication does not assign a handler to the System.Classes.WakeMainThread callback (Vcl.TApplication does). TThread calls WakeMainThread to notify the main thread that a Synchronize/Queue() request is pending. Thus, if the main message loop is idle when Synchronize/Queue() is called, nothing happens until a later time if/when something else puts a new message in the main message queue. Without that, TApplication.Idle() is not called, and so CheckSynchronize() is not called to process pending Synchronize/Queue() requests. That means for background/nonvisual processes, Synchronize/Queue() may never work correctly at all, and sporadically in GUI processes. Until Embarcadero fixes that bug, a workaround would be to either post a custom message to the main message queue to "wake" it up, or else manually call CheckSynchronize() in the main thread periodically, such as in a timer.
Has anyone been able to take pictures from camera on Android from within the app written in Delphi Firemonkey XE5? How about the video capture?
This is believed to be either a bug in a framework or just something with missing documentation about it.
Can anyone tell why the code bellow doesn't work / retrieve any image from a camera on Android?
Dropped a TCameraComponent on a form, and a TImage component as well, and nothing happens.
procedure TCameraComponentForm.OnCreate(Sender: TObject);
begin
CameraComponent1.Kind := FMX.Media.TCameraKind.ckFrontCamera;
CameraComponent1.FlashMode := FMX.Media.TFlashMode.fmFlashOff;
CameraComponent1.Active := True;
end;
procedure TCameraComponentForm.CameraComponent1SampleBufferReady(
Sender: TObject; const ATime: Int64);
begin
CameraComponent1.SampleBufferToBitmap(Image1.Bitmap, True);
Image1.Width := Image1.Bitmap.Width;
Image1.Height := Image1.Bitmap.Height;
end;
Permissions are set correctly.
This code works fine:
procedure TfrmPrincipal.SampleBufferSync;
begin
cmcPrincipal.SampleBufferToBitmap(imgFoto.Bitmap, true);
end;
procedure TfrmPrincipal.cmcPrincipalSampleBufferReady(Sender: TObject;
const ATime: Int64);
begin
TThread.Synchronize(TThread.CurrentThread, SampleBufferSync);
// CameraComponent1.SampleBufferToBitmap(imgFoto.Bitmap, True);
// imgFoto.Width := imgFoto.Bitmap.Width;
// imgFoto.Height := imgFoto.Bitmap.Height;
end;
procedure TfrmPrincipal.FormShow(Sender: TObject);
begin
cmcPrincipal.Kind := FMX.Media.TCameraKind.ckBackCamera;
try
cmcPrincipal.FlashMode := FMX.Media.TFlashMode.fmFlashOff;
except
end;
cmcPrincipal.Active := True;
end;
I am developing an app that must run on iOS, Android, and W32/64 using Delphi FireMonkey on RAD Studio XE5. I am trying to access the video camera in order to scan in barcodes.
I've got the example VideoCaptureHD app running on W32 using a USB webcam, but on Android the app just shows the background color, and no image from the camera. I've added a breakpoint to the SampleBufferReady method, but it never gets called.
Also, the capture button is enabled indicating that the camera device is recognized at some level.
//---------------------------------------------------------------------------
// This software is Copyright (c) 2012 Embarcadero Technologies, Inc.
// You may only use this software if you are an authorized licensee
// of Delphi, C++Builder or RAD Studio (Embarcadero Products).
// This software is considered a Redistributable as defined under
// the software license agreement that comes with the Embarcadero Products
// and is subject to that software license agreement.
//---------------------------------------------------------------------------
unit CaptureForm;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Objects, FMX.Media, FMX.StdCtrls,
FMX.Layouts, FMX.ListBox;
type
TForm1 = class(TForm)
Image1: TImage;
CaptureButton: TSpeedButton;
Layout1: TLayout;
SaveDialog1: TSaveDialog;
Ellipse1: TEllipse;
StopButton: TSpeedButton;
procedure FormCreate(Sender: TObject);
procedure CaptureButtonClick(Sender: TObject);
procedure StopButtonClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
VideoCamera: TVideoCaptureDevice;
procedure SampleBufferReady(Sender: TObject; const ATime: TMediaTime);
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.FormCreate(Sender: TObject);
begin
VideoCamera := TCaptureDeviceManager.Current.DefaultVideoCaptureDevice;
if VideoCamera <> nil then
begin
Ellipse1.AnimateFloat('Opacity', 1, 1.0);
VideoCamera.OnSampleBufferReady := SampleBufferReady;
VideoCamera.StartCapture;
end
else
begin
CaptureButton.Enabled := False;
Caption := 'Video capture devices not available.';
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if VideoCamera <> nil then
VideoCamera.StopCapture;
end;
procedure TForm1.SampleBufferReady(Sender: TObject; const ATime: TMediaTime);
begin
VideoCamera.SampleBufferToBitmap(Image1.Bitmap, True);
end;
procedure TForm1.StopButtonClick(Sender: TObject);
begin
if VideoCamera <> nil then
begin
if VideoCamera.State = TCaptureDeviceState.Capturing then
begin
Ellipse1.AnimateFloat('Opacity', 0, 1.0);
StopButton.Text := 'Capture';
VideoCamera.StopCapture;
end
else
begin
Ellipse1.AnimateFloat('Opacity', 1, 1.0);
StopButton.Text := 'Stop';
VideoCamera.StartCapture
end;
end;
end;
procedure TForm1.CaptureButtonClick(Sender: TObject);
begin
if SaveDialog1.Execute then
Image1.Bitmap.SaveToFile(SaveDialog1.FileName);
end;
end.
I can't find bug or mistake in your application.
You should also check if in Project->Options->UsesPermisions->Android->Camera if is True.
I did similar application and it works (public variables the same as yours):
procedure TForm1.Button1Click(Sender: TObject); //starts capturing on click
begin
VideoCamera.StartCapture;
end;
procedure TForm1.Button2Click(Sender: TObject); //stops capturing on click
begin
VideoCamera.StopCapture;
end;
procedure TForm1.Button3Click(Sender: TObject); //create camera on click
begin
VideoCamera := TCaptureDeviceManager.Current.DefaultVideoCaptureDevice;
if VideoCamera <> nil then
begin
VideoCamera.OnSampleBufferReady := SampleBufferReady;
end
else
begin
ShowMessage('Video capture devices not available.');
end;
end;
procedure TForm1.SampleBufferReady(Sender: TObject; const ATime: TMediaTime); // copy to bitmap works perfect
begin
VideoCamera.SampleBufferToBitmap(Image1.Bitmap, True);
end;