mirror of
https://github.com/M66B/FairEmail.git
synced 2026-04-01 22:56:33 +02:00
Removed select account
This commit is contained in:
@@ -19,24 +19,11 @@ package eu.faircode.email;
|
||||
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.Manifest;
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.accounts.AccountManagerCallback;
|
||||
import android.accounts.AccountManagerFuture;
|
||||
import android.accounts.AuthenticatorException;
|
||||
import android.accounts.OperationCanceledException;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.util.Patterns;
|
||||
import android.view.KeyEvent;
|
||||
@@ -54,14 +41,12 @@ import android.widget.TextView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.constraintlayout.widget.Group;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
import com.sun.mail.imap.IMAPFolder;
|
||||
import com.sun.mail.imap.IMAPStore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
@@ -72,17 +57,11 @@ import javax.mail.Folder;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
|
||||
import static android.accounts.AccountManager.newChooseAccountIntent;
|
||||
import static android.app.Activity.RESULT_OK;
|
||||
import static com.google.android.material.textfield.TextInputLayout.END_ICON_NONE;
|
||||
import static com.google.android.material.textfield.TextInputLayout.END_ICON_PASSWORD_TOGGLE;
|
||||
|
||||
public class FragmentQuickSetup extends FragmentBase {
|
||||
private ViewGroup view;
|
||||
|
||||
private EditText etName;
|
||||
private EditText etEmail;
|
||||
private Button btnAuthorize;
|
||||
private TextInputLayout tilPassword;
|
||||
private Button btnCheck;
|
||||
|
||||
@@ -94,8 +73,6 @@ public class FragmentQuickSetup extends FragmentBase {
|
||||
private Button btnSave;
|
||||
private Group grpSetup;
|
||||
|
||||
private int auth_type = ConnectionHelper.AUTH_TYPE_PASSWORD;
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
@@ -106,7 +83,6 @@ public class FragmentQuickSetup extends FragmentBase {
|
||||
|
||||
// Get controls
|
||||
etName = view.findViewById(R.id.etName);
|
||||
btnAuthorize = view.findViewById(R.id.btnAuthorize);
|
||||
etEmail = view.findViewById(R.id.etEmail);
|
||||
tilPassword = view.findViewById(R.id.tilPassword);
|
||||
btnCheck = view.findViewById(R.id.btnCheck);
|
||||
@@ -121,54 +97,6 @@ public class FragmentQuickSetup extends FragmentBase {
|
||||
|
||||
// Wire controls
|
||||
|
||||
btnAuthorize.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (!Helper.hasValidFingerprint(getContext())) {
|
||||
Snackbar snackbar = Snackbar.make(view, R.string.title_no_xoauth2, Snackbar.LENGTH_LONG);
|
||||
final Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setData(Uri.parse(Helper.FAQ_URI + "#user-content-faq109"));
|
||||
if (intent.resolveActivity(getContext().getPackageManager()) != null)
|
||||
snackbar.setAction(R.string.title_info, new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
snackbar.show();
|
||||
return;
|
||||
}
|
||||
|
||||
String permission = Manifest.permission.GET_ACCOUNTS;
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O &&
|
||||
!Helper.hasPermission(getContext(), permission)) {
|
||||
Log.i("Requesting " + permission);
|
||||
requestPermissions(new String[]{permission}, ActivitySetup.REQUEST_CHOOSE_ACCOUNT);
|
||||
} else
|
||||
selectAccount();
|
||||
}
|
||||
});
|
||||
|
||||
etEmail.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
if (auth_type != ConnectionHelper.AUTH_TYPE_PASSWORD) {
|
||||
auth_type = ConnectionHelper.AUTH_TYPE_PASSWORD;
|
||||
tilPassword.getEditText().setText(null);
|
||||
tilPassword.setEnabled(true);
|
||||
tilPassword.setEndIconMode(END_ICON_PASSWORD_TOGGLE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
}
|
||||
});
|
||||
|
||||
tilPassword.setHintEnabled(false);
|
||||
|
||||
tilPassword.getEditText().setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
||||
@@ -237,7 +165,6 @@ public class FragmentQuickSetup extends FragmentBase {
|
||||
args.putString("name", etName.getText().toString());
|
||||
args.putString("email", etEmail.getText().toString().trim());
|
||||
args.putString("password", tilPassword.getEditText().getText().toString());
|
||||
args.putInt("auth_type", auth_type);
|
||||
args.putBoolean("check", check);
|
||||
|
||||
new SimpleTask<EmailProvider>() {
|
||||
@@ -261,7 +188,6 @@ public class FragmentQuickSetup extends FragmentBase {
|
||||
String name = args.getString("name");
|
||||
String email = args.getString("email");
|
||||
String password = args.getString("password");
|
||||
int auth_type = args.getInt("auth_type");
|
||||
boolean check = args.getBoolean("check");
|
||||
|
||||
if (TextUtils.isEmpty(name))
|
||||
@@ -283,7 +209,7 @@ public class FragmentQuickSetup extends FragmentBase {
|
||||
long now = new Date().getTime();
|
||||
|
||||
{
|
||||
Properties props = MessageHelper.getSessionProperties(auth_type, null, false);
|
||||
Properties props = MessageHelper.getSessionProperties(null, false);
|
||||
Session isession = Session.getInstance(props, null);
|
||||
isession.setDebug(true);
|
||||
try (IMAPStore istore = (IMAPStore) isession.getStore(provider.imap_starttls ? "imap" : "imaps")) {
|
||||
@@ -326,7 +252,7 @@ public class FragmentQuickSetup extends FragmentBase {
|
||||
}
|
||||
|
||||
{
|
||||
Properties props = MessageHelper.getSessionProperties(auth_type, null, false);
|
||||
Properties props = MessageHelper.getSessionProperties(null, false);
|
||||
Session isession = Session.getInstance(props, null);
|
||||
isession.setDebug(true);
|
||||
try (Transport itransport = isession.getTransport(provider.smtp_starttls ? "smtp" : "smtps")) {
|
||||
@@ -345,7 +271,7 @@ public class FragmentQuickSetup extends FragmentBase {
|
||||
// Create account
|
||||
EntityAccount account = new EntityAccount();
|
||||
|
||||
account.auth_type = auth_type;
|
||||
account.auth_type = ConnectionHelper.AUTH_TYPE_PASSWORD;
|
||||
account.host = provider.imap_host;
|
||||
account.starttls = provider.imap_starttls;
|
||||
account.insecure = false;
|
||||
@@ -398,7 +324,7 @@ public class FragmentQuickSetup extends FragmentBase {
|
||||
identity.color = null;
|
||||
identity.signature = null;
|
||||
|
||||
identity.auth_type = auth_type;
|
||||
identity.auth_type = ConnectionHelper.AUTH_TYPE_PASSWORD;
|
||||
identity.host = provider.smtp_host;
|
||||
identity.starttls = provider.smtp_starttls;
|
||||
identity.insecure = false;
|
||||
@@ -466,96 +392,4 @@ public class FragmentQuickSetup extends FragmentBase {
|
||||
}
|
||||
}.execute(FragmentQuickSetup.this, args, "setup:quick");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
if (requestCode == ActivitySetup.REQUEST_CHOOSE_ACCOUNT)
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
|
||||
selectAccount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == ActivitySetup.REQUEST_CHOOSE_ACCOUNT)
|
||||
if (resultCode == RESULT_OK && data != null)
|
||||
accountSelected(data);
|
||||
}
|
||||
|
||||
private void selectAccount() {
|
||||
Log.i("Select account");
|
||||
startActivityForResult(
|
||||
Helper.getChooser(getContext(), newChooseAccountIntent(
|
||||
null,
|
||||
null,
|
||||
new String[]{"com.google"},
|
||||
false,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null)),
|
||||
ActivitySetup.REQUEST_CHOOSE_ACCOUNT);
|
||||
}
|
||||
|
||||
private void accountSelected(Intent data) {
|
||||
Log.i("Selected 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("Accounts=" + accounts.length);
|
||||
for (final Account account : accounts)
|
||||
if (name.equals(account.name)) {
|
||||
etEmail.setEnabled(false);
|
||||
tilPassword.setEnabled(false);
|
||||
btnAuthorize.setEnabled(false);
|
||||
btnCheck.setEnabled(false);
|
||||
final Snackbar snackbar = Snackbar.make(view, R.string.title_authorizing, Snackbar.LENGTH_SHORT);
|
||||
snackbar.show();
|
||||
|
||||
am.getAuthToken(
|
||||
account,
|
||||
ConnectionHelper.getAuthTokenType(type),
|
||||
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("Got token");
|
||||
|
||||
etEmail.setText(account.name);
|
||||
tilPassword.getEditText().setText(token);
|
||||
auth_type = ConnectionHelper.AUTH_TYPE_GMAIL;
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
if (ex instanceof OperationCanceledException ||
|
||||
ex instanceof AuthenticatorException ||
|
||||
ex instanceof IOException) {
|
||||
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
|
||||
Snackbar.make(view, Helper.formatThrowable(ex), Snackbar.LENGTH_LONG).show();
|
||||
} else
|
||||
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
|
||||
} finally {
|
||||
etEmail.setEnabled(true);
|
||||
tilPassword.setEnabled(auth_type == ConnectionHelper.AUTH_TYPE_PASSWORD);
|
||||
tilPassword.setEndIconMode(auth_type == ConnectionHelper.AUTH_TYPE_PASSWORD
|
||||
? END_ICON_PASSWORD_TOGGLE : END_ICON_NONE);
|
||||
btnAuthorize.setEnabled(true);
|
||||
btnCheck.setEnabled(true);
|
||||
new Handler().postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
snackbar.dismiss();
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
},
|
||||
null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user