Small behavior improvements

This commit is contained in:
M66B 2019-10-02 14:54:10 +02:00
parent 58647ad2a2
commit 6b7f8142a2
2 changed files with 60 additions and 30 deletions

View File

@ -33,6 +33,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -101,6 +102,15 @@ public class FragmentGmail extends FragmentBase {
btnSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etName.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
tvError.setText(R.string.title_no_name);
tvError.setVisibility(View.VISIBLE);
return;
}
tvError.setVisibility(View.GONE);
startActivityForResult(
Helper.getChooser(getContext(), newChooseAccountIntent(
null,
@ -230,6 +240,7 @@ public class FragmentGmail extends FragmentBase {
} catch (Throwable ex) {
Log.e(ex);
tvError.setText(Helper.formatThrowable(ex));
tvError.setVisibility(View.VISIBLE);
new Handler().post(new Runnable() {
@Override
@ -247,7 +258,7 @@ public class FragmentGmail extends FragmentBase {
private void onAuthorized(String user, String password) {
Bundle args = new Bundle();
args.putString("name", etName.getText().toString());
args.putString("name", etName.getText().toString().trim());
args.putString("user", user);
args.putString("password", password);
@ -257,6 +268,7 @@ public class FragmentGmail extends FragmentBase {
etName.setEnabled(false);
btnSelect.setEnabled(false);
pbSelect.setVisibility(View.VISIBLE);
tvError.setVisibility(View.GONE);
}
@Override
@ -272,9 +284,11 @@ public class FragmentGmail extends FragmentBase {
String user = args.getString("user");
String password = args.getString("password");
if (!user.contains("@"))
throw new IllegalArgumentException(
context.getString(R.string.title_email_invalid, user));
// Safety checks
if (!Patterns.EMAIL_ADDRESS.matcher(user).matches())
throw new IllegalArgumentException(context.getString(R.string.title_email_invalid, user));
if (TextUtils.isEmpty(password))
throw new IllegalArgumentException(context.getString(R.string.title_no_password));
String domain = user.split("@")[1];
EmailProvider provider = EmailProvider.fromDomain(context, domain, EmailProvider.Discover.ALL);
@ -384,7 +398,12 @@ public class FragmentGmail extends FragmentBase {
@Override
protected void onException(Bundle args, Throwable ex) {
Log.e(ex);
tvError.setText(Helper.formatThrowable(ex));
if (ex instanceof IllegalArgumentException)
tvError.setText(ex.getMessage());
else
tvError.setText(Helper.formatThrowable(ex));
tvError.setVisibility(View.VISIBLE);
new Handler().post(new Runnable() {
@Override

View File

@ -216,7 +216,7 @@ public class FragmentQuickSetup extends FragmentBase {
@Override
protected EmailProvider onExecute(Context context, Bundle args) throws Throwable {
String name = args.getString("name");
String name = args.getString("name").trim();
String email = args.getString("email").trim();
String password = args.getString("password");
boolean check = args.getBoolean("check");
@ -227,6 +227,8 @@ public class FragmentQuickSetup extends FragmentBase {
throw new IllegalArgumentException(context.getString(R.string.title_no_email));
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches())
throw new IllegalArgumentException(context.getString(R.string.title_email_invalid, email));
if (TextUtils.isEmpty(password))
throw new IllegalArgumentException(context.getString(R.string.title_no_password));
String[] dparts = email.split("@");
EmailProvider provider = EmailProvider.fromDomain(context, dparts[1], EmailProvider.Discover.ALL);
@ -363,35 +365,44 @@ public class FragmentQuickSetup extends FragmentBase {
@Override
protected void onException(final Bundle args, Throwable ex) {
Log.e(ex);
if (ex instanceof IllegalArgumentException || ex instanceof UnknownHostException)
if (ex instanceof IllegalArgumentException || ex instanceof UnknownHostException) {
tvError.setText(ex.getMessage());
else
tvError.setVisibility(View.VISIBLE);
new Handler().post(new Runnable() {
@Override
public void run() {
scroll.smoothScrollTo(0, tvError.getBottom());
}
});
} else {
tvError.setText(Helper.formatThrowable(ex, false));
tvError.setVisibility(View.VISIBLE);
tvError.setVisibility(View.VISIBLE);
if (args.containsKey("link")) {
Uri uri = Uri.parse(args.getString("link"));
btnHelp.setTag(uri);
btnHelp.setVisibility(View.VISIBLE);
}
btnSupport.setVisibility(View.VISIBLE);
if (args.containsKey("documentation")) {
tvInstructions.setText(HtmlHelper.fromHtml(args.getString("documentation")));
tvInstructions.setVisibility(View.VISIBLE);
}
new Handler().post(new Runnable() {
@Override
public void run() {
if (args.containsKey("documentation"))
scroll.smoothScrollTo(0, tvInstructions.getBottom());
else
scroll.smoothScrollTo(0, btnSupport.getBottom());
if (args.containsKey("link")) {
Uri uri = Uri.parse(args.getString("link"));
btnHelp.setTag(uri);
btnHelp.setVisibility(View.VISIBLE);
}
});
btnSupport.setVisibility(View.VISIBLE);
if (args.containsKey("documentation")) {
tvInstructions.setText(HtmlHelper.fromHtml(args.getString("documentation")));
tvInstructions.setVisibility(View.VISIBLE);
}
new Handler().post(new Runnable() {
@Override
public void run() {
if (args.containsKey("documentation"))
scroll.smoothScrollTo(0, tvInstructions.getBottom());
else
scroll.smoothScrollTo(0, btnSupport.getBottom());
}
});
}
}
}.execute(this, args, "setup:quick");
}