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.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<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);
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());

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);

View File

@ -243,6 +243,19 @@
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btnOAuth"
style="@style/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:minWidth="0dp"
android:minHeight="0dp"
android:tag="disable"
android:text="@string/title_setup_authentication"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/tilPassword" />
<TextView
android:id="@+id/tvOAuthSupport"
android:layout_width="wrap_content"
@ -254,7 +267,7 @@
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="?android:attr/textColorLink"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tilPassword" />
app:layout_constraintTop_toBottomOf="@id/btnOAuth" />
<TextView
android:id="@+id/tvRealm"
@ -883,7 +896,7 @@
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="
tvUser,etUser,tvPassword,tilPassword,tvOAuthSupport,tvRealm,etRealm,
tvUser,etUser,tvPassword,tilPassword,btnOAuth,tvOAuthSupport,tvRealm,etRealm,
tvName,tvNameRemark,etName,
tvColor,btnColor,tvColorHint,tvColorPro" />

View File

@ -430,6 +430,19 @@
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btnOAuth"
style="@style/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:minWidth="0dp"
android:minHeight="0dp"
android:tag="disable"
android:text="@string/title_setup_authentication"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/tilPassword" />
<TextView
android:id="@+id/tvRealm"
android:layout_width="wrap_content"
@ -438,7 +451,7 @@
android:text="@string/title_realm"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tilPassword" />
app:layout_constraintTop_toBottomOf="@id/btnOAuth" />
<EditText
android:id="@+id/etRealm"
@ -691,7 +704,7 @@
tvProvider,spProvider,
tvDomain,tvDomainHint,etDomain,btnAutoConfig,
tvSmtp,tvHost,etHost,rgEncryption,cbInsecure,tvInsecureRemark,tvPort,etPort,
tvUser,etUser,tvPassword,tilPassword,
tvUser,etUser,tvPassword,tilPassword,btnOAuth,
tvRealm,etRealm,
cbUseIp,tvUseIpHint,
cbSynchronize,cbPrimary,