Added re OAuth

This commit is contained in:
M66B
2019-12-25 10:24:31 +01:00
parent 5ceeae833d
commit a129611dbe
4 changed files with 161 additions and 4 deletions

View File

@@ -19,6 +19,8 @@ package eu.faircode.email;
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Dialog;
import android.app.NotificationManager;
import android.content.Context;
@@ -50,6 +52,7 @@ import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -96,6 +99,7 @@ public class FragmentIdentity extends FragmentBase {
private EditText etPort;
private EditText etUser;
private TextInputLayout tilPassword;
private Button btnOAuth;
private EditText etRealm;
private CheckBox cbUseIp;
@@ -179,6 +183,7 @@ public class FragmentIdentity extends FragmentBase {
etPort = view.findViewById(R.id.etPort);
etUser = view.findViewById(R.id.etUser);
tilPassword = view.findViewById(R.id.tilPassword);
btnOAuth = view.findViewById(R.id.btnOAuth);
etRealm = view.findViewById(R.id.etRealm);
cbUseIp = view.findViewById(R.id.cbUseIp);
@@ -388,6 +393,13 @@ public class FragmentIdentity extends FragmentBase {
}
});
btnOAuth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onAuth();
}
});
cbSynchronize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@@ -841,6 +853,56 @@ public class FragmentIdentity extends FragmentBase {
}.execute(this, args, "identity:save");
}
private void onAuth() {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<String>() {
@Override
protected void onPreExecute(Bundle args) {
btnOAuth.setEnabled(false);
}
@Override
protected void onPostExecute(Bundle args) {
btnOAuth.setEnabled(true);
}
@Override
protected String onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
DB db = DB.getInstance(context);
EntityIdentity identity = db.identity().getIdentity(id);
if (identity == null)
return null;
AccountManager am = AccountManager.get(getContext());
Account[] accounts = am.getAccountsByType("com.google");
for (Account google : accounts)
if (identity.user.equals(google.name))
return am.blockingGetAuthToken(
google,
MailService.getAuthTokenType("com.google"),
true);
return null;
}
@Override
protected void onExecuted(Bundle args, String token) {
ToastEx.makeText(getContext(), R.string.title_completed, Toast.LENGTH_LONG).show();
tilPassword.getEditText().setText(token);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "account:oauth");
}
private void showError(Throwable ex) {
tvError.setText(Log.formatThrowable(ex, false));
grpError.setVisibility(View.VISIBLE);
@@ -979,6 +1041,9 @@ public class FragmentIdentity extends FragmentBase {
tilPassword.setEnabled(false);
}
if (identity == null && identity.auth_type != MailService.AUTH_TYPE_GMAIL)
Helper.hide(btnOAuth);
cbPrimary.setEnabled(cbSynchronize.isChecked());
pbWait.setVisibility(View.GONE);