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.

159 lines
5.8 KiB

  1. package eu.faircode.email;
  2. /*
  3. This file is part of Safe email.
  4. Safe email 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.graphics.ColorMatrix;
  19. import android.graphics.ColorMatrixColorFilter;
  20. import android.graphics.drawable.Drawable;
  21. import android.os.Build;
  22. import android.text.TextUtils;
  23. import android.util.Log;
  24. import android.view.View;
  25. import android.view.ViewGroup;
  26. import android.widget.CheckBox;
  27. import android.widget.EditText;
  28. import android.widget.ImageButton;
  29. import android.widget.Spinner;
  30. import java.io.BufferedReader;
  31. import java.io.IOException;
  32. import java.io.InputStreamReader;
  33. import androidx.annotation.NonNull;
  34. public class Helper {
  35. static final String TAG = BuildConfig.APPLICATION_ID;
  36. static int resolveColor(Context context, int attr) {
  37. int[] attrs = new int[]{attr};
  38. TypedArray a = context.getTheme().obtainStyledAttributes(attrs);
  39. int color = a.getColor(0, 0xFF0000);
  40. a.recycle();
  41. return color;
  42. }
  43. static Drawable toDimmed(@NonNull Drawable drawable) {
  44. ColorMatrix matrix = new ColorMatrix();
  45. matrix.setSaturation(0);
  46. ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
  47. Drawable mutated = drawable.mutate();
  48. mutated.setColorFilter(filter);
  49. mutated.setAlpha(128);
  50. return mutated;
  51. }
  52. static void setViewsEnabled(ViewGroup view, boolean enabled) {
  53. for (int i = 0; i < view.getChildCount(); i++) {
  54. View child = view.getChildAt(i);
  55. if (child instanceof Spinner ||
  56. child instanceof EditText ||
  57. child instanceof CheckBox ||
  58. child instanceof ImageButton)
  59. child.setEnabled(enabled);
  60. else if (child instanceof ViewGroup)
  61. setViewsEnabled((ViewGroup) child, enabled);
  62. }
  63. }
  64. static String localizeFolderName(Context context, String name) {
  65. if ("INBOX".equals(name))
  66. return context.getString(R.string.title_folder_inbox);
  67. else if ("OUTBOX".equals(name))
  68. return context.getString(R.string.title_folder_outbox);
  69. else
  70. return name;
  71. }
  72. static String localizeFolderName(Context context, TupleFolderEx folder) {
  73. if (TextUtils.isEmpty(folder.accountName))
  74. return localizeFolderName(context, folder.name);
  75. else
  76. return localizeFolderName(context, folder.name) + "/" + folder.accountName;
  77. }
  78. static String formatThrowable(Throwable ex) {
  79. StringBuilder sb = new StringBuilder();
  80. sb.append(ex.getMessage());
  81. Throwable cause = ex.getCause();
  82. while (cause != null) {
  83. sb.append(" ").append(cause.getMessage());
  84. cause = cause.getCause();
  85. }
  86. return sb.toString();
  87. }
  88. static String humanReadableByteCount(long bytes, boolean si) {
  89. int unit = si ? 1000 : 1024;
  90. if (bytes < unit) return bytes + " B";
  91. int exp = (int) (Math.log(bytes) / Math.log(unit));
  92. String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
  93. return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
  94. }
  95. static StringBuilder getDebugInfo() {
  96. StringBuilder sb = new StringBuilder();
  97. // Get version info
  98. sb.append(String.format("%s: %s/%d\r\n", BuildConfig.APPLICATION_ID, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE));
  99. sb.append(String.format("Android: %s (SDK %d)\r\n", Build.VERSION.RELEASE, Build.VERSION.SDK_INT));
  100. sb.append("\r\n");
  101. // Get device info
  102. sb.append(String.format("Brand: %s\r\n", Build.BRAND));
  103. sb.append(String.format("Manufacturer: %s\r\n", Build.MANUFACTURER));
  104. sb.append(String.format("Model: %s\r\n", Build.MODEL));
  105. sb.append(String.format("Product: %s\r\n", Build.PRODUCT));
  106. sb.append(String.format("Device: %s\r\n", Build.DEVICE));
  107. sb.append(String.format("Host: %s\r\n", Build.HOST));
  108. sb.append(String.format("Display: %s\r\n", Build.DISPLAY));
  109. sb.append(String.format("Id: %s\r\n", Build.ID));
  110. sb.append("\r\n");
  111. // Get logcat
  112. Process proc = null;
  113. BufferedReader br = null;
  114. try {
  115. String[] cmd = new String[]{"logcat", "-d", "-v", "threadtime"};
  116. proc = Runtime.getRuntime().exec(cmd);
  117. br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
  118. String line;
  119. while ((line = br.readLine()) != null)
  120. sb.append(line).append("\r\n");
  121. } catch (IOException ex) {
  122. Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
  123. } finally {
  124. if (br != null)
  125. try {
  126. br.close();
  127. } catch (IOException ignored) {
  128. }
  129. if (proc != null)
  130. try {
  131. proc.destroy();
  132. } catch (Throwable ex) {
  133. Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
  134. }
  135. }
  136. return sb;
  137. }
  138. }