Files
FairEmail/app/src/main/java/eu/faircode/email/FragmentDialogMarkdown.java

106 lines
3.8 KiB
Java
Raw Normal View History

package eu.faircode.email;
2019-10-20 12:15:13 +02:00
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2021-01-01 08:56:36 +01:00
Copyright 2018-2021 by Marcel Bokhorst (M66B)
2019-10-20 12:15:13 +02:00
*/
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.InputStream;
import io.noties.markwon.Markwon;
2019-09-11 21:43:27 +02:00
public class FragmentDialogMarkdown extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
2020-10-30 10:36:16 +01:00
final View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_markdown, null);
final TextView tvMarkdown = dview.findViewById(R.id.tvMarkdown);
final ContentLoadingProgressBar pbWait = dview.findViewById(R.id.pbWait);
tvMarkdown.setText(null);
2020-10-30 10:36:16 +01:00
Dialog dialog = new Dialog(getContext());
//dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(dview);
dialog.getWindow().setLayout(
WindowManager.LayoutParams.MATCH_PARENT,
2019-07-23 17:05:30 +02:00
WindowManager.LayoutParams.WRAP_CONTENT);
new SimpleTask<Spanned>() {
@Override
protected void onPreExecute(Bundle args) {
tvMarkdown.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bundle args) {
tvMarkdown.setVisibility(View.VISIBLE);
pbWait.setVisibility(View.GONE);
}
@Override
protected Spanned onExecute(Context context, Bundle args) throws Throwable {
String name = args.getString("name");
2020-07-20 14:17:20 +02:00
2021-05-05 07:44:19 +02:00
String markdown;
2020-07-20 14:50:10 +02:00
String asset = Helper.getLocalizedAsset(context, name);
2020-07-20 14:17:20 +02:00
try (InputStream is = context.getAssets().open(asset)) {
byte[] buffer = new byte[is.available()];
is.read(buffer);
2021-05-05 07:44:19 +02:00
markdown = new String(buffer);
}
2021-05-05 07:44:19 +02:00
String locale = Helper.getFAQLocale();
if (locale != null)
markdown = markdown.replace(
"https://github.com/M66B/FairEmail/blob/master/FAQ.md",
"https://github.com/M66B/FairEmail/blob/master/docs/FAQ-" + locale + ".md");
Markwon markwon = Markwon.create(context);
return markwon.toMarkdown(markdown);
}
@Override
protected void onExecuted(Bundle args, Spanned markdown) {
tvMarkdown.setText(markdown);
tvMarkdown.setMovementMethod(LinkMovementMethod.getInstance());
}
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 08:50:46 +01:00
Log.unexpectedError(getParentFragmentManager(), ex);
}
2019-09-11 14:03:59 +02:00
}.execute(this, getArguments(), "markdown:read");
return dialog;
}
}