2018-08-27 07:06:03 +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.
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-08-27 07:06:03 +00:00
|
|
|
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
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-27 07:06:03 +00:00
|
|
|
|
2022-01-01 08:46:36 +00:00
|
|
|
Copyright 2018-2022 by Marcel Bokhorst (M66B)
|
2018-08-27 07:06:03 +00:00
|
|
|
*/
|
|
|
|
|
2021-08-01 14:14:48 +00:00
|
|
|
import static android.app.Activity.RESULT_OK;
|
|
|
|
|
2019-09-27 18:04:33 +00:00
|
|
|
import android.content.ClipboardManager;
|
2018-08-27 09:39:27 +00:00
|
|
|
import android.content.Context;
|
2019-07-01 13:01:37 +00:00
|
|
|
import android.content.Intent;
|
2021-01-31 10:56:28 +00:00
|
|
|
import android.content.SharedPreferences;
|
2020-04-27 07:40:43 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2019-09-27 18:04:33 +00:00
|
|
|
import android.net.Uri;
|
2018-08-27 07:06:03 +00:00
|
|
|
import android.os.Bundle;
|
2021-06-26 08:36:25 +00:00
|
|
|
import android.text.Editable;
|
2020-04-27 07:40:43 +00:00
|
|
|
import android.text.Html;
|
|
|
|
import android.text.SpannableStringBuilder;
|
2019-05-03 12:26:01 +00:00
|
|
|
import android.text.Spanned;
|
2020-02-02 09:13:42 +00:00
|
|
|
import android.text.TextUtils;
|
2020-04-27 07:40:43 +00:00
|
|
|
import android.text.style.ImageSpan;
|
2021-06-26 08:36:25 +00:00
|
|
|
import android.text.style.SuggestionSpan;
|
2021-11-07 08:33:17 +00:00
|
|
|
import android.util.TypedValue;
|
2018-08-27 07:06:03 +00:00
|
|
|
import android.view.LayoutInflater;
|
2020-02-02 09:13:42 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
2018-08-27 09:39:27 +00:00
|
|
|
import android.view.MenuItem;
|
2018-08-27 07:06:03 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2019-04-20 08:35:30 +00:00
|
|
|
import android.widget.CheckBox;
|
2018-09-08 06:09:47 +00:00
|
|
|
import android.widget.EditText;
|
2018-08-27 09:39:27 +00:00
|
|
|
|
2018-08-27 07:06:03 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.constraintlayout.widget.Group;
|
2021-01-31 10:56:28 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
2018-08-27 07:06:03 +00:00
|
|
|
|
2019-04-17 18:21:44 +00:00
|
|
|
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
2020-02-02 09:13:42 +00:00
|
|
|
import com.google.android.material.snackbar.Snackbar;
|
2019-04-17 18:21:44 +00:00
|
|
|
|
2020-08-04 13:20:41 +00:00
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
|
2019-01-15 17:41:55 +00:00
|
|
|
public class FragmentAnswer extends FragmentBase {
|
2018-08-27 07:06:03 +00:00
|
|
|
private ViewGroup view;
|
2018-09-08 06:09:47 +00:00
|
|
|
private EditText etName;
|
2020-09-05 15:06:58 +00:00
|
|
|
private EditText etGroup;
|
2020-09-13 16:50:47 +00:00
|
|
|
private CheckBox cbStandard;
|
2021-04-26 07:33:04 +00:00
|
|
|
private CheckBox cbReceipt;
|
2021-05-06 19:11:51 +00:00
|
|
|
private CheckBox cbFavorite;
|
2019-04-20 08:35:30 +00:00
|
|
|
private CheckBox cbHide;
|
2021-08-01 14:14:48 +00:00
|
|
|
private CheckBox cbExternal;
|
2019-09-27 18:04:33 +00:00
|
|
|
private EditTextCompose etText;
|
|
|
|
private BottomNavigationView style_bar;
|
2020-02-02 13:36:18 +00:00
|
|
|
private BottomNavigationView bottom_navigation;
|
2018-12-27 12:31:02 +00:00
|
|
|
private ContentLoadingProgressBar pbWait;
|
2018-08-27 07:06:03 +00:00
|
|
|
private Group grpReady;
|
|
|
|
|
2018-08-27 09:39:27 +00:00
|
|
|
private long id = -1;
|
2019-08-20 20:18:42 +00:00
|
|
|
private long copy = -1;
|
2018-08-27 09:39:27 +00:00
|
|
|
|
2020-04-27 07:40:43 +00:00
|
|
|
private static final int REQUEST_IMAGE = 1;
|
|
|
|
private static final int REQUEST_LINK = 2;
|
|
|
|
private final static int REQUEST_DELETE = 3;
|
2019-07-01 13:01:37 +00:00
|
|
|
|
2018-08-27 09:39:27 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
// Get arguments
|
|
|
|
Bundle args = getArguments();
|
2019-08-21 18:34:26 +00:00
|
|
|
if (args != null)
|
|
|
|
if (args.getBoolean("copy"))
|
|
|
|
copy = args.getLong("id", -1);
|
|
|
|
else
|
|
|
|
id = args.getLong("id", -1);
|
2018-08-27 09:39:27 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 07:06:03 +00:00
|
|
|
@Override
|
|
|
|
@Nullable
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2021-11-07 08:33:17 +00:00
|
|
|
final Context context = getContext();
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2021-01-31 10:56:28 +00:00
|
|
|
boolean monospaced = prefs.getBoolean("monospaced", false);
|
2021-11-07 08:33:17 +00:00
|
|
|
String compose_font = prefs.getString("compose_font", monospaced ? "monospace" : "sans-serif");
|
|
|
|
boolean compact = prefs.getBoolean("compose_compact", false);
|
|
|
|
int zoom = prefs.getInt("compose_zoom", compact ? 0 : 1);
|
|
|
|
int message_zoom = prefs.getInt("message_zoom", 100);
|
2021-01-31 10:56:28 +00:00
|
|
|
|
2019-05-03 05:28:36 +00:00
|
|
|
setSubtitle(R.string.title_answer_caption);
|
2020-02-02 09:13:42 +00:00
|
|
|
setHasOptionsMenu(true);
|
2019-02-21 20:17:09 +00:00
|
|
|
|
2018-08-27 07:06:03 +00:00
|
|
|
view = (ViewGroup) inflater.inflate(R.layout.fragment_answer, container, false);
|
|
|
|
|
|
|
|
// Get controls
|
|
|
|
etName = view.findViewById(R.id.etName);
|
2020-09-05 15:06:58 +00:00
|
|
|
etGroup = view.findViewById(R.id.etGroup);
|
2020-09-13 16:50:47 +00:00
|
|
|
cbStandard = view.findViewById(R.id.cbStandard);
|
2021-04-26 07:33:04 +00:00
|
|
|
cbReceipt = view.findViewById(R.id.cbReceipt);
|
2021-05-06 19:11:51 +00:00
|
|
|
cbFavorite = view.findViewById(R.id.cbFavorite);
|
2019-04-20 08:35:30 +00:00
|
|
|
cbHide = view.findViewById(R.id.cbHide);
|
2021-08-01 14:14:48 +00:00
|
|
|
cbExternal = view.findViewById(R.id.cbExternal);
|
2018-08-27 09:39:27 +00:00
|
|
|
etText = view.findViewById(R.id.etText);
|
2019-06-20 08:33:29 +00:00
|
|
|
|
2019-09-27 18:04:33 +00:00
|
|
|
style_bar = view.findViewById(R.id.style_bar);
|
2020-02-02 13:36:18 +00:00
|
|
|
bottom_navigation = view.findViewById(R.id.bottom_navigation);
|
2019-06-20 08:33:29 +00:00
|
|
|
|
2018-08-27 07:06:03 +00:00
|
|
|
pbWait = view.findViewById(R.id.pbWait);
|
|
|
|
grpReady = view.findViewById(R.id.grpReady);
|
|
|
|
|
2021-11-07 08:33:17 +00:00
|
|
|
etText.setTypeface(StyleHelper.getTypeface(compose_font, context));
|
|
|
|
|
|
|
|
float textSize = Helper.getTextSize(context, zoom);
|
|
|
|
if (textSize != 0)
|
|
|
|
etText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * message_zoom / 100f);
|
2021-01-31 10:56:28 +00:00
|
|
|
|
2019-09-27 18:04:33 +00:00
|
|
|
etText.setSelectionListener(new EditTextCompose.ISelection() {
|
|
|
|
@Override
|
|
|
|
public void onSelected(boolean selection) {
|
|
|
|
style_bar.setVisibility(selection ? View.VISIBLE : View.GONE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
style_bar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
|
|
|
return onActionStyle(item.getItemId());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-02 13:36:18 +00:00
|
|
|
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
|
2018-08-27 09:39:27 +00:00
|
|
|
@Override
|
2020-02-02 13:36:18 +00:00
|
|
|
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
|
2021-02-05 10:15:02 +00:00
|
|
|
int itemId = menuItem.getItemId();
|
|
|
|
if (itemId == R.id.action_insert_image) {
|
|
|
|
onInsertImage();
|
|
|
|
return true;
|
|
|
|
} else if (itemId == R.id.action_delete) {
|
|
|
|
onActionDelete();
|
|
|
|
return true;
|
|
|
|
} else if (itemId == R.id.action_save) {
|
|
|
|
onActionSave();
|
|
|
|
return true;
|
2020-02-02 13:36:18 +00:00
|
|
|
}
|
2021-02-05 10:15:02 +00:00
|
|
|
return false;
|
2018-08-27 09:39:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Initialize
|
2021-11-07 08:33:17 +00:00
|
|
|
FragmentDialogTheme.setBackground(context, view, true);
|
2021-09-12 09:05:15 +00:00
|
|
|
|
2021-08-01 14:14:48 +00:00
|
|
|
cbExternal.setVisibility(View.GONE);
|
2018-08-27 07:06:03 +00:00
|
|
|
grpReady.setVisibility(View.GONE);
|
2019-09-27 18:04:33 +00:00
|
|
|
style_bar.setVisibility(View.GONE);
|
2020-11-03 08:39:27 +00:00
|
|
|
bottom_navigation.setVisibility(View.GONE);
|
2020-08-03 06:31:48 +00:00
|
|
|
|
2018-11-05 09:03:02 +00:00
|
|
|
pbWait.setVisibility(View.VISIBLE);
|
2018-08-27 07:06:03 +00:00
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
2018-11-14 16:21:53 +00:00
|
|
|
Bundle args = new Bundle();
|
2019-08-20 20:18:42 +00:00
|
|
|
args.putLong("id", copy < 0 ? id : copy);
|
2018-11-14 16:21:53 +00:00
|
|
|
|
|
|
|
new SimpleTask<EntityAnswer>() {
|
2020-08-03 06:31:48 +00:00
|
|
|
@Override
|
|
|
|
protected void onPreExecute(Bundle args) {
|
|
|
|
pbWait.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bundle args) {
|
|
|
|
pbWait.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2018-08-27 07:06:03 +00:00
|
|
|
@Override
|
2019-01-17 12:14:16 +00:00
|
|
|
protected EntityAnswer onExecute(Context context, Bundle args) {
|
2018-11-14 16:21:53 +00:00
|
|
|
long id = args.getLong("id");
|
|
|
|
return DB.getInstance(context).answer().getAnswer(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-31 07:03:48 +00:00
|
|
|
protected void onExecuted(Bundle args, EntityAnswer answer) {
|
2021-08-01 16:45:47 +00:00
|
|
|
final Context context = getContext();
|
|
|
|
|
2021-05-13 15:25:34 +00:00
|
|
|
if (copy > 0 && answer != null) {
|
|
|
|
answer.applied = 0;
|
|
|
|
answer.last_applied = null;
|
|
|
|
}
|
|
|
|
|
2020-05-31 09:48:18 +00:00
|
|
|
if (savedInstanceState == null) {
|
2021-03-05 11:12:03 +00:00
|
|
|
Bundle a = getArguments();
|
|
|
|
if (a == null)
|
|
|
|
a = new Bundle();
|
|
|
|
|
|
|
|
etName.setText(answer == null ? a.getString("subject") : answer.name);
|
2020-09-05 15:06:58 +00:00
|
|
|
etGroup.setText(answer == null ? null : answer.group);
|
2020-09-13 16:50:47 +00:00
|
|
|
cbStandard.setChecked(answer == null ? false : answer.standard);
|
2021-04-26 07:33:04 +00:00
|
|
|
cbReceipt.setChecked(answer == null ? false : answer.receipt);
|
2021-05-06 19:11:51 +00:00
|
|
|
cbFavorite.setChecked(answer == null ? false : answer.favorite);
|
2020-05-31 09:48:18 +00:00
|
|
|
cbHide.setChecked(answer == null ? false : answer.hide);
|
2021-08-01 14:14:48 +00:00
|
|
|
cbExternal.setChecked(answer == null ? false : answer.external);
|
2021-03-05 11:12:03 +00:00
|
|
|
|
|
|
|
String html = (answer == null ? a.getString("html") : answer.text);
|
2021-03-05 07:52:33 +00:00
|
|
|
if (html == null)
|
2020-05-31 09:48:18 +00:00
|
|
|
etText.setText(null);
|
|
|
|
else
|
2021-03-05 07:52:33 +00:00
|
|
|
etText.setText(HtmlHelper.fromHtml(html, new Html.ImageGetter() {
|
2020-05-31 09:48:18 +00:00
|
|
|
@Override
|
|
|
|
public Drawable getDrawable(String source) {
|
2021-02-12 16:12:39 +00:00
|
|
|
if (source != null && source.startsWith("cid:"))
|
|
|
|
source = null;
|
2021-08-01 16:45:47 +00:00
|
|
|
return ImageHelper.decodeImage(context, -1, source, true, 0, 1.0f, etText);
|
2020-05-31 09:48:18 +00:00
|
|
|
}
|
2021-08-01 16:45:47 +00:00
|
|
|
}, null, context));
|
2020-05-31 09:48:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 17:47:36 +00:00
|
|
|
if (answer == null)
|
|
|
|
bottom_navigation.getMenu().removeItem(R.id.action_delete);
|
2018-08-27 07:06:03 +00:00
|
|
|
|
2021-08-01 16:45:47 +00:00
|
|
|
if (ActivityAnswer.canAnswer(context))
|
2021-08-01 14:14:48 +00:00
|
|
|
cbExternal.setVisibility(View.VISIBLE);
|
2018-08-27 07:06:03 +00:00
|
|
|
grpReady.setVisibility(View.VISIBLE);
|
2020-11-03 08:39:27 +00:00
|
|
|
bottom_navigation.setVisibility(View.VISIBLE);
|
2018-08-27 07:06:03 +00:00
|
|
|
}
|
2018-12-01 09:47:08 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-12-06 07:50:46 +00:00
|
|
|
Log.unexpectedError(getParentFragmentManager(), ex);
|
2018-12-01 09:47:08 +00:00
|
|
|
}
|
2019-01-12 13:15:15 +00:00
|
|
|
}.execute(this, args, "answer:get");
|
2018-08-27 07:06:03 +00:00
|
|
|
}
|
2018-08-27 09:39:27 +00:00
|
|
|
|
2020-02-02 09:13:42 +00:00
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
2020-02-02 13:36:18 +00:00
|
|
|
inflater.inflate(R.menu.menu_answer, menu);
|
2020-02-02 09:13:42 +00:00
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:02:27 +00:00
|
|
|
@Override
|
|
|
|
public void onPrepareOptionsMenu(@NonNull Menu menu) {
|
|
|
|
menu.findItem(R.id.menu_placeholder_firstname).setVisible(BuildConfig.DEBUG);
|
|
|
|
menu.findItem(R.id.menu_placeholder_lastname).setVisible(BuildConfig.DEBUG);
|
|
|
|
super.onPrepareOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
2020-02-02 09:13:42 +00:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2021-03-05 11:02:27 +00:00
|
|
|
int itemId = item.getItemId();
|
|
|
|
if (itemId == R.id.menu_placeholder_name) {
|
|
|
|
onMenuPlaceholder("$name$");
|
|
|
|
return true;
|
|
|
|
} else if (itemId == R.id.menu_placeholder_email) {
|
|
|
|
onMenuPlaceholder("$email$");
|
|
|
|
return true;
|
|
|
|
} else if (itemId == R.id.menu_placeholder_firstname) {
|
|
|
|
onMenuPlaceholder("$firstname$");
|
|
|
|
return true;
|
|
|
|
} else if (itemId == R.id.menu_placeholder_lastname) {
|
|
|
|
onMenuPlaceholder("$lastname$");
|
2021-02-05 10:15:02 +00:00
|
|
|
return true;
|
2020-02-02 09:13:42 +00:00
|
|
|
}
|
2021-02-05 10:15:02 +00:00
|
|
|
return super.onOptionsItemSelected(item);
|
2020-02-02 09:13:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 11:02:27 +00:00
|
|
|
private void onMenuPlaceholder(String name) {
|
|
|
|
int start = etText.getSelectionStart();
|
|
|
|
int end = etText.getSelectionEnd();
|
|
|
|
if (start > end) {
|
|
|
|
int tmp = start;
|
|
|
|
start = end;
|
|
|
|
end = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start >= 0 && start < end)
|
|
|
|
etText.getText().replace(start, end, name);
|
|
|
|
else {
|
|
|
|
if (start < 0) {
|
|
|
|
start = etText.length() - 1;
|
|
|
|
if (start < 0)
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
etText.getText().insert(start, name);
|
|
|
|
}
|
2020-02-02 09:13:42 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 07:40:43 +00:00
|
|
|
private void onInsertImage() {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
intent.setType("image/*");
|
|
|
|
Helper.openAdvanced(intent);
|
|
|
|
startActivityForResult(intent, REQUEST_IMAGE);
|
|
|
|
}
|
|
|
|
|
2020-02-02 13:36:18 +00:00
|
|
|
private void onActionDelete() {
|
2019-07-01 13:01:37 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putString("question", getString(R.string.title_ask_delete_answer));
|
2020-10-25 13:55:58 +00:00
|
|
|
args.putBoolean("warning", true);
|
2019-07-01 13:01:37 +00:00
|
|
|
|
|
|
|
FragmentDialogAsk fragment = new FragmentDialogAsk();
|
|
|
|
fragment.setArguments(args);
|
|
|
|
fragment.setTargetFragment(FragmentAnswer.this, REQUEST_DELETE);
|
2019-10-12 15:16:53 +00:00
|
|
|
fragment.show(getParentFragmentManager(), "answer:delete");
|
2018-08-27 09:39:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 13:36:18 +00:00
|
|
|
private void onActionSave() {
|
2019-09-23 09:56:54 +00:00
|
|
|
etText.clearComposingText();
|
|
|
|
|
2021-06-26 08:36:25 +00:00
|
|
|
// Prevent splitting placeholders
|
|
|
|
Editable edit = etText.getText();
|
|
|
|
SuggestionSpan[] suggestions = edit.getSpans(0, etText.length(), SuggestionSpan.class);
|
|
|
|
for (SuggestionSpan suggestion : suggestions)
|
|
|
|
edit.removeSpan(suggestion);
|
|
|
|
|
2018-08-27 09:39:27 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("id", id);
|
2020-02-02 09:13:42 +00:00
|
|
|
args.putString("name", etName.getText().toString().trim());
|
2020-09-05 15:06:58 +00:00
|
|
|
args.putString("group", etGroup.getText().toString().trim());
|
2020-09-13 16:50:47 +00:00
|
|
|
args.putBoolean("standard", cbStandard.isChecked());
|
2021-04-26 07:33:04 +00:00
|
|
|
args.putBoolean("receipt", cbReceipt.isChecked());
|
2021-05-06 19:11:51 +00:00
|
|
|
args.putBoolean("favorite", cbFavorite.isChecked());
|
2019-04-20 08:35:30 +00:00
|
|
|
args.putBoolean("hide", cbHide.isChecked());
|
2021-08-01 14:14:48 +00:00
|
|
|
args.putBoolean("external", cbExternal.isChecked());
|
2020-08-04 13:20:41 +00:00
|
|
|
args.putString("html", HtmlHelper.toHtml(etText.getText(), getContext()));
|
2018-08-27 09:39:27 +00:00
|
|
|
|
|
|
|
new SimpleTask<Void>() {
|
2019-01-17 19:22:16 +00:00
|
|
|
@Override
|
|
|
|
protected void onPreExecute(Bundle args) {
|
|
|
|
Helper.setViewsEnabled(view, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bundle args) {
|
|
|
|
Helper.setViewsEnabled(view, true);
|
|
|
|
}
|
|
|
|
|
2018-08-27 09:39:27 +00:00
|
|
|
@Override
|
2018-12-31 07:03:48 +00:00
|
|
|
protected Void onExecute(Context context, Bundle args) {
|
2018-08-27 09:39:27 +00:00
|
|
|
long id = args.getLong("id");
|
|
|
|
String name = args.getString("name");
|
2020-09-05 15:06:58 +00:00
|
|
|
String group = args.getString("group");
|
2020-09-13 16:50:47 +00:00
|
|
|
boolean standard = args.getBoolean("standard");
|
2021-04-26 07:33:04 +00:00
|
|
|
boolean receipt = args.getBoolean("receipt");
|
2021-05-06 19:11:51 +00:00
|
|
|
boolean favorite = args.getBoolean("favorite");
|
2019-04-20 08:35:30 +00:00
|
|
|
boolean hide = args.getBoolean("hide");
|
2021-08-01 14:14:48 +00:00
|
|
|
boolean external = args.getBoolean("external");
|
2020-08-04 13:20:41 +00:00
|
|
|
String html = args.getString("html");
|
2018-08-27 09:39:27 +00:00
|
|
|
|
2020-02-02 09:13:42 +00:00
|
|
|
if (TextUtils.isEmpty(name))
|
|
|
|
throw new IllegalArgumentException(context.getString(R.string.title_no_name));
|
2020-09-05 15:06:58 +00:00
|
|
|
if (TextUtils.isEmpty(group))
|
|
|
|
group = null;
|
2020-02-02 09:13:42 +00:00
|
|
|
|
2020-08-11 11:21:28 +00:00
|
|
|
Document document = JsoupEx.parse(html);
|
2020-08-04 13:20:41 +00:00
|
|
|
|
2018-08-27 09:39:27 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2020-09-13 16:50:47 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
if (standard)
|
|
|
|
db.answer().resetStandard();
|
2021-04-26 07:33:04 +00:00
|
|
|
if (receipt)
|
|
|
|
db.answer().resetReceipt();
|
2020-09-13 16:50:47 +00:00
|
|
|
|
2021-08-01 17:05:17 +00:00
|
|
|
EntityAnswer answer;
|
|
|
|
if (id < 0)
|
|
|
|
answer = new EntityAnswer();
|
|
|
|
else
|
|
|
|
answer = db.answer().getAnswer(id);
|
|
|
|
|
|
|
|
answer.name = name;
|
|
|
|
answer.group = group;
|
|
|
|
answer.standard = standard;
|
|
|
|
answer.receipt = receipt;
|
|
|
|
answer.favorite = favorite;
|
|
|
|
answer.hide = hide;
|
|
|
|
answer.external = external;
|
|
|
|
answer.text = document.body().html();
|
|
|
|
|
|
|
|
if (id < 0)
|
2020-09-13 16:50:47 +00:00
|
|
|
answer.id = db.answer().insertAnswer(answer);
|
2021-08-01 17:05:17 +00:00
|
|
|
else
|
2020-09-13 16:50:47 +00:00
|
|
|
db.answer().updateAnswer(answer);
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2018-08-27 09:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-31 07:03:48 +00:00
|
|
|
protected void onExecuted(Bundle args, Void data) {
|
2018-08-27 09:39:27 +00:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2020-02-02 09:13:42 +00:00
|
|
|
if (ex instanceof IllegalArgumentException)
|
2020-07-06 17:02:01 +00:00
|
|
|
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG)
|
|
|
|
.setGestureInsetBottomIgnored(true).show();
|
2020-02-02 09:13:42 +00:00
|
|
|
else
|
|
|
|
Log.unexpectedError(getParentFragmentManager(), ex);
|
2018-08-27 09:39:27 +00:00
|
|
|
}
|
2019-01-12 13:15:15 +00:00
|
|
|
}.execute(this, args, "answer:save");
|
2018-08-27 09:39:27 +00:00
|
|
|
}
|
2019-06-30 21:05:00 +00:00
|
|
|
|
2020-02-02 13:36:18 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (requestCode) {
|
2020-04-27 07:40:43 +00:00
|
|
|
case REQUEST_IMAGE:
|
|
|
|
if (resultCode == RESULT_OK && data != null)
|
|
|
|
onImageSelected(data.getData());
|
|
|
|
break;
|
2020-02-02 13:36:18 +00:00
|
|
|
case REQUEST_LINK:
|
|
|
|
if (resultCode == RESULT_OK && data != null)
|
|
|
|
onLinkSelected(data.getBundleExtra("args"));
|
|
|
|
break;
|
|
|
|
case REQUEST_DELETE:
|
|
|
|
if (resultCode == RESULT_OK)
|
|
|
|
onDelete();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 07:40:43 +00:00
|
|
|
private void onImageSelected(Uri uri) {
|
|
|
|
try {
|
|
|
|
getContext().getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
|
|
|
|
int start = etText.getSelectionStart();
|
2021-09-09 10:52:10 +00:00
|
|
|
SpannableStringBuilder ssb = new SpannableStringBuilderEx(etText.getText());
|
2020-04-27 08:27:52 +00:00
|
|
|
ssb.insert(start, " \uFFFC"); // Object replacement character
|
2020-04-27 07:40:43 +00:00
|
|
|
String source = uri.toString();
|
2020-09-06 16:12:17 +00:00
|
|
|
Drawable d = ImageHelper.decodeImage(getContext(), -1, source, true, 0, 1.0f, etText);
|
2020-04-27 07:40:43 +00:00
|
|
|
ImageSpan is = new ImageSpan(d, source);
|
2020-04-27 08:27:52 +00:00
|
|
|
ssb.setSpan(is, start + 1, start + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
2020-04-27 07:40:43 +00:00
|
|
|
etText.setText(ssb);
|
2020-04-27 08:27:52 +00:00
|
|
|
etText.setSelection(start + 2);
|
2020-06-07 13:52:29 +00:00
|
|
|
} catch (SecurityException ex) {
|
2020-07-06 17:02:01 +00:00
|
|
|
Snackbar sb = Snackbar.make(view, R.string.title_no_stream, Snackbar.LENGTH_INDEFINITE)
|
|
|
|
.setGestureInsetBottomIgnored(true);
|
2020-06-07 13:52:29 +00:00
|
|
|
sb.setAction(R.string.title_info, new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2020-11-11 16:35:32 +00:00
|
|
|
Helper.viewFAQ(v.getContext(), 49);
|
2020-06-07 13:52:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
sb.show();
|
2020-04-27 07:40:43 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.unexpectedError(getParentFragmentManager(), ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 13:36:18 +00:00
|
|
|
private void onLinkSelected(Bundle args) {
|
|
|
|
String link = args.getString("link");
|
2020-05-16 14:58:56 +00:00
|
|
|
int start = args.getInt("start");
|
|
|
|
int end = args.getInt("end");
|
|
|
|
etText.setSelection(start, end);
|
2020-11-12 14:08:34 +00:00
|
|
|
StyleHelper.apply(R.id.menu_link, getViewLifecycleOwner(), null, etText, link);
|
2020-02-02 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 13:01:37 +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).answer().deleteAnswer(id);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, Void data) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-12-06 07:50:46 +00:00
|
|
|
Log.unexpectedError(getParentFragmentManager(), ex);
|
2019-07-01 13:01:37 +00:00
|
|
|
}
|
2019-09-11 12:03:59 +00:00
|
|
|
}.execute(this, args, "answer:delete");
|
2019-07-01 13:01:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 18:04:33 +00:00
|
|
|
private boolean onActionStyle(int action) {
|
|
|
|
Log.i("Style action=" + action);
|
|
|
|
|
|
|
|
if (action == R.id.menu_link) {
|
|
|
|
Uri uri = null;
|
|
|
|
|
|
|
|
ClipboardManager cbm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
|
2020-02-23 10:16:40 +00:00
|
|
|
if (cbm != null && cbm.hasPrimaryClip()) {
|
2019-09-27 18:04:33 +00:00
|
|
|
String link = cbm.getPrimaryClip().getItemAt(0).coerceToText(getContext()).toString();
|
|
|
|
uri = Uri.parse(link);
|
|
|
|
if (uri.getScheme() == null)
|
|
|
|
uri = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putParcelable("uri", uri);
|
2020-05-16 14:58:56 +00:00
|
|
|
args.putInt("start", etText.getSelectionStart());
|
|
|
|
args.putInt("end", etText.getSelectionEnd());
|
2019-09-27 18:04:33 +00:00
|
|
|
|
2021-04-04 11:45:32 +00:00
|
|
|
FragmentDialogInsertLink fragment = new FragmentDialogInsertLink();
|
2019-09-27 18:04:33 +00:00
|
|
|
fragment.setArguments(args);
|
|
|
|
fragment.setTargetFragment(this, REQUEST_LINK);
|
2020-02-23 10:16:40 +00:00
|
|
|
fragment.show(getParentFragmentManager(), "answer:link");
|
2019-09-27 18:04:33 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} else
|
2020-11-12 14:08:34 +00:00
|
|
|
return StyleHelper.apply(action, getViewLifecycleOwner(), view.findViewById(action), etText);
|
2019-09-27 18:04:33 +00:00
|
|
|
}
|
2018-08-27 07:06:03 +00:00
|
|
|
}
|