2016-07-03 20:25:58 +00:00
|
|
|
package com.android_app.matan.ara.sagi.thesocialnotework;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
import android.widget.Toast;
|
|
|
|
import com.android.volley.Response;
|
|
|
|
import com.android.volley.VolleyError;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2016-07-06 11:00:51 +00:00
|
|
|
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
|
2016-07-03 20:25:58 +00:00
|
|
|
|
|
|
|
private EditText mUsernameView;
|
|
|
|
private EditText mPasswordView;
|
|
|
|
private EditText mEmailView;
|
|
|
|
private Button registerButton;
|
|
|
|
private Button testBtn;
|
|
|
|
private RegisterActivity self;
|
|
|
|
|
|
|
|
protected RelativeLayout layout;
|
|
|
|
private final String TAG = "Register Activity";
|
|
|
|
|
|
|
|
private final String BASE_URL = "http://thesocialnotework-api.appspot.com/api";
|
|
|
|
private final String REG_PATH = "/register";
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_register);
|
|
|
|
|
2016-07-06 14:15:16 +00:00
|
|
|
this.mUsernameView = (EditText) findViewById(R.id.ra_username);
|
|
|
|
this.mPasswordView = (EditText) findViewById(R.id.ra_password);
|
|
|
|
this.mEmailView = (EditText) findViewById(R.id.ra_email);
|
2016-07-03 20:25:58 +00:00
|
|
|
|
|
|
|
this.self = this;
|
|
|
|
this.layout = (RelativeLayout) findViewById(R.id.ra_layout);
|
|
|
|
|
|
|
|
// Remove Auto Focus from the Text Fields
|
2016-07-06 14:15:16 +00:00
|
|
|
this.layout.setFocusable(true);
|
|
|
|
this.layout.setFocusableInTouchMode(true);
|
|
|
|
// Buttons And Listeners
|
|
|
|
this.testBtn = (Button) findViewById(R.id.btn_cancel);
|
|
|
|
this.testBtn.setOnClickListener(this);
|
|
|
|
this.registerButton = (Button) findViewById(R.id.ra_register_button);
|
|
|
|
this.registerButton.setOnClickListener(this);
|
2016-07-03 20:25:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isUsernameValid(String username) {
|
2016-07-06 11:00:51 +00:00
|
|
|
return !TextUtils.isEmpty(username) && username.length() > 0;
|
2016-07-03 20:25:58 +00:00
|
|
|
}
|
|
|
|
private boolean isPasswordValid(String password) {
|
2016-07-06 11:00:51 +00:00
|
|
|
return !TextUtils.isEmpty(password) && password.length() > 3;
|
2016-07-03 20:25:58 +00:00
|
|
|
}
|
|
|
|
private boolean isEmailValid(String email) {
|
2016-07-06 11:00:51 +00:00
|
|
|
if(TextUtils.isEmpty(email))
|
|
|
|
return false;
|
2016-07-03 20:25:58 +00:00
|
|
|
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
|
|
|
|
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
|
2016-07-06 14:15:16 +00:00
|
|
|
Matcher matcher = pattern.matcher(email);
|
2016-07-06 11:00:51 +00:00
|
|
|
return matcher.matches();
|
2016-07-03 20:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isParamsValid(String username, String password, String email) {
|
2016-07-06 11:00:51 +00:00
|
|
|
return (isUsernameValid(username) && isPasswordValid(password) && isEmailValid(email));
|
2016-07-03 20:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void attemptRegister() {
|
2016-07-07 21:06:09 +00:00
|
|
|
Utils.showLoadingDialog(this, "Registering", "Please Wait...");
|
2016-07-03 20:25:58 +00:00
|
|
|
Log.d(TAG, "in attemptRegister: Registering..");
|
|
|
|
if (isParamsValid(mUsernameView.getText().toString(), mPasswordView.getText().toString(), mEmailView.getText().toString())) {
|
|
|
|
Log.d(TAG, "params are valid");
|
|
|
|
String username = mUsernameView.getText().toString();
|
|
|
|
String password = mPasswordView.getText().toString();
|
|
|
|
String email = mEmailView.getText().toString();
|
|
|
|
// http request register
|
|
|
|
JSONObject tempJson = new JSONObject();
|
|
|
|
try {
|
|
|
|
tempJson.put("username", username);
|
|
|
|
tempJson.put("password", password);
|
|
|
|
tempJson.put("email", email);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d(TAG, e.toString());
|
|
|
|
}
|
2016-07-04 20:10:54 +00:00
|
|
|
Log.d(TAG,"JSON: "+tempJson.toString());
|
2016-07-03 20:25:58 +00:00
|
|
|
VolleyUtilSingleton.getInstance(RegisterActivity.this).post(BASE_URL + REG_PATH, tempJson, onRegisterSuccess, onRegisterError);
|
|
|
|
} else {
|
2016-07-07 21:06:09 +00:00
|
|
|
Utils.dismissLoadingDialog();
|
2016-07-03 20:25:58 +00:00
|
|
|
Log.d(TAG, "Invalid params - make sure username exist, password is 4 characters or more & email is valid");
|
2016-07-06 11:00:51 +00:00
|
|
|
Toast.makeText(this, "Make Sure tou have entered a valid email. password at least 4 chars", Toast.LENGTH_LONG).show();
|
2016-07-03 20:25:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Response.Listener<JSONObject> onRegisterSuccess = new Response.Listener<JSONObject>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONObject response) {
|
2016-07-06 14:15:16 +00:00
|
|
|
Log.d(TAG,"response: "+ response.toString());
|
2016-07-07 21:06:09 +00:00
|
|
|
Utils.dismissLoadingDialog();
|
2016-07-03 20:25:58 +00:00
|
|
|
try {
|
2016-07-04 20:10:54 +00:00
|
|
|
if(response.getString("message").equals("created")) {
|
|
|
|
Log.d(TAG, "onRegisterSuccess => user created"); // TODO: REMOVE console
|
2016-07-03 20:25:58 +00:00
|
|
|
Intent loginActivity = new Intent(RegisterActivity.this, LoginActivity.class);
|
2016-07-06 11:00:51 +00:00
|
|
|
Toast.makeText(self, "You are now a social notework member - You May Login...", Toast.LENGTH_LONG).show();
|
2016-07-03 20:25:58 +00:00
|
|
|
startActivity(loginActivity);
|
|
|
|
} else {
|
2016-07-06 11:00:51 +00:00
|
|
|
Toast.makeText(self , "Username is already taken. maybe: " + mUsernameView.getText().toString()+"_666 ? :)", Toast.LENGTH_LONG).show();
|
2016-07-04 20:10:54 +00:00
|
|
|
Log.d(TAG, "Cannot create user, " + response.getString("message"));
|
2016-07-03 20:25:58 +00:00
|
|
|
}
|
|
|
|
}catch (Exception e) {
|
|
|
|
Log.e(TAG, "onRegisterSuccess:" + e.getMessage());
|
2016-07-06 11:00:51 +00:00
|
|
|
|
2016-07-03 20:25:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Response.ErrorListener onRegisterError = new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
2016-07-07 21:06:09 +00:00
|
|
|
Utils.dismissLoadingDialog();
|
2016-07-06 11:00:51 +00:00
|
|
|
Toast.makeText(self , "Username is already taken. maybe: " + mUsernameView.getText().toString()+"_666 ? :)", Toast.LENGTH_LONG).show();
|
2016-07-03 20:25:58 +00:00
|
|
|
Log.d(TAG, "onRegisterError: msg: " + error.getMessage());
|
|
|
|
}
|
|
|
|
};
|
2016-07-06 11:00:51 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
switch(view.getId()){
|
|
|
|
case R.id.ra_register_button:
|
|
|
|
attemptRegister();
|
|
|
|
break;
|
|
|
|
case R.id.btn_cancel:
|
|
|
|
returnToLogin();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void returnToLogin(){
|
|
|
|
Intent i = new Intent(RegisterActivity.this, LoginActivity.class);
|
|
|
|
startActivity(i);
|
|
|
|
finish();
|
|
|
|
}
|
2016-07-03 20:25:58 +00:00
|
|
|
}
|