Back to project page Wardrobe_app.
The source code is released under:
Apache License
If you think the Android project Wardrobe_app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.android.busolo.apps.wardrobe.engine; // w w w. jav a 2s.c o m import android.app.Activity; import android.content.Intent; import android.graphics.drawable.Drawable; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.support.v4.app.Fragment; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.util.Log; import android.util.Patterns; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.android.busolo.apps.wardrobe.utils.AccountUtils; import com.android.busolo.apps.wardrobe.utils.PrefUtils; import com.android.busolo.apps.wardrobe.utils.VolleyAppController; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.VolleyLog; import com.android.volley.toolbox.JsonObjectRequest; import org.json.JSONObject; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by james on 1/07/14. */ public class LoginFragment extends Fragment { static String LOG_TAG = "LoginFragment"; EditText text_login_email; EditText text_login_password; Button btn_login_cancel; Button btn_login_login; TextView text_login_resetPassword; ProgressBar loading; View section_login; String login_username; String login_password; Activity activity; public LoginFragment() {} public void onAttach(Activity activity) { super.onAttach(activity); this.activity = activity; } @Override public void onResume() { super.onResume(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_login, container, false); text_login_email = (EditText) rootView.findViewById(R.id.text_login_userName); text_login_password = (EditText) rootView.findViewById(R.id.text_login_password); btn_login_cancel = (Button) rootView.findViewById(R.id.btn_login_cancel); btn_login_login = (Button) rootView.findViewById(R.id.btn_login_login); text_login_resetPassword = (TextView) rootView.findViewById(R.id.text_login_resetPassword); loading = (ProgressBar)rootView.findViewById(R.id.loading); section_login = (LinearLayout)rootView.findViewById(R.id.section_login); btn_login_login.setOnClickListener(onClickListener); btn_login_cancel.setOnClickListener(onClickListener); text_login_resetPassword.setOnClickListener(onClickListener); text_login_email.addTextChangedListener(new watcher(text_login_email)); text_login_password.addTextChangedListener(new watcher(text_login_password)); return rootView; } private class watcher implements TextWatcher { private EditText editText; public watcher(){} public watcher(EditText editText){ this.editText = editText; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.e(LOG_TAG, "B4 text changed"); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.e(LOG_TAG, "on text changed"); } @Override public void afterTextChanged(Editable s) { Log.e(LOG_TAG, "after text changed"); Drawable email_disable = activity.getResources().getDrawable(R.drawable.ic_email); Drawable email_green = activity.getResources().getDrawable(R.drawable.ic_email); Drawable email_red = activity.getResources().getDrawable(R.drawable.ic_email); Drawable pass_disable = activity.getResources().getDrawable(R.drawable.ic_password); Drawable pass_green = activity.getResources().getDrawable(R.drawable.ic_password); Drawable pass_red = activity.getResources().getDrawable(R.drawable.ic_password); if(editText == text_login_email){ Log.e(LOG_TAG, "after text changed_email"); if(!TextUtils.isEmpty(text_login_email.getText()) && isEmail(text_login_email.getText().toString()) ) { text_login_email.setCompoundDrawablesWithIntrinsicBounds(email_green, null, null, null); btn_login_login.setEnabled(true); }else{ text_login_email.setCompoundDrawablesWithIntrinsicBounds(email_red, null, null, null); btn_login_login.setEnabled(false); } }else if(editText == text_login_password){ Log.e(LOG_TAG, "after text changed_pass"); if(!TextUtils.isEmpty(text_login_password.getText()) && text_login_password.getText().length() >= 3) { text_login_password.setCompoundDrawablesWithIntrinsicBounds(pass_green, null, null, null); btn_login_login.setEnabled(true); }else{ text_login_password.setCompoundDrawablesWithIntrinsicBounds(pass_red, null, null, null); btn_login_login.setEnabled(false); } } } } /** * On click listener for buttons in the activity */ private View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(final View v) { switch (v.getId()) { case R.id.btn_login_login: ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(activity.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (activeNetwork == null || !activeNetwork.isConnected()) { Toast.makeText(activity, R.string.no_connection_cant_login, Toast.LENGTH_SHORT).show(); return; } if(!validateForm()) { Log.e(LOG_TAG, "form not valid"); return; }else{ Log.e(LOG_TAG, "form valid"); btn_login_login.setEnabled(true); login_username = text_login_email.getText().toString(); login_password = text_login_password.getText().toString(); //TODO call api server startSignUp(); } break; case R.id.text_login_resetPassword: toast("coming soon..."); break; case R.id.btn_login_cancel: activity.onBackPressed(); break; default:break; } } }; boolean validateForm() { if (TextUtils.isEmpty(text_login_email.getText()) && !isEmail(text_login_email.getText())) { Drawable email_red = activity.getResources().getDrawable(R.drawable.ic_email); text_login_email.setCompoundDrawablesWithIntrinsicBounds(email_red, null, null, null); // text_login_email.setError(getString(R.string.lbl_err_username)); text_login_email.requestFocus(); return false; } else if (TextUtils.isEmpty(text_login_password.getText()) && !isEmail(text_login_password.getText())) { Drawable pass_red = activity.getResources().getDrawable(R.drawable.ic_password); text_login_password.setCompoundDrawablesWithIntrinsicBounds(pass_red, null, null, null); // text_login_password.setError(getString(R.string.lbl_err_password)); // text_login_password.requestFocus(); return false; } else return true; } boolean isEmailValid(String email){ boolean isValid = false; String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$"; CharSequence inputStr = email; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputStr); if (matcher.matches()) { isValid = true; } return isValid; } boolean isEmail(CharSequence email){ return Patterns.EMAIL_ADDRESS.matcher(email).matches(); } void toast(final String text){ Toast.makeText(this.activity, text, Toast.LENGTH_SHORT).show(); } void startSignUp() { loading.setVisibility(View.VISIBLE); section_login.setVisibility(View.GONE); final String tag_json_obj = "json_obj_req"; //TODO change to backend URL String URL = "http://api.wardrobe.com"; JsonObjectRequest jsonObjectReq = new JsonObjectRequest(Request.Method.POST, URL, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.e(LOG_TAG, "response: " + response.toString()); //TODO process response finishLogin(); loading.setVisibility(View.GONE); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { VolleyLog.e(LOG_TAG, "Error: " + volleyError.getMessage()); //TODO uncomment the following lines...to show error // loading.setVisibility(View.GONE); // section_login.setVisibility(View.VISIBLE); // toast("Something's not right, please try again later :( "); //TODO remove this call...just for demo purposes finishLogin(); } }){ @Override protected Map<String, String> getParams(){ Map<String, String> params = new HashMap<String, String>(); params.put("email", "jdoe@gmail.com"); params.put("password", "jdoe_opt"); return params; } }; //add request to request queue VolleyAppController.getInstance().addToRequestQueue(jsonObjectReq, tag_json_obj); } void finishLogin(){ // AccountUtils.setChosenAccountName(this.activity, text_su_username.getText().toString()); AccountUtils.setChosenAccountName(this.activity, "brian"); PrefUtils.markSetupDone(this.activity); Intent intent = new Intent(this.activity, HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); this.activity.startActivity(intent); this.activity.finish(); } }