unit MainForm;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  FMX.Edit, FMX.Controls.Presentation, FMX.Platform.Android, FMX.Layouts,
  Androidapi.JNI.Os, System.Permissions, System.IOUtils, FMX.ListBox,
  FMX.Objects;
type
  TToolsForm = class(TForm)
    StyleBook1: TStyleBook;
    ComboBox1: TComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Image1: TImage;
    Rectangle1: TRectangle;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    Panel1: TPanel;
    Layout1: TLayout;
    Layout2: TLayout;

    procedure FormCreate(Sender: TObject);
    procedure TakePicturePermissionRequestResult(Sender: TObject;
      const APermissions: TArray<string>;
      const AGrantResults: TArray<TPermissionStatus>);
    procedure DisplayRationale(Sender: TObject;
      const APermissions: TArray<string>; const APostRationaleProc: TProc);
    procedure ComboBox1Change(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure ComboBox1ClosePopup(Sender: TObject);

  private
    { Private declarations }

  public
    { Public declarations }
  end;

var
  ToolsForm: TToolsForm;

implementation

{$R *.fmx}
{$R *.LgXhdpiPh.fmx ANDROID}

uses
  Androidapi.JNI.GraphicsContentViewText, Androidapi.Helpers,
  Androidapi.JNI.Net, Androidapi.JNI.javatypes, Androidapi.JNI.app,
  Androidapi.JNI.Provider, Androidapi.JNIBridge, Androidapi.JNI.Location,
  unit1,unit2,unit3,
  System.Diagnostics,
  FMX.Helpers.Android;

var
  FPermissionReadExternalStorage, FPermissionWriteExternalStorage: string;
  // 申请权限用
  urllist: TStringList; // 读取网址url数据用

procedure TToolsForm.ComboBox1Change(Sender: TObject);
begin
  Edit1.text := urllist.Strings[ComboBox1.itemindex];
end;

procedure TToolsForm.FormCreate(Sender: TObject);

begin

{$IFDEF ANDROID}
  FPermissionReadExternalStorage :=
    JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE);
  FPermissionWriteExternalStorage :=
    JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE);
{$ENDIF}
  PermissionsService.RequestPermissions([FPermissionReadExternalStorage,
    FPermissionWriteExternalStorage], TakePicturePermissionRequestResult,
    DisplayRationale);

end;

procedure TToolsForm.FormShow(Sender: TObject);
var
  txtlist, namelist: TStringList;
  i: integer;
  favoritedir:string;
  favoritefile: String;

begin

  txtlist := TStringList.Create;
  namelist := TStringList.Create;
  urllist := TStringList.Create;
  favoritedir := System.IOUtils.Tpath.GetSharedDocumentsPath +
    '/myfavorite';
  favoritefile := System.IOUtils.Tpath.GetSharedDocumentsPath +
    '/myfavorite/myfavorite.txt';
  if fileexists(favoritefile) then
    txtlist.LoadFromFile(favoritefile)
  else
  begin
    if not directoryexists(favoritedir) then
      createdir(favoritedir);
    txtlist.add('百度');
    txtlist.add('www.baidu.com');
    txtlist.savetoFile(favoritefile)
  end;

  for i := 0 to txtlist.Count - 1 do
  begin
    if Odd(i) then
      urllist.add(txtlist.Strings[i])
    else
      namelist.add(txtlist.Strings[i]);
  end;
  ComboBox1.items.addstrings(namelist);

end;


procedure TToolsForm.SpeedButton1Click(Sender: TObject);  //确认
var
  Intent: JIntent;
  urlstr: string;
begin

  if Edit1.text = 'www.baidu.com' then
    urlstr := Edit1.text + '/s?&wd=' + Edit2.text
  else
    urlstr := Edit1.text;

  Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
    TJnet_Uri.JavaClass.parse(StringToJString('https://' + urlstr)));
  Intent.setPackage(StringToJString('com.android.browser')); // 设置包名；
  TAndroidHelper.Activity.startActivity(Intent);
end;

procedure TToolsForm.SpeedButton2Click(Sender: TObject);  //清空
begin
  Edit1.text := '';
  Edit2.text := '';
end;

procedure TToolsForm.SpeedButton3Click(Sender: TObject);  //音乐盒
begin
  form3.show;
end;

procedure TToolsForm.SpeedButton4Click(Sender: TObject);  //记事本
begin
  form1.show;
end;

procedure TToolsForm.SpeedButton5Click(Sender: TObject);  //日程表
begin
  form2.show;
end;

procedure RequestPermissionsResult(Sender: TObject;
  const APermissions: TArray<string>;
  const AGrantResults: TArray<TPermissionStatus>);
begin

end;

procedure TToolsForm.TakePicturePermissionRequestResult(Sender: TObject;
  const APermissions: TArray<string>;
  const AGrantResults: TArray<TPermissionStatus>);
begin

  // 申请的2个权限: READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE
  if (Length(AGrantResults) = 2) and
    (AGrantResults[0] = TPermissionStatus.Granted) and
    (AGrantResults[1] = TPermissionStatus.Granted) then
  begin
    // 权限被用户允许的代码
  end
  else
    // 权限被用户禁止的代码
    ShowMessage('权限受限，程序不能正常运行！')

end;

procedure TToolsForm.ComboBox1ClosePopup(Sender: TObject);
begin
  ComboBox1.resetfocus;  //ComboBox1失去焦点
end;

procedure TToolsForm.DisplayRationale(Sender: TObject;
  const APermissions: TArray<string>; const APostRationaleProc: TProc);
var
  i: integer;
  RationaleMsg: string;
begin
  for i := 0 to High(APermissions) do
  begin
    if APermissions[i] = FPermissionReadExternalStorage then
      RationaleMsg := RationaleMsg + '应用需要读文件' + SLineBreak + SLineBreak
    else if APermissions[i] = FPermissionWriteExternalStorage then
      RationaleMsg := RationaleMsg + '应用需要写文件';
  end;

  ShowMessage(RationaleMsg)

end;

end.
