Refactoring

pull/147/head
M66B 6 years ago
parent 31bf9cfaa9
commit 9dc1f04604

@ -834,94 +834,101 @@ public class FragmentSetup extends FragmentEx {
@Override @Override
public void onActivityResult(final int requestCode, int resultCode, final Intent data) { public void onActivityResult(final int requestCode, int resultCode, final Intent data) {
if (requestCode == ActivitySetup.REQUEST_EXPORT || requestCode == ActivitySetup.REQUEST_IMPORT) { if (requestCode == ActivitySetup.REQUEST_EXPORT || requestCode == ActivitySetup.REQUEST_IMPORT) {
if (resultCode == RESULT_OK && data != null) { if (resultCode == RESULT_OK && data != null)
final View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_password, null); fileSelected(requestCode == ActivitySetup.REQUEST_EXPORT, data);
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) } else if (requestCode == ActivitySetup.REQUEST_CHOOSE_ACCOUNT) {
.setView(dview) if (resultCode == RESULT_OK && data != null)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { accountSelected(data);
}
}
private void fileSelected(final boolean export, final Intent data) {
View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_password, null);
final TextInputLayout etPassword1 = dview.findViewById(R.id.tilPassword1);
final TextInputLayout etPassword2 = dview.findViewById(R.id.tilPassword2);
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String password1 = etPassword1.getEditText().getText().toString();
String password2 = etPassword2.getEditText().getText().toString();
if (TextUtils.isEmpty(password1))
Snackbar.make(view, R.string.title_setup_password_missing, Snackbar.LENGTH_LONG).show();
else {
if (password1.equals(password2)) {
if (export)
handleExport(data, password1);
else
handleImport(data, password1);
} else
Snackbar.make(view, R.string.title_setup_password_different, Snackbar.LENGTH_LONG).show();
}
}
})
.show();
}
private void accountSelected(Intent data) {
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);
btnQuick.setEnabled(false);
final Snackbar snackbar = Snackbar.make(view, R.string.title_authorizing, Snackbar.LENGTH_SHORT);
snackbar.show();
am.getAuthToken(
account,
Helper.getAuthTokenType(type),
new Bundle(),
getActivity(),
new AccountManagerCallback<Bundle>() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void run(AccountManagerFuture<Bundle> future) {
TextInputLayout etPassword1 = dview.findViewById(R.id.tilPassword1); try {
TextInputLayout etPassword2 = dview.findViewById(R.id.tilPassword2); Bundle bundle = future.getResult();
String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
String password1 = etPassword1.getEditText().getText().toString(); Log.i("Got token");
String password2 = etPassword2.getEditText().getText().toString();
etEmail.setText(account.name);
if (TextUtils.isEmpty(password1)) tilPassword.getEditText().setText(token);
Snackbar.make(view, R.string.title_setup_password_missing, Snackbar.LENGTH_LONG).show(); auth_type = Helper.AUTH_TYPE_GMAIL;
else { } catch (Throwable ex) {
if (password1.equals(password2)) { Log.e(ex);
if (requestCode == ActivitySetup.REQUEST_EXPORT) if (ex instanceof OperationCanceledException ||
handleExport(data, password1); ex instanceof AuthenticatorException ||
else ex instanceof IOException)
handleImport(data, password1); Snackbar.make(view, Helper.formatThrowable(ex), Snackbar.LENGTH_LONG).show();
} else else
Snackbar.make(view, R.string.title_setup_password_different, Snackbar.LENGTH_LONG).show(); Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
} finally {
etEmail.setEnabled(true);
tilPassword.setEnabled(true);
btnAuthorize.setEnabled(true);
btnQuick.setEnabled(true);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
snackbar.dismiss();
}
}, 1000);
} }
} }
}) },
.show(); null);
} break;
} else if (requestCode == ActivitySetup.REQUEST_CHOOSE_ACCOUNT) {
if (resultCode == RESULT_OK && data != null) {
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);
btnQuick.setEnabled(false);
final Snackbar snackbar = Snackbar.make(view, R.string.title_authorizing, Snackbar.LENGTH_SHORT);
snackbar.show();
am.getAuthToken(
account,
Helper.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 = Helper.AUTH_TYPE_GMAIL;
} catch (Throwable ex) {
Log.e(ex);
if (ex instanceof OperationCanceledException ||
ex instanceof AuthenticatorException ||
ex instanceof IOException)
Snackbar.make(view, Helper.formatThrowable(ex), Snackbar.LENGTH_LONG).show();
else
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
} finally {
etEmail.setEnabled(true);
tilPassword.setEnabled(true);
btnAuthorize.setEnabled(true);
btnQuick.setEnabled(true);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
snackbar.dismiss();
}
}, 1000);
}
}
},
null);
break;
}
} }
}
} }
private void onMenuPrivacy() { private void onMenuPrivacy() {

Loading…
Cancel
Save