var MyArrayPermissionsExternalStorage: TArray<string>; MyArrayPermissionsAddressBook : TArray<string>;
procedure TForm2.FormCreate(Sender: TObject); begin // end;
procedure TForm2.GetAllGroupsOnAddressBook; var LAddressBookGroups: TAddressBookGroups; begin LAddressBookGroups := nil; LAddressBookGroups := TAddressBookGroups.Create; // AddressBook1.AllGroups(AddressBook1.DefaultSource, LAddressBookGroups); // ComboBox1.Items.Clear; // ComboBox1.BeginUpdate; try for var MyItem in LAddressBookGroups do ComboBox1.Items.Add(MyItem.ID.ToString + ', ' + MyItem.Name); finally ComboBox1.EndUpdate; end; end;
procedure TForm2.GetAllContactsInGroupOnAddressBook(ABookGroup: TAddressBookGroup); var LAddressBookContacts: TAddressBookContacts; begin LAddressBookContacts := nil; // LAddressBookContacts := TAddressBookContacts.Create; // AddressBook1.AllContactsInGroup(ABookGroup, LAddressBookContacts); // FillListViewContacts(LAddressBookContacts); end;
procedure TForm2.GetAllContactsOnAddressBook; var LAddressBookContacts: TAddressBookContacts; begin LAddressBookContacts := nil; // LAddressBookContacts := TAddressBookContacts.Create; // AddressBook1.AllContacts(AddressBook1.DefaultSource, LAddressBookContacts); // FillListViewContacts(LAddressBookContacts); end;
procedure TForm2.FillListViewContacts(ABookContacts: TAddressBookContacts); var LListViewItem: TListViewItem; begin ListView1.Items.Clear; // ListView1.BeginUpdate; try for var MyItem in ABookContacts do begin LListViewItem := ListView1.Items.Add; LListViewItem.Text := MyItem.DisplayName; end; finally ListView1.EndUpdate; end; end;
procedure TForm2.RequestPermAddressBook(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray); function AllGrantsItsOK(const AGrants: TClassicPermissionStatusDynArray): boolean; var LResult: integer; begin LResult := 1; // => TPermissionStatus = Denied // for var MyBool in AGrants do LResult := LResult + integer(MyBool); // result := (LResult - 1) = 0; end;
begin // It's mandatory: GetAccounts, ReadContacts!!! if (Length(APermissions) = Length(MyArrayPermissionsAddressBook)) and AllGrantsItsOK(AGrantResults) then begin GetAllGroupsOnAddressBook; // ComboBox1.ItemIndex := 0; // fill the listview... end; end;
procedure TForm2.DisplayRationale(Sender: TObject; const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc); function HaveIThisPermission(const APerm: TClassicStringDynArray; const AHave: TArray<string>): boolean; begin result := false; // for var A in APerm do for var B in AHave do if A = B then exit(true); end;
var RationaleMsg: string; begin RationaleMsg := ''; // // It's possible "join it" for just one avaliation!!! -> it's needs review like do it to show the "message"!!! // if HaveIThisPermission(APermissions, MyArrayPermissionsAddressBook) then RationaleMsg := RationaleMsg + 'The app needs permissions to look in your address book' else if HaveIThisPermission(APermissions, MyArrayPermissionsExternalStorage) then RationaleMsg := RationaleMsg + 'The app needs permissions to look in your external storage' else // other... ; // // Show an explanation to the user *asynchronously* - don't block this thread waiting for the user's response! // After the user sees the explanation, invoke the post-rationale routine to request the permissions TDialogService.ShowMessage(RationaleMsg, procedure(const AResult: TModalResult) begin APostRationaleProc end) end;
procedure TForm2.ComboBox1Change(Sender: TObject); var LAddressBookGroup: TAddressBookGroup; begin if (ComboBox1.ItemIndex > -1) then begin LAddressBookGroup := AddressBook1.GroupByID(ComboBox1.ItemIndex + 1); // { First Group = 1 } // GetAllContactsInGroupOnAddressBook(LAddressBookGroup); end; end;
procedure TForm2.SBreadAddressBookClick(Sender: TObject); begin PermissionsService.RequestPermissions(MyArrayPermissionsAddressBook, RequestPermAddressBook, DisplayRationale); end;
initialization
MyArrayPermissionsExternalStorage := [ { not necessary in AddressBook reading... } JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE), { if used... } JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE) { if used... } ];
MyArrayPermissionsAddressBook := [ { } JStringToString(TJManifest_permission.JavaClass.GET_ACCOUNTS), { mandatory! } JStringToString(TJManifest_permission.JavaClass.READ_CONTACTS), { mandatory! } JStringToString(TJManifest_permission.JavaClass.WRITE_CONTACTS) { only if save new contacts! } ];
finalization
end.
此帖子包含附件: 大小:37.3K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3