2019-01-17 13:29:35 +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-2019 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2019-07-01 13:25:31 +00:00
|
|
|
import android.app.Dialog;
|
2019-08-04 17:14:53 +00:00
|
|
|
import android.app.TimePickerDialog;
|
2019-01-17 13:29:35 +00:00
|
|
|
import android.content.Context;
|
2019-02-01 08:51:11 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
2019-10-10 15:09:08 +00:00
|
|
|
import android.graphics.Color;
|
2019-02-01 08:51:11 +00:00
|
|
|
import android.net.Uri;
|
2019-01-17 13:29:35 +00:00
|
|
|
import android.os.Bundle;
|
2019-01-18 15:44:28 +00:00
|
|
|
import android.os.Handler;
|
2019-02-01 08:51:11 +00:00
|
|
|
import android.provider.ContactsContract;
|
2019-01-17 13:29:35 +00:00
|
|
|
import android.text.TextUtils;
|
2019-08-04 17:14:53 +00:00
|
|
|
import android.text.format.DateFormat;
|
2019-01-17 13:29:35 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ArrayAdapter;
|
2019-05-15 10:52:39 +00:00
|
|
|
import android.widget.Button;
|
2019-01-17 13:29:35 +00:00
|
|
|
import android.widget.CheckBox;
|
2019-09-22 09:44:11 +00:00
|
|
|
import android.widget.CompoundButton;
|
2019-01-17 13:29:35 +00:00
|
|
|
import android.widget.EditText;
|
2019-02-01 08:51:11 +00:00
|
|
|
import android.widget.ImageView;
|
2019-06-01 13:27:21 +00:00
|
|
|
import android.widget.NumberPicker;
|
2019-01-18 15:44:28 +00:00
|
|
|
import android.widget.ScrollView;
|
2019-01-17 13:29:35 +00:00
|
|
|
import android.widget.Spinner;
|
2019-02-01 08:51:11 +00:00
|
|
|
import android.widget.TextView;
|
2019-08-04 17:14:53 +00:00
|
|
|
import android.widget.TimePicker;
|
2019-09-21 13:43:59 +00:00
|
|
|
import android.widget.Toast;
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2019-07-01 13:25:31 +00:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
|
|
import androidx.constraintlayout.widget.Group;
|
2019-08-04 17:14:53 +00:00
|
|
|
import androidx.fragment.app.DialogFragment;
|
|
|
|
import androidx.fragment.app.Fragment;
|
2019-05-22 07:01:25 +00:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2019-04-17 18:21:44 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
|
|
|
import com.google.android.material.snackbar.Snackbar;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2019-08-04 17:14:53 +00:00
|
|
|
import java.text.DateFormatSymbols;
|
|
|
|
import java.text.SimpleDateFormat;
|
2019-01-17 13:29:35 +00:00
|
|
|
import java.util.ArrayList;
|
2019-08-04 17:14:53 +00:00
|
|
|
import java.util.Calendar;
|
2019-05-06 20:34:18 +00:00
|
|
|
import java.util.Collections;
|
2019-01-17 13:29:35 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-02-01 08:51:11 +00:00
|
|
|
import static android.app.Activity.RESULT_OK;
|
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
public class FragmentRule extends FragmentBase {
|
|
|
|
private ViewGroup view;
|
2019-01-18 15:44:28 +00:00
|
|
|
private ScrollView scroll;
|
|
|
|
private ConstraintLayout content;
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-03-01 12:51:57 +00:00
|
|
|
private TextView tvFolder;
|
2019-01-17 13:29:35 +00:00
|
|
|
private EditText etName;
|
2019-01-17 21:25:22 +00:00
|
|
|
private EditText etOrder;
|
|
|
|
private CheckBox cbEnabled;
|
2019-01-18 10:58:55 +00:00
|
|
|
private CheckBox cbStop;
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
private EditText etSender;
|
2019-01-18 10:58:55 +00:00
|
|
|
private CheckBox cbSender;
|
2019-09-01 17:11:39 +00:00
|
|
|
private ImageView ibSender;
|
2019-09-22 09:44:11 +00:00
|
|
|
private CheckBox cbKnownSender;
|
2019-03-17 11:11:12 +00:00
|
|
|
|
|
|
|
private EditText etRecipient;
|
|
|
|
private CheckBox cbRecipient;
|
2019-09-01 17:11:39 +00:00
|
|
|
private ImageView ibRecipient;
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
private EditText etSubject;
|
2019-01-18 10:58:55 +00:00
|
|
|
private CheckBox cbSubject;
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-10-13 14:47:35 +00:00
|
|
|
private CheckBox cbAttachments;
|
|
|
|
|
2019-01-18 13:04:05 +00:00
|
|
|
private EditText etHeader;
|
|
|
|
private CheckBox cbHeader;
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-08-21 08:51:40 +00:00
|
|
|
private Spinner spScheduleDayStart;
|
|
|
|
private Spinner spScheduleDayEnd;
|
|
|
|
private TextView tvScheduleHourStart;
|
|
|
|
private TextView tvScheduleHourEnd;
|
2019-08-04 17:14:53 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
private Spinner spAction;
|
2019-02-01 08:51:11 +00:00
|
|
|
private TextView tvActionRemark;
|
2019-05-15 10:52:39 +00:00
|
|
|
|
2019-06-01 13:27:21 +00:00
|
|
|
private NumberPicker npDuration;
|
2019-08-04 20:06:22 +00:00
|
|
|
private CheckBox cbScheduleEnd;
|
2019-06-01 13:27:21 +00:00
|
|
|
|
2019-10-01 08:13:22 +00:00
|
|
|
private ViewButtonColor btnColor;
|
2019-05-15 10:52:39 +00:00
|
|
|
|
2019-10-09 09:09:00 +00:00
|
|
|
private EditText etKeyword;
|
|
|
|
|
2019-01-17 21:41:00 +00:00
|
|
|
private Spinner spTarget;
|
2019-06-09 19:25:56 +00:00
|
|
|
private CheckBox cbMoveSeen;
|
|
|
|
private CheckBox cbMoveThread;
|
2019-05-15 10:52:39 +00:00
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
private Spinner spIdent;
|
2019-05-15 10:52:39 +00:00
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
private Spinner spAnswer;
|
2019-02-18 09:19:12 +00:00
|
|
|
private CheckBox cbCc;
|
2019-05-15 10:52:39 +00:00
|
|
|
|
2019-03-17 12:49:10 +00:00
|
|
|
private TextView tvAutomation;
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
private BottomNavigationView bottom_navigation;
|
|
|
|
private ContentLoadingProgressBar pbWait;
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
private Group grpReady;
|
2019-06-01 13:27:21 +00:00
|
|
|
private Group grpSnooze;
|
2019-05-15 10:52:39 +00:00
|
|
|
private Group grpFlag;
|
2019-10-09 09:09:00 +00:00
|
|
|
private Group grpKeyword;
|
2019-01-17 13:29:35 +00:00
|
|
|
private Group grpMove;
|
2019-06-09 19:25:56 +00:00
|
|
|
private Group grpMoveProp;
|
2019-01-18 18:40:51 +00:00
|
|
|
private Group grpAnswer;
|
2019-03-17 12:49:10 +00:00
|
|
|
private Group grpAutomation;
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-08-04 17:14:53 +00:00
|
|
|
private ArrayAdapter<String> adapterDay;
|
2019-01-17 13:29:35 +00:00
|
|
|
private ArrayAdapter<Action> adapterAction;
|
2019-01-17 21:41:00 +00:00
|
|
|
private ArrayAdapter<EntityFolder> adapterTarget;
|
2019-01-18 18:40:51 +00:00
|
|
|
private ArrayAdapter<EntityIdentity> adapterIdentity;
|
|
|
|
private ArrayAdapter<EntityAnswer> adapterAnswer;
|
2019-01-17 13:29:35 +00:00
|
|
|
|
|
|
|
private long id = -1;
|
2019-08-20 20:08:38 +00:00
|
|
|
private long copy = -1;
|
2019-01-18 08:23:53 +00:00
|
|
|
private long account = -1;
|
|
|
|
private long folder = -1;
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-05-22 07:01:25 +00:00
|
|
|
private final static int MAX_CHECK = 10;
|
|
|
|
|
2019-06-30 14:55:15 +00:00
|
|
|
private static final int REQUEST_SENDER = 1;
|
|
|
|
private static final int REQUEST_RECIPIENT = 2;
|
|
|
|
private static final int REQUEST_COLOR = 3;
|
2019-07-01 13:25:31 +00:00
|
|
|
private final static int REQUEST_DELETE = 4;
|
2019-08-04 17:14:53 +00:00
|
|
|
private final static int REQUEST_SCHEDULE_START = 5;
|
|
|
|
private final static int REQUEST_SCHEDULE_END = 6;
|
2019-06-30 14:55:15 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
// Get arguments
|
|
|
|
Bundle args = getArguments();
|
2019-08-20 20:08:38 +00:00
|
|
|
if (args.getBoolean("copy"))
|
|
|
|
copy = args.getLong("id", -1);
|
|
|
|
else
|
|
|
|
id = args.getLong("id", -1);
|
2019-01-18 08:23:53 +00:00
|
|
|
account = args.getLong("account", -1);
|
|
|
|
folder = args.getLong("folder", -1);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Nullable
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2019-05-03 05:28:36 +00:00
|
|
|
setSubtitle(R.string.title_rule_caption);
|
2019-03-01 12:51:57 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
view = (ViewGroup) inflater.inflate(R.layout.fragment_rule, container, false);
|
|
|
|
|
|
|
|
// Get controls
|
2019-01-18 15:44:28 +00:00
|
|
|
scroll = view.findViewById(R.id.scroll);
|
|
|
|
content = view.findViewById(R.id.content);
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-03-01 12:51:57 +00:00
|
|
|
tvFolder = view.findViewById(R.id.tvFolder);
|
2019-01-17 13:29:35 +00:00
|
|
|
etName = view.findViewById(R.id.etName);
|
2019-01-17 21:25:22 +00:00
|
|
|
etOrder = view.findViewById(R.id.etOrder);
|
|
|
|
cbEnabled = view.findViewById(R.id.cbEnabled);
|
2019-01-18 10:58:55 +00:00
|
|
|
cbStop = view.findViewById(R.id.cbStop);
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
etSender = view.findViewById(R.id.etSender);
|
2019-01-18 10:58:55 +00:00
|
|
|
cbSender = view.findViewById(R.id.cbSender);
|
2019-09-01 17:11:39 +00:00
|
|
|
ibSender = view.findViewById(R.id.ibSender);
|
2019-09-22 09:44:11 +00:00
|
|
|
cbKnownSender = view.findViewById(R.id.cbKnownSender);
|
2019-03-17 11:11:12 +00:00
|
|
|
|
|
|
|
etRecipient = view.findViewById(R.id.etRecipient);
|
|
|
|
cbRecipient = view.findViewById(R.id.cbRecipient);
|
2019-09-01 17:11:39 +00:00
|
|
|
ibRecipient = view.findViewById(R.id.ibRecipient);
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
etSubject = view.findViewById(R.id.etSubject);
|
2019-01-18 10:58:55 +00:00
|
|
|
cbSubject = view.findViewById(R.id.cbSubject);
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-10-13 14:47:35 +00:00
|
|
|
cbAttachments = view.findViewById(R.id.cbAttachments);
|
|
|
|
|
2019-01-18 13:04:05 +00:00
|
|
|
etHeader = view.findViewById(R.id.etHeader);
|
|
|
|
cbHeader = view.findViewById(R.id.cbHeader);
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-08-21 08:51:40 +00:00
|
|
|
spScheduleDayStart = view.findViewById(R.id.spScheduleDayStart);
|
|
|
|
spScheduleDayEnd = view.findViewById(R.id.spScheduleDayEnd);
|
|
|
|
tvScheduleHourStart = view.findViewById(R.id.tvScheduleHourStart);
|
|
|
|
tvScheduleHourEnd = view.findViewById(R.id.tvScheduleHourEnd);
|
2019-08-04 17:14:53 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
spAction = view.findViewById(R.id.spAction);
|
2019-02-01 08:51:11 +00:00
|
|
|
tvActionRemark = view.findViewById(R.id.tvActionRemark);
|
2019-05-15 10:52:39 +00:00
|
|
|
|
2019-06-01 13:27:21 +00:00
|
|
|
npDuration = view.findViewById(R.id.npDuration);
|
2019-08-04 20:06:22 +00:00
|
|
|
cbScheduleEnd = view.findViewById(R.id.cbScheduleEnd);
|
2019-06-01 13:27:21 +00:00
|
|
|
|
2019-05-15 10:52:39 +00:00
|
|
|
btnColor = view.findViewById(R.id.btnColor);
|
|
|
|
|
2019-10-09 09:09:00 +00:00
|
|
|
etKeyword = view.findViewById(R.id.etKeyword);
|
|
|
|
|
2019-01-17 21:41:00 +00:00
|
|
|
spTarget = view.findViewById(R.id.spTarget);
|
2019-06-09 19:25:56 +00:00
|
|
|
cbMoveSeen = view.findViewById(R.id.cbMoveSeen);
|
|
|
|
cbMoveThread = view.findViewById(R.id.cbMoveThread);
|
2019-05-15 10:52:39 +00:00
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
spIdent = view.findViewById(R.id.spIdent);
|
2019-05-15 10:52:39 +00:00
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
spAnswer = view.findViewById(R.id.spAnswer);
|
2019-02-18 09:19:12 +00:00
|
|
|
cbCc = view.findViewById(R.id.cbCc);
|
2019-05-15 10:52:39 +00:00
|
|
|
|
2019-03-17 12:49:10 +00:00
|
|
|
tvAutomation = view.findViewById(R.id.tvAutomation);
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
bottom_navigation = view.findViewById(R.id.bottom_navigation);
|
2019-06-20 08:33:29 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
pbWait = view.findViewById(R.id.pbWait);
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
grpReady = view.findViewById(R.id.grpReady);
|
2019-06-01 13:27:21 +00:00
|
|
|
grpSnooze = view.findViewById(R.id.grpSnooze);
|
2019-05-15 10:52:39 +00:00
|
|
|
grpFlag = view.findViewById(R.id.grpFlag);
|
2019-10-09 09:09:00 +00:00
|
|
|
grpKeyword = view.findViewById(R.id.grpKeyword);
|
2019-01-17 13:29:35 +00:00
|
|
|
grpMove = view.findViewById(R.id.grpMove);
|
2019-06-09 19:25:56 +00:00
|
|
|
grpMoveProp = view.findViewById(R.id.grpMoveProp);
|
2019-01-18 18:40:51 +00:00
|
|
|
grpAnswer = view.findViewById(R.id.grpAnswer);
|
2019-03-17 12:49:10 +00:00
|
|
|
grpAutomation = view.findViewById(R.id.grpAutomation);
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-09-01 17:11:39 +00:00
|
|
|
ibSender.setOnClickListener(new View.OnClickListener() {
|
2019-02-01 08:51:11 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent pick = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);
|
|
|
|
if (pick.resolveActivity(getContext().getPackageManager()) == null)
|
|
|
|
Snackbar.make(view, R.string.title_no_contacts, Snackbar.LENGTH_LONG).show();
|
|
|
|
else
|
2019-06-30 14:55:15 +00:00
|
|
|
startActivityForResult(Helper.getChooser(getContext(), pick), REQUEST_SENDER);
|
2019-02-01 08:51:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-22 09:44:11 +00:00
|
|
|
cbKnownSender.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
|
etSender.setEnabled(!isChecked);
|
|
|
|
ibSender.setEnabled(!isChecked);
|
|
|
|
cbSender.setEnabled(!isChecked);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-01 17:11:39 +00:00
|
|
|
ibRecipient.setOnClickListener(new View.OnClickListener() {
|
2019-03-17 11:11:12 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent pick = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);
|
|
|
|
if (pick.resolveActivity(getContext().getPackageManager()) == null)
|
|
|
|
Snackbar.make(view, R.string.title_no_contacts, Snackbar.LENGTH_LONG).show();
|
|
|
|
else
|
2019-06-30 14:55:15 +00:00
|
|
|
startActivityForResult(Helper.getChooser(getContext(), pick), REQUEST_RECIPIENT);
|
2019-03-17 11:11:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-04 17:14:53 +00:00
|
|
|
adapterDay = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<String>());
|
|
|
|
adapterDay.setDropDownViewResource(R.layout.spinner_item1_dropdown);
|
2019-08-21 08:51:40 +00:00
|
|
|
spScheduleDayStart.setAdapter(adapterDay);
|
|
|
|
spScheduleDayEnd.setAdapter(adapterDay);
|
2019-08-04 17:14:53 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
adapterAction = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<Action>());
|
|
|
|
adapterAction.setDropDownViewResource(R.layout.spinner_item1_dropdown);
|
|
|
|
spAction.setAdapter(adapterAction);
|
|
|
|
|
2019-01-17 21:41:00 +00:00
|
|
|
adapterTarget = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<EntityFolder>());
|
|
|
|
adapterTarget.setDropDownViewResource(R.layout.spinner_item1_dropdown);
|
|
|
|
spTarget.setAdapter(adapterTarget);
|
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
adapterIdentity = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<EntityIdentity>());
|
|
|
|
adapterIdentity.setDropDownViewResource(R.layout.spinner_item1_dropdown);
|
|
|
|
spIdent.setAdapter(adapterIdentity);
|
|
|
|
|
|
|
|
adapterAnswer = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<EntityAnswer>());
|
|
|
|
adapterAnswer.setDropDownViewResource(R.layout.spinner_item1_dropdown);
|
|
|
|
spAnswer.setAdapter(adapterAnswer);
|
|
|
|
|
2019-08-04 17:14:53 +00:00
|
|
|
String[] dayNames = DateFormatSymbols.getInstance().getWeekdays();
|
|
|
|
for (int day = Calendar.SUNDAY; day <= Calendar.SATURDAY; day++)
|
|
|
|
adapterDay.add(dayNames[day]);
|
|
|
|
|
2019-08-21 08:51:40 +00:00
|
|
|
tvScheduleHourStart.setOnClickListener(new View.OnClickListener() {
|
2019-08-04 17:14:53 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Object time = v.getTag();
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("minutes", time == null ? 0 : (int) time);
|
|
|
|
DialogFragment timePicker = new TimePickerFragment();
|
|
|
|
timePicker.setArguments(args);
|
|
|
|
timePicker.setTargetFragment(FragmentRule.this, REQUEST_SCHEDULE_START);
|
2019-10-12 15:16:53 +00:00
|
|
|
timePicker.show(getParentFragmentManager(), "timePicker");
|
2019-08-04 17:14:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-21 08:51:40 +00:00
|
|
|
tvScheduleHourEnd.setOnClickListener(new View.OnClickListener() {
|
2019-08-04 17:14:53 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Object time = v.getTag();
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("minutes", time == null ? 0 : (int) time);
|
|
|
|
DialogFragment timePicker = new TimePickerFragment();
|
|
|
|
timePicker.setArguments(args);
|
|
|
|
timePicker.setTargetFragment(FragmentRule.this, REQUEST_SCHEDULE_END);
|
2019-10-12 15:16:53 +00:00
|
|
|
timePicker.show(getParentFragmentManager(), "timePicker");
|
2019-08-04 17:14:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
List<Action> actions = new ArrayList<>();
|
2019-10-01 10:40:54 +00:00
|
|
|
actions.add(new Action(EntityRule.TYPE_NOOP, getString(R.string.title_rule_noop)));
|
2019-05-18 06:34:26 +00:00
|
|
|
actions.add(new Action(EntityRule.TYPE_SEEN, getString(R.string.title_rule_seen)));
|
|
|
|
actions.add(new Action(EntityRule.TYPE_UNSEEN, getString(R.string.title_rule_unseen)));
|
2019-10-12 09:09:54 +00:00
|
|
|
actions.add(new Action(EntityRule.TYPE_HIDE, getString(R.string.title_rule_hide)));
|
2019-09-22 09:54:29 +00:00
|
|
|
actions.add(new Action(EntityRule.TYPE_IGNORE, getString(R.string.title_rule_ignore)));
|
2019-06-01 13:27:21 +00:00
|
|
|
actions.add(new Action(EntityRule.TYPE_SNOOZE, getString(R.string.title_rule_snooze)));
|
2019-05-18 06:34:26 +00:00
|
|
|
actions.add(new Action(EntityRule.TYPE_FLAG, getString(R.string.title_rule_flag)));
|
2019-10-09 09:09:00 +00:00
|
|
|
actions.add(new Action(EntityRule.TYPE_KEYWORD, getString(R.string.title_rule_keyword)));
|
2019-05-18 06:34:26 +00:00
|
|
|
actions.add(new Action(EntityRule.TYPE_MOVE, getString(R.string.title_rule_move)));
|
|
|
|
actions.add(new Action(EntityRule.TYPE_COPY, getString(R.string.title_rule_copy)));
|
|
|
|
actions.add(new Action(EntityRule.TYPE_ANSWER, getString(R.string.title_rule_answer)));
|
2019-03-17 12:49:10 +00:00
|
|
|
actions.add(new Action(EntityRule.TYPE_AUTOMATION, getString(R.string.title_rule_automation)));
|
2019-01-17 13:29:35 +00:00
|
|
|
adapterAction.addAll(actions);
|
|
|
|
|
2019-01-17 21:41:00 +00:00
|
|
|
spAction.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
|
2019-01-18 20:35:10 +00:00
|
|
|
Integer prev = (Integer) adapterView.getTag();
|
|
|
|
if (prev != null && !prev.equals(position)) {
|
|
|
|
Action action = (Action) adapterView.getAdapter().getItem(position);
|
|
|
|
onActionSelected(action.type);
|
|
|
|
}
|
|
|
|
adapterView.setTag(position);
|
2019-01-17 21:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNothingSelected(AdapterView<?> parent) {
|
|
|
|
onActionSelected(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onActionSelected(int type) {
|
2019-01-19 17:07:31 +00:00
|
|
|
showActionParameters(type);
|
2019-01-18 15:44:28 +00:00
|
|
|
|
|
|
|
new Handler().post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
scroll.smoothScrollTo(0, content.getBottom());
|
|
|
|
}
|
|
|
|
});
|
2019-01-17 21:41:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-20 19:12:45 +00:00
|
|
|
npDuration.setMinValue(0);
|
|
|
|
npDuration.setMaxValue(999);
|
2019-08-04 20:06:22 +00:00
|
|
|
|
2019-02-01 08:51:11 +00:00
|
|
|
tvActionRemark.setVisibility(View.GONE);
|
|
|
|
|
2019-05-15 10:52:39 +00:00
|
|
|
btnColor.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2019-09-29 14:31:13 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putInt("color", btnColor.getColor());
|
|
|
|
args.putString("title", getString(R.string.title_flag_color));
|
2019-09-29 17:04:21 +00:00
|
|
|
args.putBoolean("reset", true);
|
2019-09-29 14:31:13 +00:00
|
|
|
|
2019-06-30 17:56:23 +00:00
|
|
|
FragmentDialogColor fragment = new FragmentDialogColor();
|
2019-09-29 14:31:13 +00:00
|
|
|
fragment.setArguments(args);
|
2019-06-30 14:55:15 +00:00
|
|
|
fragment.setTargetFragment(FragmentRule.this, REQUEST_COLOR);
|
2019-10-12 15:16:53 +00:00
|
|
|
fragment.show(getParentFragmentManager(), "rule:color");
|
2019-05-15 10:52:39 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-17 12:49:10 +00:00
|
|
|
tvAutomation.setText(getString(R.string.title_rule_automation_hint,
|
|
|
|
EntityRule.ACTION_AUTOMATION,
|
|
|
|
TextUtils.join(",", new String[]{
|
|
|
|
EntityRule.EXTRA_RULE,
|
|
|
|
EntityRule.EXTRA_SENDER,
|
|
|
|
EntityRule.EXTRA_SUBJECT})));
|
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
|
|
|
|
@Override
|
2019-01-18 11:26:02 +00:00
|
|
|
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
|
2019-01-17 13:29:35 +00:00
|
|
|
switch (menuItem.getItemId()) {
|
|
|
|
case R.id.action_delete:
|
2019-07-01 13:25:31 +00:00
|
|
|
onActionDelete();
|
2019-01-17 13:29:35 +00:00
|
|
|
return true;
|
2019-05-22 07:01:25 +00:00
|
|
|
case R.id.action_check:
|
|
|
|
onActionCheck();
|
|
|
|
return true;
|
2019-01-17 13:29:35 +00:00
|
|
|
case R.id.action_save:
|
|
|
|
onActionSave();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Initialize
|
2019-03-01 12:51:57 +00:00
|
|
|
tvFolder.setText(null);
|
2019-01-17 13:29:35 +00:00
|
|
|
bottom_navigation.setVisibility(View.GONE);
|
|
|
|
grpReady.setVisibility(View.GONE);
|
2019-06-01 13:27:21 +00:00
|
|
|
grpSnooze.setVisibility(View.GONE);
|
2019-05-15 10:52:39 +00:00
|
|
|
grpFlag.setVisibility(View.GONE);
|
2019-10-09 09:09:00 +00:00
|
|
|
grpKeyword.setVisibility(View.GONE);
|
2019-01-17 13:29:35 +00:00
|
|
|
grpMove.setVisibility(View.GONE);
|
2019-06-09 19:25:56 +00:00
|
|
|
grpMoveProp.setVisibility(View.GONE);
|
2019-01-18 18:40:51 +00:00
|
|
|
grpAnswer.setVisibility(View.GONE);
|
2019-03-17 12:49:10 +00:00
|
|
|
grpAutomation.setVisibility(View.GONE);
|
2019-01-17 13:29:35 +00:00
|
|
|
pbWait.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
2019-01-18 08:23:53 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("account", account);
|
2019-02-01 08:51:11 +00:00
|
|
|
args.putLong("folder", folder);
|
2019-01-18 08:23:53 +00:00
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
new SimpleTask<RefData>() {
|
2019-01-17 13:29:35 +00:00
|
|
|
@Override
|
2019-01-18 18:40:51 +00:00
|
|
|
protected RefData onExecute(Context context, Bundle args) {
|
2019-02-01 08:51:11 +00:00
|
|
|
long aid = args.getLong("account");
|
|
|
|
long fid = args.getLong("folder");
|
2019-01-18 11:15:07 +00:00
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
RefData data = new RefData();
|
|
|
|
|
2019-01-18 11:15:07 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2019-02-01 08:51:11 +00:00
|
|
|
data.folder = db.folder().getFolder(fid);
|
2019-07-07 07:25:52 +00:00
|
|
|
data.folders = db.folder().getFolders(aid, true, true);
|
2019-01-18 11:15:07 +00:00
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
if (data.folders == null)
|
|
|
|
data.folders = new ArrayList<>();
|
|
|
|
|
2019-05-06 20:34:18 +00:00
|
|
|
if (data.folders.size() > 0)
|
2019-06-28 07:46:18 +00:00
|
|
|
Collections.sort(data.folders, data.folders.get(0).getComparator(null));
|
2019-01-18 18:40:51 +00:00
|
|
|
|
2019-09-23 08:47:05 +00:00
|
|
|
data.identities = db.identity().getSynchronizingIdentities(aid);
|
2019-04-20 08:35:30 +00:00
|
|
|
data.answers = db.answer().getAnswers(false);
|
2019-01-18 11:15:07 +00:00
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
return data;
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-18 18:40:51 +00:00
|
|
|
protected void onExecuted(Bundle args, RefData data) {
|
2019-03-01 12:51:57 +00:00
|
|
|
tvFolder.setText(data.folder.getDisplayName(getContext()));
|
|
|
|
|
2019-01-18 08:23:53 +00:00
|
|
|
adapterTarget.clear();
|
2019-01-18 18:40:51 +00:00
|
|
|
adapterTarget.addAll(data.folders);
|
|
|
|
|
|
|
|
adapterIdentity.clear();
|
|
|
|
adapterIdentity.addAll(data.identities);
|
|
|
|
|
|
|
|
adapterAnswer.clear();
|
|
|
|
adapterAnswer.addAll(data.answers);
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-02-01 08:51:11 +00:00
|
|
|
tvActionRemark.setText(
|
|
|
|
getString(R.string.title_rule_action_remark, data.folder.getDisplayName(getContext())));
|
|
|
|
tvActionRemark.setVisibility(View.VISIBLE);
|
|
|
|
|
2019-02-07 19:45:57 +00:00
|
|
|
loadRule();
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-10-12 15:16:53 +00:00
|
|
|
Helper.unexpectedError(getParentFragmentManager(), ex);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
2019-01-18 08:23:53 +00:00
|
|
|
}.execute(this, args, "rule:accounts");
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 08:51:11 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
2019-06-30 14:55:15 +00:00
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
2019-07-15 12:11:55 +00:00
|
|
|
try {
|
|
|
|
switch (requestCode) {
|
|
|
|
case REQUEST_SENDER:
|
|
|
|
if (resultCode == RESULT_OK && data != null)
|
|
|
|
onPickContact(data, true);
|
|
|
|
break;
|
|
|
|
case REQUEST_RECIPIENT:
|
|
|
|
if (resultCode == RESULT_OK && data != null)
|
|
|
|
onPickContact(data, true);
|
|
|
|
break;
|
|
|
|
case REQUEST_COLOR:
|
|
|
|
if (resultCode == RESULT_OK && data != null) {
|
2019-08-13 08:27:17 +00:00
|
|
|
if (!ActivityBilling.isPro(getContext())) {
|
2019-10-05 13:22:48 +00:00
|
|
|
startActivity(new Intent(getContext(), ActivityBilling.class));
|
2019-07-15 12:11:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bundle args = data.getBundleExtra("args");
|
2019-09-29 12:45:00 +00:00
|
|
|
btnColor.setColor(args.getInt("color"));
|
2019-06-30 14:55:15 +00:00
|
|
|
}
|
2019-07-15 12:11:55 +00:00
|
|
|
break;
|
|
|
|
case REQUEST_DELETE:
|
|
|
|
if (resultCode == RESULT_OK)
|
|
|
|
onDelete();
|
|
|
|
break;
|
2019-08-04 17:14:53 +00:00
|
|
|
case REQUEST_SCHEDULE_START:
|
|
|
|
if (resultCode == RESULT_OK)
|
|
|
|
onScheduleStart(data);
|
|
|
|
break;
|
|
|
|
case REQUEST_SCHEDULE_END:
|
|
|
|
if (resultCode == RESULT_OK)
|
|
|
|
onScheduleEnd(data);
|
|
|
|
break;
|
2019-07-15 12:11:55 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2019-02-01 08:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 13:25:31 +00:00
|
|
|
private void onPickContact(Intent data, boolean sender) {
|
2019-02-22 15:59:23 +00:00
|
|
|
Uri uri = data.getData();
|
|
|
|
if (uri == null) return;
|
|
|
|
try (Cursor cursor = getContext().getContentResolver().query(uri,
|
|
|
|
new String[]{
|
|
|
|
ContactsContract.CommonDataKinds.Email.ADDRESS
|
|
|
|
},
|
|
|
|
null, null, null)) {
|
2019-02-01 08:51:11 +00:00
|
|
|
if (cursor != null && cursor.moveToFirst())
|
2019-03-17 11:11:12 +00:00
|
|
|
if (sender)
|
|
|
|
etSender.setText(cursor.getString(0));
|
|
|
|
else
|
|
|
|
etRecipient.setText(cursor.getString(0));
|
2019-02-01 08:51:11 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2019-10-12 15:16:53 +00:00
|
|
|
Helper.unexpectedError(getParentFragmentManager(), ex);
|
2019-02-01 08:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 13:25:31 +00:00
|
|
|
private void onDelete() {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("id", id);
|
|
|
|
|
|
|
|
new SimpleTask<Void>() {
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute(Bundle args) {
|
|
|
|
Helper.setViewsEnabled(view, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bundle args) {
|
|
|
|
Helper.setViewsEnabled(view, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void onExecute(Context context, Bundle args) {
|
|
|
|
long id = args.getLong("id");
|
|
|
|
DB.getInstance(context).rule().deleteRule(id);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, Void data) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-10-12 15:16:53 +00:00
|
|
|
Helper.unexpectedError(getParentFragmentManager(), ex);
|
2019-07-01 13:25:31 +00:00
|
|
|
}
|
2019-09-11 12:03:59 +00:00
|
|
|
}.execute(this, args, "rule:delete");
|
2019-07-01 13:25:31 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 17:14:53 +00:00
|
|
|
private void onScheduleStart(Intent data) {
|
|
|
|
int minutes = data.getIntExtra("minutes", 0);
|
2019-08-21 08:51:40 +00:00
|
|
|
tvScheduleHourStart.setTag(minutes);
|
|
|
|
tvScheduleHourStart.setText(formatHour(getContext(), minutes));
|
2019-08-04 20:06:22 +00:00
|
|
|
cbScheduleEnd.setChecked(true);
|
2019-08-04 17:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onScheduleEnd(Intent data) {
|
|
|
|
int minutes = data.getIntExtra("minutes", 0);
|
2019-08-21 08:51:40 +00:00
|
|
|
tvScheduleHourEnd.setTag(minutes);
|
|
|
|
tvScheduleHourEnd.setText(formatHour(getContext(), minutes));
|
2019-08-04 20:06:22 +00:00
|
|
|
cbScheduleEnd.setChecked(true);
|
2019-08-04 17:14:53 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 19:45:57 +00:00
|
|
|
private void loadRule() {
|
|
|
|
Bundle rargs = new Bundle();
|
2019-08-20 20:08:38 +00:00
|
|
|
rargs.putLong("id", copy < 0 ? id : copy);
|
2019-03-01 12:51:57 +00:00
|
|
|
rargs.putString("sender", getArguments().getString("sender"));
|
2019-05-02 11:34:42 +00:00
|
|
|
rargs.putString("recipient", getArguments().getString("recipient"));
|
2019-03-01 12:51:57 +00:00
|
|
|
rargs.putString("subject", getArguments().getString("subject"));
|
2019-02-07 19:45:57 +00:00
|
|
|
|
|
|
|
new SimpleTask<TupleRuleEx>() {
|
|
|
|
@Override
|
|
|
|
protected TupleRuleEx onExecute(Context context, Bundle args) {
|
|
|
|
long id = args.getLong("id");
|
|
|
|
return DB.getInstance(context).rule().getRule(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, TupleRuleEx rule) {
|
|
|
|
try {
|
|
|
|
JSONObject jcondition = (rule == null ? new JSONObject() : new JSONObject(rule.condition));
|
|
|
|
JSONObject jaction = (rule == null ? new JSONObject() : new JSONObject(rule.action));
|
|
|
|
|
|
|
|
JSONObject jsender = jcondition.optJSONObject("sender");
|
2019-03-17 11:11:12 +00:00
|
|
|
JSONObject jrecipient = jcondition.optJSONObject("recipient");
|
2019-02-07 19:45:57 +00:00
|
|
|
JSONObject jsubject = jcondition.optJSONObject("subject");
|
|
|
|
JSONObject jheader = jcondition.optJSONObject("header");
|
2019-08-04 17:14:53 +00:00
|
|
|
JSONObject jschedule = jcondition.optJSONObject("schedule");
|
2019-02-07 19:45:57 +00:00
|
|
|
|
2019-07-13 07:29:29 +00:00
|
|
|
etName.setText(rule == null ? args.getString("subject") : rule.name);
|
2019-02-07 19:45:57 +00:00
|
|
|
etOrder.setText(rule == null ? null : Integer.toString(rule.order));
|
|
|
|
cbEnabled.setChecked(rule == null || rule.enabled);
|
|
|
|
cbStop.setChecked(rule != null && rule.stop);
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-03-01 12:51:57 +00:00
|
|
|
etSender.setText(jsender == null ? args.getString("sender") : jsender.getString("value"));
|
2019-02-07 19:45:57 +00:00
|
|
|
cbSender.setChecked(jsender != null && jsender.getBoolean("regex"));
|
2019-09-22 09:44:11 +00:00
|
|
|
cbKnownSender.setChecked(jsender != null && jsender.optBoolean("known"));
|
|
|
|
etSender.setEnabled(!cbKnownSender.isChecked());
|
|
|
|
ibSender.setEnabled(!cbKnownSender.isChecked());
|
|
|
|
cbSender.setEnabled(!cbKnownSender.isChecked());
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-05-02 11:34:42 +00:00
|
|
|
etRecipient.setText(jrecipient == null ? args.getString("recipient") : jrecipient.getString("value"));
|
2019-03-17 11:11:12 +00:00
|
|
|
cbRecipient.setChecked(jrecipient != null && jrecipient.getBoolean("regex"));
|
|
|
|
|
2019-03-01 12:51:57 +00:00
|
|
|
etSubject.setText(jsubject == null ? args.getString("subject") : jsubject.getString("value"));
|
2019-02-07 19:45:57 +00:00
|
|
|
cbSubject.setChecked(jsubject != null && jsubject.getBoolean("regex"));
|
2019-03-17 11:11:12 +00:00
|
|
|
|
2019-10-13 14:47:35 +00:00
|
|
|
cbAttachments.setChecked(jcondition.optBoolean("attachments"));
|
|
|
|
|
2019-02-07 19:45:57 +00:00
|
|
|
etHeader.setText(jheader == null ? null : jheader.getString("value"));
|
|
|
|
cbHeader.setChecked(jheader != null && jheader.getBoolean("regex"));
|
|
|
|
|
2019-08-04 17:14:53 +00:00
|
|
|
int start = (jschedule != null && jschedule.has("start") ? jschedule.getInt("start") : 0);
|
|
|
|
int end = (jschedule != null && jschedule.has("end") ? jschedule.getInt("end") : 0);
|
2019-08-21 08:51:40 +00:00
|
|
|
|
|
|
|
spScheduleDayStart.setSelection(start / (24 * 60));
|
|
|
|
spScheduleDayEnd.setSelection(end / (24 * 60));
|
|
|
|
|
|
|
|
tvScheduleHourStart.setTag(start % (24 * 60));
|
|
|
|
tvScheduleHourStart.setText(formatHour(getContext(), start % (24 * 60)));
|
|
|
|
|
|
|
|
tvScheduleHourEnd.setTag(end % (24 * 60));
|
|
|
|
tvScheduleHourEnd.setText(formatHour(getContext(), end % (24 * 60)));
|
2019-08-04 17:14:53 +00:00
|
|
|
|
2019-02-07 19:45:57 +00:00
|
|
|
if (rule == null) {
|
|
|
|
for (int pos = 0; pos < adapterIdentity.getCount(); pos++)
|
|
|
|
if (adapterIdentity.getItem(pos).primary) {
|
|
|
|
spIdent.setSelection(pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int type = jaction.getInt("type");
|
|
|
|
switch (type) {
|
2019-06-01 13:27:21 +00:00
|
|
|
case EntityRule.TYPE_SNOOZE:
|
2019-09-09 06:08:24 +00:00
|
|
|
npDuration.setValue(jaction.optInt("duration", 0));
|
2019-08-04 20:06:22 +00:00
|
|
|
cbScheduleEnd.setChecked(jaction.optBoolean("schedule_end", false));
|
2019-06-01 13:27:21 +00:00
|
|
|
break;
|
|
|
|
|
2019-05-15 10:52:39 +00:00
|
|
|
case EntityRule.TYPE_FLAG:
|
2019-09-29 12:45:00 +00:00
|
|
|
btnColor.setColor(
|
|
|
|
!jaction.has("color") || jaction.isNull("color")
|
|
|
|
? null : jaction.getInt("color"));
|
2019-05-15 10:52:39 +00:00
|
|
|
break;
|
|
|
|
|
2019-10-09 09:09:00 +00:00
|
|
|
case EntityRule.TYPE_KEYWORD:
|
|
|
|
etKeyword.setText(jaction.getString("keyword"));
|
|
|
|
break;
|
|
|
|
|
2019-02-07 19:45:57 +00:00
|
|
|
case EntityRule.TYPE_MOVE:
|
2019-05-18 06:34:26 +00:00
|
|
|
case EntityRule.TYPE_COPY:
|
2019-09-09 06:08:24 +00:00
|
|
|
long target = jaction.optLong("target", -1);
|
2019-02-07 19:45:57 +00:00
|
|
|
for (int pos = 0; pos < adapterTarget.getCount(); pos++)
|
|
|
|
if (adapterTarget.getItem(pos).id.equals(target)) {
|
|
|
|
spTarget.setSelection(pos);
|
|
|
|
break;
|
|
|
|
}
|
2019-06-09 19:25:56 +00:00
|
|
|
if (type == EntityRule.TYPE_MOVE) {
|
|
|
|
cbMoveSeen.setChecked(jaction.optBoolean("seen"));
|
|
|
|
cbMoveThread.setChecked(jaction.optBoolean("thread"));
|
|
|
|
}
|
2019-02-07 19:45:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityRule.TYPE_ANSWER:
|
2019-09-09 06:08:24 +00:00
|
|
|
long identity = jaction.optLong("identity", -1);
|
2019-02-07 19:45:57 +00:00
|
|
|
for (int pos = 0; pos < adapterIdentity.getCount(); pos++)
|
|
|
|
if (adapterIdentity.getItem(pos).id.equals(identity)) {
|
|
|
|
spIdent.setSelection(pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-09-09 06:08:24 +00:00
|
|
|
long answer = jaction.optLong("answer", -1);
|
2019-02-07 19:45:57 +00:00
|
|
|
for (int pos = 0; pos < adapterAnswer.getCount(); pos++)
|
|
|
|
if (adapterAnswer.getItem(pos).id.equals(answer)) {
|
|
|
|
spAnswer.setSelection(pos);
|
|
|
|
break;
|
|
|
|
}
|
2019-02-18 09:19:12 +00:00
|
|
|
|
2019-06-09 19:25:56 +00:00
|
|
|
cbCc.setChecked(jaction.optBoolean("cc"));
|
2019-02-07 19:45:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int pos = 0; pos < adapterAction.getCount(); pos++)
|
|
|
|
if (adapterAction.getItem(pos).type == type) {
|
|
|
|
spAction.setTag(pos);
|
|
|
|
spAction.setSelection(pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
showActionParameters(type);
|
|
|
|
}
|
2019-09-09 06:08:24 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
} finally {
|
2019-02-07 19:45:57 +00:00
|
|
|
grpReady.setVisibility(View.VISIBLE);
|
|
|
|
bottom_navigation.findViewById(R.id.action_delete).setVisibility(rule == null ? View.GONE : View.VISIBLE);
|
|
|
|
bottom_navigation.setVisibility(View.VISIBLE);
|
|
|
|
pbWait.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-10-12 15:16:53 +00:00
|
|
|
Helper.unexpectedError(getParentFragmentManager(), ex);
|
2019-02-07 19:45:57 +00:00
|
|
|
}
|
2019-09-11 12:03:59 +00:00
|
|
|
}.execute(this, rargs, "rule:get");
|
2019-02-07 19:45:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 13:25:31 +00:00
|
|
|
private void showActionParameters(int type) {
|
|
|
|
grpSnooze.setVisibility(type == EntityRule.TYPE_SNOOZE ? View.VISIBLE : View.GONE);
|
|
|
|
grpFlag.setVisibility(type == EntityRule.TYPE_FLAG ? View.VISIBLE : View.GONE);
|
2019-10-09 09:09:00 +00:00
|
|
|
grpKeyword.setVisibility(type == EntityRule.TYPE_KEYWORD ? View.VISIBLE : View.GONE);
|
2019-07-01 13:25:31 +00:00
|
|
|
grpMove.setVisibility(type == EntityRule.TYPE_MOVE || type == EntityRule.TYPE_COPY ? View.VISIBLE : View.GONE);
|
|
|
|
grpMoveProp.setVisibility(type == EntityRule.TYPE_MOVE ? View.VISIBLE : View.GONE);
|
|
|
|
grpAnswer.setVisibility(type == EntityRule.TYPE_ANSWER ? View.VISIBLE : View.GONE);
|
|
|
|
grpAutomation.setVisibility(type == EntityRule.TYPE_AUTOMATION ? View.VISIBLE : View.GONE);
|
|
|
|
}
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-07-01 13:25:31 +00:00
|
|
|
private void onActionDelete() {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putString("question", getString(R.string.title_ask_delete_rule));
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-07-01 13:25:31 +00:00
|
|
|
FragmentDialogAsk fragment = new FragmentDialogAsk();
|
|
|
|
fragment.setArguments(args);
|
|
|
|
fragment.setTargetFragment(FragmentRule.this, REQUEST_DELETE);
|
2019-10-12 15:16:53 +00:00
|
|
|
fragment.show(getParentFragmentManager(), "answer:delete");
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 07:01:25 +00:00
|
|
|
private void onActionCheck() {
|
|
|
|
try {
|
|
|
|
JSONObject jcondition = getCondition();
|
2019-09-21 13:43:59 +00:00
|
|
|
JSONObject jaction = getAction();
|
2019-05-22 07:01:25 +00:00
|
|
|
|
|
|
|
JSONObject jheader = jcondition.optJSONObject("header");
|
|
|
|
if (jheader != null) {
|
|
|
|
Snackbar.make(view, R.string.title_rule_no_headers, Snackbar.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("folder", folder);
|
|
|
|
args.putString("condition", jcondition.toString());
|
2019-09-21 13:43:59 +00:00
|
|
|
args.putString("action", jaction.toString());
|
2019-05-22 07:01:25 +00:00
|
|
|
|
2019-07-01 13:25:31 +00:00
|
|
|
FragmentDialogCheck fragment = new FragmentDialogCheck();
|
|
|
|
fragment.setArguments(args);
|
2019-10-12 15:16:53 +00:00
|
|
|
fragment.show(getParentFragmentManager(), "rule:check");
|
2019-05-22 07:01:25 +00:00
|
|
|
|
|
|
|
} catch (JSONException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
private void onActionSave() {
|
2019-08-13 08:27:17 +00:00
|
|
|
if (!ActivityBilling.isPro(getContext())) {
|
2019-10-05 13:22:48 +00:00
|
|
|
startActivity(new Intent(getContext(), ActivityBilling.class));
|
2019-01-21 18:37:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
try {
|
|
|
|
Helper.setViewsEnabled(view, false);
|
|
|
|
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("id", id);
|
2019-01-18 08:23:53 +00:00
|
|
|
args.putLong("folder", folder);
|
2019-01-17 13:29:35 +00:00
|
|
|
args.putString("name", etName.getText().toString());
|
|
|
|
args.putString("order", etOrder.getText().toString());
|
2019-01-17 21:25:22 +00:00
|
|
|
args.putBoolean("enabled", cbEnabled.isChecked());
|
2019-01-18 10:58:55 +00:00
|
|
|
args.putBoolean("stop", cbStop.isChecked());
|
2019-02-07 19:29:22 +00:00
|
|
|
args.putString("condition", getCondition().toString());
|
|
|
|
args.putString("action", getAction().toString());
|
2019-01-17 13:29:35 +00:00
|
|
|
|
|
|
|
new SimpleTask<Void>() {
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute(Bundle args) {
|
|
|
|
Helper.setViewsEnabled(view, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bundle args) {
|
|
|
|
Helper.setViewsEnabled(view, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-18 10:58:55 +00:00
|
|
|
protected Void onExecute(Context context, Bundle args) throws JSONException {
|
2019-01-17 13:29:35 +00:00
|
|
|
long id = args.getLong("id");
|
|
|
|
long folder = args.getLong("folder");
|
|
|
|
String name = args.getString("name");
|
|
|
|
String order = args.getString("order");
|
2019-01-17 21:25:22 +00:00
|
|
|
boolean enabled = args.getBoolean("enabled");
|
2019-01-18 10:58:55 +00:00
|
|
|
boolean stop = args.getBoolean("stop");
|
2019-01-17 13:29:35 +00:00
|
|
|
String condition = args.getString("condition");
|
|
|
|
String action = args.getString("action");
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(name))
|
2019-01-18 10:58:55 +00:00
|
|
|
throw new IllegalArgumentException(context.getString(R.string.title_rule_name_missing));
|
|
|
|
|
|
|
|
JSONObject jcondition = new JSONObject(condition);
|
|
|
|
JSONObject jsender = jcondition.optJSONObject("sender");
|
2019-03-17 11:11:12 +00:00
|
|
|
JSONObject jrecipient = jcondition.optJSONObject("recipient");
|
2019-01-18 10:58:55 +00:00
|
|
|
JSONObject jsubject = jcondition.optJSONObject("subject");
|
2019-01-18 13:04:05 +00:00
|
|
|
JSONObject jheader = jcondition.optJSONObject("header");
|
2019-08-04 17:14:53 +00:00
|
|
|
JSONObject jschedule = jcondition.optJSONObject("schedule");
|
2019-01-18 10:58:55 +00:00
|
|
|
|
2019-10-13 14:47:35 +00:00
|
|
|
if (jsender == null &&
|
|
|
|
jrecipient == null &&
|
|
|
|
jsubject == null &&
|
|
|
|
!jcondition.optBoolean("attachments") &&
|
|
|
|
jheader == null &&
|
|
|
|
jschedule == null)
|
2019-01-18 10:58:55 +00:00
|
|
|
throw new IllegalArgumentException(context.getString(R.string.title_rule_condition_missing));
|
2019-01-17 13:29:35 +00:00
|
|
|
|
|
|
|
if (TextUtils.isEmpty(order))
|
2019-09-30 10:39:14 +00:00
|
|
|
order = "10";
|
2019-01-17 13:29:35 +00:00
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
if (id < 0) {
|
|
|
|
EntityRule rule = new EntityRule();
|
|
|
|
rule.folder = folder;
|
|
|
|
rule.name = name;
|
|
|
|
rule.order = Integer.parseInt(order);
|
2019-01-17 21:25:22 +00:00
|
|
|
rule.enabled = enabled;
|
2019-01-18 10:58:55 +00:00
|
|
|
rule.stop = stop;
|
2019-01-17 13:29:35 +00:00
|
|
|
rule.condition = condition;
|
|
|
|
rule.action = action;
|
|
|
|
rule.id = db.rule().insertRule(rule);
|
|
|
|
} else {
|
|
|
|
EntityRule rule = db.rule().getRule(id);
|
|
|
|
rule.folder = folder;
|
|
|
|
rule.name = name;
|
|
|
|
rule.order = Integer.parseInt(order);
|
2019-01-17 21:25:22 +00:00
|
|
|
rule.enabled = enabled;
|
2019-01-18 10:58:55 +00:00
|
|
|
rule.stop = stop;
|
2019-01-17 13:29:35 +00:00
|
|
|
rule.condition = condition;
|
|
|
|
rule.action = action;
|
|
|
|
db.rule().updateRule(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, Void data) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
|
|
|
if (ex instanceof IllegalArgumentException)
|
|
|
|
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
|
|
|
|
else
|
2019-10-12 15:16:53 +00:00
|
|
|
Helper.unexpectedError(getParentFragmentManager(), ex);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
}.execute(this, args, "rule:save");
|
|
|
|
} catch (JSONException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 19:29:22 +00:00
|
|
|
private JSONObject getCondition() throws JSONException {
|
|
|
|
JSONObject jcondition = new JSONObject();
|
|
|
|
|
|
|
|
String sender = etSender.getText().toString();
|
2019-09-22 09:44:11 +00:00
|
|
|
boolean known = cbKnownSender.isChecked();
|
|
|
|
if (!TextUtils.isEmpty(sender) || known) {
|
2019-02-07 19:29:22 +00:00
|
|
|
JSONObject jsender = new JSONObject();
|
|
|
|
jsender.put("value", sender);
|
|
|
|
jsender.put("regex", cbSender.isChecked());
|
2019-09-22 09:44:11 +00:00
|
|
|
jsender.put("known", known);
|
2019-02-07 19:29:22 +00:00
|
|
|
jcondition.put("sender", jsender);
|
|
|
|
}
|
|
|
|
|
2019-03-17 11:11:12 +00:00
|
|
|
String recipient = etRecipient.getText().toString();
|
|
|
|
if (!TextUtils.isEmpty(recipient)) {
|
|
|
|
JSONObject jrecipient = new JSONObject();
|
|
|
|
jrecipient.put("value", recipient);
|
|
|
|
jrecipient.put("regex", cbRecipient.isChecked());
|
|
|
|
jcondition.put("recipient", jrecipient);
|
|
|
|
}
|
|
|
|
|
2019-02-07 19:29:22 +00:00
|
|
|
String subject = etSubject.getText().toString();
|
|
|
|
if (!TextUtils.isEmpty(subject)) {
|
|
|
|
JSONObject jsubject = new JSONObject();
|
|
|
|
jsubject.put("value", subject);
|
|
|
|
jsubject.put("regex", cbSubject.isChecked());
|
|
|
|
jcondition.put("subject", jsubject);
|
|
|
|
}
|
|
|
|
|
2019-10-13 14:47:35 +00:00
|
|
|
jcondition.put("attachments", cbAttachments.isChecked());
|
|
|
|
|
2019-02-07 19:29:22 +00:00
|
|
|
String header = etHeader.getText().toString();
|
|
|
|
if (!TextUtils.isEmpty(header)) {
|
|
|
|
JSONObject jheader = new JSONObject();
|
|
|
|
jheader.put("value", header);
|
|
|
|
jheader.put("regex", cbHeader.isChecked());
|
|
|
|
jcondition.put("header", jheader);
|
|
|
|
}
|
|
|
|
|
2019-08-21 08:51:40 +00:00
|
|
|
int dstart = spScheduleDayStart.getSelectedItemPosition();
|
|
|
|
int dend = spScheduleDayEnd.getSelectedItemPosition();
|
|
|
|
Object hstart = tvScheduleHourStart.getTag();
|
|
|
|
Object hend = tvScheduleHourEnd.getTag();
|
|
|
|
if (hstart == null)
|
|
|
|
hstart = 0;
|
|
|
|
if (hend == null)
|
|
|
|
hend = 0;
|
|
|
|
|
|
|
|
int start = dstart * 24 * 60 + (int) hstart;
|
|
|
|
int end = dend * 24 * 60 + (int) hend;
|
|
|
|
|
|
|
|
if (start != end) {
|
2019-08-04 17:14:53 +00:00
|
|
|
JSONObject jschedule = new JSONObject();
|
2019-08-21 08:51:40 +00:00
|
|
|
jschedule.put("start", start);
|
|
|
|
jschedule.put("end", end);
|
2019-08-04 17:14:53 +00:00
|
|
|
jcondition.put("schedule", jschedule);
|
|
|
|
}
|
|
|
|
|
2019-02-07 19:29:22 +00:00
|
|
|
return jcondition;
|
|
|
|
}
|
|
|
|
|
|
|
|
private JSONObject getAction() throws JSONException {
|
|
|
|
JSONObject jaction = new JSONObject();
|
|
|
|
|
|
|
|
Action action = (Action) spAction.getSelectedItem();
|
|
|
|
if (action != null) {
|
|
|
|
jaction.put("type", action.type);
|
|
|
|
switch (action.type) {
|
2019-06-01 13:27:21 +00:00
|
|
|
case EntityRule.TYPE_SNOOZE:
|
|
|
|
jaction.put("duration", npDuration.getValue());
|
2019-08-04 20:06:22 +00:00
|
|
|
jaction.put("schedule_end", cbScheduleEnd.isChecked());
|
2019-06-01 13:27:21 +00:00
|
|
|
break;
|
|
|
|
|
2019-05-15 10:52:39 +00:00
|
|
|
case EntityRule.TYPE_FLAG:
|
2019-10-10 15:09:08 +00:00
|
|
|
int color = btnColor.getColor();
|
|
|
|
if (color != Color.TRANSPARENT)
|
|
|
|
jaction.put("color", color);
|
2019-05-15 10:52:39 +00:00
|
|
|
break;
|
|
|
|
|
2019-10-09 09:09:00 +00:00
|
|
|
case EntityRule.TYPE_KEYWORD:
|
2019-10-09 12:38:29 +00:00
|
|
|
jaction.put("keyword", MessageHelper.sanitizeKeyword(etKeyword.getText().toString()));
|
2019-10-09 09:09:00 +00:00
|
|
|
break;
|
|
|
|
|
2019-02-07 19:29:22 +00:00
|
|
|
case EntityRule.TYPE_MOVE:
|
2019-05-18 06:34:26 +00:00
|
|
|
case EntityRule.TYPE_COPY:
|
2019-02-07 19:29:22 +00:00
|
|
|
EntityFolder target = (EntityFolder) spTarget.getSelectedItem();
|
2019-05-23 13:57:44 +00:00
|
|
|
jaction.put("target", target == null ? -1 : target.id);
|
2019-06-09 19:25:56 +00:00
|
|
|
if (action.type == EntityRule.TYPE_MOVE) {
|
|
|
|
jaction.put("seen", cbMoveSeen.isChecked());
|
|
|
|
jaction.put("thread", cbMoveThread.isChecked());
|
|
|
|
}
|
2019-02-07 19:29:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityRule.TYPE_ANSWER:
|
|
|
|
EntityIdentity identity = (EntityIdentity) spIdent.getSelectedItem();
|
|
|
|
EntityAnswer answer = (EntityAnswer) spAnswer.getSelectedItem();
|
2019-05-23 13:57:44 +00:00
|
|
|
jaction.put("identity", identity == null ? -1 : identity.id);
|
|
|
|
jaction.put("answer", answer == null ? -1 : answer.id);
|
2019-06-09 19:25:56 +00:00
|
|
|
jaction.put("cc", cbCc.isChecked());
|
2019-02-07 19:29:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return jaction;
|
|
|
|
}
|
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
private class RefData {
|
2019-02-01 08:51:11 +00:00
|
|
|
EntityFolder folder;
|
2019-01-18 18:40:51 +00:00
|
|
|
List<EntityFolder> folders;
|
|
|
|
List<EntityIdentity> identities;
|
|
|
|
List<EntityAnswer> answers;
|
|
|
|
}
|
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
private class Action {
|
|
|
|
int type;
|
|
|
|
String name;
|
|
|
|
|
|
|
|
Action(int type, String name) {
|
|
|
|
this.type = type;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
2019-07-01 13:25:31 +00:00
|
|
|
|
2019-08-04 17:14:53 +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-08-04 17:14:53 +00:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
|
|
Bundle args = getArguments();
|
|
|
|
int minutes = args.getInt("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 new TimePickerDialog(getContext(), this,
|
|
|
|
cal.get(Calendar.HOUR_OF_DAY),
|
|
|
|
cal.get(Calendar.MINUTE),
|
|
|
|
DateFormat.is24HourFormat(getContext()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onTimeSet(TimePicker view, int hour, int minute) {
|
|
|
|
Fragment target = getTargetFragment();
|
|
|
|
if (target != null) {
|
|
|
|
Intent data = new Intent();
|
|
|
|
data.putExtra("minutes", hour * 60 + minute);
|
|
|
|
target.onActivityResult(getTargetRequestCode(), RESULT_OK, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 19:43:27 +00:00
|
|
|
public static class FragmentDialogCheck extends FragmentDialogBase {
|
2019-07-01 13:25:31 +00:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
|
|
|
long folder = getArguments().getLong("folder");
|
|
|
|
String condition = getArguments().getString("condition");
|
2019-09-21 13:43:59 +00:00
|
|
|
String action = getArguments().getString("action");
|
2019-07-01 13:25:31 +00:00
|
|
|
|
|
|
|
final View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_rule_match, null);
|
|
|
|
final TextView tvNoMessages = dview.findViewById(R.id.tvNoMessages);
|
|
|
|
final RecyclerView rvMessage = dview.findViewById(R.id.rvMessage);
|
2019-09-21 13:43:59 +00:00
|
|
|
final Button btnExecute = dview.findViewById(R.id.btnExecute);
|
2019-07-01 13:25:31 +00:00
|
|
|
final ContentLoadingProgressBar pbWait = dview.findViewById(R.id.pbWait);
|
|
|
|
|
|
|
|
rvMessage.setHasFixedSize(false);
|
|
|
|
LinearLayoutManager llm = new LinearLayoutManager(getContext());
|
|
|
|
rvMessage.setLayoutManager(llm);
|
|
|
|
|
2019-09-11 12:03:59 +00:00
|
|
|
final AdapterRuleMatch adapter = new AdapterRuleMatch(getContext(), getViewLifecycleOwner());
|
2019-07-01 13:25:31 +00:00
|
|
|
rvMessage.setAdapter(adapter);
|
|
|
|
|
|
|
|
tvNoMessages.setVisibility(View.GONE);
|
|
|
|
rvMessage.setVisibility(View.GONE);
|
2019-09-21 13:43:59 +00:00
|
|
|
btnExecute.setVisibility(View.GONE);
|
2019-07-01 13:25:31 +00:00
|
|
|
pbWait.setVisibility(View.VISIBLE);
|
|
|
|
|
2019-09-21 13:43:59 +00:00
|
|
|
final Bundle args = new Bundle();
|
2019-07-01 13:25:31 +00:00
|
|
|
args.putLong("folder", folder);
|
|
|
|
args.putString("condition", condition);
|
2019-09-21 13:43:59 +00:00
|
|
|
args.putString("action", action);
|
|
|
|
|
|
|
|
btnExecute.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
new SimpleTask<Integer>() {
|
|
|
|
@Override
|
|
|
|
protected Integer onExecute(Context context, Bundle args) throws Throwable {
|
|
|
|
EntityRule rule = new EntityRule();
|
|
|
|
rule.folder = args.getLong("folder");
|
|
|
|
rule.condition = args.getString("condition");
|
|
|
|
rule.action = args.getString("action");
|
|
|
|
|
|
|
|
int applied = 0;
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
2019-10-02 19:13:10 +00:00
|
|
|
List<Long> ids =
|
2019-10-23 18:01:43 +00:00
|
|
|
db.message().getMessageIdsByFolder(rule.folder);
|
2019-09-21 13:43:59 +00:00
|
|
|
for (long mid : ids)
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
EntityMessage message = db.message().getMessage(mid);
|
2019-09-28 13:30:18 +00:00
|
|
|
if (message == null)
|
|
|
|
continue;
|
2019-09-21 13:43:59 +00:00
|
|
|
|
|
|
|
if (rule.matches(context, message, null))
|
|
|
|
if (rule.execute(context, message))
|
|
|
|
applied++;
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
return applied;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, Integer applied) {
|
|
|
|
ToastEx.makeText(getContext(), getString(R.string.title_rule_applied, applied), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-10-12 15:16:53 +00:00
|
|
|
Helper.unexpectedError(getParentFragmentManager(), ex);
|
2019-09-21 13:43:59 +00:00
|
|
|
}
|
|
|
|
}.execute(FragmentDialogCheck.this, args, "rule:execute");
|
|
|
|
}
|
|
|
|
});
|
2019-07-01 13:25:31 +00:00
|
|
|
|
|
|
|
new SimpleTask<List<EntityMessage>>() {
|
|
|
|
@Override
|
|
|
|
protected List<EntityMessage> onExecute(Context context, Bundle args) throws Throwable {
|
|
|
|
EntityRule rule = new EntityRule();
|
2019-09-21 13:43:59 +00:00
|
|
|
rule.folder = args.getLong("folder");
|
2019-07-01 13:25:31 +00:00
|
|
|
rule.condition = args.getString("condition");
|
2019-09-21 13:43:59 +00:00
|
|
|
rule.action = args.getString("action");
|
2019-07-01 13:25:31 +00:00
|
|
|
|
|
|
|
List<EntityMessage> matching = new ArrayList<>();
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
2019-10-02 19:13:10 +00:00
|
|
|
List<Long> ids =
|
2019-10-23 18:01:43 +00:00
|
|
|
db.message().getMessageIdsByFolder(rule.folder);
|
2019-07-01 13:25:31 +00:00
|
|
|
for (long id : ids) {
|
|
|
|
EntityMessage message = db.message().getMessage(id);
|
2019-10-16 14:57:43 +00:00
|
|
|
if (message == null)
|
|
|
|
continue;
|
2019-07-01 13:25:31 +00:00
|
|
|
|
|
|
|
if (rule.matches(context, message, null))
|
|
|
|
matching.add(message);
|
|
|
|
|
|
|
|
if (matching.size() >= MAX_CHECK)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return matching;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, List<EntityMessage> messages) {
|
|
|
|
adapter.set(messages);
|
|
|
|
|
|
|
|
pbWait.setVisibility(View.GONE);
|
2019-09-21 13:43:59 +00:00
|
|
|
if (messages.size() > 0) {
|
2019-07-01 13:25:31 +00:00
|
|
|
rvMessage.setVisibility(View.VISIBLE);
|
2019-09-21 13:43:59 +00:00
|
|
|
btnExecute.setVisibility(View.VISIBLE);
|
|
|
|
} else
|
2019-07-01 13:25:31 +00:00
|
|
|
tvNoMessages.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-10-12 15:16:53 +00:00
|
|
|
Helper.unexpectedError(getParentFragmentManager(), ex);
|
2019-07-01 13:25:31 +00:00
|
|
|
}
|
2019-09-11 12:03:59 +00:00
|
|
|
}.execute(this, args, "rule:check");
|
2019-07-01 13:25:31 +00:00
|
|
|
|
|
|
|
return new AlertDialog.Builder(getContext())
|
|
|
|
.setTitle(R.string.title_rule_matched)
|
|
|
|
.setView(dview)
|
2019-09-12 11:23:32 +00:00
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
2019-07-01 13:25:31 +00:00
|
|
|
.create();
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|