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.

319 lines
12 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.arch.lifecycle.Observer;
  17. import android.content.Context;
  18. import android.os.Bundle;
  19. import android.support.annotation.NonNull;
  20. import android.support.annotation.Nullable;
  21. import android.support.v4.app.Fragment;
  22. import android.support.v4.app.LoaderManager;
  23. import android.support.v4.content.AsyncTaskLoader;
  24. import android.support.v4.content.Loader;
  25. import android.support.v7.app.AppCompatActivity;
  26. import android.text.TextUtils;
  27. import android.util.Log;
  28. import android.view.LayoutInflater;
  29. import android.view.View;
  30. import android.view.ViewGroup;
  31. import android.widget.AdapterView;
  32. import android.widget.ArrayAdapter;
  33. import android.widget.Button;
  34. import android.widget.CheckBox;
  35. import android.widget.EditText;
  36. import android.widget.ProgressBar;
  37. import android.widget.Spinner;
  38. import android.widget.Toast;
  39. import com.sun.mail.imap.IMAPFolder;
  40. import com.sun.mail.imap.IMAPStore;
  41. import java.util.ArrayList;
  42. import java.util.Arrays;
  43. import java.util.List;
  44. import java.util.Objects;
  45. import javax.mail.Folder;
  46. import javax.mail.MessagingException;
  47. import javax.mail.Session;
  48. public class FragmentAccount extends Fragment {
  49. private List<Provider> providers;
  50. private EditText etName;
  51. private Spinner spProfile;
  52. private EditText etHost;
  53. private EditText etPort;
  54. private EditText etUser;
  55. private EditText etPassword;
  56. private CheckBox cbPrimary;
  57. private CheckBox cbSynchronize;
  58. private Button btnOk;
  59. private ProgressBar pbCheck;
  60. static final int DEFAULT_INBOX_SYNC = 30;
  61. static final int DEFAULT_STANDARD_SYNC = 7;
  62. private static final List<String> standard_sync = Arrays.asList(
  63. EntityFolder.TYPE_DRAFTS,
  64. EntityFolder.TYPE_SENT
  65. );
  66. @Override
  67. @Nullable
  68. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  69. View view = inflater.inflate(R.layout.fragment_account, container, false);
  70. // Get arguments
  71. Bundle args = getArguments();
  72. final long id = args.getLong("id", -1);
  73. // Get providers
  74. providers = Provider.loadProfiles(getContext());
  75. providers.add(0, new Provider(getString(R.string.title_custom)));
  76. // Get controls
  77. spProfile = view.findViewById(R.id.spProvider);
  78. etName = view.findViewById(R.id.etName);
  79. etHost = view.findViewById(R.id.etHost);
  80. etPort = view.findViewById(R.id.etPort);
  81. etUser = view.findViewById(R.id.etUser);
  82. etPassword = view.findViewById(R.id.etPassword);
  83. cbPrimary = view.findViewById(R.id.cbPrimary);
  84. cbSynchronize = view.findViewById(R.id.cbSynchronize);
  85. btnOk = view.findViewById(R.id.btnOk);
  86. pbCheck = view.findViewById(R.id.pbCheck);
  87. // Wire controls
  88. spProfile.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  89. @Override
  90. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  91. Provider provider = providers.get(position);
  92. if (provider.imap_port != 0) {
  93. etName.setText(provider.name);
  94. etHost.setText(provider.imap_host);
  95. etPort.setText(Integer.toString(provider.imap_port));
  96. }
  97. }
  98. @Override
  99. public void onNothingSelected(AdapterView<?> adapterView) {
  100. }
  101. });
  102. ArrayAdapter<Provider> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, providers);
  103. adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
  104. spProfile.setAdapter(adapter);
  105. pbCheck.setVisibility(View.GONE);
  106. btnOk.setOnClickListener(new View.OnClickListener() {
  107. @Override
  108. public void onClick(View v) {
  109. btnOk.setEnabled(false);
  110. pbCheck.setVisibility(View.VISIBLE);
  111. Bundle args = new Bundle();
  112. args.putLong("id", id);
  113. args.putString("name", etName.getText().toString());
  114. args.putString("host", etHost.getText().toString());
  115. args.putString("port", etPort.getText().toString());
  116. args.putString("user", etUser.getText().toString());
  117. args.putString("password", etPassword.getText().toString());
  118. args.putBoolean("primary", cbPrimary.isChecked());
  119. args.putBoolean("synchronize", cbSynchronize.isChecked());
  120. getLoaderManager().restartLoader(ActivityView.LOADER_ACCOUNT_PUT, args, putLoaderCallbacks).forceLoad();
  121. }
  122. });
  123. DB.getInstance(getContext()).account().liveAccount(id).observe(this, new Observer<EntityAccount>() {
  124. @Override
  125. public void onChanged(@Nullable EntityAccount account) {
  126. etName.setText(account == null ? null : account.name);
  127. etHost.setText(account == null ? null : account.host);
  128. etPort.setText(account == null ? null : Long.toString(account.port));
  129. etUser.setText(account == null ? null : account.user);
  130. etPassword.setText(account == null ? null : account.password);
  131. cbPrimary.setChecked(account == null ? true : account.primary);
  132. cbSynchronize.setChecked(account == null ? true : account.synchronize);
  133. }
  134. });
  135. return view;
  136. }
  137. @Override
  138. public void onResume() {
  139. super.onResume();
  140. ((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle(R.string.title_edit_account);
  141. }
  142. private static class PutLoader extends AsyncTaskLoader<Throwable> {
  143. private Bundle args;
  144. PutLoader(Context context) {
  145. super(context);
  146. }
  147. void setArgs(Bundle args) {
  148. this.args = args;
  149. }
  150. @Override
  151. public Throwable loadInBackground() {
  152. try {
  153. String name = args.getString("name");
  154. String host = args.getString("host");
  155. String port = args.getString("port");
  156. String user = args.getString("user");
  157. if (TextUtils.isEmpty(name))
  158. name = host + "/" + user;
  159. if (TextUtils.isEmpty(port))
  160. port = "0";
  161. DB db = DB.getInstance(getContext());
  162. EntityAccount account = db.account().getAccount(args.getLong("id"));
  163. boolean update = (account != null);
  164. if (account == null)
  165. account = new EntityAccount();
  166. account.name = name;
  167. account.host = host;
  168. account.port = Integer.parseInt(port);
  169. account.user = user;
  170. account.password = Objects.requireNonNull(args.getString("password"));
  171. account.primary = args.getBoolean("primary");
  172. account.synchronize = args.getBoolean("synchronize");
  173. // Check IMAP server
  174. List<EntityFolder> folders = new ArrayList<>();
  175. if (account.synchronize) {
  176. Session isession = Session.getDefaultInstance(MessageHelper.getSessionProperties(), null);
  177. IMAPStore istore = null;
  178. try {
  179. istore = (IMAPStore) isession.getStore("imaps");
  180. istore.connect(account.host, account.port, account.user, account.password);
  181. if (!istore.hasCapability("IDLE"))
  182. throw new MessagingException(getContext().getString(R.string.title_no_idle));
  183. boolean drafts = false;
  184. for (Folder ifolder : istore.getDefaultFolder().list("*")) {
  185. String[] attrs = ((IMAPFolder) ifolder).getAttributes();
  186. for (String attr : attrs) {
  187. if (attr.startsWith("\\")) {
  188. int index = EntityFolder.STANDARD_FOLDER_ATTR.indexOf(attr.substring(1));
  189. if (index >= 0) {
  190. EntityFolder folder = new EntityFolder();
  191. folder.name = ifolder.getFullName();
  192. folder.type = EntityFolder.STANDARD_FOLDER_TYPE.get(index);
  193. folder.synchronize = standard_sync.contains(folder.type);
  194. folder.after = DEFAULT_STANDARD_SYNC;
  195. folders.add(folder);
  196. Log.i(Helper.TAG, "Standard folder=" + folder.name +
  197. " type=" + folder.type + " attr=" + TextUtils.join(",", attrs));
  198. if (EntityFolder.TYPE_DRAFTS.equals(folder.type))
  199. drafts = true;
  200. break;
  201. }
  202. }
  203. }
  204. }
  205. if (!drafts)
  206. throw new MessagingException(getContext().getString(R.string.title_no_drafts));
  207. } finally {
  208. if (istore != null)
  209. istore.close();
  210. }
  211. }
  212. if (account.primary)
  213. db.account().resetPrimary();
  214. if (update)
  215. db.account().updateAccount(account);
  216. else
  217. try {
  218. db.beginTransaction();
  219. account.id = db.account().insertAccount(account);
  220. EntityFolder inbox = new EntityFolder();
  221. inbox.name = "INBOX";
  222. inbox.type = EntityFolder.TYPE_INBOX;
  223. inbox.synchronize = true;
  224. inbox.after = DEFAULT_INBOX_SYNC;
  225. folders.add(0, inbox);
  226. for (EntityFolder folder : folders) {
  227. folder.account = account.id;
  228. Log.i(Helper.TAG, "Creating folder=" + folder.name + " (" + folder.type + ")");
  229. folder.id = db.folder().insertFolder(folder);
  230. }
  231. db.setTransactionSuccessful();
  232. } finally {
  233. db.endTransaction();
  234. }
  235. ServiceSynchronize.restart(getContext(), "account");
  236. return null;
  237. } catch (Throwable ex) {
  238. Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
  239. return ex;
  240. }
  241. }
  242. }
  243. private LoaderManager.LoaderCallbacks putLoaderCallbacks = new LoaderManager.LoaderCallbacks<Throwable>() {
  244. @NonNull
  245. @Override
  246. public Loader<Throwable> onCreateLoader(int id, Bundle args) {
  247. PutLoader loader = new PutLoader(getActivity());
  248. loader.setArgs(args);
  249. return loader;
  250. }
  251. @Override
  252. public void onLoadFinished(@NonNull Loader<Throwable> loader, Throwable ex) {
  253. getLoaderManager().destroyLoader(loader.getId());
  254. btnOk.setEnabled(true);
  255. pbCheck.setVisibility(View.GONE);
  256. if (ex == null)
  257. getFragmentManager().popBackStack();
  258. else {
  259. Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
  260. Toast.makeText(getContext(), Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
  261. }
  262. }
  263. @Override
  264. public void onLoaderReset(@NonNull Loader<Throwable> loader) {
  265. }
  266. };
  267. }