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

745 lines
30 KiB
Java
Raw Normal View History

2019-05-06 07:10:13 +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/>.
2021-01-01 07:56:36 +00:00
Copyright 2018-2021 by Marcel Bokhorst (M66B)
2019-05-06 07:10:13 +00:00
*/
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
2019-05-06 12:41:03 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2019-05-06 07:10:13 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
2019-12-23 10:21:27 +00:00
import android.widget.CheckBox;
2019-05-06 07:10:13 +00:00
import android.widget.CompoundButton;
2020-07-06 06:42:17 +00:00
import android.widget.ImageButton;
2019-05-06 07:10:13 +00:00
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.constraintlayout.widget.Group;
2019-05-06 07:10:13 +00:00
import androidx.fragment.app.DialogFragment;
2019-09-10 06:53:07 +00:00
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
2019-05-06 07:10:13 +00:00
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2019-05-06 07:10:13 +00:00
2019-12-23 10:21:27 +00:00
import java.text.DateFormatSymbols;
2019-05-06 07:10:13 +00:00
import java.text.SimpleDateFormat;
import java.util.ArrayList;
2019-05-06 07:10:13 +00:00
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
2019-05-06 07:10:13 +00:00
2019-05-06 13:03:35 +00:00
public class FragmentOptionsSynchronize extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
2019-05-06 07:10:13 +00:00
private SwitchCompat swEnabled;
2020-06-12 17:08:08 +00:00
private SwitchCompat swOptimize;
2020-07-06 06:42:17 +00:00
private ImageButton ibOptimizeInfo;
2020-07-06 07:29:53 +00:00
private Spinner spPollInterval;
private RecyclerView rvExempted;
2019-05-06 07:10:13 +00:00
private SwitchCompat swSchedule;
2019-12-23 10:21:27 +00:00
private TextView tvSchedulePro;
2019-05-06 07:10:13 +00:00
private TextView tvScheduleStart;
private TextView tvScheduleEnd;
2019-12-23 10:21:27 +00:00
private CheckBox[] cbDay;
2020-11-14 17:05:50 +00:00
private ImageButton ibSchedules;
2021-07-18 16:27:48 +00:00
2021-08-06 11:36:52 +00:00
private SwitchCompat swQuickSyncImap;
private SwitchCompat swQuickSyncPop;
private SwitchCompat swNodate;
private SwitchCompat swUnseen;
private SwitchCompat swFlagged;
private SwitchCompat swDeleteUnseen;
private SwitchCompat swSyncKept;
2020-05-01 19:47:59 +00:00
private SwitchCompat swGmailThread;
private SwitchCompat swSyncFolders;
2020-06-09 14:46:37 +00:00
private SwitchCompat swSyncSharedFolders;
private SwitchCompat swSubscriptions;
2021-07-18 16:27:48 +00:00
private SwitchCompat swTuneKeepAlive;
private SwitchCompat swCheckAuthentication;
private SwitchCompat swCheckReply;
private SwitchCompat swCheckMx;
2021-06-19 08:00:38 +00:00
private SwitchCompat swCheckBlocklist;
private SwitchCompat swUseBlocklist;
2021-06-26 13:50:20 +00:00
private RecyclerView rvBlocklist;
2021-07-18 16:27:48 +00:00
private Group grpExempted;
private AdapterAccountExempted adapter;
2019-05-06 07:10:13 +00:00
2019-05-06 12:41:03 +00:00
private final static String[] RESET_OPTIONS = new String[]{
2020-06-12 17:08:08 +00:00
"enabled", "poll_interval", "auto_optimize", "schedule", "schedule_start", "schedule_end",
2021-08-06 11:36:52 +00:00
"sync_quick_imap", "sync_quick_pop",
2020-05-01 19:47:59 +00:00
"sync_nodate", "sync_unseen", "sync_flagged", "delete_unseen", "sync_kept", "gmail_thread_id",
"sync_folders", "sync_shared_folders", "subscriptions",
"check_authentication", "check_reply_domain", "check_mx", "check_blocklist", "use_blocklist",
"tune_keep_alive"
2019-05-06 12:41:03 +00:00
};
2019-05-06 07:10:13 +00:00
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2019-06-12 14:15:46 +00:00
setSubtitle(R.string.title_setup);
2019-05-06 12:41:03 +00:00
setHasOptionsMenu(true);
2019-05-06 07:10:13 +00:00
2019-05-06 13:03:35 +00:00
View view = inflater.inflate(R.layout.fragment_options_synchronize, container, false);
2019-05-06 07:10:13 +00:00
// Get controls
swEnabled = view.findViewById(R.id.swEnabled);
2020-06-12 17:08:08 +00:00
swOptimize = view.findViewById(R.id.swOptimize);
2020-07-06 06:42:17 +00:00
ibOptimizeInfo = view.findViewById(R.id.ibOptimizeInfo);
2020-07-06 07:29:53 +00:00
spPollInterval = view.findViewById(R.id.spPollInterval);
2020-11-14 17:05:50 +00:00
2019-05-06 07:10:13 +00:00
swSchedule = view.findViewById(R.id.swSchedule);
rvExempted = view.findViewById(R.id.rvExempted);
2019-12-23 10:21:27 +00:00
tvSchedulePro = view.findViewById(R.id.tvSchedulePro);
2019-05-06 07:10:13 +00:00
tvScheduleStart = view.findViewById(R.id.tvScheduleStart);
tvScheduleEnd = view.findViewById(R.id.tvScheduleEnd);
2019-12-23 10:21:27 +00:00
cbDay = new CheckBox[]{
view.findViewById(R.id.cbDay0),
view.findViewById(R.id.cbDay1),
view.findViewById(R.id.cbDay2),
view.findViewById(R.id.cbDay3),
view.findViewById(R.id.cbDay4),
view.findViewById(R.id.cbDay5),
view.findViewById(R.id.cbDay6)
};
2020-11-14 17:05:50 +00:00
ibSchedules = view.findViewById(R.id.ibSchedules);
2021-08-06 11:36:52 +00:00
swQuickSyncImap = view.findViewById(R.id.swQuickSyncImap);
swQuickSyncPop = view.findViewById(R.id.swQuickSyncPop);
swNodate = view.findViewById(R.id.swNodate);
swUnseen = view.findViewById(R.id.swUnseen);
swFlagged = view.findViewById(R.id.swFlagged);
swDeleteUnseen = view.findViewById(R.id.swDeleteUnseen);
swSyncKept = view.findViewById(R.id.swSyncKept);
2020-05-01 19:47:59 +00:00
swGmailThread = view.findViewById(R.id.swGmailThread);
swSyncFolders = view.findViewById(R.id.swSyncFolders);
2020-06-09 14:46:37 +00:00
swSyncSharedFolders = view.findViewById(R.id.swSyncSharedFolders);
swSubscriptions = view.findViewById(R.id.swSubscriptions);
2021-07-18 16:27:48 +00:00
swTuneKeepAlive = view.findViewById(R.id.swTuneKeepAlive);
swCheckAuthentication = view.findViewById(R.id.swCheckAuthentication);
swCheckReply = view.findViewById(R.id.swCheckReply);
swCheckMx = view.findViewById(R.id.swCheckMx);
2021-06-19 08:00:38 +00:00
swCheckBlocklist = view.findViewById(R.id.swCheckBlocklist);
swUseBlocklist = view.findViewById(R.id.swUseBlocklist);
2021-06-26 13:50:20 +00:00
rvBlocklist = view.findViewById(R.id.rvBlocklist);
2021-07-18 16:27:48 +00:00
grpExempted = view.findViewById(R.id.grpExempted);
2019-05-06 07:10:13 +00:00
2019-05-06 13:30:30 +00:00
setOptions();
2019-05-06 07:10:13 +00:00
// Wire controls
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
swEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("enabled", checked).apply();
}
});
2020-07-06 07:29:53 +00:00
swOptimize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("auto_optimize", checked).apply();
ServiceSynchronize.reload(getContext(), null, false, "optimize");
}
});
ibOptimizeInfo.setOnClickListener(new View.OnClickListener() {
@Override
2020-11-11 16:35:32 +00:00
public void onClick(View v) {
Helper.viewFAQ(v.getContext(), 39);
2020-07-06 07:29:53 +00:00
}
});
2019-05-06 07:10:13 +00:00
spPollInterval.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
Object tag = adapterView.getTag();
int current = (tag == null ? 0 : (Integer) tag);
int[] values = getResources().getIntArray(R.array.pollIntervalValues);
int value = values[position];
if (value != current) {
adapterView.setTag(value);
prefs.edit().putInt("poll_interval", value).apply();
grpExempted.setVisibility(value == 0 ? View.GONE : View.VISIBLE);
2019-05-06 07:10:13 +00:00
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
adapterView.setTag(null);
2019-05-06 07:10:13 +00:00
prefs.edit().remove("poll_interval").apply();
grpExempted.setVisibility(View.GONE);
2019-05-06 07:10:13 +00:00
}
});
rvExempted.setHasFixedSize(false);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rvExempted.setLayoutManager(llm);
adapter = new AdapterAccountExempted(getViewLifecycleOwner(), getContext());
rvExempted.setAdapter(adapter);
2019-05-06 07:10:13 +00:00
swSchedule.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-06-28 11:08:04 +00:00
prefs.edit().putBoolean("schedule", checked).apply();
2019-05-06 07:10:13 +00:00
}
});
2019-12-23 10:21:27 +00:00
Helper.linkPro(tvSchedulePro);
2019-05-06 07:10:13 +00:00
tvScheduleStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
args.putBoolean("start", true);
DialogFragment timePicker = new TimePickerFragment();
timePicker.setArguments(args);
2019-10-12 15:16:53 +00:00
timePicker.show(getParentFragmentManager(), "timePicker");
2019-05-06 07:10:13 +00:00
}
});
tvScheduleEnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
args.putBoolean("start", false);
DialogFragment timePicker = new TimePickerFragment();
timePicker.setArguments(args);
2019-10-12 15:16:53 +00:00
timePicker.show(getParentFragmentManager(), "timePicker");
2019-05-06 07:10:13 +00:00
}
});
2019-12-23 10:21:27 +00:00
String[] daynames = new DateFormatSymbols().getWeekdays();
for (int i = 0; i < 7; i++) {
final int day = i;
cbDay[i].setText(daynames[i + 1]);
cbDay[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
prefs.edit().putBoolean("schedule_day" + day, isChecked).apply();
}
});
}
2019-06-28 19:22:20 +00:00
2020-11-14 17:05:50 +00:00
ibSchedules.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.viewFAQ(v.getContext(), 78);
}
});
2021-08-06 11:36:52 +00:00
swQuickSyncImap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_quick_imap", checked).apply();
}
});
swQuickSyncPop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_quick_pop", checked).apply();
}
});
swNodate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_nodate", checked).apply();
}
});
swUnseen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_unseen", checked).apply();
}
});
swFlagged.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_flagged", checked).apply();
}
});
swDeleteUnseen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("delete_unseen", checked).apply();
}
});
swSyncKept.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_kept", checked).apply();
}
});
2020-05-01 19:47:59 +00:00
swGmailThread.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("gmail_thread_id", checked).apply();
}
});
swSyncFolders.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_folders", checked).apply();
2020-06-09 14:46:37 +00:00
swSyncSharedFolders.setEnabled(checked);
}
});
2020-06-09 14:46:37 +00:00
swSyncSharedFolders.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_shared_folders", checked).apply();
}
});
swSubscriptions.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("subscriptions", checked).apply();
}
});
2021-07-18 16:27:48 +00:00
swTuneKeepAlive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("tune_keep_alive", checked).apply();
}
});
swCheckAuthentication.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean checked) {
prefs.edit().putBoolean("check_authentication", checked).apply();
}
});
swCheckReply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("check_reply_domain", checked).apply();
}
});
swCheckMx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("check_mx", checked).apply();
}
});
2021-06-19 08:00:38 +00:00
swCheckBlocklist.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("check_blocklist", checked).apply();
swUseBlocklist.setEnabled(checked);
}
});
swUseBlocklist.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("use_blocklist", checked).apply();
2021-06-19 08:00:38 +00:00
}
});
2021-06-26 13:50:20 +00:00
rvBlocklist.setHasFixedSize(false);
rvBlocklist.setLayoutManager(new LinearLayoutManager(getContext()));
2021-07-01 16:59:05 +00:00
AdapterBlocklist badapter = new AdapterBlocklist(getContext(), DnsBlockList.getListsAvailable());
2021-06-26 13:50:20 +00:00
rvBlocklist.setAdapter(badapter);
2021-05-12 06:45:56 +00:00
// Initialize
2021-06-27 06:13:56 +00:00
FragmentDialogTheme.setBackground(getContext(), view, false);
2021-05-12 06:45:56 +00:00
DB db = DB.getInstance(getContext());
db.account().liveSynchronizingAccounts().observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
@Override
public void onChanged(List<EntityAccount> accounts) {
if (accounts == null)
accounts = new ArrayList<>();
adapter.set(accounts);
}
});
2019-05-06 07:10:13 +00:00
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
return view;
}
@Override
public void onDestroyView() {
PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroyView();
}
2019-05-06 12:41:03 +00:00
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
2019-09-10 06:53:07 +00:00
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
setOptions();
2019-05-06 12:41:03 +00:00
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_options, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2021-02-05 10:15:02 +00:00
if (item.getItemId() == R.id.menu_default) {
2021-06-26 13:50:20 +00:00
FragmentOptions.reset(getContext(), RESET_OPTIONS, new Runnable() {
@Override
public void run() {
DnsBlockList.reset(getContext());
rvBlocklist.getAdapter().notifyDataSetChanged();
}
});
2021-02-05 10:15:02 +00:00
return true;
2019-05-06 12:41:03 +00:00
}
2021-02-05 10:15:02 +00:00
return super.onOptionsItemSelected(item);
2019-05-06 12:41:03 +00:00
}
2019-05-06 07:10:13 +00:00
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean pro = ActivityBilling.isPro(getContext());
2019-05-06 07:10:13 +00:00
swEnabled.setChecked(prefs.getBoolean("enabled", true));
2020-07-09 06:53:16 +00:00
swOptimize.setChecked(prefs.getBoolean("auto_optimize", false));
2019-05-06 07:10:13 +00:00
2021-05-01 17:02:39 +00:00
int pollInterval = ServiceSynchronize.getPollInterval(getContext());
2019-05-06 07:10:13 +00:00
int[] pollIntervalValues = getResources().getIntArray(R.array.pollIntervalValues);
for (int pos = 0; pos < pollIntervalValues.length; pos++)
if (pollIntervalValues[pos] == pollInterval) {
spPollInterval.setTag(pollInterval);
spPollInterval.setSelection(pos);
break;
}
2020-06-12 17:08:08 +00:00
grpExempted.setVisibility(pollInterval == 0 ? View.GONE : View.VISIBLE);
2019-05-06 07:10:13 +00:00
2019-06-28 11:08:04 +00:00
swSchedule.setChecked(prefs.getBoolean("schedule", false) && pro);
swSchedule.setEnabled(pro);
2019-05-06 07:10:13 +00:00
tvScheduleStart.setText(formatHour(getContext(), prefs.getInt("schedule_start", 0)));
tvScheduleEnd.setText(formatHour(getContext(), prefs.getInt("schedule_end", 0)));
2019-12-23 10:21:27 +00:00
for (int i = 0; i < 7; i++)
cbDay[i].setChecked(prefs.getBoolean("schedule_day" + i, true));
2021-08-06 11:36:52 +00:00
swQuickSyncImap.setChecked(prefs.getBoolean("sync_quick_imap", false));
swQuickSyncPop.setChecked(prefs.getBoolean("sync_quick_pop", true));
swNodate.setChecked(prefs.getBoolean("sync_nodate", false));
swUnseen.setChecked(prefs.getBoolean("sync_unseen", false));
swFlagged.setChecked(prefs.getBoolean("sync_flagged", false));
swDeleteUnseen.setChecked(prefs.getBoolean("delete_unseen", false));
2019-06-15 11:14:41 +00:00
swSyncKept.setChecked(prefs.getBoolean("sync_kept", true));
2020-05-01 19:47:59 +00:00
swGmailThread.setChecked(prefs.getBoolean("gmail_thread_id", false));
swSyncFolders.setChecked(prefs.getBoolean("sync_folders", true));
2020-06-09 14:46:37 +00:00
swSyncSharedFolders.setChecked(prefs.getBoolean("sync_shared_folders", false));
swSyncSharedFolders.setEnabled(swSyncFolders.isChecked());
2020-06-17 14:27:18 +00:00
swSubscriptions.setChecked(prefs.getBoolean("subscriptions", false));
2021-07-18 16:27:48 +00:00
swTuneKeepAlive.setChecked(prefs.getBoolean("tune_keep_alive", true));
swCheckAuthentication.setChecked(prefs.getBoolean("check_authentication", true));
swCheckReply.setChecked(prefs.getBoolean("check_reply_domain", true));
swCheckMx.setChecked(prefs.getBoolean("check_mx", false));
2021-06-19 08:00:38 +00:00
swCheckBlocklist.setChecked(prefs.getBoolean("check_blocklist", false));
swUseBlocklist.setChecked(prefs.getBoolean("use_blocklist", false));
swUseBlocklist.setEnabled(swCheckBlocklist.isChecked());
2019-05-06 07:10:13 +00:00
}
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());
}
2019-09-11 19:43:27 +00:00
public static class TimePickerFragment extends FragmentDialogBase implements TimePickerDialog.OnTimeSetListener {
2019-05-06 07:10:13 +00:00
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = getArguments();
boolean start = args.getBoolean("start");
2020-10-30 09:36:16 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
2019-05-06 07:10:13 +00:00
int minutes = prefs.getInt("schedule_" + (start ? "start" : "end"), 0);
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);
2020-10-30 09:36:16 +00:00
return new TimePickerDialog(getContext(), this,
2019-05-06 07:10:13 +00:00
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
2020-10-30 09:36:16 +00:00
DateFormat.is24HourFormat(getContext()));
2019-05-06 07:10:13 +00:00
}
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();
}
}
2021-06-26 13:14:59 +00:00
public static class AdapterAccountExempted extends RecyclerView.Adapter<AdapterAccountExempted.ViewHolder> {
private Context context;
private LifecycleOwner owner;
private LayoutInflater inflater;
private List<EntityAccount> items = new ArrayList<>();
public class ViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener {
private CheckBox cbExempted;
ViewHolder(View itemView) {
super(itemView);
cbExempted = itemView.findViewById(R.id.cbExempted);
}
private void wire() {
cbExempted.setOnCheckedChangeListener(this);
}
private void unwire() {
cbExempted.setOnCheckedChangeListener(null);
}
private void bindTo(EntityAccount account) {
2020-04-04 06:34:37 +00:00
cbExempted.setEnabled(!account.ondemand && account.protocol == EntityAccount.TYPE_IMAP);
cbExempted.setChecked(account.poll_exempted);
cbExempted.setText(account.name);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
EntityAccount account = items.get(pos);
Bundle args = new Bundle();
args.putLong("id", account.id);
args.putBoolean("exempted", isChecked);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
boolean exempted = args.getBoolean("exempted");
DB db = DB.getInstance(context);
db.account().setAccountPollExempted(id, exempted);
ServiceSynchronize.eval(context, "exempted");
return null;
2021-03-25 08:36:45 +00:00
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.e(ex);
}
}.execute(context, owner, args, "set:exempted");
}
}
AdapterAccountExempted(LifecycleOwner owner, Context context) {
this.owner = owner;
this.context = context;
this.inflater = LayoutInflater.from(context);
setHasStableIds(true);
}
public void set(@NonNull List<EntityAccount> accounts) {
Log.i("Set accounts=" + accounts.size());
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, accounts), false);
items = accounts;
diff.dispatchUpdatesTo(this);
}
private class DiffCallback extends DiffUtil.Callback {
private List<EntityAccount> prev = new ArrayList<>();
private List<EntityAccount> next = new ArrayList<>();
DiffCallback(List<EntityAccount> prev, List<EntityAccount> next) {
this.prev.addAll(prev);
this.next.addAll(next);
}
@Override
public int getOldListSize() {
return prev.size();
}
@Override
public int getNewListSize() {
return next.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
EntityAccount a1 = prev.get(oldItemPosition);
EntityAccount a2 = next.get(newItemPosition);
return a1.id.equals(a2.id);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
EntityAccount a1 = prev.get(oldItemPosition);
EntityAccount a2 = next.get(newItemPosition);
return (a1.ondemand == a2.ondemand &&
a1.poll_exempted == a2.poll_exempted &&
Objects.equals(a1.name, a2.name));
}
}
@Override
public long getItemId(int position) {
return items.get(position).id;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(inflater.inflate(R.layout.item_account_exempted, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire();
EntityAccount account = items.get(position);
holder.bindTo(account);
holder.wire();
}
}
2021-06-26 13:14:59 +00:00
public static class AdapterBlocklist extends RecyclerView.Adapter<AdapterBlocklist.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private List<DnsBlockList.BlockList> items;
public class ViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener {
private CheckBox cbEnabled;
ViewHolder(View itemView) {
super(itemView);
cbEnabled = itemView.findViewById(R.id.cbEnabled);
}
private void wire() {
cbEnabled.setOnCheckedChangeListener(this);
}
private void unwire() {
cbEnabled.setOnCheckedChangeListener(null);
}
private void bindTo(DnsBlockList.BlockList blocklist) {
cbEnabled.setText(blocklist.name);
cbEnabled.setChecked(DnsBlockList.isEnabled(context, blocklist));
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
DnsBlockList.BlockList blocklist = items.get(pos);
DnsBlockList.setEnabled(context, blocklist, isChecked);
}
}
AdapterBlocklist(Context context, List<DnsBlockList.BlockList> items) {
this.context = context;
this.inflater = LayoutInflater.from(context);
setHasStableIds(true);
this.items = items;
}
@Override
public long getItemId(int position) {
return items.get(position).id;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(inflater.inflate(R.layout.item_blocklist_enabled, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire();
DnsBlockList.BlockList blocklist = items.get(position);
holder.bindTo(blocklist);
holder.wire();
}
}
2019-05-06 07:10:13 +00:00
}