mirror of
https://github.com/M66B/FairEmail.git
synced 2026-03-29 21:34:44 +02:00
Added weekend scheduling
This commit is contained in:
@@ -22,8 +22,10 @@ package eu.faircode.email;
|
||||
import android.app.Dialog;
|
||||
import android.app.TimePickerDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Typeface;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.text.format.DateFormat;
|
||||
@@ -44,6 +46,7 @@ import android.widget.TimePicker;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.widget.SwitchCompat;
|
||||
import androidx.constraintlayout.widget.Group;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
@@ -56,8 +59,8 @@ import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import java.text.DateFormatSymbols;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@@ -75,6 +78,9 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
|
||||
private TextView tvSchedulePro;
|
||||
private TextView tvScheduleStart;
|
||||
private TextView tvScheduleEnd;
|
||||
private TextView tvScheduleStartWeekend;
|
||||
private TextView tvScheduleEndWeekend;
|
||||
private ImageButton ibWeekend;
|
||||
private CheckBox[] cbDay;
|
||||
private TextView tvScheduleIgnore;
|
||||
private ImageButton ibSchedules;
|
||||
@@ -114,8 +120,12 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
|
||||
|
||||
private AdapterAccountExempted adapter;
|
||||
|
||||
private int textColorTertiary;
|
||||
private int colorAccent;
|
||||
|
||||
private final static String[] RESET_OPTIONS = new String[]{
|
||||
"enabled", "poll_interval", "auto_optimize", "schedule", "schedule_start", "schedule_end",
|
||||
"enabled", "poll_interval", "auto_optimize",
|
||||
"schedule", "schedule_start", "schedule_end", "schedule_start_weekend", "schedule_end_weekend", "weekend",
|
||||
"sync_quick_imap", "sync_quick_pop",
|
||||
"sync_nodate", "sync_unseen", "sync_flagged", "delete_unseen", "sync_kept",
|
||||
"gmail_thread_id", "outlook_thread_id", "subject_threading",
|
||||
@@ -125,6 +135,15 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
|
||||
"tune_keep_alive"
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
final Context context = getContext();
|
||||
this.textColorTertiary = Helper.resolveColor(context, android.R.attr.textColorTertiary);
|
||||
this.colorAccent = Helper.resolveColor(context, R.attr.colorAccent);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
@@ -147,6 +166,9 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
|
||||
tvSchedulePro = view.findViewById(R.id.tvSchedulePro);
|
||||
tvScheduleStart = view.findViewById(R.id.tvScheduleStart);
|
||||
tvScheduleEnd = view.findViewById(R.id.tvScheduleEnd);
|
||||
tvScheduleStartWeekend = view.findViewById(R.id.tvScheduleStartWeekend);
|
||||
tvScheduleEndWeekend = view.findViewById(R.id.tvScheduleEndWeekend);
|
||||
ibWeekend = view.findViewById(R.id.ibWeekend);
|
||||
cbDay = new CheckBox[]{
|
||||
view.findViewById(R.id.cbDay0),
|
||||
view.findViewById(R.id.cbDay1),
|
||||
@@ -268,27 +290,24 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
|
||||
|
||||
Helper.linkPro(tvSchedulePro);
|
||||
|
||||
tvScheduleStart.setOnClickListener(new View.OnClickListener() {
|
||||
View.OnClickListener onSchedule = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Bundle args = new Bundle();
|
||||
args.putBoolean("start", true);
|
||||
DialogFragment timePicker = new TimePickerFragment();
|
||||
timePicker.setArguments(args);
|
||||
timePicker.show(getParentFragmentManager(), "timePicker");
|
||||
}
|
||||
});
|
||||
int id = v.getId();
|
||||
|
||||
tvScheduleEnd.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Bundle args = new Bundle();
|
||||
args.putBoolean("start", false);
|
||||
args.putBoolean("start", id == R.id.tvScheduleStart || id == R.id.tvScheduleStartWeekend);
|
||||
args.putBoolean("weekend", id == R.id.tvScheduleStartWeekend || id == R.id.tvScheduleEndWeekend);
|
||||
DialogFragment timePicker = new TimePickerFragment();
|
||||
timePicker.setArguments(args);
|
||||
timePicker.show(getParentFragmentManager(), "timePicker");
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
tvScheduleStart.setOnClickListener(onSchedule);
|
||||
tvScheduleEnd.setOnClickListener(onSchedule);
|
||||
tvScheduleStartWeekend.setOnClickListener(onSchedule);
|
||||
tvScheduleEndWeekend.setOnClickListener(onSchedule);
|
||||
|
||||
String[] daynames = new DateFormatSymbols().getWeekdays();
|
||||
for (int i = 0; i < 7; i++) {
|
||||
@@ -302,6 +321,14 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
|
||||
});
|
||||
}
|
||||
|
||||
ibWeekend.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
FragmentDialogWeekend fragment = new FragmentDialogWeekend();
|
||||
fragment.show(getParentFragmentManager(), "weekend");
|
||||
}
|
||||
});
|
||||
|
||||
tvScheduleIgnore.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
@@ -571,13 +598,14 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
|
||||
if (view == null || getContext() == null)
|
||||
return;
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
boolean pro = ActivityBilling.isPro(getContext());
|
||||
final Context context = getContext();
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
boolean pro = ActivityBilling.isPro(context);
|
||||
|
||||
swEnabled.setChecked(prefs.getBoolean("enabled", true));
|
||||
swOptimize.setChecked(prefs.getBoolean("auto_optimize", false));
|
||||
|
||||
int pollInterval = ServiceSynchronize.getPollInterval(getContext());
|
||||
int pollInterval = ServiceSynchronize.getPollInterval(context);
|
||||
int[] pollIntervalValues = getResources().getIntArray(R.array.pollIntervalValues);
|
||||
for (int pos = 0; pos < pollIntervalValues.length; pos++)
|
||||
if (pollIntervalValues[pos] == pollInterval) {
|
||||
@@ -591,10 +619,22 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
|
||||
|
||||
swSchedule.setChecked(prefs.getBoolean("schedule", false) && pro);
|
||||
swSchedule.setEnabled(pro);
|
||||
tvScheduleStart.setText(formatHour(getContext(), prefs.getInt("schedule_start", 0)));
|
||||
tvScheduleEnd.setText(formatHour(getContext(), prefs.getInt("schedule_end", 0)));
|
||||
for (int i = 0; i < 7; i++)
|
||||
|
||||
int schedule_start = prefs.getInt("schedule_start", 0);
|
||||
int schedule_end = prefs.getInt("schedule_end", 0);
|
||||
int schedule_start_weekend = prefs.getInt("schedule_start_weekend", schedule_start);
|
||||
int schedule_end_weekend = prefs.getInt("schedule_end_weekend", schedule_end);
|
||||
tvScheduleStart.setText(CalendarHelper.formatHour(context, schedule_start));
|
||||
tvScheduleEnd.setText(CalendarHelper.formatHour(context, schedule_end));
|
||||
tvScheduleStartWeekend.setText(CalendarHelper.formatHour(context, schedule_start_weekend));
|
||||
tvScheduleEndWeekend.setText(CalendarHelper.formatHour(context, schedule_end_weekend));
|
||||
|
||||
for (int i = 0; i < 7; i++) {
|
||||
boolean weekend = CalendarHelper.isWeekend(context, i + 1);
|
||||
cbDay[i].setTypeface(weekend ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
|
||||
cbDay[i].setTextColor(weekend ? colorAccent : textColorTertiary);
|
||||
cbDay[i].setChecked(prefs.getBoolean("schedule_day" + i, true));
|
||||
}
|
||||
|
||||
swQuickSyncImap.setChecked(prefs.getBoolean("sync_quick_imap", false));
|
||||
swQuickSyncPop.setChecked(prefs.getBoolean("sync_quick_pop", true));
|
||||
@@ -628,24 +668,13 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
|
||||
rvBlocklist.setAlpha(swCheckBlocklist.isChecked() ? 1.0f : Helper.LOW_LIGHT);
|
||||
}
|
||||
|
||||
private String formatHour(Context context, int minutes) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.set(Calendar.HOUR_OF_DAY, minutes / 60);
|
||||
cal.set(Calendar.MINUTE, minutes % 60);
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
cal.set(Calendar.MILLISECOND, 0);
|
||||
return Helper.getTimeInstance(context, SimpleDateFormat.SHORT).format(cal.getTime());
|
||||
}
|
||||
|
||||
public static class TimePickerFragment extends FragmentDialogBase implements TimePickerDialog.OnTimeSetListener {
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
Bundle args = getArguments();
|
||||
boolean start = args.getBoolean("start");
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
int minutes = prefs.getInt("schedule_" + (start ? "start" : "end"), 0);
|
||||
final Context context = getContext();
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
int minutes = prefs.getInt(getKey(), 0);
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.set(Calendar.HOUR_OF_DAY, minutes / 60);
|
||||
@@ -653,21 +682,58 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
cal.set(Calendar.MILLISECOND, 0);
|
||||
|
||||
return new TimePickerDialog(getContext(), this,
|
||||
return new TimePickerDialog(context, this,
|
||||
cal.get(Calendar.HOUR_OF_DAY),
|
||||
cal.get(Calendar.MINUTE),
|
||||
DateFormat.is24HourFormat(getContext()));
|
||||
DateFormat.is24HourFormat(context));
|
||||
}
|
||||
|
||||
public void onTimeSet(TimePicker view, int hour, int minute) {
|
||||
Bundle args = getArguments();
|
||||
boolean start = args.getBoolean("start");
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
editor.putInt("schedule_" + (start ? "start" : "end"), hour * 60 + minute);
|
||||
editor.putBoolean("schedule", true);
|
||||
editor.apply();
|
||||
editor.putInt(getKey(), hour * 60 + minute).putBoolean("schedule", true).apply();
|
||||
}
|
||||
|
||||
private String getKey() {
|
||||
Bundle args = getArguments();
|
||||
boolean start = args.getBoolean("start");
|
||||
boolean weekend = args.getBoolean("weekend");
|
||||
return "schedule" + (start ? "_start" : "_end") + (weekend ? "_weekend" : "");
|
||||
}
|
||||
}
|
||||
|
||||
public static class FragmentDialogWeekend extends FragmentDialogBase {
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
||||
boolean[] days = new boolean[7];
|
||||
|
||||
final Context context = getContext();
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
String[] daynames = Arrays.copyOfRange(new DateFormatSymbols().getWeekdays(), 1, 8);
|
||||
|
||||
String weekend = prefs.getString("weekend", Calendar.SATURDAY + "," + Calendar.SUNDAY);
|
||||
for (String day : weekend.split(","))
|
||||
days[Integer.parseInt(day) - 1] = true;
|
||||
|
||||
return new AlertDialog.Builder(context)
|
||||
.setTitle(R.string.title_advanced_schedule_weekend)
|
||||
.setMultiChoiceItems(daynames, days, new DialogInterface.OnMultiChoiceClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < days.length; i++)
|
||||
if (days[i]) {
|
||||
if (sb.length() > 0)
|
||||
sb.append(",");
|
||||
sb.append(i + 1);
|
||||
}
|
||||
prefs.edit().putString("weekend", sb.toString()).apply();
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.title_setup_done, null)
|
||||
.create();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user