Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.5 KiB

6 years ago
  1. package eu.faircode.email;
  2. /*
  3. This file is part of FairEmail.
  4. FairEmail is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. NetGuard is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
  14. Copyright 2018 by Marcel Bokhorst (M66B)
  15. */
  16. import android.content.Context;
  17. import android.content.res.TypedArray;
  18. import android.util.Log;
  19. import android.view.Menu;
  20. import android.view.View;
  21. import android.view.ViewGroup;
  22. import android.widget.CheckBox;
  23. import android.widget.EditText;
  24. import android.widget.ImageView;
  25. import android.widget.Spinner;
  26. import com.google.android.material.bottomnavigation.BottomNavigationView;
  27. import java.io.BufferedReader;
  28. import java.io.IOException;
  29. import java.io.InputStreamReader;
  30. import java.io.UnsupportedEncodingException;
  31. import javax.mail.Address;
  32. import javax.mail.internet.InternetAddress;
  33. public class Helper {
  34. static final String TAG = "fairemail";
  35. static int resolveColor(Context context, int attr) {
  36. int[] attrs = new int[]{attr};
  37. TypedArray a = context.getTheme().obtainStyledAttributes(attrs);
  38. int color = a.getColor(0, 0xFF0000);
  39. a.recycle();
  40. return color;
  41. }
  42. static void setViewsEnabled(ViewGroup view, boolean enabled) {
  43. for (int i = 0; i < view.getChildCount(); i++) {
  44. View child = view.getChildAt(i);
  45. if (child instanceof Spinner ||
  46. child instanceof EditText ||
  47. child instanceof CheckBox ||
  48. child instanceof ImageView /* =ImageButton */)
  49. child.setEnabled(enabled);
  50. if (child instanceof BottomNavigationView) {
  51. Menu menu = ((BottomNavigationView) child).getMenu();
  52. menu.setGroupEnabled(0, enabled);
  53. } else if (child instanceof ViewGroup)
  54. setViewsEnabled((ViewGroup) child, enabled);
  55. }
  56. }
  57. static String localizeFolderName(Context context, String name) {
  58. if ("INBOX".equals(name))
  59. return context.getString(R.string.title_folder_inbox);
  60. else if ("OUTBOX".equals(name))
  61. return context.getString(R.string.title_folder_outbox);
  62. else
  63. return name;
  64. }
  65. static String formatThrowable(Throwable ex) {
  66. StringBuilder sb = new StringBuilder();
  67. sb.append(ex.getMessage());
  68. Throwable cause = ex.getCause();
  69. while (cause != null) {
  70. sb.append(" ").append(cause.getMessage());
  71. cause = cause.getCause();
  72. }
  73. return sb.toString();
  74. }
  75. static String humanReadableByteCount(long bytes, boolean si) {
  76. int unit = si ? 1000 : 1024;
  77. if (bytes < unit) return bytes + " B";
  78. int exp = (int) (Math.log(bytes) / Math.log(unit));
  79. String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
  80. return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
  81. }
  82. static StringBuilder getLogcat() {
  83. StringBuilder sb = new StringBuilder();
  84. Process proc = null;
  85. BufferedReader br = null;
  86. try {
  87. String[] cmd = new String[]{"logcat", "-d", "-v", "threadtime", TAG + ":I"};
  88. proc = Runtime.getRuntime().exec(cmd);
  89. br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
  90. String line;
  91. while ((line = br.readLine()) != null)
  92. sb.append(line).append("\r\n");
  93. } catch (IOException ex) {
  94. Log.e(TAG, ex + "\n" + Log.getStackTraceString(ex));
  95. } finally {
  96. if (br != null)
  97. try {
  98. br.close();
  99. } catch (IOException ignored) {
  100. }
  101. if (proc != null)
  102. try {
  103. proc.destroy();
  104. } catch (Throwable ex) {
  105. Log.w(TAG, ex + "\n" + Log.getStackTraceString(ex));
  106. }
  107. }
  108. return sb;
  109. }
  110. static Address myAddress() throws UnsupportedEncodingException {
  111. return new InternetAddress("marcel+fairemail@faircode.eu", "FairCode");
  112. }
  113. }