FairEmail/app/src/main/java/eu/faircode/email/FragmentDialogCalendar.java

167 lines
6.6 KiB
Java
Raw Normal View History

2022-11-01 17:57:37 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/
import static android.app.Activity.RESULT_OK;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CalendarContract;
2022-11-03 10:36:34 +00:00
import android.text.TextUtils;
2022-11-01 17:57:37 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2022-11-03 10:36:34 +00:00
import org.json.JSONObject;
2022-11-01 17:57:37 +00:00
import java.util.ArrayList;
import java.util.List;
2022-11-03 10:36:34 +00:00
import java.util.Objects;
2022-11-01 17:57:37 +00:00
public class FragmentDialogCalendar extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Context context = getContext();
final ContentResolver resolver = context.getContentResolver();
2022-11-03 10:36:34 +00:00
String selectedCalendar = getArguments().getString("calendar");
String selectedAccount;
String selectedName;
try {
JSONObject jselected = new JSONObject(selectedCalendar);
selectedAccount = jselected.getString("account");
2022-11-04 10:20:29 +00:00
selectedName = jselected.optString("name", null);
2022-11-03 10:36:34 +00:00
} catch (Throwable ex) {
Log.i(ex);
selectedAccount = selectedCalendar;
selectedName = null;
}
2022-11-01 17:57:37 +00:00
List<Calendar> calendars = new ArrayList<>();
try (Cursor cursor = resolver.query(CalendarContract.Calendars.CONTENT_URI,
new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.ACCOUNT_NAME,
2022-11-03 10:36:34 +00:00
CalendarContract.Calendars.ACCOUNT_TYPE,
CalendarContract.Calendars.IS_PRIMARY,
CalendarContract.Calendars.VISIBLE,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME
2022-11-01 17:57:37 +00:00
},
2022-11-03 13:36:02 +00:00
CalendarContract.Calendars.VISIBLE + " <> 0",
2022-11-01 17:57:37 +00:00
null,
2022-11-03 13:36:02 +00:00
CalendarContract.Calendars.ACCOUNT_NAME + "," +
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME
)) {
2022-11-01 17:57:37 +00:00
2022-11-03 12:59:27 +00:00
int colId = cursor.getColumnIndexOrThrow(CalendarContract.Calendars._ID);
int colAccount = cursor.getColumnIndexOrThrow(CalendarContract.Calendars.ACCOUNT_NAME);
int colType = cursor.getColumnIndexOrThrow(CalendarContract.Calendars.ACCOUNT_TYPE);
int colPrimary = cursor.getColumnIndexOrThrow(CalendarContract.Calendars.IS_PRIMARY);
int colVisible = cursor.getColumnIndexOrThrow(CalendarContract.Calendars.VISIBLE);
int colDisplay = cursor.getColumnIndexOrThrow(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME);
2022-11-01 17:57:37 +00:00
while (cursor.moveToNext()) {
2022-11-03 12:59:27 +00:00
long id = cursor.getLong(colId);
String account = cursor.getString(colAccount);
String type = cursor.getString(colType);
boolean primary = (cursor.getInt(colPrimary) != 0);
boolean visible = (cursor.getInt(colVisible) != 0);
String name = cursor.getString(colDisplay);
2022-11-01 17:57:37 +00:00
if (account != null)
2022-11-03 10:36:34 +00:00
calendars.add(new Calendar(id, account, type, primary, visible, name));
2022-11-01 17:57:37 +00:00
}
}
int checkedItem = -1;
List<String> names = new ArrayList<>();
for (int i = 0; i < calendars.size(); i++) {
Calendar calendar = calendars.get(i);
names.add(calendar.getTitle());
2022-11-03 10:36:34 +00:00
if (Objects.equals(calendar.account, selectedAccount) &&
(selectedName == null || Objects.equals(calendar.name, selectedName)))
2022-11-01 17:57:37 +00:00
checkedItem = i;
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
2022-11-02 05:59:05 +00:00
builder.setIcon(R.drawable.twotone_event_24);
builder.setTitle(R.string.title_calendar);
2022-11-01 17:57:37 +00:00
builder.setSingleChoiceItems(names.toArray(new String[0]), checkedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Calendar calendar = calendars.get(which);
getArguments().putLong("id", calendar.id);
getArguments().putString("account", calendar.account);
getArguments().putString("type", calendar.type);
2022-11-03 10:36:34 +00:00
getArguments().putString("name", calendar.name);
2022-11-01 17:57:37 +00:00
sendResult(RESULT_OK);
dismiss();
}
});
builder.setNegativeButton(R.string.title_reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getArguments().putLong("id", -1);
getArguments().putString("account", null);
getArguments().putString("type", null);
sendResult(RESULT_OK);
}
});
builder.setPositiveButton(android.R.string.cancel, null);
return builder.create();
}
private class Calendar {
2022-11-03 10:36:34 +00:00
private long id;
private String account;
private String type;
private boolean primary;
private boolean visible;
private String name;
Calendar(long id, String account, String type, boolean primary, boolean visible, String name) {
2022-11-01 17:57:37 +00:00
this.id = id;
this.account = account;
this.type = type;
2022-11-03 10:36:34 +00:00
this.primary = primary;
this.visible = visible;
this.name = (Objects.equals(account, name) ? null : name);
2022-11-01 17:57:37 +00:00
}
String getTitle() {
2022-11-03 10:36:34 +00:00
return (this.visible ? "" : "(") +
(this.account == null ? "-" : this.account) +
(BuildConfig.DEBUG && false ? ":" + (this.type == null ? "-" : this.type) : "") +
2022-11-03 13:36:02 +00:00
(TextUtils.isEmpty(this.name) ? "" : ":" + this.name) +
2022-11-03 10:36:34 +00:00
(this.visible ? "" : ")") +
2022-11-03 13:36:02 +00:00
" " + (this.primary ? "*" : "");
2022-11-01 17:57:37 +00:00
}
}
}