Fix exception when bad domain on login screen
This commit is contained in:
parent
17b958f8ed
commit
4db1d54d79
|
@ -22,7 +22,6 @@ import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v7.widget.Toolbar;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
|
@ -34,6 +33,8 @@ import com.keylesspalace.tusky.entity.AppCredentials;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import butterknife.BindView;
|
||||||
|
import butterknife.ButterKnife;
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
|
@ -45,10 +46,14 @@ public class LoginActivity extends BaseActivity {
|
||||||
private static String OAUTH_SCOPES = "read write follow";
|
private static String OAUTH_SCOPES = "read write follow";
|
||||||
|
|
||||||
private SharedPreferences preferences;
|
private SharedPreferences preferences;
|
||||||
|
|
||||||
private String domain;
|
private String domain;
|
||||||
private String clientId;
|
private String clientId;
|
||||||
private String clientSecret;
|
private String clientSecret;
|
||||||
private EditText editText;
|
|
||||||
|
@BindView(R.id.edit_text_domain) EditText editText;
|
||||||
|
@BindView(R.id.button_login) Button button;
|
||||||
|
@BindView(R.id.no_account) TextView noAccount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Chain together the key-value pairs into a query string, for either appending to a URL or
|
* Chain together the key-value pairs into a query string, for either appending to a URL or
|
||||||
|
@ -117,6 +122,7 @@ public class LoginActivity extends BaseActivity {
|
||||||
* time. */
|
* time. */
|
||||||
String prefClientId = preferences.getString(domain + "/client_id", null);
|
String prefClientId = preferences.getString(domain + "/client_id", null);
|
||||||
String prefClientSecret = preferences.getString(domain + "/client_secret", null);
|
String prefClientSecret = preferences.getString(domain + "/client_secret", null);
|
||||||
|
|
||||||
if (prefClientId != null && prefClientSecret != null) {
|
if (prefClientId != null && prefClientSecret != null) {
|
||||||
clientId = prefClientId;
|
clientId = prefClientId;
|
||||||
clientSecret = prefClientSecret;
|
clientSecret = prefClientSecret;
|
||||||
|
@ -126,9 +132,7 @@ public class LoginActivity extends BaseActivity {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<AppCredentials> call, Response<AppCredentials> response) {
|
public void onResponse(Call<AppCredentials> call, Response<AppCredentials> response) {
|
||||||
if (!response.isSuccessful()) {
|
if (!response.isSuccessful()) {
|
||||||
editText.setError(
|
editText.setError(getString(R.string.error_failed_app_registration));
|
||||||
"This app could not obtain authentication from that server " +
|
|
||||||
"instance.");
|
|
||||||
Log.e(TAG, "App authentication failed. " + response.message());
|
Log.e(TAG, "App authentication failed. " + response.message());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -144,15 +148,17 @@ public class LoginActivity extends BaseActivity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<AppCredentials> call, Throwable t) {
|
public void onFailure(Call<AppCredentials> call, Throwable t) {
|
||||||
editText.setError(
|
editText.setError(getString(R.string.error_failed_app_registration));
|
||||||
"This app could not obtain authentication from that server " +
|
|
||||||
"instance.");
|
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
getApiFor(domain).authenticateApp(getString(R.string.app_name), getOauthRedirectUri(), OAUTH_SCOPES,
|
getApiFor(domain).authenticateApp(getString(R.string.app_name), getOauthRedirectUri(), OAUTH_SCOPES,
|
||||||
getString(R.string.app_website)).enqueue(callback);
|
getString(R.string.app_website)).enqueue(callback);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
editText.setError(getString(R.string.error_invalid_domain));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -161,25 +167,26 @@ public class LoginActivity extends BaseActivity {
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_login);
|
setContentView(R.layout.activity_login);
|
||||||
|
ButterKnife.bind(this);
|
||||||
|
|
||||||
if (savedInstanceState != null) {
|
if (savedInstanceState != null) {
|
||||||
domain = savedInstanceState.getString("domain");
|
domain = savedInstanceState.getString("domain");
|
||||||
clientId = savedInstanceState.getString("clientId");
|
clientId = savedInstanceState.getString("clientId");
|
||||||
clientSecret = savedInstanceState.getString("clientSecret");
|
clientSecret = savedInstanceState.getString("clientSecret");
|
||||||
}
|
}
|
||||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
||||||
setSupportActionBar(toolbar);
|
|
||||||
preferences = getSharedPreferences(
|
preferences = getSharedPreferences(
|
||||||
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
||||||
Button button = (Button) findViewById(R.id.button_login);
|
|
||||||
editText = (EditText) findViewById(R.id.edit_text_domain);
|
|
||||||
button.setOnClickListener(new View.OnClickListener() {
|
button.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
onButtonClick(editText);
|
onButtonClick(editText);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
TextView noAccount = (TextView) findViewById(R.id.no_account);
|
|
||||||
final Context context = this;
|
final Context context = this;
|
||||||
|
|
||||||
noAccount.setOnClickListener(new View.OnClickListener() {
|
noAccount.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
@ -233,11 +240,12 @@ public class LoginActivity extends BaseActivity {
|
||||||
* redirect that was given to the server. If so, its response is here! */
|
* redirect that was given to the server. If so, its response is here! */
|
||||||
Uri uri = getIntent().getData();
|
Uri uri = getIntent().getData();
|
||||||
String redirectUri = getOauthRedirectUri();
|
String redirectUri = getOauthRedirectUri();
|
||||||
|
|
||||||
if (uri != null && uri.toString().startsWith(redirectUri)) {
|
if (uri != null && uri.toString().startsWith(redirectUri)) {
|
||||||
// This should either have returned an authorization code or an error.
|
// This should either have returned an authorization code or an error.
|
||||||
String code = uri.getQueryParameter("code");
|
String code = uri.getQueryParameter("code");
|
||||||
String error = uri.getQueryParameter("error");
|
String error = uri.getQueryParameter("error");
|
||||||
final TextView errorText = (TextView) findViewById(R.id.text_error);
|
|
||||||
if (code != null) {
|
if (code != null) {
|
||||||
/* During the redirect roundtrip this Activity usually dies, which wipes out the
|
/* During the redirect roundtrip this Activity usually dies, which wipes out the
|
||||||
* instance variables, so they have to be recovered from where they were saved in
|
* instance variables, so they have to be recovered from where they were saved in
|
||||||
|
@ -264,15 +272,16 @@ public class LoginActivity extends BaseActivity {
|
||||||
editText.setError(t.getMessage());
|
editText.setError(t.getMessage());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
getApiFor(domain).fetchOAuthToken(clientId, clientSecret, redirectUri, code,
|
getApiFor(domain).fetchOAuthToken(clientId, clientSecret, redirectUri, code,
|
||||||
"authorization_code").enqueue(callback);
|
"authorization_code").enqueue(callback);
|
||||||
} else if (error != null) {
|
} else if (error != null) {
|
||||||
/* Authorization failed. Put the error response where the user can read it and they
|
/* Authorization failed. Put the error response where the user can read it and they
|
||||||
* can try again. */
|
* can try again. */
|
||||||
errorText.setText(error);
|
editText.setError(error);
|
||||||
} else {
|
} else {
|
||||||
// This case means a junk response was received somehow.
|
// This case means a junk response was received somehow.
|
||||||
errorText.setText(getString(R.string.error_authorization_unknown));
|
editText.setError(getString(R.string.error_authorization_unknown));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,5 +131,7 @@
|
||||||
<string name="action_mention">Mention</string>
|
<string name="action_mention">Mention</string>
|
||||||
<string name="tusky_api_url">http://tuskynotify.keylesspalace.com</string>
|
<string name="tusky_api_url">http://tuskynotify.keylesspalace.com</string>
|
||||||
<string name="notification_mention_format">%s mentioned you</string>
|
<string name="notification_mention_format">%s mentioned you</string>
|
||||||
|
<string name="error_invalid_domain">Invalid domain entered</string>
|
||||||
|
<string name="error_failed_app_registration">This app could not obtain authentication from that server instance.</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Reference in New Issue