use of email.schaal.ocreader.util.LoginError in project ocreader by schaal.
the class LoginActivity method attemptLogin.
/**
* Attempts to sign in or register the account specified by the login form.
* If there are form errors (invalid email, missing fields, etc.), the
* errors are presented and no actual login attempt is made.
*/
private void attemptLogin() {
// Reset errors.
binding.username.setError(null);
binding.password.setError(null);
binding.url.setError(null);
LoginError error = null;
// Store values at the time of the login attempt.
String username = binding.username.getText().toString();
String password = binding.password.getText().toString();
String urlString = binding.url.getText().toString();
if (!urlString.startsWith("https://") && !urlString.startsWith("http://")) {
urlString = "https://" + urlString;
binding.url.setText(urlString);
binding.url.setTag(SCHEME_ADDED);
}
final HttpUrl url = HttpUrl.parse(urlString);
// Check for a valid username
if (TextUtils.isEmpty(username)) {
error = new LoginError(LoginError.Section.USER, getString(R.string.error_field_required));
}
if (TextUtils.isEmpty(password)) {
error = new LoginError(LoginError.Section.PASSWORD, getString(R.string.error_field_required));
}
if (TextUtils.isEmpty(urlString)) {
error = new LoginError(LoginError.Section.URL, getString(R.string.error_field_required));
} else if (url == null) {
error = new LoginError(LoginError.Section.URL, getString(R.string.error_incorrect_url));
} else if (binding.signInButton.getTag() == null && !url.isHttps()) {
error = new LoginError(LoginError.Section.URL, getString(R.string.error_insecure_connection));
updateSecureState(false);
}
if (error != null) {
// There was an error; don't attempt login and focus the first
// form field with an error.
showError(error);
} else {
// Show a progress spinner, and kick off a background task to
// perform the user login attempt.
showProgress(true);
final HttpUrl fixedUrl = url.newBuilder().addPathSegment("").build();
API.login(this, fixedUrl, username, password, new API.APICallback<Status, LoginError>() {
@Override
public void onSuccess(Status status) {
Intent data = new Intent(Intent.ACTION_VIEW);
data.putExtra(EXTRA_IMPROPERLY_CONFIGURED_CRON, status.isImproperlyConfiguredCron());
setResult(RESULT_OK);
finish();
}
@Override
public void onFailure(LoginError loginError) {
if (SCHEME_ADDED.equals(binding.url.getTag())) {
binding.url.setText(fixedUrl.newBuilder().scheme("http").toString());
binding.url.setTag(null);
attemptLogin();
} else {
showError(loginError);
}
showProgress(false);
}
});
}
}
use of email.schaal.ocreader.util.LoginError in project ocreader by schaal.
the class API method login.
public static void login(final Context context, final HttpUrl baseUrl, final String username, final String password, final APICallback<Status, LoginError> loginCallback) {
final HttpManager httpManager = new HttpManager(username, password, baseUrl);
final HttpUrl resolvedBaseUrl = baseUrl.resolve("");
if (resolvedBaseUrl == null) {
loginCallback.onFailure(new LoginError("Couldn't parse URL"));
return;
}
final Moshi moshi = new Moshi.Builder().build();
final JsonAdapter<NewsError> errorJsonAdapter = moshi.adapter(NewsError.class);
final Retrofit retrofit = new Retrofit.Builder().baseUrl(resolvedBaseUrl).client(httpManager.getClient()).addConverterFactory(MoshiConverterFactory.create(moshi)).build();
final CommonAPI commonAPI = retrofit.create(CommonAPI.class);
commonAPI.apiLevels().enqueue(new Callback<APILevels>() {
@Override
public void onResponse(@NonNull Call<APILevels> call, @NonNull Response<APILevels> response) {
if (response.isSuccessful()) {
loginInstance = null;
final APILevels apiLevels = response.body();
final Level apiLevel = apiLevels != null ? apiLevels.highestSupportedApi() : null;
loginInstance = Level.getAPI(context, apiLevel);
if (apiLevel == null) {
loginCallback.onFailure(new LoginError(context.getString(R.string.error_not_compatible)));
} else {
loginInstance.setupApi(httpManager);
loginInstance.metaData(new Callback<Status>() {
@Override
public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
if (response.isSuccessful()) {
final Status status = response.body();
final Version version = status != null ? status.getVersion() : null;
if (version != null && MIN_VERSION.lessThanOrEqualTo(version)) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(Preferences.USERNAME.getKey(), username).putString(Preferences.PASSWORD.getKey(), password).putString(Preferences.URL.getKey(), resolvedBaseUrl.toString()).putString(Preferences.SYS_DETECTED_API_LEVEL.getKey(), apiLevel.getLevel()).apply();
instance = loginInstance;
loginInstance = null;
loginCallback.onSuccess(status);
} else {
if (version != null) {
Log.d(TAG, String.format("Nextcloud News version is less than minimally supported version: %s < %s", version.toString(), MIN_VERSION.toString()));
loginCallback.onFailure(new LoginError(context.getString(R.string.ncnews_too_old, MIN_VERSION.toString())));
} else {
Log.d(TAG, "Couldn't parse Nextcloud News version");
loginCallback.onFailure(new LoginError(context.getString(R.string.failed_detect_nc_version)));
}
}
} else {
String message = getErrorMessage(errorJsonAdapter, response);
Log.d(TAG, "Metadata call failed with error: " + message);
loginCallback.onFailure(LoginError.getError(context, response.code(), message));
}
}
@Override
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
Log.e(TAG, "Failed to log in", t);
loginCallback.onFailure(LoginError.getError(context, t));
}
});
}
} else {
Log.d(TAG, "API level call failed with error: " + response.code() + " " + getErrorMessage(errorJsonAdapter, response));
// Either nextcloud news is not installed or version prior 8.8.0
loginCallback.onFailure(LoginError.getError(context, response.code(), context.getString(R.string.ncnews_too_old, MIN_VERSION.toString())));
}
}
@Override
public void onFailure(@NonNull Call<APILevels> call, @NonNull Throwable t) {
Log.e(TAG, "Failed to log in", t);
loginCallback.onFailure(LoginError.getError(context, t));
}
});
}
use of email.schaal.ocreader.util.LoginError in project ocreader by schaal.
the class API method get.
public static void get(Context context, final InstanceReadyCallback callback) {
if (instance == null) {
Level detectedApiLevel = Level.get(Preferences.SYS_DETECTED_API_LEVEL.getString(PreferenceManager.getDefaultSharedPreferences(context)));
if (detectedApiLevel != null) {
instance = Level.getAPI(context, detectedApiLevel);
callback.onInstanceReady(instance);
} else {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
API.login(context, HttpUrl.parse(Preferences.URL.getString(preferences)), Preferences.USERNAME.getString(preferences), Preferences.PASSWORD.getString(preferences), new APICallback<Status, LoginError>() {
@Override
public void onSuccess(Status success) {
callback.onInstanceReady(instance);
}
@Override
public void onFailure(LoginError failure) {
callback.onLoginFailure(failure.getThrowable());
}
});
}
} else {
callback.onInstanceReady(instance);
}
}
Aggregations