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.

111 lines
3.9 KiB

  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.XmlResourceParser;
  18. import android.util.Log;
  19. import org.xmlpull.v1.XmlPullParser;
  20. import java.text.Collator;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.Comparator;
  24. import java.util.List;
  25. import java.util.Locale;
  26. public class Provider {
  27. public String name;
  28. public String link;
  29. public String type;
  30. public String imap_host;
  31. public int imap_port;
  32. public String smtp_host;
  33. public int smtp_port;
  34. public boolean starttls;
  35. private Provider() {
  36. }
  37. Provider(String name) {
  38. this.name = name;
  39. }
  40. static List<Provider> loadProfiles(Context context) {
  41. List<Provider> result = null;
  42. try {
  43. XmlResourceParser xml = context.getResources().getXml(R.xml.providers);
  44. int eventType = xml.getEventType();
  45. Provider provider = null;
  46. while (eventType != XmlPullParser.END_DOCUMENT) {
  47. if (eventType == XmlPullParser.START_TAG) {
  48. if ("providers".equals(xml.getName()))
  49. result = new ArrayList<>();
  50. else if ("provider".equals(xml.getName())) {
  51. provider = new Provider();
  52. provider.name = xml.getAttributeValue(null, "name");
  53. provider.link = xml.getAttributeValue(null, "link");
  54. provider.type = xml.getAttributeValue(null, "type");
  55. } else if ("imap".equals(xml.getName())) {
  56. provider.imap_host = xml.getAttributeValue(null, "host");
  57. provider.imap_port = xml.getAttributeIntValue(null, "port", 0);
  58. } else if ("smtp".equals(xml.getName())) {
  59. provider.smtp_host = xml.getAttributeValue(null, "host");
  60. provider.smtp_port = xml.getAttributeIntValue(null, "port", 0);
  61. provider.starttls = xml.getAttributeBooleanValue(null, "starttls", false);
  62. } else
  63. throw new IllegalAccessException(xml.getName());
  64. } else if (eventType == XmlPullParser.END_TAG) {
  65. if ("provider".equals(xml.getName())) {
  66. result.add(provider);
  67. provider = null;
  68. }
  69. }
  70. eventType = xml.next();
  71. }
  72. } catch (Throwable ex) {
  73. Log.e(Helper.TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
  74. }
  75. final Collator collator = Collator.getInstance(Locale.getDefault());
  76. collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
  77. Collections.sort(result, new Comparator<Provider>() {
  78. @Override
  79. public int compare(Provider p1, Provider p2) {
  80. return collator.compare(p1.name, p2.name);
  81. }
  82. });
  83. return result;
  84. }
  85. public int getAuthType() {
  86. if ("com.google".equals(type))
  87. return Helper.AUTH_TYPE_GMAIL;
  88. return Helper.AUTH_TYPE_PASSWORD;
  89. }
  90. @Override
  91. public String toString() {
  92. return name;
  93. }
  94. }