2018-02-12 02:08:56 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Paul Schaub
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2018-02-10 17:34:01 +01:00
|
|
|
package de.vanitasvitae.slam.mvp.view;
|
2018-01-27 03:33:50 +01:00
|
|
|
|
2018-08-01 11:43:18 +02:00
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Context;
|
2018-01-27 03:33:50 +01:00
|
|
|
import android.content.Intent;
|
2018-08-01 11:43:18 +02:00
|
|
|
import android.content.ServiceConnection;
|
2018-01-27 03:33:50 +01:00
|
|
|
import android.os.Bundle;
|
2018-08-01 11:43:18 +02:00
|
|
|
import android.os.IBinder;
|
2018-01-27 03:33:50 +01:00
|
|
|
import android.support.design.widget.TextInputEditText;
|
2018-02-01 08:24:08 +01:00
|
|
|
import android.support.design.widget.TextInputLayout;
|
2018-01-27 03:33:50 +01:00
|
|
|
import android.support.v7.widget.Toolbar;
|
2018-02-01 08:24:08 +01:00
|
|
|
import android.text.Editable;
|
2018-01-27 03:33:50 +01:00
|
|
|
import android.view.Menu;
|
2018-02-01 08:24:08 +01:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.WindowManager;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ProgressBar;
|
2018-01-27 03:33:50 +01:00
|
|
|
import android.widget.TextView;
|
2018-02-01 08:24:08 +01:00
|
|
|
import android.widget.Toast;
|
2018-01-27 03:33:50 +01:00
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
import butterknife.OnClick;
|
2019-12-12 15:09:41 +01:00
|
|
|
import de.vanitasvitae.slam.util.AbstractTextWatcher;
|
|
|
|
import de.vanitasvitae.slam.util.EditorActionDoneListener;
|
2018-01-27 03:33:50 +01:00
|
|
|
import de.vanitasvitae.slam.R;
|
2018-02-12 02:08:56 +01:00
|
|
|
import de.vanitasvitae.slam.mvp.PresenterFactory;
|
2018-02-10 17:34:01 +01:00
|
|
|
import de.vanitasvitae.slam.mvp.view.abstr.ThemedAppCompatActivity;
|
|
|
|
import de.vanitasvitae.slam.mvp.contracts.LoginContract;
|
2018-08-01 11:43:18 +02:00
|
|
|
import de.vanitasvitae.slam.service.SlamXmppService;
|
2018-01-27 03:33:50 +01:00
|
|
|
|
2018-08-01 11:43:18 +02:00
|
|
|
public class LoginActivity extends ThemedAppCompatActivity implements LoginContract.View, ServiceConnection {
|
2018-02-01 08:24:08 +01:00
|
|
|
|
|
|
|
// Presenter of this view
|
|
|
|
private LoginContract.Presenter presenter;
|
2018-01-27 03:33:50 +01:00
|
|
|
|
|
|
|
@BindView(R.id.toolbar)
|
|
|
|
Toolbar toolbar;
|
|
|
|
|
2018-02-01 08:24:08 +01:00
|
|
|
@BindView(R.id.progressbar)
|
|
|
|
ProgressBar progressBar;
|
|
|
|
|
|
|
|
@BindView(R.id.login_username_layout)
|
|
|
|
TextInputLayout inputUsernameLayout;
|
|
|
|
|
2018-01-27 03:33:50 +01:00
|
|
|
@BindView(R.id.login_username)
|
|
|
|
TextInputEditText inputUsername;
|
|
|
|
|
2018-02-01 08:24:08 +01:00
|
|
|
@BindView(R.id.login_password_layout)
|
|
|
|
TextInputLayout inputPasswordLayout;
|
|
|
|
|
2018-01-27 03:33:50 +01:00
|
|
|
@BindView(R.id.login_password)
|
|
|
|
TextInputEditText inputPassword;
|
|
|
|
|
2018-02-01 08:24:08 +01:00
|
|
|
@BindView(R.id.button_login)
|
|
|
|
Button buttonLogin;
|
|
|
|
|
2018-08-01 11:43:18 +02:00
|
|
|
private SlamXmppService slamService;
|
|
|
|
|
2018-02-12 02:08:56 +01:00
|
|
|
public LoginActivity() {
|
|
|
|
super();
|
2018-08-01 11:43:18 +02:00
|
|
|
setPresenter(PresenterFactory.getInstance().createLoginPresenter(this));
|
2018-02-12 02:08:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-27 03:33:50 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_login);
|
|
|
|
ButterKnife.bind(this);
|
|
|
|
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
2018-02-01 08:24:08 +01:00
|
|
|
// attempt login on editor action done
|
|
|
|
inputPassword.setOnEditorActionListener(new EditorActionDoneListener() {
|
2018-01-27 03:33:50 +01:00
|
|
|
@Override
|
2018-02-01 08:24:08 +01:00
|
|
|
public void onEditorActionDone(TextView v) {
|
|
|
|
login();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-02-10 17:34:01 +01:00
|
|
|
inputUsername.addTextChangedListener(new AbstractTextWatcher() {
|
2018-02-01 08:24:08 +01:00
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
presenter.jidChanged(s.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-02-10 17:34:01 +01:00
|
|
|
inputPassword.addTextChangedListener(new AbstractTextWatcher() {
|
2018-02-01 08:24:08 +01:00
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
presenter.passwordChanged(s.toString());
|
2018-01-27 03:33:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-01 11:43:18 +02:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
// Start xmpp service
|
|
|
|
Intent serviceIntent = new Intent(this, SlamXmppService.class);
|
|
|
|
bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
unbindService(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setPresenter(LoginContract.Presenter presenter) {
|
|
|
|
this.presenter = presenter;
|
|
|
|
}
|
|
|
|
|
2018-01-27 03:33:50 +01:00
|
|
|
@OnClick(R.id.button_login)
|
|
|
|
void login() {
|
2018-08-01 11:43:18 +02:00
|
|
|
Intent serviceIntent = new Intent(getApplicationContext(), SlamXmppService.class);
|
|
|
|
getApplicationContext().startService(serviceIntent);
|
2018-02-01 08:24:08 +01:00
|
|
|
presenter.loginClicked();
|
2018-01-27 03:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.menu_login, menu);
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-01 08:24:08 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showInvalidJidError() {
|
2018-02-13 21:02:46 +01:00
|
|
|
inputUsernameLayout.setError(getResources().getText(R.string.login__error_invalid_jid));
|
2018-02-01 08:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showInvalidPasswordError() {
|
2018-02-13 21:02:46 +01:00
|
|
|
inputPasswordLayout.setError(getResources().getText(R.string.login__error_invalid_password));
|
2018-02-01 08:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showIncorrectPasswordError() {
|
2018-02-13 21:02:46 +01:00
|
|
|
inputPasswordLayout.setError(getResources().getText(R.string.login__error_incorrect_password));
|
2018-02-01 08:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void hideInvalidJidError() {
|
|
|
|
inputUsernameLayout.setError(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void hidePasswordError() {
|
|
|
|
inputPasswordLayout.setError(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showServerNotFoundError() {
|
2018-02-13 21:02:46 +01:00
|
|
|
Toast.makeText(this, R.string.login__error_server_not_found, Toast.LENGTH_LONG).show();
|
2018-02-01 08:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showProgressIndicator() {
|
|
|
|
progressBar.setVisibility(View.VISIBLE);
|
|
|
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
|
|
|
|
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void hideProgressIndicator() {
|
|
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void navigateToMainActivity() {
|
|
|
|
startActivity(new Intent(this, MainActivity.class));
|
|
|
|
finish();
|
|
|
|
}
|
2018-08-01 11:43:18 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void disableLoginButton() {
|
|
|
|
buttonLogin.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void enableLoginButton() {
|
|
|
|
buttonLogin.setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
|
|
|
SlamXmppService.SlamBinder binder = (SlamXmppService.SlamBinder) service;
|
|
|
|
slamService = binder.getService();
|
|
|
|
presenter.bindService(slamService);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onServiceDisconnected(ComponentName name) {
|
|
|
|
slamService = null;
|
|
|
|
presenter.unbindService();
|
|
|
|
}
|
2018-01-27 03:33:50 +01:00
|
|
|
}
|
|
|
|
|