diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index fadea97575..f428796b93 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -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.NotificationManager; import android.content.Context; import android.content.Intent; @@ -46,6 +48,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; @@ -85,6 +88,7 @@ public class FragmentAccount extends FragmentBase { private EditText etPort; private EditText etUser; private TextInputLayout tilPassword; + private Button btnOAuth; private TextView tvOAuthSupport; private EditText etRealm; @@ -191,6 +195,7 @@ public class FragmentAccount extends FragmentBase { cbInsecure = view.findViewById(R.id.cbInsecure); etUser = view.findViewById(R.id.etUser); tilPassword = view.findViewById(R.id.tilPassword); + btnOAuth = view.findViewById(R.id.btnOAuth); tvOAuthSupport = view.findViewById(R.id.tvOAuthSupport); etRealm = view.findViewById(R.id.etRealm); @@ -273,6 +278,7 @@ public class FragmentAccount extends FragmentBase { etUser.setTag(null); etUser.setText(null); tilPassword.getEditText().setText(null); + btnOAuth.setEnabled(false); etRealm.setText(null); cbTrust.setChecked(false); @@ -304,6 +310,13 @@ public class FragmentAccount extends FragmentBase { } }); + btnOAuth.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + onAuth(); + } + }); + tvOAuthSupport.setPaintFlags(tvOAuthSupport.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); tvOAuthSupport.setOnClickListener(new View.OnClickListener() { @Override @@ -1175,6 +1188,56 @@ public class FragmentAccount extends FragmentBase { }.execute(this, args, "account:save"); } + private void onAuth() { + Bundle args = new Bundle(); + args.putLong("id", id); + + new SimpleTask() { + @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); + + EntityAccount account = db.account().getAccount(id); + if (account == null) + return null; + + AccountManager am = AccountManager.get(getContext()); + Account[] accounts = am.getAccountsByType("com.google"); + for (Account google : accounts) + if (account.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); @@ -1342,6 +1405,9 @@ public class FragmentAccount extends FragmentBase { tilPassword.setEnabled(false); } + if (account == null || account.auth_type != MailService.AUTH_TYPE_GMAIL) + Helper.hide((btnOAuth)); + cbOnDemand.setEnabled(cbSynchronize.isChecked()); cbPrimary.setEnabled(cbSynchronize.isChecked()); diff --git a/app/src/main/java/eu/faircode/email/FragmentIdentity.java b/app/src/main/java/eu/faircode/email/FragmentIdentity.java index 0ba9fcc03e..68e6c42880 100644 --- a/app/src/main/java/eu/faircode/email/FragmentIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentIdentity.java @@ -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() { + @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); diff --git a/app/src/main/res/layout/fragment_account.xml b/app/src/main/res/layout/fragment_account.xml index ded0b075a5..68b60df1eb 100644 --- a/app/src/main/res/layout/fragment_account.xml +++ b/app/src/main/res/layout/fragment_account.xml @@ -243,6 +243,19 @@ android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> +