-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
ChatGPT.Android.pas
101 lines (82 loc) · 2.72 KB
/
ChatGPT.Android.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
unit ChatGPT.Android;
{$IFDEF ANDROID}
interface
uses
System.SysUtils, System.Types, System.IOUtils,
System.Messaging, FMX.Dialogs;
type
TDialog = class
private
FProc: TProc<string>;
FRequestCode: Integer;
procedure ResultCallback(const Sender: TObject; const M: TMessage);
public
constructor Create;
destructor Destroy; override;
procedure Show(const MimeType: string; Proc: TProc<string>);
end;
procedure OpenFileDialog(const MimeType: string; Proc: TProc<string>);
implementation
uses
System.Permissions,Androidapi.Helpers, Androidapi.JNI.Os,
Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.JavaTypes,
Androidapi.JNI.Webkit, Androidapi.JNI.Net, Androidapi.JNI.App,
Androidapi.JNI.Support, FMX.Platform.Android;
constructor TDialog.Create;
begin
FRequestCode := Random(9999999);
TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification, ResultCallback);
end;
destructor TDialog.Destroy;
begin
TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, ResultCallback);
end;
function GetRealPathFromURI(Uri: Jnet_Uri): string;
var
Cursor: JCursor;
ColumnIndex: Integer;
begin
Cursor := TAndroidHelper.Context.getContentResolver.query(Uri, nil, nil, nil, nil);
ColumnIndex := Cursor.getColumnIndexOrThrow(StringToJString('_data'));
Cursor.moveToFirst;
Result := JStringToString(Cursor.getString(ColumnIndex));
Cursor.close;
end;
procedure TDialog.ResultCallback(const Sender: TObject; const M: TMessage);
begin
try
if TMessageResultNotification(M).RequestCode = FRequestCode then
if TMessageResultNotification(M).ResultCode = TJActivity.JavaClass.RESULT_OK then
try
FProc(GetRealPathFromURI(TMessageResultNotification(M).Value.getData));
except
on E: Exception do
ShowMessage(E.Message);
end;
finally
Free;
end;
end;
procedure TDialog.Show(const MimeType: string; Proc: TProc<string>);
begin
FProc := Proc;
var Intent: JIntent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_PICK);
Intent.setType(StringToJString(MimeType));
//Intent.putExtra(TJIntent.JavaClass.EXTRA_ALLOW_MULTIPLE, False);
MainActivity.startActivityForResult(Intent, FRequestCode);
end;
procedure OpenFileDialog(const MimeType: string; Proc: TProc<string>);
begin
PermissionsService.RequestPermissions([JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE)],
procedure(const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray)
begin
if (Length(AGrantResults) > 0) and (AGrantResults[0] = TPermissionStatus.Granted) then
TDialog.Create.Show(MimeType, Proc);
end);
end;
{$ELSE}
interface
implementation
{$ENDIF}
end.