Files
FairEmail/app/src/main/java/eu/faircode/email/FragmentAccount.java

805 lines
38 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
2018-08-27 14:31:45 +00:00
import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
2018-08-02 13:33:06 +00:00
import android.content.Context;
2018-08-07 19:35:21 +00:00
import android.content.DialogInterface;
2018-08-27 14:31:45 +00:00
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
import android.os.Handler;
2018-08-24 16:41:22 +00:00
import android.text.Html;
2018-08-02 13:33:06 +00:00
import android.text.TextUtils;
2018-08-24 16:41:22 +00:00
import android.text.method.LinkMovementMethod;
2018-08-02 13:33:06 +00:00
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
2018-08-06 10:02:47 +00:00
import android.widget.CompoundButton;
2018-08-02 13:33:06 +00:00
import android.widget.EditText;
2018-08-07 19:35:21 +00:00
import android.widget.ImageButton;
2018-08-02 13:33:06 +00:00
import android.widget.ProgressBar;
import android.widget.ScrollView;
2018-08-02 13:33:06 +00:00
import android.widget.Spinner;
2018-08-23 09:48:27 +00:00
import android.widget.TextView;
2018-08-02 13:33:06 +00:00
import android.widget.Toast;
2018-08-08 06:55:47 +00:00
import com.google.android.material.textfield.TextInputLayout;
2018-08-02 13:33:06 +00:00
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
2018-08-10 09:45:36 +00:00
import java.text.Collator;
2018-08-02 13:33:06 +00:00
import java.util.ArrayList;
2018-08-10 09:45:36 +00:00
import java.util.Collections;
import java.util.Comparator;
2018-08-02 13:33:06 +00:00
import java.util.List;
2018-08-10 09:45:36 +00:00
import java.util.Locale;
2018-08-27 14:31:45 +00:00
import java.util.Properties;
2018-08-02 13:33:06 +00:00
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Session;
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
2018-08-10 09:45:36 +00:00
import androidx.constraintlayout.widget.Group;
2018-08-27 14:31:45 +00:00
import androidx.core.content.ContextCompat;
2018-08-08 06:55:47 +00:00
import androidx.lifecycle.Observer;
2018-08-27 14:31:45 +00:00
import static android.accounts.AccountManager.newChooseAccountIntent;
public class FragmentAccount extends FragmentEx {
private ViewGroup view;
2018-08-02 13:33:06 +00:00
private EditText etName;
2018-08-15 09:50:39 +00:00
private Spinner spProvider;
2018-08-02 13:33:06 +00:00
private EditText etHost;
private EditText etPort;
2018-08-27 14:31:45 +00:00
private Button btnAuthorize;
2018-08-02 13:33:06 +00:00
private EditText etUser;
private TextInputLayout tilPassword;
2018-08-24 16:41:22 +00:00
private TextView tvLink;
2018-08-02 13:33:06 +00:00
private CheckBox cbSynchronize;
2018-08-06 10:02:47 +00:00
private CheckBox cbPrimary;
2018-08-23 09:48:27 +00:00
private EditText etInterval;
2018-08-10 09:45:36 +00:00
private Button btnCheck;
2018-08-02 13:33:06 +00:00
private ProgressBar pbCheck;
2018-08-23 09:48:27 +00:00
private TextView tvIdle;
2018-08-10 09:45:36 +00:00
private Spinner spDrafts;
private Spinner spSent;
private Spinner spAll;
private Spinner spTrash;
private Spinner spJunk;
private Button btnSave;
private ProgressBar pbSave;
2018-08-07 19:35:21 +00:00
private ImageButton ibDelete;
private ProgressBar pbWait;
2018-08-24 16:41:22 +00:00
private Group grpInstructions;
2018-08-10 09:45:36 +00:00
private Group grpFolders;
2018-08-02 13:33:06 +00:00
2018-08-27 14:31:45 +00:00
private long id = -1;
private boolean authorized = true;
2018-08-27 14:31:45 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get arguments
Bundle args = getArguments();
id = (args == null ? -1 : args.getLong("id", -1));
}
2018-08-02 13:33:06 +00:00
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_edit_account);
view = (ViewGroup) inflater.inflate(R.layout.fragment_account, container, false);
2018-08-02 13:33:06 +00:00
// Get controls
2018-08-15 09:50:39 +00:00
spProvider = view.findViewById(R.id.spProvider);
etName = view.findViewById(R.id.etName);
2018-08-02 13:33:06 +00:00
etHost = view.findViewById(R.id.etHost);
2018-08-27 14:31:45 +00:00
btnAuthorize = view.findViewById(R.id.btnAuthorize);
2018-08-02 13:33:06 +00:00
etPort = view.findViewById(R.id.etPort);
etUser = view.findViewById(R.id.etUser);
tilPassword = view.findViewById(R.id.tilPassword);
2018-08-24 16:41:22 +00:00
tvLink = view.findViewById(R.id.tvLink);
2018-08-02 13:33:06 +00:00
cbSynchronize = view.findViewById(R.id.cbSynchronize);
2018-08-06 10:02:47 +00:00
cbPrimary = view.findViewById(R.id.cbPrimary);
2018-08-23 09:48:27 +00:00
etInterval = view.findViewById(R.id.etInterval);
2018-08-10 09:45:36 +00:00
btnCheck = view.findViewById(R.id.btnCheck);
2018-08-02 13:33:06 +00:00
pbCheck = view.findViewById(R.id.pbCheck);
2018-08-23 09:48:27 +00:00
tvIdle = view.findViewById(R.id.tvIdle);
2018-08-10 09:45:36 +00:00
spDrafts = view.findViewById(R.id.spDrafts);
spSent = view.findViewById(R.id.spSent);
spAll = view.findViewById(R.id.spAll);
spTrash = view.findViewById(R.id.spTrash);
spJunk = view.findViewById(R.id.spJunk);
btnSave = view.findViewById(R.id.btnSave);
pbSave = view.findViewById(R.id.pbSave);
2018-08-07 19:35:21 +00:00
ibDelete = view.findViewById(R.id.ibDelete);
pbWait = view.findViewById(R.id.pbWait);
2018-08-24 16:41:22 +00:00
grpInstructions = view.findViewById(R.id.grpInstructions);
2018-08-10 09:45:36 +00:00
grpFolders = view.findViewById(R.id.grpFolders);
2018-08-02 13:33:06 +00:00
// Wire controls
2018-08-15 09:50:39 +00:00
spProvider.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2018-08-02 13:33:06 +00:00
@Override
2018-08-15 09:50:39 +00:00
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
Integer tag = (Integer) adapterView.getTag();
if (tag != null && tag.equals(position))
return;
adapterView.setTag(position);
Provider provider = (Provider) adapterView.getSelectedItem();
2018-08-24 16:41:22 +00:00
etName.setText(provider.name);
btnAuthorize.setVisibility(provider.type == null ? View.GONE : View.VISIBLE);
if (authorized) {
authorized = false;
etUser.setText(null);
tilPassword.getEditText().setText(null);
2018-08-02 13:33:06 +00:00
}
2018-08-27 14:31:45 +00:00
etHost.setText(provider.imap_host);
etPort.setText(position == 0 ? null : Integer.toString(provider.imap_port));
2018-08-27 15:08:23 +00:00
tvLink.setText(Html.fromHtml("<a href=\"" + provider.link + "\">" + provider.link + "</a>"));
grpInstructions.setVisibility(provider.link == null ? View.GONE : View.VISIBLE);
2018-08-02 13:33:06 +00:00
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
2018-08-27 14:31:45 +00:00
btnAuthorize.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String permission = Manifest.permission.GET_ACCOUNTS;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O &&
ContextCompat.checkSelfPermission(getContext(), permission) != PackageManager.PERMISSION_GRANTED) {
Log.i(Helper.TAG, "Requesting " + permission);
requestPermissions(new String[]{permission}, ActivitySetup.REQUEST_PERMISSION);
} else
selectAccount();
}
});
2018-08-06 10:02:47 +00:00
cbSynchronize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
cbPrimary.setEnabled(checked);
btnCheck.setVisibility(checked ? View.VISIBLE : View.GONE);
btnSave.setVisibility(checked ? View.GONE : View.VISIBLE);
2018-08-06 10:02:47 +00:00
}
});
2018-08-10 09:45:36 +00:00
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.setViewsEnabled(view, false);
2018-08-10 09:45:36 +00:00
btnCheck.setEnabled(false);
pbCheck.setVisibility(View.VISIBLE);
btnSave.setVisibility(View.GONE);
grpFolders.setVisibility(View.GONE);
2018-08-10 09:45:36 +00:00
Provider provider = (Provider) spProvider.getSelectedItem();
2018-08-10 09:45:36 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
args.putString("name", etName.getText().toString());
args.putString("host", etHost.getText().toString());
args.putString("port", etPort.getText().toString());
args.putString("user", etUser.getText().toString());
args.putString("password", tilPassword.getEditText().getText().toString());
args.putInt("auth_type", authorized ? provider.getAuthType() : Helper.AUTH_TYPE_PASSWORD);
2018-08-10 09:45:36 +00:00
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("primary", cbPrimary.isChecked());
new SimpleTask<List<EntityFolder>>() {
@Override
protected List<EntityFolder> onLoad(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
String host = args.getString("host");
String port = args.getString("port");
String user = args.getString("user");
String password = args.getString("password");
2018-08-27 14:31:45 +00:00
int auth_type = args.getInt("auth_type");
if (TextUtils.isEmpty(host))
throw new Throwable(getContext().getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
throw new Throwable(getContext().getString(R.string.title_no_port));
if (TextUtils.isEmpty(user))
throw new Throwable(getContext().getString(R.string.title_no_user));
if (TextUtils.isEmpty(password))
throw new Throwable(getContext().getString(R.string.title_no_password));
2018-08-27 16:19:56 +00:00
// Refresh token
if (id >= 0 && auth_type == Helper.AUTH_TYPE_GMAIL) {
password = Helper.refreshToken(getContext(), "com.google", user, password);
args.putString("password", password);
}
// Check IMAP server / get folders
List<EntityFolder> folders = new ArrayList<>();
2018-08-27 14:31:45 +00:00
Properties props = MessageHelper.getSessionProperties(auth_type);
Session isession = Session.getInstance(props, null);
isession.setDebug(true);
IMAPStore istore = null;
try {
istore = (IMAPStore) isession.getStore("imaps");
istore.connect(host, Integer.parseInt(port), user, password);
if (!istore.hasCapability("UIDPLUS"))
throw new MessagingException(getContext().getString(R.string.title_no_uidplus));
2018-08-23 09:48:27 +00:00
args.putBoolean("idle", istore.hasCapability("IDLE"));
for (Folder ifolder : istore.getDefaultFolder().list("*")) {
String type = null;
// First check folder attributes
boolean selectable = true;
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
for (String attr : attrs) {
if ("\\Noselect".equals(attr))
selectable = false;
if (attr.startsWith("\\")) {
int index = EntityFolder.SYSTEM_FOLDER_ATTR.indexOf(attr.substring(1));
if (index >= 0) {
type = EntityFolder.SYSTEM_FOLDER_TYPE.get(index);
break;
}
}
}
if (selectable) {
// Next check folder full name
if (type == null) {
String fullname = ifolder.getFullName();
for (String attr : EntityFolder.SYSTEM_FOLDER_ATTR)
if (attr.equals(fullname)) {
int index = EntityFolder.SYSTEM_FOLDER_ATTR.indexOf(attr);
type = EntityFolder.SYSTEM_FOLDER_TYPE.get(index);
break;
}
}
// Create entry
DB db = DB.getInstance(getContext());
EntityFolder folder = db.folder().getFolderByName(id, ifolder.getFullName());
if (folder == null) {
folder = new EntityFolder();
folder.name = ifolder.getFullName();
folder.type = (type == null ? EntityFolder.USER : type);
folder.synchronize = (type != null && EntityFolder.SYSTEM_FOLDER_SYNC.contains(type));
folder.after = (type == null ? EntityFolder.DEFAULT_USER_SYNC : EntityFolder.DEFAULT_SYSTEM_SYNC);
}
folders.add(folder);
Log.i(Helper.TAG, folder.name + " id=" + folder.id +
" type=" + folder.type + " attr=" + TextUtils.join(",", attrs));
}
}
} finally {
if (istore != null)
istore.close();
}
return folders;
}
@Override
protected void onLoaded(Bundle args, List<EntityFolder> folders) {
Helper.setViewsEnabled(view, true);
btnCheck.setEnabled(true);
pbCheck.setVisibility(View.GONE);
2018-08-27 16:19:56 +00:00
// Refreshed token
tilPassword.getEditText().setText(args.getString("password"));
2018-08-23 09:48:27 +00:00
tvIdle.setVisibility(args.getBoolean("idle") ? View.GONE : View.VISIBLE);
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(folders, new Comparator<EntityFolder>() {
@Override
public int compare(EntityFolder f1, EntityFolder f2) {
int s = Integer.compare(
EntityFolder.FOLDER_SORT_ORDER.indexOf(f1.type),
EntityFolder.FOLDER_SORT_ORDER.indexOf(f2.type));
if (s != 0)
return s;
int c = -f1.synchronize.compareTo(f2.synchronize);
if (c != 0)
return c;
return collator.compare(
f1.name == null ? "" : f1.name,
f2.name == null ? "" : f2.name);
}
});
EntityFolder none = new EntityFolder();
none.name = "";
folders.add(0, none);
ArrayAdapter<EntityFolder> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, folders);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spDrafts.setAdapter(adapter);
spSent.setAdapter(adapter);
spAll.setAdapter(adapter);
spTrash.setAdapter(adapter);
spJunk.setAdapter(adapter);
for (int pos = 0; pos < folders.size(); pos++) {
if (EntityFolder.DRAFTS.equals(folders.get(pos).type))
spDrafts.setSelection(pos);
else if (EntityFolder.SENT.equals(folders.get(pos).type))
spSent.setSelection(pos);
else if (EntityFolder.ARCHIVE.equals(folders.get(pos).type))
spAll.setSelection(pos);
else if (EntityFolder.TRASH.equals(folders.get(pos).type))
spTrash.setSelection(pos);
else if (EntityFolder.JUNK.equals(folders.get(pos).type))
spJunk.setSelection(pos);
}
grpFolders.setVisibility(View.VISIBLE);
btnSave.setVisibility(View.VISIBLE);
new Handler().post(new Runnable() {
@Override
public void run() {
((ScrollView) view).smoothScrollTo(0, btnSave.getBottom());
}
});
}
@Override
protected void onException(Bundle args, Throwable ex) {
2018-08-12 15:31:43 +00:00
Helper.setViewsEnabled(view, true);
btnCheck.setEnabled(true);
pbCheck.setVisibility(View.GONE);
grpFolders.setVisibility(View.GONE);
btnSave.setVisibility(View.GONE);
Toast.makeText(getContext(), Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
}.load(FragmentAccount.this, args);
2018-08-10 09:45:36 +00:00
}
});
2018-08-06 10:27:21 +00:00
btnSave.setOnClickListener(new View.OnClickListener() {
2018-08-02 13:33:06 +00:00
@Override
public void onClick(View v) {
Helper.setViewsEnabled(view, false);
btnCheck.setEnabled(false);
2018-08-06 10:27:21 +00:00
btnSave.setEnabled(false);
2018-08-10 09:45:36 +00:00
pbSave.setVisibility(View.VISIBLE);
Provider provider = (Provider) spProvider.getSelectedItem();
2018-08-10 09:45:36 +00:00
EntityFolder drafts = (EntityFolder) spDrafts.getSelectedItem();
EntityFolder sent = (EntityFolder) spSent.getSelectedItem();
EntityFolder all = (EntityFolder) spAll.getSelectedItem();
EntityFolder trash = (EntityFolder) spTrash.getSelectedItem();
EntityFolder junk = (EntityFolder) spJunk.getSelectedItem();
if (drafts != null && drafts.type == null)
2018-08-10 09:45:36 +00:00
drafts = null;
if (sent != null && sent.type == null)
2018-08-10 09:45:36 +00:00
sent = null;
if (all != null && all.type == null)
2018-08-10 09:45:36 +00:00
all = null;
if (trash != null && trash.type == null)
2018-08-10 09:45:36 +00:00
trash = null;
if (junk != null && junk.type == null)
2018-08-10 09:45:36 +00:00
junk = null;
2018-08-02 13:33:06 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
args.putString("name", etName.getText().toString());
args.putString("host", etHost.getText().toString());
args.putString("port", etPort.getText().toString());
args.putString("user", etUser.getText().toString());
args.putString("password", tilPassword.getEditText().getText().toString());
args.putInt("auth_type", authorized ? provider.getAuthType() : Helper.AUTH_TYPE_PASSWORD);
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("primary", cbPrimary.isChecked());
2018-08-23 09:48:27 +00:00
args.putString("poll_interval", etInterval.getText().toString());
2018-08-15 09:50:39 +00:00
args.putParcelable("drafts", drafts);
args.putParcelable("sent", sent);
args.putParcelable("all", all);
args.putParcelable("trash", trash);
args.putParcelable("junk", junk);
2018-08-02 13:33:06 +00:00
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
2018-08-12 15:31:43 +00:00
String name = args.getString("name");
String host = args.getString("host");
String port = args.getString("port");
String user = args.getString("user");
String password = args.getString("password");
2018-08-27 14:31:45 +00:00
int auth_type = args.getInt("auth_type");
2018-08-12 15:31:43 +00:00
boolean synchronize = args.getBoolean("synchronize");
boolean primary = args.getBoolean("primary");
2018-08-23 09:48:27 +00:00
String poll_interval = args.getString("poll_interval");
2018-08-15 09:50:39 +00:00
EntityFolder drafts = args.getParcelable("drafts");
EntityFolder sent = args.getParcelable("sent");
EntityFolder all = args.getParcelable("all");
EntityFolder trash = args.getParcelable("trash");
EntityFolder junk = args.getParcelable("junk");
2018-08-12 15:31:43 +00:00
if (TextUtils.isEmpty(host))
throw new Throwable(getContext().getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
throw new Throwable(getContext().getString(R.string.title_no_port));
if (TextUtils.isEmpty(user))
throw new Throwable(getContext().getString(R.string.title_no_user));
if (TextUtils.isEmpty(password))
throw new Throwable(getContext().getString(R.string.title_no_password));
if (synchronize && drafts == null)
2018-08-12 15:31:43 +00:00
throw new Throwable(getContext().getString(R.string.title_no_drafts));
2018-08-23 09:48:27 +00:00
if (TextUtils.isEmpty(poll_interval))
poll_interval = "9";
2018-08-12 15:31:43 +00:00
// Check IMAP server
if (synchronize) {
2018-08-27 14:31:45 +00:00
Session isession = Session.getInstance(MessageHelper.getSessionProperties(auth_type), null);
2018-08-27 15:08:23 +00:00
isession.setDebug(true);
IMAPStore istore = null;
try {
istore = (IMAPStore) isession.getStore("imaps");
istore.connect(host, Integer.parseInt(port), user, password);
2018-08-12 15:31:43 +00:00
2018-08-23 09:48:27 +00:00
if (!istore.hasCapability("UIDPLUS"))
throw new MessagingException(getContext().getString(R.string.title_no_uidplus));
} finally {
if (istore != null)
istore.close();
}
2018-08-12 15:31:43 +00:00
}
2018-08-12 15:31:43 +00:00
if (TextUtils.isEmpty(name))
name = host + "/" + user;
2018-08-22 12:30:27 +00:00
DB db = DB.getInstance(getContext());
2018-08-12 15:31:43 +00:00
try {
2018-08-22 12:30:27 +00:00
db.beginTransaction();
EntityAccount account = db.account().getAccount(args.getLong("id"));
boolean update = (account != null);
if (account == null)
account = new EntityAccount();
account.name = name;
account.host = host;
account.port = Integer.parseInt(port);
account.user = user;
account.password = password;
2018-08-27 14:31:45 +00:00
account.auth_type = auth_type;
2018-08-22 12:30:27 +00:00
account.synchronize = synchronize;
account.primary = (account.synchronize && primary);
2018-08-25 18:26:07 +00:00
account.store_sent = false;
2018-08-23 09:48:27 +00:00
account.poll_interval = Integer.parseInt(poll_interval);
2018-08-22 12:30:27 +00:00
if (!synchronize)
account.error = null;
if (account.primary)
db.account().resetPrimary();
if (update)
db.account().updateAccount(account);
else
account.id = db.account().insertAccount(account);
List<EntityFolder> folders = new ArrayList<>();
EntityFolder inbox = new EntityFolder();
inbox.name = "INBOX";
inbox.type = EntityFolder.INBOX;
inbox.synchronize = true;
inbox.after = EntityFolder.DEFAULT_INBOX_SYNC;
folders.add(inbox);
if (drafts != null) {
drafts.type = EntityFolder.DRAFTS;
folders.add(drafts);
}
2018-08-22 12:30:27 +00:00
if (sent != null) {
sent.type = EntityFolder.SENT;
folders.add(sent);
}
if (all != null) {
all.type = EntityFolder.ARCHIVE;
folders.add(all);
}
if (trash != null) {
trash.type = EntityFolder.TRASH;
folders.add(trash);
}
if (junk != null) {
junk.type = EntityFolder.JUNK;
folders.add(junk);
}
2018-08-22 12:30:27 +00:00
for (EntityFolder folder : folders) {
db.folder().setFolderUser(account.id, folder.type);
EntityFolder existing = db.folder().getFolderByName(account.id, folder.name);
if (existing == null) {
folder.account = account.id;
Log.i(Helper.TAG, "Creating folder=" + folder.name + " (" + folder.type + ")");
folder.id = db.folder().insertFolder(folder);
} else
db.folder().setFolderType(existing.id, folder.type);
}
2018-08-22 12:30:27 +00:00
db.setTransactionSuccessful();
} finally {
2018-08-22 12:30:27 +00:00
db.endTransaction();
}
2018-08-22 12:30:27 +00:00
ServiceSynchronize.reload(getContext(), "save account");
return null;
}
@Override
protected void onLoaded(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.setViewsEnabled(view, true);
btnCheck.setEnabled(true);
btnSave.setEnabled(true);
pbSave.setVisibility(View.GONE);
Toast.makeText(getContext(), Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
}.load(FragmentAccount.this, args);
2018-08-02 13:33:06 +00:00
}
});
2018-08-07 19:35:21 +00:00
ibDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2018-08-07 19:35:21 +00:00
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder
.setMessage(R.string.title_account_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Helper.setViewsEnabled(view, false);
btnCheck.setEnabled(false);
btnSave.setEnabled(false);
pbWait.setVisibility(View.VISIBLE);
2018-08-11 18:16:35 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
2018-08-11 18:16:35 +00:00
@Override
protected Void onLoad(Context context, Bundle args) {
2018-08-22 12:30:27 +00:00
long id = args.getLong("id");
DB.getInstance(context).account().deleteAccount(id);
ServiceSynchronize.reload(getContext(), "delete account");
return null;
2018-08-11 18:16:35 +00:00
}
@Override
protected void onLoaded(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
2018-08-07 19:35:21 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
2018-08-11 18:16:35 +00:00
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
2018-08-07 19:35:21 +00:00
}
}.load(FragmentAccount.this, args);
2018-08-07 19:35:21 +00:00
}
})
.setNegativeButton(android.R.string.cancel, null).show();
}
});
// Initialize
Helper.setViewsEnabled(view, false);
2018-08-27 14:31:45 +00:00
btnAuthorize.setVisibility(View.GONE);
tilPassword.setPasswordVisibilityToggleEnabled(id < 0);
2018-08-24 16:41:22 +00:00
tvLink.setMovementMethod(LinkMovementMethod.getInstance());
btnCheck.setEnabled(false);
pbCheck.setVisibility(View.GONE);
2018-08-10 09:45:36 +00:00
btnSave.setVisibility(View.GONE);
pbSave.setVisibility(View.GONE);
2018-08-23 09:48:27 +00:00
tvIdle.setVisibility(View.GONE);
2018-08-10 09:45:36 +00:00
grpFolders.setVisibility(View.GONE);
ibDelete.setVisibility(View.GONE);
return view;
}
@Override
2018-08-15 09:50:39 +00:00
public void onSaveInstanceState(Bundle outState) {
2018-08-21 18:00:06 +00:00
super.onSaveInstanceState(outState);
2018-08-15 09:50:39 +00:00
outState.putInt("provider", spProvider.getSelectedItemPosition());
outState.putBoolean("authorized", authorized);
2018-08-15 09:50:39 +00:00
outState.putString("password", tilPassword.getEditText().getText().toString());
}
@Override
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Observe
DB.getInstance(getContext()).account().liveAccount(id).observe(getViewLifecycleOwner(), new Observer<EntityAccount>() {
boolean once = false;
2018-08-02 13:33:06 +00:00
@Override
public void onChanged(@Nullable EntityAccount account) {
2018-08-15 09:50:39 +00:00
// Get providers
List<Provider> providers = Provider.loadProfiles(getContext());
providers.add(0, new Provider(getString(R.string.title_custom)));
ArrayAdapter<Provider> padapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, providers);
padapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spProvider.setAdapter(padapter);
if (savedInstanceState == null) {
2018-08-16 11:26:53 +00:00
if (once)
return;
once = true;
2018-08-27 15:08:23 +00:00
spProvider.setTag(0);
spProvider.setSelection(0);
if (account != null) {
for (int pos = 1; pos < providers.size(); pos++)
if (providers.get(pos).imap_host.equals(account.host)) {
spProvider.setTag(pos);
spProvider.setSelection(pos);
break;
}
}
2018-08-16 11:26:53 +00:00
etName.setText(account == null ? null : account.name);
2018-08-27 15:08:23 +00:00
2018-08-16 11:26:53 +00:00
etHost.setText(account == null ? null : account.host);
etPort.setText(account == null ? null : Long.toString(account.port));
2018-08-27 15:08:23 +00:00
2018-08-16 11:26:53 +00:00
etUser.setText(account == null ? null : account.user);
tilPassword.getEditText().setText(account == null ? null : account.password);
2018-08-27 15:08:23 +00:00
2018-08-16 11:26:53 +00:00
cbSynchronize.setChecked(account == null ? true : account.synchronize);
cbPrimary.setChecked(account == null ? true : account.primary);
2018-08-23 09:48:27 +00:00
etInterval.setText(account == null ? "9" : Integer.toString(account.poll_interval));
2018-08-15 09:50:39 +00:00
} else {
int provider = savedInstanceState.getInt("provider");
spProvider.setTag(provider);
spProvider.setSelection(provider);
2018-08-27 15:08:23 +00:00
authorized = savedInstanceState.getBoolean("authorized");
2018-08-15 09:50:39 +00:00
tilPassword.getEditText().setText(savedInstanceState.getString("password"));
}
Helper.setViewsEnabled(view, true);
Provider provider = (Provider) spProvider.getSelectedItem();
btnAuthorize.setVisibility(provider.getAuthType() == Helper.AUTH_TYPE_PASSWORD ? View.GONE : View.VISIBLE);
tvLink.setText(Html.fromHtml("<a href=\"" + provider.link + "\">" + provider.link + "</a>"));
grpInstructions.setVisibility(provider.link == null ? View.GONE : View.VISIBLE);
cbPrimary.setEnabled(cbSynchronize.isChecked());
2018-08-15 09:50:39 +00:00
btnCheck.setVisibility(cbSynchronize.isChecked() ? View.VISIBLE : View.GONE);
btnSave.setVisibility(cbSynchronize.isChecked() ? View.GONE : View.VISIBLE);
2018-08-16 11:26:53 +00:00
// Consider previous check/save/delete as cancelled
ibDelete.setVisibility(account == null ? View.GONE : View.VISIBLE);
btnCheck.setEnabled(true);
pbWait.setVisibility(View.GONE);
2018-08-02 13:33:06 +00:00
}
});
}
2018-08-27 14:31:45 +00:00
void selectAccount() {
Log.i(Helper.TAG, "Select account");
Provider provider = (Provider) spProvider.getSelectedItem();
if (provider.type != null)
startActivityForResult(newChooseAccountIntent(
null,
null,
new String[]{provider.type},
null,
null,
null,
null), ActivitySetup.REQUEST_CHOOSE_ACCOUNT);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == ActivitySetup.REQUEST_PERMISSION)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
selectAccount();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(Helper.TAG, "Activity result request=" + requestCode + " result=" + resultCode + " data=" + data);
if (resultCode == Activity.RESULT_OK)
if (requestCode == ActivitySetup.REQUEST_CHOOSE_ACCOUNT) {
String name = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
String type = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
AccountManager am = AccountManager.get(getContext());
Account[] accounts = am.getAccountsByType(type);
Log.i(Helper.TAG, "Accounts=" + accounts.length);
for (final Account account : accounts)
if (name.equals(account.name)) {
am.getAuthToken(
account,
2018-08-27 16:19:56 +00:00
Helper.getAuthTokenType(type),
2018-08-27 14:31:45 +00:00
new Bundle(),
getActivity(),
new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle bundle = future.getResult();
String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
Log.i(Helper.TAG, "Got token");
authorized = true;
2018-08-27 14:31:45 +00:00
etUser.setText(account.name);
tilPassword.getEditText().setText(token);
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
Toast.makeText(getContext(), Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
}
},
null);
break;
}
}
}
2018-08-02 13:33:06 +00:00
}