{hint: Pascal files location: ...\ReptileDatabase\jni }
unit unit1;
{$mode delphi}
interface
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, StrUtils, AndroidWidget, menu, Laz_And_Controls, And_jni,
customdialog, broadcastreceiver, downloadmanager, intentmanager, downloadservice;
type
{ TAndroidModule1 }
TAndroidModule1 = class(jForm)
jBroadcastReceiver1: jBroadcastReceiver;
jDownloadService1: jDownloadService;
jIntentManager1: jIntentManager;
jWebView1: jWebView;
jMenu1: jMenu;
jPanel1: jPanel;
jPanel2: jPanel;
jPanel3: jPanel;
jTextView2: jTextView;
jTextView3: jTextView;
jWebView1: jWebView;
Panel1: jPanel;
Panel2: jPanel;
TextView1: jTextView;
TextView2: jTextView;
TextView3: jTextView;
TextView4: jTextView;
TextView5: jTextView;
TextView6: jTextView;
TextView7: jTextView;
procedure AndroidModule1ClickOptionMenuItem(Sender: TObject;
jObjMenuItem: jObject; itemID: integer; itemCaption: string;
Checked: boolean);
procedure AndroidModule1CreateOptionMenu(Sender: TObject; jObjMenu: jObject);
procedure AndroidModule1Show(Sender: TObject);
procedure AndroidModule1SpecialKeyDown(Sender: TObject; keyChar: char;
keyCode: integer; keyCodeString: string; var mute: boolean);
procedure jBroadcastReceiver1Receiver(Sender: TObject; intent: jObject);
procedure jWebView1Status(Sender: TObject; Status: TWebViewStatus;
URL: String; var CanNavi: Boolean);
private
{private declarations}
public
{public declarations}
end;
var
AndroidModule1: TAndroidModule1;
DownloadURL, URLFileName, Extension: string;
res: TAndroidResult;
implementation
{$R *.lfm}
{ TAndroidModule1 }
procedure TAndroidModule1.jWebView1Status(Sender: TObject;
Status: TWebViewStatus; URL: String; var CanNavi: Boolean);
var
SlashPos: integer;
begin
//Pass the URL variable to our own variable to use in jDownloadReceiver later
DownloadURL := URL;
//Extract the file extension from the URL
Extension := ExtractFileExt(URL);
if (Extension = '.xlsx') or (Extension = '.xls') or (Extension = '.pdf') then
begin
//Get filename after the last slash in the URL using SlashPos position
SlashPos := LastDelimiter('/', URL); //Get the last occurrence of slash in the URL
//Get the actual filename so can use this later to save the file with the same name
URLFilename := AnsiRightStr(URL, Length(URL) - Slashpos);
//Let's delete the file if it exists in case we cache an old file, as we will need to download the latest version of the file
if fileexists(Self.GetEnvironmentDirectoryPath(dirInternalAppStorage)+'/'+URLFilename) then
begin
Self.DeleteFile(self.GetEnvironmentDirectoryPath(dirInternalAppStorage)+'/'+URLFilename);
end;
//Register Intent for download
jBroadcastReceiver1.RegisterIntentActionFilter('org.lamw.AppShowDown.DOWNLOAD_RECEIVER'); //Register intent for Broadcast receiver
//What your downloaded file will be called, let's use the URL filename (there may be a method to get the filename?)
//Save file to project Download directory
jDownloadService1.SaveToFile(Self.GetEnvironmentDirectoryPath(dirInternalAppStorage), URLFIlename);
//Start the download
jDownloadService1.Start(URL, 'org.lamw.AppShowDown.DOWNLOAD_RECEIVER');
//Let user know we have started downloading
ShowMessage('Downloading file...please wait');
end;
end;
procedure TAndroidModule1.jBroadcastReceiver1Receiver(Sender: TObject;
intent: jObject);
begin
//Store result code so we can check later if the download was successful
res := jBroadcastReceiver1.GetResultCode();
//Use res to check for successful download or fail
if (res = RESULT_OK) then
begin
ShowMessage('Download of' + sLineBreak + DownloadURL + sLineBreak + 'successful');
//Launch the file once it has downloaded
if (Extension = '.xls') then // alternative office document mime type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
begin
Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirInternalAppStorage) + '/'+ URLFIlename,'application/vnd.ms-excel'); //Start the downloaded file using Excel
end
else if (Extension = '.xlsx') then
begin
Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirInternalAppStorage) + '/'+ URLFIlename,'application/vnd.ms-excel'); //Start the downloaded file using Excel
end
else if (Extension = '.pdf') then
begin
Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirInternalAppStorage) + '/'+ URLFIlename,'application/pdf'); //Start the downloaded file using PDF VIewer
end;
end
else if (res <> RESULT_OK) then
begin
ShowMessage('Download of' + sLineBreak + DownloadURL + sLineBreak + 'FAILED!');
end;
//unregister BroadcastReceiver ...
jBroadcastReceiver1.Unregister();
end;
procedure TAndroidModule1.AndroidModule1Show(Sender: TObject);
begin
jWebView1.Navigate('http://biotupe.org/test/');
end;
procedure TAndroidModule1.AndroidModule1SpecialKeyDown(Sender: TObject;
keyChar: char; keyCode: integer; keyCodeString: string; var mute: boolean);
begin
if (KeycodeString = 'KEYCODE_BACK') then
begin
if jWebView1.CanGoBack then
jWebView1.GoBack;
mute := True;
end;
end;
procedure TAndroidModule1.AndroidModule1CreateOptionMenu(Sender: TObject;
jObjMenu: jObject);
var
i: integer;
begin
for i := 0 to jMenu1.Options.Count - 1 do
jMenu1.AddItem(jObjMenu, i, jMenu1.Options.Strings[i], '',
mitDefault, misNever);
end;
procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
begin
jWebView1.SetLoadWithOverviewMode(True);
jWebView1.SetUseWideViewPort(True);
end;
procedure TAndroidModule1.AndroidModule1ClickOptionMenuItem(Sender: TObject;
jObjMenuItem: jObject; itemID: integer; itemCaption: string; Checked: boolean);
begin
case itemID of
0:
begin
ShowMessage('The ShowDown App' + sLineBreak +
'This app demonstrates how to download and open' + sLineBreak +
'a linked file from a webpage with' + sLineBreak +
'the associated app' + sLineBreak + sLineBreak +
'This code does not belongs to anyone. Property is theft. Help yourself.',
gvCenter, slLong);
end;
1:
AndroidModule1.Close;
end;
end;
end.