// service
unit fDownloader;
interface
uses
System.SysUtils,
System.Classes,
System.Android.Service,
AndroidApi.JNI.GraphicsContentViewText,
Androidapi.JNI.Os,
System.SyncObjs,
System.Generics.Collections,
Network, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdExplicitTLSClientServerBase, IdFTP;
type
TThreadUploader =
class;
TDM =
class(TAndroidService)
function AndroidServiceStartCommand(
const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
procedure AndroidServiceCreate(Sender: TObject);
procedure AndroidServiceDestroy(Sender: TObject);
private
{ Private declarations }
FThreads: TObjectList<TThread>;
FThreadsUploader: TList<TThreadUploader>;
procedure UploadFoto(AEvent: TEvent);
public
{ Public declarations }
procedure MultiUpload;
end;
TThreadUploader =
class(TThread)
private
FEvent: TEvent;
public
procedure Execute;
override;
property Event: TEvent
read FEvent
write FEvent;
end;
var
DM: TDM;
FDir:
string;
fsFilter,fsName,s:
string;
DirImage:
string;
DirSettings:
string;
FTP: TidFTP;
slSettings: TStringList;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
{$R *.dfm}
uses
Androidapi.JNI.App, Math, System.IOUtils;
procedure TDM.AndroidServiceCreate(Sender: TObject);
begin
FThreads := TObjectList<TThread>.Create;
FThreadsUploader := TList<TThreadUploader>.Create;
end;
procedure TDM.AndroidServiceDestroy(Sender: TObject);
begin
FThreads.Free;
FThreadsUploader.Free;
end;
function TDM.AndroidServiceStartCommand(
const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
var
lThread: TThread;
begin
lThread := TThread.CreateAnonymousThread(
procedure
var
LEvent: TEvent;
begin
LEvent := TEvent.Create();
UploadFoto(LEvent);
LEvent.WaitFor(INFINITE);
JavaService.stopSelf(StartId);
end
);
LThread.FreeOnTerminate := True;
FThreads.Add(LThread);
LThread.Start();
Result := TJService.JavaClass.START_STICKY;
end;
procedure TDM.UploadFoto(AEvent: TEvent);
var
LThread: TThreadUploader;
begin
LThread := TThreadUploader.Create(True);
LThread.FreeOnTerminate := True;
LThread.Event := AEvent;
FThreadsUploader.Add(LThread);
LThread.Start();
end;
procedure TDM.MultiUpload;
var
isOK: boolean;
begin
if IsConnected = True
then
begin
try
fsFilter := '
*.jpeg';
for fsName
in TDirectory.GetFiles(DirImage, fsFilter)
do
begin
if (fsName <> '
')
and (FileExists(DirSettings + System.SysUtils.PathDelim + '
settings.ini'))
then
begin
slSettings := TStringList.Create;
slSettings.LoadFromFile(DirSettings + System.SysUtils.PathDelim + '
settings.ini');
FTP := TidFTP.Create(
nil);
FTP.Host := slSettings[8];
FTP.Port := StrToInt(slSettings[9]);
FTP.Username := slSettings[10];
FTP.Password := slSettings[11];
FDir := slSettings[12];
FTP.Connect;
FTP.ChangeDir(FDir);
try
FTP.Delete(TPath.GetFileName(fsName));
except
isOK := False;
end;
FTP.Put(fsName, TPath.GetFileName(fsName));
DeleteFile(fsName);
FTP.Site('
CHMOD 777 '+ TPath.GetFileName(fsName));
FTP.Disconnect;
FTP.Free;
slSettings.Free;
end;
end;
except
on E:
exception do
begin
s := E.
Message;
end;
end;
end;
end;
procedure TThreadUploader.Execute;
begin
while not Terminated
do
begin
dm.MultiUpload;
Sleep(10000);
FEvent.SetEvent;
Terminate;
end;
end;
end.