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

503 lines
18 KiB
Java
Raw Normal View History

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/>.
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2019-01-17 13:29:35 +00:00
*/
2020-09-08 12:43:46 +00:00
import android.content.ContentResolver;
2019-09-08 07:54:03 +00:00
import android.content.Context;
import android.content.Intent;
2019-08-14 15:21:37 +00:00
import android.content.SharedPreferences;
2020-09-08 12:43:46 +00:00
import android.net.Uri;
2019-01-17 13:29:35 +00:00
import android.os.Bundle;
2020-09-06 07:21:38 +00:00
import android.text.TextUtils;
2019-01-17 13:29:35 +00:00
import android.view.LayoutInflater;
2020-09-05 20:55:34 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2019-01-17 13:29:35 +00:00
import android.view.View;
import android.view.ViewGroup;
2020-09-08 12:43:46 +00:00
import android.widget.Toast;
2019-01-17 13:29:35 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2020-09-06 07:21:38 +00:00
import androidx.appcompat.widget.SearchView;
2019-01-17 13:29:35 +00:00
import androidx.constraintlayout.widget.Group;
2019-08-16 10:14:41 +00:00
import androidx.core.content.ContextCompat;
2019-01-17 13:29:35 +00:00
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
2019-08-14 15:21:37 +00:00
import androidx.preference.PreferenceManager;
2019-04-01 07:23:43 +00:00
import androidx.recyclerview.widget.DividerItemDecoration;
2019-01-17 13:29:35 +00:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
2020-09-08 12:43:46 +00:00
import org.json.JSONArray;
import org.json.JSONException;
2020-09-05 20:55:34 +00:00
import org.json.JSONObject;
2020-09-08 12:43:46 +00:00
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
2020-09-08 12:43:46 +00:00
import java.util.Date;
import java.util.List;
2019-09-08 07:54:03 +00:00
import static android.app.Activity.RESULT_OK;
2020-09-05 20:55:34 +00:00
import static eu.faircode.email.EntityRule.TYPE_MOVE;
2019-09-08 07:54:03 +00:00
2019-01-17 13:29:35 +00:00
public class FragmentRules extends FragmentBase {
2019-08-14 15:21:37 +00:00
private long account;
2020-07-01 06:57:17 +00:00
private int protocol;
2020-09-05 20:55:34 +00:00
private long folder;
private String type;
2019-08-14 15:21:37 +00:00
private boolean cards;
2020-09-25 07:09:41 +00:00
private boolean beige;
2019-08-14 15:21:37 +00:00
2019-01-17 13:29:35 +00:00
private RecyclerView rvRule;
private ContentLoadingProgressBar pbWait;
private Group grpReady;
private FloatingActionButton fab;
2020-09-06 07:21:38 +00:00
private String searching = null;
2019-01-17 13:29:35 +00:00
private AdapterRule adapter;
2020-09-08 12:43:46 +00:00
private static final int REQUEST_EXPORT = 1;
private static final int REQUEST_IMPORT = 2;
static final int REQUEST_MOVE = 3;
private static final int REQUEST_CLEAR = 4;
2019-09-08 07:54:03 +00:00
2019-01-18 08:23:53 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get arguments
Bundle args = getArguments();
account = args.getLong("account", -1);
2020-07-01 06:57:17 +00:00
protocol = args.getInt("protocol", -1);
2020-09-05 20:55:34 +00:00
folder = args.getLong("folder", -1);
type = args.getString("type");
2019-08-14 15:21:37 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
cards = prefs.getBoolean("cards", true);
2020-09-25 08:19:15 +00:00
beige = prefs.getBoolean("beige", true);
2019-01-18 08:23:53 +00:00
}
2019-01-17 13:29:35 +00:00
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2019-01-18 08:23:53 +00:00
setSubtitle(R.string.title_edit_rules);
2020-09-05 20:55:34 +00:00
setHasOptionsMenu(true);
2019-01-17 13:29:35 +00:00
View view = inflater.inflate(R.layout.fragment_rules, container, false);
// Get controls
rvRule = view.findViewById(R.id.rvRule);
pbWait = view.findViewById(R.id.pbWait);
grpReady = view.findViewById(R.id.grpReady);
fab = view.findViewById(R.id.fab);
// Wire controls
2020-08-30 14:39:03 +00:00
rvRule.setHasFixedSize(true);
2019-01-17 13:29:35 +00:00
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rvRule.setLayoutManager(llm);
adapter = new AdapterRule(this);
2019-01-17 13:29:35 +00:00
rvRule.setAdapter(adapter);
2019-08-14 15:21:37 +00:00
if (!cards) {
DividerItemDecoration itemDecorator = new DividerItemDecoration(getContext(), llm.getOrientation());
itemDecorator.setDrawable(getContext().getDrawable(R.drawable.divider));
rvRule.addItemDecoration(itemDecorator);
}
2019-04-01 07:23:43 +00:00
2019-01-17 13:29:35 +00:00
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2019-01-18 08:23:53 +00:00
Bundle args = new Bundle();
args.putLong("account", account);
2020-07-01 06:57:17 +00:00
args.putInt("protocol", protocol);
2020-09-05 20:55:34 +00:00
args.putLong("folder", folder);
2019-01-18 08:23:53 +00:00
FragmentRule fragment = new FragmentRule();
fragment.setArguments(args);
2019-10-12 15:16:53 +00:00
FragmentTransaction fragmentTransaction = getParentFragmentManager().beginTransaction();
2019-01-18 08:23:53 +00:00
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("rule");
2019-01-17 13:29:35 +00:00
fragmentTransaction.commit();
}
});
// Initialize
2019-08-16 10:14:41 +00:00
if (cards && !Helper.isDarkTheme(getContext()))
2020-09-25 07:09:41 +00:00
view.setBackgroundColor(ContextCompat.getColor(getContext(), beige
? R.color.lightColorBackground_cards_beige
: R.color.lightColorBackground_cards));
2019-08-16 10:14:41 +00:00
2019-01-17 13:29:35 +00:00
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
return view;
}
2020-09-06 07:21:38 +00:00
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("fair:searching", searching);
super.onSaveInstanceState(outState);
}
2019-01-17 13:29:35 +00:00
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
2020-09-06 07:21:38 +00:00
if (savedInstanceState != null) {
searching = savedInstanceState.getString("fair:searching");
adapter.search(searching);
}
2019-01-17 13:29:35 +00:00
DB db = DB.getInstance(getContext());
2019-01-18 08:23:53 +00:00
db.rule().liveRules(folder).observe(getViewLifecycleOwner(), new Observer<List<TupleRuleEx>>() {
2019-01-17 13:29:35 +00:00
@Override
2019-01-17 21:41:00 +00:00
public void onChanged(List<TupleRuleEx> rules) {
2019-01-17 13:29:35 +00:00
if (rules == null)
rules = new ArrayList<>();
2020-07-01 06:57:17 +00:00
adapter.set(protocol, rules);
2019-01-17 13:29:35 +00:00
pbWait.setVisibility(View.GONE);
grpReady.setVisibility(View.VISIBLE);
}
});
}
2019-09-08 07:54:03 +00:00
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
2020-09-08 12:43:46 +00:00
case REQUEST_EXPORT:
if (resultCode == RESULT_OK && data != null)
onExport(data);
break;
case REQUEST_IMPORT:
if (resultCode == RESULT_OK && data != null)
onImport(data);
break;
2019-09-08 07:54:03 +00:00
case REQUEST_MOVE:
if (resultCode == RESULT_OK && data != null)
onMove(data.getBundleExtra("args"));
break;
2020-09-05 20:55:34 +00:00
case REQUEST_CLEAR:
if (resultCode == RESULT_OK && data != null)
onClear(data.getBundleExtra("args"));
break;
2019-09-08 07:54:03 +00:00
}
} catch (Throwable ex) {
Log.e(ex);
}
}
2020-09-05 20:55:34 +00:00
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_rules, menu);
2020-09-06 07:21:38 +00:00
MenuItem menuSearch = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) menuSearch.getActionView();
2020-09-06 11:13:32 +00:00
searchView.setQueryHint(getString(R.string.title_rules_search_hint));
2020-09-06 07:21:38 +00:00
if (!TextUtils.isEmpty(searching)) {
menuSearch.expandActionView();
searchView.setQuery(searching, true);
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
searching = newText;
adapter.search(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
searching = query;
adapter.search(query);
return true;
}
});
2020-09-05 20:55:34 +00:00
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
2020-09-08 12:02:19 +00:00
menu.findItem(R.id.menu_delete_junk).setVisible(!EntityFolder.JUNK.equals(type));
2020-09-05 20:55:34 +00:00
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
2020-09-08 12:43:46 +00:00
case R.id.menu_export:
onMenuExport();
return true;
case R.id.menu_import:
onMenuImport();
return true;
2020-09-08 12:02:19 +00:00
case R.id.menu_delete_all:
onMenuDelete(true);
return true;
case R.id.menu_delete_junk:
onMenuDelete(false);
2020-09-05 20:55:34 +00:00
return true;
default:
return super.onOptionsItemSelected(item);
}
}
2020-09-08 12:43:46 +00:00
private void onMenuExport() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TITLE, "fairemail_" +
new SimpleDateFormat("yyyyMMdd").format(new Date().getTime()) + ".rules");
Helper.openAdvanced(intent);
startActivityForResult(intent, REQUEST_EXPORT);
}
private void onMenuImport() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, REQUEST_IMPORT);
}
2020-09-08 12:02:19 +00:00
private void onMenuDelete(boolean all) {
2020-09-05 20:55:34 +00:00
Bundle aargs = new Bundle();
2020-09-08 12:02:19 +00:00
aargs.putString("question", getString(all
? R.string.title_rules_delete_all_confirm
: R.string.title_rules_delete_junk_confirm));
2020-09-05 20:55:34 +00:00
aargs.putLong("folder", folder);
2020-09-08 12:02:19 +00:00
aargs.putBoolean("all", all);
2020-09-05 20:55:34 +00:00
FragmentDialogAsk ask = new FragmentDialogAsk();
ask.setArguments(aargs);
ask.setTargetFragment(this, REQUEST_CLEAR);
ask.show(getParentFragmentManager(), "rules:clear");
}
2020-09-08 12:43:46 +00:00
private void onExport(Intent data) {
Bundle args = new Bundle();
args.putLong("folder", folder);
args.putParcelable("uri", data.getData());
new SimpleTask<Void>() {
@Override
protected void onPreExecute(Bundle args) {
ToastEx.makeText(getContext(), R.string.title_executing, Toast.LENGTH_LONG).show();
}
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long fid = args.getLong("folder");
Uri uri = args.getParcelable("uri");
if (!"content".equals(uri.getScheme())) {
Log.w("Export uri=" + uri);
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
}
DB db = DB.getInstance(context);
JSONArray jrules = new JSONArray();
for (EntityRule rule : db.rule().getRules(fid))
jrules.put(rule.toJSON());
ContentResolver resolver = context.getContentResolver();
try (OutputStream os = resolver.openOutputStream(uri)) {
Log.i("Writing URI=" + uri);
os.write(jrules.toString(2).getBytes());
}
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
ToastEx.makeText(getContext(), R.string.title_completed, Toast.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException ||
ex instanceof FileNotFoundException)
ToastEx.makeText(getContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
else
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "rules:export");
}
private void onImport(Intent data) {
Bundle args = new Bundle();
args.putLong("folder", folder);
args.putParcelable("uri", data.getData());
new SimpleTask<Void>() {
@Override
protected void onPreExecute(Bundle args) {
ToastEx.makeText(getContext(), R.string.title_executing, Toast.LENGTH_LONG).show();
}
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long fid = args.getLong("folder");
Uri uri = args.getParcelable("uri");
if (!"content".equals(uri.getScheme())) {
Log.w("Import uri=" + uri);
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
}
StringBuilder data = new StringBuilder();
Log.i("Reading URI=" + uri);
ContentResolver resolver = context.getContentResolver();
2020-10-14 14:50:15 +00:00
try (InputStream is = resolver.openInputStream(uri)) {
2020-09-08 12:43:46 +00:00
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null)
data.append(line);
}
JSONArray jrules = new JSONArray(data.toString());
DB db = DB.getInstance(context);
try {
db.beginTransaction();
for (int i = 0; i < jrules.length(); i++) {
JSONObject jrule = jrules.getJSONObject(i);
EntityRule rule = EntityRule.fromJSON(jrule);
rule.folder = fid;
rule.applied = 0;
rule.id = db.rule().insertRule(rule);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
ToastEx.makeText(getContext(), R.string.title_completed, Toast.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException ||
ex instanceof FileNotFoundException ||
ex instanceof JSONException)
ToastEx.makeText(getContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
else
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "rules:import");
}
2019-09-08 07:54:03 +00:00
private void onMove(Bundle args) {
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("rule");
long folder = args.getLong("folder");
DB db = DB.getInstance(context);
db.rule().setRuleFolder(id, folder);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
2020-09-05 20:55:34 +00:00
Log.unexpectedError(getParentFragmentManager(), ex);
2019-09-08 07:54:03 +00:00
}
}.execute(this, args, "rule:move");
}
2020-09-05 20:55:34 +00:00
private void onClear(Bundle args) {
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long fid = args.getLong("folder");
2020-09-08 12:02:19 +00:00
boolean all = args.getBoolean("all");
2020-09-05 20:55:34 +00:00
DB db = DB.getInstance(context);
2020-09-08 12:47:32 +00:00
try {
db.beginTransaction();
2020-09-08 12:02:19 +00:00
2020-09-08 12:47:32 +00:00
if (all)
db.rule().deleteRules(fid);
else {
EntityFolder folder = db.folder().getFolder(fid);
if (folder == null)
return null;
EntityFolder junk = db.folder().getFolderByType(folder.account, EntityFolder.JUNK);
if (junk == null)
return null;
List<EntityRule> rules = db.rule().getRules(fid);
if (rules == null)
return null;
for (EntityRule rule : rules) {
JSONObject jaction = new JSONObject(rule.action);
int type = jaction.optInt("type", -1);
long target = jaction.optLong("target", -1);
if (type == TYPE_MOVE && target == junk.id)
db.rule().deleteRule(rule.id);
}
}
2020-09-05 20:55:34 +00:00
2020-09-08 12:47:32 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
2020-09-05 20:55:34 +00:00
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "rules:clear");
}
2019-01-17 13:29:35 +00:00
}