Added Gmail/OAuth/POP3

pull/208/head
M66B 3 years ago
parent 5ea4e8a10b
commit 8e8fc9328a

@ -67,6 +67,7 @@ import java.util.concurrent.TimeUnit;
public class FragmentGmail extends FragmentBase { public class FragmentGmail extends FragmentBase {
private String personal; private String personal;
private String address; private String address;
private boolean pop;
private boolean update; private boolean update;
private ViewGroup view; private ViewGroup view;
@ -77,6 +78,7 @@ public class FragmentGmail extends FragmentBase {
private Button btnGrant; private Button btnGrant;
private TextView tvGranted; private TextView tvGranted;
private EditText etName; private EditText etName;
private CheckBox cbPop;
private CheckBox cbUpdate; private CheckBox cbUpdate;
private Button btnSelect; private Button btnSelect;
private ContentLoadingProgressBar pbSelect; private ContentLoadingProgressBar pbSelect;
@ -98,6 +100,7 @@ public class FragmentGmail extends FragmentBase {
Bundle args = getArguments(); Bundle args = getArguments();
personal = args.getString("personal"); personal = args.getString("personal");
address = args.getString("address"); address = args.getString("address");
pop = args.getBoolean("pop", false);
update = args.getBoolean("update", true); update = args.getBoolean("update", true);
} }
@ -116,6 +119,7 @@ public class FragmentGmail extends FragmentBase {
btnGrant = view.findViewById(R.id.btnGrant); btnGrant = view.findViewById(R.id.btnGrant);
tvGranted = view.findViewById(R.id.tvGranted); tvGranted = view.findViewById(R.id.tvGranted);
etName = view.findViewById(R.id.etName); etName = view.findViewById(R.id.etName);
cbPop = view.findViewById(R.id.cbPop);
cbUpdate = view.findViewById(R.id.cbUpdate); cbUpdate = view.findViewById(R.id.cbUpdate);
btnSelect = view.findViewById(R.id.btnSelect); btnSelect = view.findViewById(R.id.btnSelect);
pbSelect = view.findViewById(R.id.pbSelect); pbSelect = view.findViewById(R.id.pbSelect);
@ -203,6 +207,7 @@ public class FragmentGmail extends FragmentBase {
Helper.setViewsEnabled(view, false); Helper.setViewsEnabled(view, false);
tvTitle.setText(getString(R.string.title_setup_oauth_rationale, "Gmail")); tvTitle.setText(getString(R.string.title_setup_oauth_rationale, "Gmail"));
etName.setText(personal); etName.setText(personal);
cbPop.setChecked(pop);
cbUpdate.setChecked(update); cbUpdate.setChecked(update);
pbSelect.setVisibility(View.GONE); pbSelect.setVisibility(View.GONE);
grpError.setVisibility(View.GONE); grpError.setVisibility(View.GONE);
@ -262,6 +267,7 @@ public class FragmentGmail extends FragmentBase {
} }
etName.setEnabled(granted); etName.setEnabled(granted);
cbPop.setEnabled(granted);
cbUpdate.setEnabled(granted); cbUpdate.setEnabled(granted);
btnSelect.setEnabled(granted); btnSelect.setEnabled(granted);
@ -374,6 +380,7 @@ public class FragmentGmail extends FragmentBase {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString("name", etName.getText().toString().trim()); args.putString("name", etName.getText().toString().trim());
args.putBoolean("pop", cbPop.isChecked());
args.putBoolean("update", cbUpdate.isChecked()); args.putBoolean("update", cbUpdate.isChecked());
args.putString("user", user); args.putString("user", user);
args.putString("password", state.jsonSerializeString()); args.putString("password", state.jsonSerializeString());
@ -382,6 +389,7 @@ public class FragmentGmail extends FragmentBase {
@Override @Override
protected void onPreExecute(Bundle args) { protected void onPreExecute(Bundle args) {
etName.setEnabled(false); etName.setEnabled(false);
cbPop.setEnabled(false);
cbUpdate.setEnabled(false); cbUpdate.setEnabled(false);
btnSelect.setEnabled(false); btnSelect.setEnabled(false);
pbSelect.setVisibility(View.VISIBLE); pbSelect.setVisibility(View.VISIBLE);
@ -390,6 +398,7 @@ public class FragmentGmail extends FragmentBase {
@Override @Override
protected void onPostExecute(Bundle args) { protected void onPostExecute(Bundle args) {
etName.setEnabled(true); etName.setEnabled(true);
cbPop.setEnabled(true);
cbUpdate.setEnabled(true); cbUpdate.setEnabled(true);
btnSelect.setEnabled(true); btnSelect.setEnabled(true);
pbSelect.setVisibility(View.GONE); pbSelect.setVisibility(View.GONE);
@ -398,6 +407,7 @@ public class FragmentGmail extends FragmentBase {
@Override @Override
protected Void onExecute(Context context, Bundle args) throws Throwable { protected Void onExecute(Context context, Bundle args) throws Throwable {
String name = args.getString("name"); String name = args.getString("name");
boolean pop = args.getBoolean("pop");
String user = args.getString("user"); String user = args.getString("user");
String password = args.getString("password"); String password = args.getString("password");
@ -418,21 +428,27 @@ public class FragmentGmail extends FragmentBase {
EmailProvider provider = EmailProvider EmailProvider provider = EmailProvider
.fromDomain(context, "gmail.com", EmailProvider.Discover.ALL) .fromDomain(context, "gmail.com", EmailProvider.Discover.ALL)
.get(0); .get(0);
if (provider.pop == null)
pop = false;
List<EntityFolder> folders; List<EntityFolder> folders;
String aprotocol = (provider.imap.starttls ? "imap" : "imaps"); EmailProvider.Server inbound = (pop ? provider.pop : provider.imap);
int aencryption = (provider.imap.starttls ? EmailService.ENCRYPTION_STARTTLS : EmailService.ENCRYPTION_SSL); String aprotocol = (pop ? (inbound.starttls ? "pop3" : "pop3s") : (inbound.starttls ? "imap" : "imaps"));
try (EmailService iservice = new EmailService( int aencryption = (inbound.starttls ? EmailService.ENCRYPTION_STARTTLS : EmailService.ENCRYPTION_SSL);
try (EmailService aservice = new EmailService(
context, aprotocol, null, aencryption, false, context, aprotocol, null, aencryption, false,
EmailService.PURPOSE_CHECK, true)) { EmailService.PURPOSE_CHECK, true)) {
iservice.connect( aservice.connect(
provider.imap.host, provider.imap.port, inbound.host, inbound.port,
AUTH_TYPE_GMAIL, null, AUTH_TYPE_GMAIL, null,
user, password, user, password,
null, null); null, null);
folders = iservice.getFolders(); if (pop)
folders = EntityFolder.getPopFolders(context);
else
folders = aservice.getFolders();
} }
Long max_size; Long max_size;
@ -450,6 +466,7 @@ public class FragmentGmail extends FragmentBase {
} }
EntityAccount update = null; EntityAccount update = null;
int protocol = (pop ? EntityAccount.TYPE_POP : EntityAccount.TYPE_IMAP);
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
try { try {
db.beginTransaction(); db.beginTransaction();
@ -457,7 +474,7 @@ public class FragmentGmail extends FragmentBase {
if (args.getBoolean("update")) { if (args.getBoolean("update")) {
List<EntityAccount> accounts = List<EntityAccount> accounts =
db.account().getAccounts(user, db.account().getAccounts(user,
EntityAccount.TYPE_IMAP, protocol,
new int[]{AUTH_TYPE_GMAIL, AUTH_TYPE_PASSWORD}); new int[]{AUTH_TYPE_GMAIL, AUTH_TYPE_PASSWORD});
if (accounts != null && accounts.size() == 1) if (accounts != null && accounts.size() == 1)
update = accounts.get(0); update = accounts.get(0);
@ -469,9 +486,10 @@ public class FragmentGmail extends FragmentBase {
// Create account // Create account
EntityAccount account = new EntityAccount(); EntityAccount account = new EntityAccount();
account.host = provider.imap.host; account.protocol = protocol;
account.host = inbound.host;
account.encryption = aencryption; account.encryption = aencryption;
account.port = provider.imap.port; account.port = inbound.port;
account.auth_type = AUTH_TYPE_GMAIL; account.auth_type = AUTH_TYPE_GMAIL;
account.user = user; account.user = user;
account.password = password; account.password = password;
@ -481,6 +499,9 @@ public class FragmentGmail extends FragmentBase {
account.synchronize = true; account.synchronize = true;
account.primary = (primary == null); account.primary = (primary == null);
if (pop)
account.max_messages = EntityAccount.DEFAULT_MAX_MESSAGES;
account.created = new Date().getTime(); account.created = new Date().getTime();
account.last_connected = account.created; account.last_connected = account.created;
@ -502,11 +523,16 @@ public class FragmentGmail extends FragmentBase {
} }
// Set swipe left/right folder // Set swipe left/right folder
for (EntityFolder folder : folders) if (pop) {
if (EntityFolder.TRASH.equals(folder.type)) account.swipe_left = EntityMessage.SWIPE_ACTION_DELETE;
account.swipe_left = folder.id; account.swipe_right = EntityMessage.SWIPE_ACTION_SEEN;
else if (EntityFolder.ARCHIVE.equals(folder.type)) } else {
account.swipe_right = folder.id; for (EntityFolder folder : folders)
if (EntityFolder.TRASH.equals(folder.type))
account.swipe_left = folder.id;
else if (EntityFolder.ARCHIVE.equals(folder.type))
account.swipe_right = folder.id;
}
db.account().updateAccount(account); db.account().updateAccount(account);

@ -90,6 +90,16 @@
<requestFocus /> <requestFocus />
</eu.faircode.email.EditTextPlain> </eu.faircode.email.EditTextPlain>
<CheckBox
android:id="@+id/cbPop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_setup_pop"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etName" />
<CheckBox <CheckBox
android:id="@+id/cbUpdate" android:id="@+id/cbUpdate"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -98,7 +108,7 @@
android:text="@string/title_setup_oauth_update" android:text="@string/title_setup_oauth_update"
android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etName" /> app:layout_constraintTop_toBottomOf="@id/cbPop" />
<Button <Button
android:id="@+id/btnSelect" android:id="@+id/btnSelect"

@ -16,10 +16,10 @@
host="smtp.gmail.com" host="smtp.gmail.com"
port="465" port="465"
starttls="false" /> starttls="false" />
<!--pop <pop
host="pop.gmail.com" host="pop.gmail.com"
port="995" port="995"
starttls="false" /--> starttls="false" />
<oauth <oauth
askAccount="true" askAccount="true"
authorizationEndpoint="https://accounts.google.com/o/oauth2/auth" authorizationEndpoint="https://accounts.google.com/o/oauth2/auth"

Loading…
Cancel
Save