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;
|
2016-07-06 14:15:16 +00:00
|
|
|
import android.text.Editable;
|
2016-07-01 11:11:36 +00:00
|
|
|
import android.text.TextUtils;
|
2016-07-06 14:15:16 +00:00
|
|
|
import android.text.TextWatcher;
|
2016-07-02 13:57:05 +00:00
|
|
|
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;
|
2016-07-02 16:27:32 +00:00
|
|
|
import com.android.volley.Response;
|
|
|
|
import com.android.volley.VolleyError;
|
2016-07-02 13:57:05 +00:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2016-07-01 11:11:36 +00:00
|
|
|
/**
|
|
|
|
* A login screen that offers login via email/password.
|
|
|
|
*/
|
2016-07-06 14:15:16 +00:00
|
|
|
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.
|
2016-07-02 16:27:32 +00:00
|
|
|
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;
|
2016-07-06 14:15:16 +00:00
|
|
|
private final String TAG = "[TSN/LoginActivity]";
|
2016-07-02 13:57:05 +00:00
|
|
|
private final String BASE_URL = "http://thesocialnotework-api.appspot.com/api";
|
2016-07-02 12:27:01 +00:00
|
|
|
private final String LOGIN_PATH = "/login";
|
2016-07-06 14:15:16 +00:00
|
|
|
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);
|
2016-07-06 14:15:16 +00:00
|
|
|
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
|
|
|
|
2016-07-06 14:15:16 +00:00
|
|
|
private void removeFocuse() {
|
2016-07-03 14:38:58 +00:00
|
|
|
// Remove Auto Focus from the Text Fields
|
2016-07-06 14:15:16 +00:00
|
|
|
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.
|
|
|
|
*/
|
2016-07-02 16:27:32 +00:00
|
|
|
|
|
|
|
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)) {
|
2016-07-02 16:27:32 +00:00
|
|
|
return false;
|
|
|
|
} else{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-07-01 21:28:39 +00:00
|
|
|
private void attemptLogin() {
|
2016-07-06 14:15:16 +00:00
|
|
|
MainActivity.showLoadingDialog(this, "Connecting", "Authenticating data");
|
2016-07-01 21:28:39 +00:00
|
|
|
mPasswordView.setError(null);
|
2016-07-02 16:27:32 +00:00
|
|
|
if (isParamsValid(mUsernameView.getText().toString(), mPasswordView.getText().toString())) {
|
2016-07-02 18:21:16 +00:00
|
|
|
|
2016-07-02 16:27:32 +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 16:27:32 +00:00
|
|
|
}
|
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 {
|
2016-07-06 14:15:16 +00:00
|
|
|
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");
|
2016-07-02 16:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Response.Listener<JSONObject> onLoginSuccess = new Response.Listener<JSONObject>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONObject response) {
|
2016-07-02 13:57:05 +00:00
|
|
|
try {
|
2016-07-02 18:21:16 +00:00
|
|
|
if(!response.isNull("user")) {
|
2016-07-02 16:27:32 +00:00
|
|
|
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);
|
2016-07-06 14:15:16 +00:00
|
|
|
MainActivity.dismissLoadingDialog();
|
2016-07-02 16:27:32 +00:00
|
|
|
startActivity(personalSpaceActivity);
|
2016-07-02 18:21:16 +00:00
|
|
|
} else {
|
2016-07-06 14:15:16 +00:00
|
|
|
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"));
|
2016-07-06 14:15:16 +00:00
|
|
|
|
2016-07-02 16:27:32 +00:00
|
|
|
}
|
|
|
|
}catch (Exception e) {
|
|
|
|
Log.e(TAG, "onLoginSuccess:" + e.getMessage());
|
2016-07-02 13:57:05 +00:00
|
|
|
}
|
2016-07-02 16:27:32 +00:00
|
|
|
}
|
|
|
|
};
|
2016-07-02 13:57:05 +00:00
|
|
|
|
2016-07-02 16:27:32 +00:00
|
|
|
Response.ErrorListener onLoginError = new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
2016-07-06 14:15:16 +00:00
|
|
|
MainActivity.dismissLoadingDialog();
|
2016-07-03 12:17:11 +00:00
|
|
|
Toast.makeText(self , "Username Or Password Incorrect", Toast.LENGTH_LONG).show();
|
2016-07-06 14:15:16 +00:00
|
|
|
//Clean texts
|
|
|
|
self.mUsernameView.getText().clear();
|
|
|
|
self.mPasswordView.getText().clear();
|
|
|
|
self.removeFocuse();
|
|
|
|
Log.d(TAG, "onErrorResponse: setting text to ''");
|
2016-07-02 16:27:32 +00:00
|
|
|
Log.d(TAG, "onLoginError: msg: " + error.getMessage());
|
2016-07-01 21:28:39 +00:00
|
|
|
}
|
2016-07-02 16:27:32 +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
|
|
|
}
|
|
|
|
|
2016-07-06 14:15:16 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 14:15:16 +00:00
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
2016-07-01 11:11:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-06 14:15:16 +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
|
|
|
}
|
2016-07-06 14:15:16 +00:00
|
|
|
else{
|
|
|
|
((TextView)findViewById(R.id.textView2)).setVisibility(View.VISIBLE);
|
|
|
|
mLoginButton.setEnabled(false);
|
2016-07-01 21:28:39 +00:00
|
|
|
}
|
2016-07-06 14:15:16 +00:00
|
|
|
}
|
2016-07-01 11:11:36 +00:00
|
|
|
|
2016-07-06 14:15:16 +00:00
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable editable) {
|
2016-07-01 11:11:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|