Friday, March 24, 2017

One codebase to rule them all - RAD Studio 10.2 Tokyo is here!

Finally, it's arrived!! The penguin invasion has started! RAD Studio 10.2 Tokyo is here bringing with it support for Linux!
Delphi for Linux is now available delivering support for Linux 64bit server-side and standalone applications for Ubuntu and RedHat Enterprise. Guys, I'm very enthusiastic about this new support! This is a very greats news for Delphi developers and not, everywhere!


By now Delphi can support and generate native code for several platforms (7 to be exact!!!): Linux (64bit), Windows (x86 and x64), OS X (32-bit ), iOS (32 and 64-bit) and Android.
I think that no citation may be more appropriate of this:
one codebase to rule them all
Below a list of the most interesting features available in this release (IMHO):

Delphi Linux Compiler


  • 64-bit Linux platform support
  • Delphi language support
  • ARC-enabled
  • 1-based strings
  • LLVM engine based

Supported Linux Distributions


  • Ubuntu Server (Ubuntu 16.04 LTS)
  • RedHat Enterprise Linux (version 7)

RTL features supported


  • Linux file system access
  • Threading and Parallel Programming Libraries support
  • Memory management
  • HTTP and REST libraries support for HTTP calls

64-bit Linux for Server Applications


  • FireDAC provides Linux support for all Linux-capable DBMS, with the only exception of Informix. See Database Connectivity.
  • WebBroker, RAD Server (EMS), and DataSnap support for Multi-tier servers (standalone and Apache modules).
  • DUnitX support.
  • PAServer support.
  • Indy-based client and server support.

FireMonkey


  • New System Status Bar Options on iOS
    • Two new properties have been added to TForm for changing the behavior of the system status bar on iOS: SystemStatusBar.BackgroundColor and SystemStatusBar.Visibility.


  • Multi-Threading Support for TBitmap, TCanvas and TContext3D
  • Unification of Delphi and Java threads on Android: CallInUIThread has been deprecated. All code is now running in the Java UI Thread, removing the need for thread synchronization

Improvements for Firebird


  • New Direct property to add support for the Direct I/O backup feature.

Improvements for MySQL

  • Support for MySQL v 5.7.
  • Support for MariaDB v 5.5 or later. Use FireDAC's MySQL node to connect to MariaDB.
  • Compatibilty with the recent MySQL versions 5.6.x and 5.7.x.

Database improvements


  • VCL TDBImage direct support for JPEG and PNG graphic formats.
  • Support for display options for BlobFields.
  • Direct GUID access for TField classes.

RTL


  • HTTP Client
  • HTTP Client Library provides support for 64-bit Linux.
  • Improved async support in HTTP client
  • RTL support for 64-bit Linux
  • RAD Studio 10.2 Tokyo provides Linux file system support using standard system functions, streams, and the IOUtils unit. It also provides Standard RTL Path Functions support for 64-bit Linux.

Improved C++ packages for mobile

RAD Studio 10.2 Tokyo links C++ packages for iOS and Android statically. Previous versions of RAD Studio generated a file with the .so extension for C++ packages, but were not actually dynamic libraries. RAD Studio 10.2 Tokyo generates C++ packages with the correct file extension (.a). If you statically link against a .so package, you may need to change to link against the new filename.

RAD Server Multi-Tenancy Support

With Multi-Tenancy support, a single RAD Server instance with a single RAD Server database connection can support multiple isolated tenants. Each tenant has a unique set of RAD Server resources including Users, Groups, Installations, Edge Modules, and other data. All tenants have custom resources that are installed in the EMS Server. Also, as an administrator you can create new tenants, edit existing ones, add, edit, or delete details of your tenants, specify if the tenant is active, and delete the tenants that you do not need.

iOS 10 Simulator, device, and macOS

Full support for iOS 10, including iOS 10 32-bit and 64-bit debugging on device, Simulator support (Delphi only) and macOS Sierra support.

The news are not only these, for the complete list of all news of this release go here.

Thursday, March 2, 2017

Intercept KeyEvent on Android with Delphi

Some days ago, I had the needed to intercept the KeyDown Event on Android. In particullary the App have to respond to FormKeyDown Event. After some work I found out that there is an opened issue about this topic (https://quality.embarcadero.com/browse/RSP-10111 - vote it if you need its resolution). So I set to work to find a solution/workaround to solve my need. I studied Android SDK and Delphi internal mechanism and I found out a way to resolve it:
  • By adding a JFMXTextListener to JFMXTextEditorProxy
JFMXTextEditorProxy is an object provided by the JFMXNativeActivity (The activity for all your Android application in Delphi) and provides the method addTextListener to register a JFMXTextListener.
JFMXTextListener is an interface that supply 3 methods to manage text event:

  1.    procedure onComposingText(beginPosition: Integer; endPosition: Integer); cdecl;
  2.    procedure onSkipKeyEvent(event: JKeyEvent); cdecl;
  3.    procedure onTextUpdated(text: JCharSequence; position: Integer); cdecl;
I created a new class TCustomAndroidTextListener that implements JFMXTextListener so I can intercept the KeyEvent in these methods:

TCustomAndroidTextListener = class(TJavaLocal, JFMXTextListener)
  private
    FForm: TCommonCustomForm;
    FLastStr: string;
  protected
    function TryAsKey(const aChar: Char; var Key: Word): Boolean;
  public
    constructor Create(aForm: TCommonCustomForm);
    procedure onComposingText(beginPosition: Integer;
      endPosition: Integer); cdecl;
    procedure onSkipKeyEvent(event: JKeyEvent); cdecl;
    procedure onTextUpdated(text: JCharSequence; position: Integer); cdecl;
  end;

Now I had to add my TextListener, TCustomAndroidTextListener, to JFMXTextEditorProxy so that implemented method can be invoked when the characters are typed:

procedure RegisterTextListener(const aForm: TCommonCustomForm);
var
  FFMXTxp: JFMXTextEditorProxy;
begin
  if Assigned(FTextListener) then
    exit;
  FTextListener := TCustomAndroidTextListener.Create(aForm);
  FFMXTxp := FMX.Platform.Android.MainActivity.getTextEditorProxy;
  // the code below should be equivalent to INPUT_ALPHABET
  // FFMXTxp.setInputType(TJInputType.JavaClass.TYPE_CLASS_TEXT or
  // TJInputType.JavaClass.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD or
  // TJInputType.JavaClass.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
  FFMXTxp.setInputType(TJFMXTextEditorProxy.JavaClass.INPUT_ALPHABET);
  FFMXTxp.addTextListener(FTextListener);
end;

I created a new sample, AndroidKeyEvent, and added it to my demos repository.
In the sample, if you look the code in onTextUpdated method you can notice a solution to understand what the user is typing and propagate keydown event on the form object passed in constructor. This solution is not complete to 100% because is very hard to figure out a way to understand what user is typing if autocomplete mode is on keyboard. It seems to work fine if autocomplete is off, this is why is set the input type to TJFMXTextEditorProxy.JavaClass.INPUT_ALPHABET.
In the coming days I will continue to work on the issue to achieve a better and complete solution. In the meantime I wanted to share the code with you so if you want to contribute you can do it.