Add LoginActivity
This commit is contained in:
177
app/src/main/java/com/github/gotify/login/LoginActivity.java
Normal file
177
app/src/main/java/com/github/gotify/login/LoginActivity.java
Normal file
@@ -0,0 +1,177 @@
|
||||
package com.github.gotify.login;
|
||||
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ProgressBar;
|
||||
|
||||
import com.github.gotify.R;
|
||||
import com.github.gotify.Settings;
|
||||
import com.github.gotify.Utils;
|
||||
import com.github.gotify.api.Api;
|
||||
import com.github.gotify.api.Callback;
|
||||
import com.github.gotify.api.ClientFactory;
|
||||
import com.github.gotify.client.ApiClient;
|
||||
import com.github.gotify.client.ApiException;
|
||||
import com.github.gotify.client.api.TokenApi;
|
||||
import com.github.gotify.client.api.UserApi;
|
||||
import com.github.gotify.client.model.Client;
|
||||
import com.github.gotify.client.model.VersionInfo;
|
||||
import com.github.gotify.init.InitializationActivity;
|
||||
import com.github.gotify.log.Log;
|
||||
import com.github.gotify.log.UncaughtExceptionHandler;
|
||||
import com.squareup.okhttp.HttpUrl;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
import butterknife.OnTextChanged;
|
||||
|
||||
public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
@BindView(R.id.username)
|
||||
EditText usernameField;
|
||||
|
||||
@BindView(R.id.gotify_url)
|
||||
EditText urlField;
|
||||
|
||||
@BindView(R.id.password)
|
||||
EditText passwordField;
|
||||
|
||||
@BindView(R.id.checkurl)
|
||||
Button checkUrlButton;
|
||||
|
||||
@BindView(R.id.login)
|
||||
Button loginButton;
|
||||
|
||||
@BindView(R.id.checkurl_progress)
|
||||
ProgressBar checkUrlProgress;
|
||||
|
||||
@BindView(R.id.login_progress)
|
||||
ProgressBar loginProgress;
|
||||
|
||||
private Settings settings;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
UncaughtExceptionHandler.registerCurrentThread();
|
||||
setContentView(R.layout.activity_login);
|
||||
Log.i("Entering " + getClass().getSimpleName());
|
||||
ButterKnife.bind(this);
|
||||
settings = new Settings(this);
|
||||
}
|
||||
|
||||
@OnTextChanged(R.id.gotify_url)
|
||||
public void onUrlChange() {
|
||||
usernameField.setVisibility(View.INVISIBLE);
|
||||
passwordField.setVisibility(View.INVISIBLE);
|
||||
loginButton.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
@OnClick(R.id.checkurl)
|
||||
public void doCheckUrl() {
|
||||
String url = urlField.getText().toString();
|
||||
if (HttpUrl.parse(url) == null) {
|
||||
Utils.showSnackBar(LoginActivity.this, "Invalid URL (include http:// or https://)");
|
||||
return;
|
||||
}
|
||||
|
||||
checkUrlProgress.setVisibility(View.VISIBLE);
|
||||
checkUrlButton.setVisibility(View.INVISIBLE);
|
||||
|
||||
final String fixedUrl = url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
|
||||
|
||||
Api.withLogging(ClientFactory.versionApi(fixedUrl)::getVersionAsync)
|
||||
.handleInUIThread(this, onValidUrl(fixedUrl), onInvalidUrl(fixedUrl));
|
||||
}
|
||||
|
||||
private Callback.SuccessCallback<VersionInfo> onValidUrl(String url) {
|
||||
return (version) -> {
|
||||
settings.url(url);
|
||||
checkUrlProgress.setVisibility(View.INVISIBLE);
|
||||
checkUrlButton.setVisibility(View.VISIBLE);
|
||||
checkUrlButton.setText(getString(R.string.found_gotify_version, version.getVersion()));
|
||||
usernameField.setVisibility(View.VISIBLE);
|
||||
usernameField.requestFocus();
|
||||
passwordField.setVisibility(View.VISIBLE);
|
||||
loginButton.setVisibility(View.VISIBLE);
|
||||
};
|
||||
}
|
||||
|
||||
private Callback.ErrorCallback onInvalidUrl(String url) {
|
||||
return (exception) -> {
|
||||
checkUrlProgress.setVisibility(View.INVISIBLE);
|
||||
checkUrlButton.setVisibility(View.VISIBLE);
|
||||
Utils.showSnackBar(LoginActivity.this, versionError(url, exception));
|
||||
};
|
||||
}
|
||||
|
||||
@OnClick(R.id.login)
|
||||
public void doLogin() {
|
||||
String username = usernameField.getText().toString();
|
||||
String password = passwordField.getText().toString();
|
||||
|
||||
loginButton.setVisibility(View.INVISIBLE);
|
||||
loginProgress.setVisibility(View.VISIBLE);
|
||||
|
||||
ApiClient client = ClientFactory.basicAuth(settings.url(), username, password);
|
||||
Api.withLogging(new UserApi(client)::currentUserAsync)
|
||||
.handleInUIThread(this, (user) -> newClientDialog(client), this::onInvalidLogin);
|
||||
}
|
||||
|
||||
private void onInvalidLogin(ApiException e) {
|
||||
loginButton.setVisibility(View.VISIBLE);
|
||||
loginProgress.setVisibility(View.INVISIBLE);
|
||||
Utils.showSnackBar(this, getString(R.string.wronguserpw));
|
||||
}
|
||||
|
||||
private void newClientDialog(ApiClient client) {
|
||||
EditText clientName = new EditText(this);
|
||||
clientName.setText(Build.MODEL);
|
||||
|
||||
new AlertDialog.Builder(this)
|
||||
.setTitle(R.string.create_client_title)
|
||||
.setMessage(R.string.create_client_message)
|
||||
.setView(clientName)
|
||||
.setPositiveButton(R.string.create, doCreateClient(client, clientName))
|
||||
.setNegativeButton(R.string.cancel, this::onCancelClientDialog)
|
||||
.show();
|
||||
}
|
||||
|
||||
public DialogInterface.OnClickListener doCreateClient(ApiClient client, EditText nameProvider) {
|
||||
return (a, b) -> {
|
||||
Client newClient = new Client().name(nameProvider.getText().toString());
|
||||
Api.<Client>withLogging((cb) -> new TokenApi(client).createClientAsync(newClient, cb))
|
||||
.handleInUIThread(this, this::onCreatedClient, this::onFailedToCreateClient);
|
||||
};
|
||||
}
|
||||
|
||||
private void onCreatedClient(Client client) {
|
||||
settings.token(client.getToken());
|
||||
Utils.showSnackBar(this, getString(R.string.created_client));
|
||||
startActivity(new Intent(this, InitializationActivity.class));
|
||||
finish();
|
||||
}
|
||||
|
||||
private void onFailedToCreateClient(ApiException e) {
|
||||
Utils.showSnackBar(this, getString(R.string.create_client_failed));
|
||||
loginProgress.setVisibility(View.INVISIBLE);
|
||||
loginButton.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
private void onCancelClientDialog(DialogInterface dialog, int which) {
|
||||
loginProgress.setVisibility(View.INVISIBLE);
|
||||
loginButton.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
private String versionError(String url, ApiException exception) {
|
||||
return getString(R.string.version_failed, url + "/version", exception.getCode());
|
||||
}
|
||||
}
|
||||
145
app/src/main/res/layout/activity_login.xml
Normal file
145
app/src/main/res/layout/activity_login.xml
Normal file
@@ -0,0 +1,145 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:fillViewport="true">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@drawable/gradient"
|
||||
android:orientation="vertical"
|
||||
tools:context=".login.LoginActivity"
|
||||
tools:layout_editor_absoluteY="25dp">
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/checkurl_progress"
|
||||
android:layout_width="49dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_weight="1"
|
||||
android:minWidth="40dp"
|
||||
android:minHeight="40dp"
|
||||
android:visibility="invisible"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.501"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@+id/checkurl" />
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/login_progress"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:minWidth="40dp"
|
||||
android:minHeight="40dp"
|
||||
android:visibility="invisible"
|
||||
app:layout_constraintEnd_toEndOf="@+id/checkurl"
|
||||
app:layout_constraintStart_toStartOf="@+id/login"
|
||||
app:layout_constraintTop_toTopOf="@+id/login" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/gotify_logo_image"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="180dp"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="16dp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:srcCompat="@drawable/gotify"
|
||||
android:contentDescription="@string/gotify_logo" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/gotify_url"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="42dp"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginTop="8dp"
|
||||
android:ems="10"
|
||||
android:hint="@string/gotify_url"
|
||||
android:inputType="textUri"
|
||||
android:singleLine="true"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/gotify_logo_image"
|
||||
app:layout_constraintWidth_max="280dp"
|
||||
tools:text="Gotify URL" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/username"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="47dp"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:ems="10"
|
||||
android:hint="@string/username"
|
||||
android:inputType="textPersonName"
|
||||
android:visibility="invisible"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/checkurl"
|
||||
app:layout_constraintWidth_max="280dp"
|
||||
tools:text="Username"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/password"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="50dp"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:ems="10"
|
||||
android:hint="@string/password"
|
||||
android:inputType="textPassword"
|
||||
android:visibility="invisible"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/username"
|
||||
app:layout_constraintWidth_max="280dp"
|
||||
tools:text="Password" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/login"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:background="@color/colorPrimaryDark"
|
||||
android:textColor="@android:color/white"
|
||||
android:text="@string/login"
|
||||
android:visibility="invisible"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/password"
|
||||
app:layout_constraintWidth_max="280dp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/checkurl"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
app:layout_constraintWidth_max="280dp"
|
||||
android:background="@color/colorPrimaryDark"
|
||||
android:text="@string/check_url"
|
||||
android:textColor="@android:color/white"
|
||||
android:visibility="visible"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/gotify_url" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</ScrollView>
|
||||
Reference in New Issue
Block a user