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/>.
|
|
|
|
|
2024-01-01 07:50:49 +00:00
|
|
|
Copyright 2018-2024 by Marcel Bokhorst (M66B)
|
2022-11-01 17:57:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2023-05-18 07:10:28 +00:00
|
|
|
Bundle args = getArguments();
|
|
|
|
boolean forevent = args.getBoolean("forevent");
|
|
|
|
|
|
|
|
String selectedCalendar = args.getString("calendar");
|
2022-11-03 10:36:34 +00:00
|
|
|
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
|
|
|
|
)) {
|
2024-01-26 06:49:59 +00:00
|
|
|
if (cursor != null) {
|
|
|
|
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);
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
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);
|
|
|
|
if (account != null)
|
|
|
|
calendars.add(new Calendar(id, account, type, primary, visible, name));
|
|
|
|
}
|
2022-11-01 17:57:37 +00:00
|
|
|
}
|
2022-11-12 07:08:56 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
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);
|
2024-01-26 06:49:59 +00:00
|
|
|
args.putLong("id", calendar.id);
|
|
|
|
args.putString("account", calendar.account);
|
|
|
|
args.putString("type", calendar.type);
|
|
|
|
args.putString("name", calendar.name);
|
2022-11-01 17:57:37 +00:00
|
|
|
sendResult(RESULT_OK);
|
|
|
|
dismiss();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-05-18 07:10:28 +00:00
|
|
|
if (!forevent)
|
|
|
|
builder.setNegativeButton(R.string.title_reset, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2024-01-26 06:49:59 +00:00
|
|
|
args.putLong("id", -1);
|
|
|
|
args.putString("account", null);
|
|
|
|
args.putString("type", null);
|
2023-05-18 07:10:28 +00:00
|
|
|
sendResult(RESULT_OK);
|
|
|
|
}
|
|
|
|
});
|
2022-11-01 17:57:37 +00:00
|
|
|
|
|
|
|
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;
|
2023-03-15 08:51:32 +00:00
|
|
|
this.name = 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) : "") +
|
2023-03-15 08:51:32 +00:00
|
|
|
(TextUtils.isEmpty(this.name) || Objects.equals(this.account, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|