() {
@Override
protected Long onLoad(Context context, Bundle args) throws Throwable {
File file = new File(context.getCacheDir(), "crash.log");
if (file.exists()) {
// Get version info
StringBuilder sb = new StringBuilder();
sb.append(context.getString(R.string.title_crash_info_remark)).append("\n\n\n\n");
sb.append(String.format("%s: %s %s/%s\r\n",
context.getString(R.string.app_name),
BuildConfig.APPLICATION_ID,
BuildConfig.VERSION_NAME,
Helper.hasValidFingerprint(context) ? "1" : "3"));
sb.append(String.format("Android: %s (SDK %d)\r\n", Build.VERSION.RELEASE, Build.VERSION.SDK_INT));
sb.append("\r\n");
// Get device info
sb.append(String.format("Brand: %s\r\n", Build.BRAND));
sb.append(String.format("Manufacturer: %s\r\n", Build.MANUFACTURER));
sb.append(String.format("Model: %s\r\n", Build.MODEL));
sb.append(String.format("Product: %s\r\n", Build.PRODUCT));
sb.append(String.format("Device: %s\r\n", Build.DEVICE));
sb.append(String.format("Host: %s\r\n", Build.HOST));
sb.append(String.format("Display: %s\r\n", Build.DISPLAY));
sb.append(String.format("Id: %s\r\n", Build.ID));
sb.append("\r\n");
BufferedReader in = null;
try {
String line;
in = new BufferedReader(new FileReader(file));
while ((line = in.readLine()) != null)
sb.append(line).append("\r\n");
} finally {
if (in != null)
in.close();
}
file.delete();
String body = "" + sb.toString().replaceAll("\\r?\\n", "
") + "
";
EntityMessage draft = null;
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityFolder drafts = db.folder().getPrimaryDrafts();
if (drafts != null) {
draft = new EntityMessage();
draft.account = drafts.account;
draft.folder = drafts.id;
draft.msgid = EntityMessage.generateMessageId();
draft.to = new Address[]{Helper.myAddress()};
draft.subject = context.getString(R.string.app_name) + " " + BuildConfig.VERSION_NAME + " crash log";
draft.content = true;
draft.received = new Date().getTime();
draft.seen = false;
draft.ui_seen = false;
draft.flagged = false;
draft.ui_flagged = false;
draft.ui_hide = false;
draft.ui_found = false;
draft.ui_ignored = false;
draft.id = db.message().insertMessage(draft);
draft.write(context, body);
}
EntityOperation.queue(db, draft, EntityOperation.ADD);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
EntityOperation.process(context);
return (draft == null ? null : draft.id);
}
return null;
}
@Override
protected void onLoaded(Bundle args, Long id) {
if (id != null)
startActivity(
new Intent(ActivityView.this, ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", id));
}
}.load(this, new Bundle());
}
private class UpdateInfo {
String tag_name; // version
String html_url;
}
private void checkUpdate() {
long now = new Date().getTime();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (!prefs.getBoolean("updates", true))
return;
if (prefs.getLong("last_update_check", 0) + UPDATE_INTERVAL > now)
return;
prefs.edit().putLong("last_update_check", now).apply();
new SimpleTask() {
@Override
protected UpdateInfo onLoad(Context context, Bundle args) throws Throwable {
StringBuilder json = new StringBuilder();
HttpsURLConnection urlConnection = null;
try {
URL latest = new URL(UPDATE_LATEST_API);
urlConnection = (HttpsURLConnection) latest.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = br.readLine()) != null)
json.append(line);
JSONObject jroot = new JSONObject(json.toString());
if (jroot.has("tag_name") &&
jroot.has("html_url") &&
jroot.has("assets")) {
// Get update info
UpdateInfo info = new UpdateInfo();
info.tag_name = jroot.getString("tag_name");
info.html_url = jroot.getString("html_url");
if (TextUtils.isEmpty(info.html_url))
return null;
// Check if new release
JSONArray jassets = jroot.getJSONArray("assets");
for (int i = 0; i < jassets.length(); i++) {
JSONObject jasset = jassets.getJSONObject(i);
if (jasset.has("name")) {
String name = jasset.getString("name");
if (name != null && name.endsWith(".apk")) {
if (TextUtils.isEmpty(info.tag_name))
info.tag_name = name;
Log.i(Helper.TAG, "Latest version=" + info.tag_name);
if (BuildConfig.VERSION_NAME.equals(info.tag_name))
break;
else
return info;
}
}
}
}
return null;
} finally {
if (urlConnection != null)
urlConnection.disconnect();
}
}
@Override
protected void onLoaded(Bundle args, UpdateInfo info) {
if (info == null)
return;
final Intent update = new Intent(Intent.ACTION_VIEW, Uri.parse(info.html_url));
if (update.resolveActivity(getPackageManager()) != null)
new DialogBuilderLifecycle(ActivityView.this, ActivityView.this)
.setMessage(getString(R.string.title_updated, info.tag_name))
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Helper.view(ActivityView.this, update);
}
})
.show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (BuildConfig.DEBUG)
Helper.unexpectedError(ActivityView.this, ex);
}
}.load(this, new Bundle());
}
private Intent getIntentFAQ() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://github.com/M66B/open-source-email/blob/master/FAQ.md"));
return intent;
}
private Intent getIntentRate() {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + BuildConfig.APPLICATION_ID));
if (intent.resolveActivity(getPackageManager()) == null)
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID));
return intent;
}
private Intent getIntentInvite() {
Intent intent = new Intent("com.google.android.gms.appinvite.ACTION_APP_INVITE");
intent.setPackage("com.google.android.gms");
intent.putExtra("com.google.android.gms.appinvite.TITLE", getString(R.string.menu_invite));
intent.putExtra("com.google.android.gms.appinvite.MESSAGE", getString(R.string.title_try));
intent.putExtra("com.google.android.gms.appinvite.BUTTON_TEXT", getString(R.string.title_try));
// com.google.android.gms.appinvite.DEEP_LINK_URL
return intent;
}
private Intent getIntentOtherApps() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/dev?id=8420080860664580239"));
return intent;
}
private void onMenuFolders(long account) {
getSupportFragmentManager().popBackStack("unified", 0);
Bundle args = new Bundle();
args.putLong("account", account);
FragmentFolders fragment = new FragmentFolders();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("folders");
fragmentTransaction.commit();
}
private void onMenuSetup() {
startActivity(new Intent(ActivityView.this, ActivitySetup.class));
}
private void onMenuAnswers() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentAnswers()).addToBackStack("answers");
fragmentTransaction.commit();
}
private void onMenuOperations() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentOperations()).addToBackStack("operations");
fragmentTransaction.commit();
}
private void onMenuLegend() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentLegend()).addToBackStack("legend");
fragmentTransaction.commit();
}
private void onMenuFAQ() {
Helper.view(this, getIntentFAQ());
}
private void onMenuPro() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
}
private void onMenuPrivacy() {
Helper.view(this, Helper.getIntentPrivacy());
}
private void onMenuAbout() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentAbout()).addToBackStack("about");
fragmentTransaction.commit();
}
private void onMenuRate() {
Intent faq = getIntentFAQ();
if (faq.resolveActivity(getPackageManager()) == null)
Helper.view(this, getIntentRate());
else {
new DialogBuilderLifecycle(this, this)
.setMessage(R.string.title_issue)
.setPositiveButton(R.string.title_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Helper.view(ActivityView.this, getIntentFAQ());
}
})
.setNegativeButton(R.string.title_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Helper.view(ActivityView.this, getIntentRate());
}
})
.show();
}
}
private void onMenuInvite() {
startActivityForResult(getIntentInvite(), REQUEST_INVITE);
}
private void onMenuOtherApps() {
Helper.view(this, getIntentOtherApps());
}
private class DrawerItem {
private int layout;
private int id;
private int icon;
private Integer color;
private String title;
private Object data;
DrawerItem(int layout) {
this.layout = layout;
}
DrawerItem(Context context, int layout, int icon, int title) {
this.layout = layout;
this.id = title;
this.icon = icon;
this.title = context.getString(title);
}
DrawerItem(int layout, int id, int icon, Integer color, String title, Object data) {
this.layout = layout;
this.id = id;
this.icon = icon;
this.color = color;
this.title = title;
this.data = data;
}
public int getId() {
return this.id;
}
public Object getData() {
return this.data;
}
}
private static class ArrayAdapterDrawer extends ArrayAdapter {
ArrayAdapterDrawer(@NonNull Context context) {
super(context, -1);
}
@NonNull
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
DrawerItem item = getItem(position);
View row = LayoutInflater.from(getContext()).inflate(item.layout, null);
ImageView iv = row.findViewById(R.id.ivItem);
TextView tv = row.findViewById(R.id.tvItem);
if (iv != null) {
iv.setImageResource(item.icon);
if (item.color != null)
iv.setColorFilter(item.color);
}
if (tv != null)
tv.setText(item.title);
return row;
}
}
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION_VIEW_MESSAGES.equals(intent.getAction()))
onViewMessages(intent);
else if (ACTION_VIEW_THREAD.equals(intent.getAction()))
onViewThread(intent);
else if (ACTION_VIEW_FULL.equals(intent.getAction()))
onViewFull(intent);
else if (ACTION_EDIT_FOLDER.equals(intent.getAction()))
onEditFolder(intent);
else if (ACTION_EDIT_ANSWER.equals(intent.getAction()))
onEditAnswer(intent);
else if (ACTION_STORE_ATTACHMENT.equals(intent.getAction()))
onStoreAttachment(intent);
else if (ACTION_DECRYPT.equals(intent.getAction()))
onDecrypt(intent);
else if (ACTION_SHOW_PRO.equals(intent.getAction()))
onShowPro(intent);
}
};
private void onViewMessages(Intent intent) {
Bundle args = new Bundle();
args.putLong("account", intent.getLongExtra("account", -1));
args.putLong("folder", intent.getLongExtra("folder", -1));
FragmentMessages fragment = new FragmentMessages();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("messages");
fragmentTransaction.commit();
}
private void onViewThread(Intent intent) {
getSupportFragmentManager().popBackStack("thread", FragmentManager.POP_BACK_STACK_INCLUSIVE);
Bundle args = new Bundle();
args.putLong("account", intent.getLongExtra("account", -1));
args.putString("thread", intent.getStringExtra("thread"));
args.putBoolean("found", intent.getBooleanExtra("found", false));
FragmentMessages fragment = new FragmentMessages();
fragment.setArguments(args);
int pane;
if (grpPane == null)
pane = R.id.content_frame;
else {
pane = R.id.content_pane;
grpPane.setVisibility(View.VISIBLE);
}
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(pane, fragment).addToBackStack("thread");
fragmentTransaction.commit();
}
private void onViewFull(Intent intent) {
FragmentWebView fragment = new FragmentWebView();
fragment.setArguments(intent.getExtras());
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("webview");
fragmentTransaction.commit();
}
private void onEditFolder(Intent intent) {
FragmentFolder fragment = new FragmentFolder();
fragment.setArguments(intent.getExtras());
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("folder");
fragmentTransaction.commit();
}
private void onEditAnswer(Intent intent) {
FragmentAnswer fragment = new FragmentAnswer();
fragment.setArguments(intent.getExtras());
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("answer");
fragmentTransaction.commit();
}
private void onStoreAttachment(Intent intent) {
attachment = intent.getLongExtra("id", -1);
Intent create = new Intent(Intent.ACTION_CREATE_DOCUMENT);
create.addCategory(Intent.CATEGORY_OPENABLE);
create.setType(intent.getStringExtra("type"));
create.putExtra(Intent.EXTRA_TITLE, intent.getStringExtra("name"));
if (create.resolveActivity(getPackageManager()) == null)
Snackbar.make(view, R.string.title_no_saf, Snackbar.LENGTH_LONG).show();
else
startActivityForResult(create, REQUEST_ATTACHMENT);
}
private void onDecrypt(Intent intent) {
if (Helper.isPro(this)) {
if (pgpService.isBound()) {
Intent data = new Intent();
data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
decrypt(data, intent.getLongExtra("id", -1));
} else {
Snackbar snackbar = Snackbar.make(view, R.string.title_no_openpgp, Snackbar.LENGTH_LONG);
if (Helper.getIntentOpenKeychain().resolveActivity(getPackageManager()) != null)
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(Helper.getIntentOpenKeychain());
}
});
snackbar.show();
}
} else
onShowPro(intent);
}
private void onShowPro(Intent intent) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
}
private void decrypt(Intent data, long id) {
Bundle args = new Bundle();
args.putLong("id", id);
args.putParcelable("data", data);
new SimpleTask() {
@Override
protected PendingIntent onLoad(Context context, Bundle args) throws Throwable {
// Get arguments
long id = args.getLong("id");
Intent data = args.getParcelable("data");
DB db = DB.getInstance(context);
boolean inline = false;
InputStream encrypted = null;
// Find encrypted data
List attachments = db.attachment().getAttachments(id);
for (EntityAttachment attachment : attachments)
if ("encrypted.asc".equals(attachment.name)) {
if (!attachment.available)
throw new IllegalArgumentException(getString(R.string.title_attachments_missing));
encrypted = new FileInputStream(EntityAttachment.getFile(context, attachment.id));
break;
}
if (encrypted == null) {
EntityMessage message = db.message().getMessage(id);
if (message.content) {
String body = message.read(context);
if (body != null) {
int begin = body.indexOf(PGP_BEGIN_MESSAGE);
int end = body.indexOf(PGP_END_MESSAGE);
if (begin >= 0 && begin < end) {
String section = body.substring(begin, end + PGP_END_MESSAGE.length());
section = section.replace("
", "\n\r");
inline = true;
encrypted = new ByteArrayInputStream(section.getBytes());
}
}
}
}
if (encrypted == null)
throw new IllegalArgumentException(getString(R.string.title_not_encrypted));
ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
// Decrypt message
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
Intent result = api.executeApi(data, encrypted, decrypted);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
if (inline) {
try {
db.beginTransaction();
// Write decrypted body
EntityMessage m = db.message().getMessage(id);
m.write(context, decrypted.toString());
db.message().setMessageStored(id, new Date().getTime());
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} else {
// Decode message
Properties props = MessageHelper.getSessionProperties(Helper.AUTH_TYPE_PASSWORD, false);
Session isession = Session.getInstance(props, null);
ByteArrayInputStream is = new ByteArrayInputStream(decrypted.toByteArray());
MimeMessage imessage = new MimeMessage(isession, is);
MessageHelper helper = new MessageHelper(imessage);
try {
db.beginTransaction();
// Write decrypted body
EntityMessage m = db.message().getMessage(id);
m.write(context, helper.getHtml());
// Remove previously decrypted attachments
for (EntityAttachment a : attachments)
if (!"encrypted.asc".equals(a.name))
db.attachment().deleteAttachment(a.id);
// Add decrypted attachments
int sequence = db.attachment().getAttachmentSequence(id);
for (EntityAttachment a : helper.getAttachments()) {
a.message = id;
a.sequence = ++sequence;
a.id = db.attachment().insertAttachment(a);
}
db.message().setMessageStored(id, new Date().getTime());
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
break;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
message = id;
return result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
case OpenPgpApi.RESULT_CODE_ERROR:
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
throw new IllegalArgumentException(error.getMessage());
}
return null;
}
@Override
protected void onLoaded(Bundle args, PendingIntent pi) {
if (pi != null)
try {
startIntentSenderForResult(
pi.getIntentSender(),
ActivityView.REQUEST_DECRYPT,
null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException ex) {
Helper.unexpectedError(ActivityView.this, ex);
}
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Helper.unexpectedError(ActivityView.this, ex);
}
}.load(ActivityView.this, args);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(Helper.TAG, "View onActivityResult request=" + requestCode + " result=" + resultCode + " data=" + data);
if (resultCode == Activity.RESULT_OK)
if (requestCode == REQUEST_ATTACHMENT) {
if (data != null) {
Bundle args = new Bundle();
args.putLong("id", attachment);
args.putParcelable("uri", data.getData());
new SimpleTask() {
@Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
Uri uri = args.getParcelable("uri");
File file = EntityAttachment.getFile(context, id);
ParcelFileDescriptor pfd = null;
FileOutputStream fos = null;
FileInputStream fis = null;
try {
pfd = context.getContentResolver().openFileDescriptor(uri, "w");
fos = new FileOutputStream(pfd.getFileDescriptor());
fis = new FileInputStream(file);
byte[] buffer = new byte[ATTACHMENT_BUFFER_SIZE];
int read;
while ((read = fis.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
} finally {
try {
if (pfd != null)
pfd.close();
} catch (Throwable ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
try {
if (fos != null)
fos.close();
} catch (Throwable ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
try {
if (fis != null)
fis.close();
} catch (Throwable ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
}
return null;
}
@Override
protected void onLoaded(Bundle args, Void data) {
Toast.makeText(ActivityView.this, R.string.title_attachment_saved, Toast.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
Helper.unexpectedError(ActivityView.this, ex);
}
}.load(this, args);
}
} else if (requestCode == REQUEST_DECRYPT) {
if (data != null)
decrypt(data, message);
}
}
}