the-social-notebook-android/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/LoginActivity.java

199 lines
7.2 KiB
Java
Raw Normal View History

2016-07-01 11:11:36 +00:00
package com.android_app.matan.ara.sagi.thesocialnotework;
2016-07-01 13:34:32 +00:00
import android.content.Intent;
2016-07-01 11:11:36 +00:00
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
2016-07-01 11:11:36 +00:00
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
2016-07-01 11:11:36 +00:00
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
2016-07-03 12:36:31 +00:00
import android.widget.LinearLayout;
2016-07-01 11:11:36 +00:00
import android.widget.TextView;
2016-07-03 12:17:11 +00:00
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import org.json.JSONObject;
2016-07-01 11:11:36 +00:00
/**
* A login screen that offers login via email/password.
*/
public class LoginActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher{
2016-07-01 11:11:36 +00:00
2016-07-01 21:28:39 +00:00
// UI references.
private EditText mUsernameView;
2016-07-01 21:28:39 +00:00
private EditText mPasswordView;
2016-07-03 14:38:58 +00:00
private TextView mRegisterButton;
private Button mLoginButton;
private final String TAG = "[TSN/LoginActivity]";
private final String BASE_URL = "http://thesocialnotework-api.appspot.com/api";
private final String LOGIN_PATH = "/login";
private LoginActivity self; //this
2016-07-03 12:36:31 +00:00
protected LinearLayout layout;
2016-07-01 21:28:39 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
2016-07-03 12:17:11 +00:00
this.self = this;
2016-07-03 12:36:31 +00:00
this.layout = (LinearLayout) findViewById(R.id.layout);
this.mUsernameView = (EditText) findViewById(R.id.al_username);
this.mUsernameView.addTextChangedListener(this);
this.mPasswordView = (EditText) findViewById(R.id.al_password);
this.mPasswordView.addTextChangedListener(this);
removeFocuse();
this.mRegisterButton = (TextView) findViewById(R.id.al_register_button);
this.mRegisterButton.setOnClickListener(this);
this.mLoginButton = (Button) findViewById(R.id.al_login_button);
this.mLoginButton.setOnClickListener(this);
this.mLoginButton.setEnabled(false);
}
2016-07-03 14:38:58 +00:00
private void removeFocuse() {
2016-07-03 14:38:58 +00:00
// Remove Auto Focus from the Text Fields
mUsernameView.clearFocus();
mPasswordView.clearFocus();
2016-07-03 14:38:58 +00:00
layout.setFocusable(true);
layout.setFocusableInTouchMode(true);
2016-07-01 11:11:36 +00:00
}
2016-07-01 21:28:39 +00:00
/**
* 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 boolean isParamsValid(String username, String password) {
Log.d(TAG, "user: " + username);
Log.d(TAG, "pwd: " + password);
2016-07-02 17:21:29 +00:00
if(TextUtils.isEmpty(username) || !isUsernameValid(username) || TextUtils.isEmpty(password) || !isPasswordValid(password)) {
return false;
} else{
return true;
}
}
2016-07-01 21:28:39 +00:00
private void attemptLogin() {
MainActivity.showLoadingDialog(this, "Connecting", "Authenticating data");
2016-07-01 21:28:39 +00:00
mPasswordView.setError(null);
if (isParamsValid(mUsernameView.getText().toString(), mPasswordView.getText().toString())) {
2016-07-02 18:21:16 +00:00
String username = mUsernameView.getText().toString();
String password = mPasswordView.getText().toString();
2016-07-01 11:11:36 +00:00
2016-07-03 14:38:58 +00:00
boolean cancel = false;
2016-07-02 17:21:29 +00:00
// http request register
JSONObject tempJson = new JSONObject();
try {
tempJson.put("username", username);
tempJson.put("password", password);
} catch (Exception e) {
Log.d(TAG, e.toString());
}
2016-07-02 17:21:29 +00:00
VolleyUtilSingleton.getInstance(LoginActivity.this).post(BASE_URL + LOGIN_PATH, tempJson, onLoginSuccess, onLoginError);
2016-07-01 21:28:39 +00:00
} else {
MainActivity.dismissLoadingDialog();
2016-07-02 18:21:16 +00:00
Log.d(TAG, "Invalid params - make sure username exist & password is 4 characters or more");
}
}
Response.Listener<JSONObject> onLoginSuccess = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
2016-07-02 18:21:16 +00:00
if(!response.isNull("user")) {
Log.e(TAG, "onLoginSuccess => user exist"); // TODO: REMOVE console
2016-07-05 14:38:58 +00:00
Intent personalSpaceActivity = new Intent(LoginActivity.this, MainActivity.class);
2016-07-02 17:21:29 +00:00
Bundle loginUserBundle = new Bundle();
loginUserBundle.putString("user_id", response.getJSONObject("user").getString("id"));
personalSpaceActivity.putExtras(loginUserBundle);
MainActivity.dismissLoadingDialog();
startActivity(personalSpaceActivity);
2016-07-02 18:21:16 +00:00
} else {
MainActivity.dismissLoadingDialog();
Toast.makeText(self, "Username or Password are incorrect", Toast.LENGTH_LONG).show();
self.mUsernameView.getText().clear();
self.mPasswordView.getText().clear();
self.removeFocuse();
((TextView)findViewById(R.id.textView2)).setVisibility(View.INVISIBLE);
2016-07-02 18:21:16 +00:00
Log.d(TAG, "No such user, " + response.get("user"));
}
}catch (Exception e) {
Log.e(TAG, "onLoginSuccess:" + e.getMessage());
}
}
};
Response.ErrorListener onLoginError = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
MainActivity.dismissLoadingDialog();
2016-07-03 12:17:11 +00:00
Toast.makeText(self , "Username Or Password Incorrect", Toast.LENGTH_LONG).show();
//Clean texts
self.mUsernameView.getText().clear();
self.mPasswordView.getText().clear();
self.removeFocuse();
Log.d(TAG, "onErrorResponse: setting text to ''");
Log.d(TAG, "onLoginError: msg: " + error.getMessage());
2016-07-01 21:28:39 +00:00
}
};
2016-07-01 11:11:36 +00:00
2016-07-02 17:21:29 +00:00
private boolean isUsernameValid(String username) {
return username.length() > 0;
2016-07-01 11:11:36 +00:00
}
2016-07-01 21:28:39 +00:00
private boolean isPasswordValid(String password) {
2016-07-02 18:21:16 +00:00
return password.length() > 3;
2016-07-01 11:11:36 +00:00
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.al_login_button:
//Do Login
Log.d(TAG, "Login.......");
attemptLogin();
break;
case R.id.al_register_button:
//Do Register
Log.d(TAG, "going to Register...page");
Intent registerActivity = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(registerActivity);
break;
2016-07-01 11:11:36 +00:00
}
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
2016-07-01 11:11:36 +00:00
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(isParamsValid(mUsernameView.getText().toString(), mPasswordView.getText().toString())) {
((TextView)findViewById(R.id.textView2)).setVisibility(View.INVISIBLE);
mLoginButton.setEnabled(true);
2016-07-01 11:11:36 +00:00
}
else{
((TextView)findViewById(R.id.textView2)).setVisibility(View.VISIBLE);
mLoginButton.setEnabled(false);
2016-07-01 21:28:39 +00:00
}
}
2016-07-01 11:11:36 +00:00
@Override
public void afterTextChanged(Editable editable) {
2016-07-01 11:11:36 +00:00
}
}