Commit 4721ab1d authored by FabioWidmer's avatar FabioWidmer

Add extraction dialog

parent 5a19a357
......@@ -4,7 +4,9 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.documentfile.provider.DocumentFile;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
......@@ -21,9 +23,12 @@ import net.freeutils.tnef.TNEFUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class TNEFActivity extends AppCompatActivity {
private static final int REQUEST_CODE_BROWSE_FOLDER = 1;
private ArrayList<CharSequence> attachmentNames = new ArrayList<>();
private ArrayList<Uri> attachmentFiles = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -72,18 +77,37 @@ public class TNEFActivity extends AppCompatActivity {
message = new Message(tnefInputStream);
extractAttachmentsTo(message, folderFile);
Toast.makeText(getApplicationContext(), getString(R.string.extraction_succeeded), Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.extraction_dialog_title)
.setCancelable(true)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialogInterface) {
finish();
}
})
.setItems(attachmentNames.toArray(new CharSequence[0]), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(attachmentFiles.get(which), getMimeType(attachmentNames.get(which).toString()));
startActivity(intent);
finish();
}
}).show();
} catch(IOException e) {
Toast.makeText(getApplicationContext(), getString(R.string.extraction_failed_all), Toast.LENGTH_LONG).show();
finish();
} finally {
try {
TNEFUtils.closeAll(message, tnefInputStream, inputStream);
} catch(IOException ignored) {
}
}
} else {
finish();
}
finish();
}
private void extractAttachmentsTo(Message message, DocumentFile folder) {
......@@ -103,6 +127,8 @@ public class TNEFActivity extends AppCompatActivity {
if(file != null) {
attachment.writeTo(contentResolver.openOutputStream(file.getUri()));
attachmentNames.add(filename);
attachmentFiles.add(file.getUri());
}
} catch(IOException e) {
Toast.makeText(getApplicationContext(), getString(R.string.extraction_failed_single, filename), Toast.LENGTH_LONG).show();
......
<resources>
<string name="app_name">TNEF Extractor</string>
<string name="extraction_dialog_title">Extracted Attachments</string>
<string name="extraction_failed_all">Failed to extract file.</string>
<string name="extraction_failed_single">Failed to extract attachment %1$s.</string>
<string name="extraction_succeeded">File extracted successfully.</string>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment