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.

166 lines
5.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.text.TextUtils;
  19. import android.util.Log;
  20. import android.view.Menu;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.widget.CheckBox;
  24. import android.widget.EditText;
  25. import android.widget.ImageView;
  26. import android.widget.Spinner;
  27. import com.google.android.material.bottomnavigation.BottomNavigationView;
  28. import java.io.BufferedReader;
  29. import java.io.File;
  30. import java.io.FileInputStream;
  31. import java.io.FileOutputStream;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.io.InputStreamReader;
  35. import java.io.OutputStream;
  36. import java.io.UnsupportedEncodingException;
  37. import javax.mail.Address;
  38. import javax.mail.internet.InternetAddress;
  39. public class Helper {
  40. static final String TAG = "fairemail";
  41. static int resolveColor(Context context, int attr) {
  42. int[] attrs = new int[]{attr};
  43. TypedArray a = context.getTheme().obtainStyledAttributes(attrs);
  44. int color = a.getColor(0, 0xFF0000);
  45. a.recycle();
  46. return color;
  47. }
  48. static void setViewsEnabled(ViewGroup view, boolean enabled) {
  49. for (int i = 0; i < view.getChildCount(); i++) {
  50. View child = view.getChildAt(i);
  51. if (child instanceof Spinner ||
  52. child instanceof EditText ||
  53. child instanceof CheckBox ||
  54. child instanceof ImageView /* =ImageButton */)
  55. child.setEnabled(enabled);
  56. if (child instanceof BottomNavigationView) {
  57. Menu menu = ((BottomNavigationView) child).getMenu();
  58. menu.setGroupEnabled(0, enabled);
  59. } else if (child instanceof ViewGroup)
  60. setViewsEnabled((ViewGroup) child, enabled);
  61. }
  62. }
  63. static String localizeFolderName(Context context, String name) {
  64. if ("INBOX".equals(name))
  65. return context.getString(R.string.title_folder_inbox);
  66. else if ("OUTBOX".equals(name))
  67. return context.getString(R.string.title_folder_outbox);
  68. else
  69. return name;
  70. }
  71. static String formatThrowable(Throwable ex) {
  72. StringBuilder sb = new StringBuilder();
  73. sb.append(ex.getMessage());
  74. Throwable cause = ex.getCause();
  75. while (cause != null) {
  76. sb.append(" ").append(cause.getMessage());
  77. cause = cause.getCause();
  78. }
  79. return sb.toString();
  80. }
  81. static String humanReadableByteCount(long bytes, boolean si) {
  82. int unit = si ? 1000 : 1024;
  83. if (bytes < unit) return bytes + " B";
  84. int exp = (int) (Math.log(bytes) / Math.log(unit));
  85. String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
  86. return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
  87. }
  88. static StringBuilder getLogcat() {
  89. StringBuilder sb = new StringBuilder();
  90. Process proc = null;
  91. BufferedReader br = null;
  92. try {
  93. String[] cmd = new String[]{"logcat",
  94. "-d",
  95. "-v", "threadtime",
  96. "-t", "500",
  97. TAG + ":I"};
  98. proc = Runtime.getRuntime().exec(cmd);
  99. br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
  100. String line;
  101. while ((line = br.readLine()) != null)
  102. sb.append(line).append("\r\n");
  103. } catch (IOException ex) {
  104. Log.e(TAG, ex + "\n" + Log.getStackTraceString(ex));
  105. } finally {
  106. if (br != null)
  107. try {
  108. br.close();
  109. } catch (IOException ignored) {
  110. }
  111. if (proc != null)
  112. try {
  113. proc.destroy();
  114. } catch (Throwable ex) {
  115. Log.w(TAG, ex + "\n" + Log.getStackTraceString(ex));
  116. }
  117. }
  118. return sb;
  119. }
  120. static Address myAddress() throws UnsupportedEncodingException {
  121. return new InternetAddress("marcel+fairemail@faircode.eu", "FairCode");
  122. }
  123. static String canonicalAddress(String address) {
  124. String[] a = address.split("\\@");
  125. if (a.length > 0)
  126. a[0] = a[0].split("\\+")[0];
  127. return TextUtils.join("@", a);
  128. }
  129. static void copy(File src, File dst) throws IOException {
  130. InputStream in = new FileInputStream(src);
  131. try {
  132. OutputStream out = new FileOutputStream(dst);
  133. try {
  134. byte[] buf = new byte[4096];
  135. int len;
  136. while ((len = in.read(buf)) > 0)
  137. out.write(buf, 0, len);
  138. } finally {
  139. out.close();
  140. }
  141. } finally {
  142. in.close();
  143. }
  144. }
  145. }