Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ class BackupListAdapter(
notifyItemInserted(calendarFiles.lastIndex)
}

@SuppressLint("NotifyDataSetChanged")
fun reloadCalendars() {
cachedAndroidCalendars = AndroidCalendar.loadAll(context.contentResolver)
notifyDataSetChanged()
}

@SuppressLint("NotifyDataSetChanged")
fun replaceVcards(vCards: List<VCard>) {
contacts.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

import javax.inject.Inject;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand Down Expand Up @@ -218,6 +220,10 @@ public View onCreateView(@NonNull final LayoutInflater inflater, ViewGroup conta
}
}

if (listAdapter.hasCalendarEntry()) {
checkAndAskForCalendarReadPermission();
}

binding.restoreSelected.setOnClickListener(v -> {
if (checkAndAskForCalendarWritePermission()) {
importCalendar();
Expand Down Expand Up @@ -408,58 +414,72 @@ private void closeFragment() {
}
}

// region permission check result listener
private final ActivityResultLauncher<String> contactsWritePermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
importContacts(selectedAccount);
} else {
showPermissionErrorMessage();
}
});

private final ActivityResultLauncher<String> calendarReadPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
listAdapter.reloadCalendars();
}
});

private final ActivityResultLauncher<String> calendarWritePermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
importCalendar();
} else {
showPermissionErrorMessage();
}
});
// endregion

// region permission checks
private boolean checkAndAskForContactsWritePermission() {
// check permissions
if (!PermissionUtil.checkSelfPermission(getContext(), Manifest.permission.WRITE_CONTACTS)) {
requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS},
PermissionUtil.PERMISSIONS_WRITE_CONTACTS);
final var context = getContext();
if (context == null) {
return false;
} else {
return true;
}
}

private boolean checkAndAskForCalendarWritePermission() {
// check permissions
if (!PermissionUtil.checkSelfPermission(getContext(), Manifest.permission.WRITE_CALENDAR)) {
requestPermissions(new String[]{Manifest.permission.WRITE_CALENDAR},
PermissionUtil.PERMISSIONS_WRITE_CALENDAR);
if (!PermissionUtil.checkSelfPermission(context, Manifest.permission.WRITE_CONTACTS)) {
contactsWritePermissionLauncher.launch(Manifest.permission.WRITE_CONTACTS);
return false;
} else {
return true;
}

return true;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == PermissionUtil.PERMISSIONS_WRITE_CONTACTS) {
for (int index = 0; index < permissions.length; index++) {
if (Manifest.permission.WRITE_CONTACTS.equalsIgnoreCase(permissions[index])) {
if (grantResults[index] >= 0) {
importContacts(selectedAccount);
} else {
showPermissionErrorMessage();
}
break;
}
}
private void checkAndAskForCalendarReadPermission() {
final var context = getContext();
if (context == null) {
return;
}

if (requestCode == PermissionUtil.PERMISSIONS_WRITE_CALENDAR) {
for (int index = 0; index < permissions.length; index++) {
if (Manifest.permission.WRITE_CALENDAR.equalsIgnoreCase(permissions[index])) {
if (grantResults[index] >= 0) {
importContacts(selectedAccount);
} else {
showPermissionErrorMessage();
}
break;
}
}
if (!PermissionUtil.checkSelfPermission(context, Manifest.permission.READ_CALENDAR)) {
calendarReadPermissionLauncher.launch(Manifest.permission.READ_CALENDAR);
}
}

private boolean checkAndAskForCalendarWritePermission() {
final var context = getContext();
if (context == null) {
return false;
}

if (!PermissionUtil.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR)) {
calendarWritePermissionLauncher.launch(Manifest.permission.WRITE_CALENDAR);
return false;
}
return true;
}
// endregion

private void showPermissionErrorMessage() {
DisplayUtils.showSnackMessage(this, R.string.contactlist_no_permission);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ private static String getString(Cursor cur, String dbName) {
}

private static boolean missing(ContentResolver resolver, Uri uri) {
// Determine if a provider is missing
// Determine if a provider is missing or inaccessible.
// acquireContentProviderClient throws SecurityException when the calendar
// permission has not been granted, so treat that as missing too.
try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) {
return provider == null;
} catch (SecurityException e) {
Log_OC.w(TAG, "Calendar provider is not accessible: " + e.getMessage());
return true;
}
}

Expand Down
Loading