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

138 lines
4.5 KiB
Java
Raw Normal View History

2019-03-17 09:18:40 +00:00
package eu.faircode.email;
2019-05-04 20:49:22 +00: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/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2019-05-04 20:49:22 +00:00
*/
2019-05-14 10:12:13 +00:00
import android.Manifest;
2019-03-17 09:18:40 +00:00
import android.content.ContentResolver;
import android.content.Context;
2020-01-27 08:37:04 +00:00
import android.content.Intent;
2019-03-17 09:18:40 +00:00
import android.net.Uri;
import android.os.Bundle;
2019-08-29 19:57:04 +00:00
import android.text.Spanned;
2019-03-17 09:18:40 +00:00
import android.view.View;
import android.widget.TextView;
import androidx.constraintlayout.widget.Group;
2019-03-17 09:18:40 +00:00
import com.google.android.material.snackbar.Snackbar;
import java.io.ByteArrayOutputStream;
2020-01-27 08:37:04 +00:00
import java.io.FileNotFoundException;
2019-03-17 09:18:40 +00:00
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
2019-03-17 09:18:40 +00:00
public class ActivityDSN extends ActivityBase {
2020-01-27 08:37:04 +00:00
private TextView tvHeaders;
private ContentLoadingProgressBar pbWait;
private Group grpReady;
2019-03-17 09:18:40 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setSubtitle("DSN");
setContentView(R.layout.activity_dsn);
2020-01-27 08:37:04 +00:00
tvHeaders = findViewById(R.id.tvHeaders);
pbWait = findViewById(R.id.pbWait);
grpReady = findViewById(R.id.grpReady);
2019-03-17 09:18:40 +00:00
grpReady.setVisibility(View.GONE);
2020-01-27 08:37:04 +00:00
load();
}
2019-03-17 09:18:40 +00:00
2020-01-27 08:37:04 +00:00
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
load();
}
private void load() {
Uri uri = getIntent().getData();
Log.i("DSN uri=" + uri);
2019-03-17 09:18:40 +00:00
Bundle args = new Bundle();
args.putParcelable("uri", uri);
new SimpleTask<Result>() {
2020-01-27 08:37:04 +00:00
@Override
protected void onPreExecute(Bundle args) {
pbWait.setVisibility(View.VISIBLE);
}
2019-03-17 09:18:40 +00:00
@Override
protected void onPostExecute(Bundle args) {
pbWait.setVisibility(View.GONE);
}
@Override
protected Result onExecute(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri");
2020-01-27 08:37:04 +00:00
if (uri == null)
throw new FileNotFoundException();
2019-11-03 19:07:26 +00:00
if (!"content".equals(uri.getScheme()) &&
2019-05-14 10:12:13 +00:00
!Helper.hasPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
Log.w("DSN uri=" + uri);
2019-03-17 09:18:40 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
2019-05-14 10:12:13 +00:00
}
2019-03-17 09:18:40 +00:00
Result result = new Result();
ContentResolver resolver = context.getContentResolver();
2020-10-14 14:50:15 +00:00
try (InputStream is = resolver.openInputStream(uri)) {
2019-03-17 09:18:40 +00:00
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[Helper.BUFFER_SIZE];
2019-03-17 09:18:40 +00:00
int length;
while ((length = is.read(buffer)) != -1)
bos.write(buffer, 0, length);
2019-08-29 19:57:04 +00:00
String headers = MessageHelper.decodeMime(bos.toString(StandardCharsets.UTF_8.name()));
2021-07-01 07:19:59 +00:00
result.headers = HtmlHelper.highlightHeaders(context, headers, false);
2019-03-17 09:18:40 +00:00
}
return result;
}
@Override
protected void onExecuted(Bundle args, Result result) {
tvHeaders.setText(result.headers);
grpReady.setVisibility(View.VISIBLE);
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(findViewById(android.R.id.content), ex.getMessage(), Snackbar.LENGTH_LONG)
.setGestureInsetBottomIgnored(true).show();
2019-03-17 09:18:40 +00:00
else
2020-08-22 05:44:39 +00:00
Log.unexpectedError(getSupportFragmentManager(), ex, false);
2019-03-17 09:18:40 +00:00
}
}.execute(this, args, "disposition:decode");
}
private class Result {
2019-08-29 19:57:04 +00:00
Spanned headers;
2019-03-17 09:18:40 +00:00
}
}