unit unit1;
{$mode delphi}
interface
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, AndroidWidget, Laz_And_Controls, And_jni, intentmanager, preferences;
type
{ TAndroidModule1 }
TAndroidModule1 = class(jForm)
Button1: jButton;
EditText1: jEditText;
IntentManager1: jIntentManager;
Preferences1: jPreferences;
procedure AndroidModule1ActivityResult(Sender: TObject; ARequestCode: Integer; AResultCode: TAndroidResult; AIntentData: jObject);
procedure AndroidModule1JNIPrompt(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
public
{public declarations}
end;
var
AndroidModule1: TAndroidModule1;
implementation
{$R *.lfm}
{ TAndroidModule1 }
procedure TAndroidModule1.Button1Click(Sender: TObject);
(* Requests creation or update of the file *)
begin
if Preferences1.GetStringData('uri', '') <> ''
then (* URI saved to preferences: simply opens the file *)
begin
EditText1.Append('Calling RequestOpenFile' + LineEnding);
RequestOpenFile(GetEnvironmentDirectoryPath(dirDownLoads), 'text/plain', 112);
end
else (* URI not saved yet to preferences: creates the file *)
begin
EditText1.Append('Calling RequestCreateFile' + LineEnding);
RequestCreateFile(GetEnvironmentDirectoryPath(dirDownLoads), 'text/plain', 'Test22', 111);
end;
end;
procedure TAndroidModule1.AndroidModule1ActivityResult(Sender: TObject; ARequestCode: Integer; AResultCode: TAndroidResult; AIntentData: jObject);
(* Saves data to file *)
var
LText: TStringList;
LUri: jObject;
begin
if AResultCode = RESULT_OK
then
begin
if AIntentData = Nil
then
begin
EditText1.Append(' No data received, exit' + LineEnding);
Exit;
end;
LUri := IntentManager1.GetDataUri(AIntentData);
if LUri = Nil
then
begin
EditText1.Append(' URI NIL received, exit' + LineEnding);
Exit;
end;
case ARequestCode of
111: begin (* Creates the file *)
EditText1.Append( 'Success! File created' + LineEnding);
Preferences1.SetStringData('uri', UriToString(LUri)); (* Saves URI to preferences so file won't be re-created *)
EditText1.Append( 'URI saved to preferences' + LineEnding);
LText := TStringList.Create;
try
LText.Append('Line 1');
LText.Append('Line 2');
SaveTextToUri(LText.Text, LUri);
EditText1.Append( 'Text written' + LineEnding);
finally
LText.Free;
end;
end;
112: begin (* Updates the file *)
EditText1.Append( 'Success! File opened' + LineEnding);
LText := TStringList.Create;
try
LText.Append('Line 3');
LText.Append('Line 4');
SaveTextToUri(LText.Text, LUri);
EditText1.Append( 'Text written' + LineEnding);
finally
LText.Free;
end;
end;
end;
end;
end;
procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
(* Erases any URI from preferences *)
begin
Preferences1.SetStringData('uri', '');
end;
end.