Back to project page Joetz-Android-V2.
The source code is released under:
GNU General Public License
If you think the Android project Joetz-Android-V2 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.example.jens.myapplication.account; // ww w . j a v a2 s. c om import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.os.Bundle; import android.support.v4.app.Fragment; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import com.example.jens.myapplication.R; import com.example.jens.myapplication.apimanager.ApiConnection; import com.example.jens.myapplication.apimanager.SimpleRequestTask; import com.example.jens.myapplication.apimanager.manager.ProfileManager; import com.example.jens.myapplication.domain.Person; import com.example.jens.myapplication.domain.User; import com.example.jens.myapplication.domain.UserRole; import com.example.jens.myapplication.domain.binding.PersonValidatorBinding; import com.example.jens.myapplication.domain.binding.UserValidatorBinding; import com.example.jens.myapplication.domain.binding.ValidatorBinding; import com.example.jens.myapplication.domain.validator.PersonValidator; import com.example.jens.myapplication.domain.validator.UserValidator; import com.example.jens.myapplication.sam.JoetzApplication; import com.example.jens.myapplication.sam.MyFragment; import com.example.jens.myapplication.util.ActivityUtils; import org.joda.time.DateTime; import java.util.LinkedList; import java.util.List; /** * Fragment with the profile information (editable) */ public class ProfileFragment extends MyFragment { private static final String ARG_OPENED_BLOCKS = "openedBlocks"; private OnFragmentInteractionListener mListener; private View rootView; private LinearLayout mRootLinearLayout; private UserValidator mUserValidator; private PersonValidator mPersonValidator; private TextView mTxtAccountType; private List<ValidatorBinding> mBindingsGeneral; private ValidatorBinding mDobValidator; private EditText mTxtFirstName; private EditText mTxtLastName; private EditText mTxtEmail; private EditText mTxtDob; private EditText mTxtPhone; private List<ValidatorBinding> mBindingsAddress; private EditText mTxtStreet; private EditText mTxtHouseNumber; private EditText mTxtBus; private EditText mTxtCity; private EditText mTxtPostalCode; private ValidatorBinding mBindingPassword; private EditText mTxtOldPassword; private EditText mTxtPassword; private EditText mTxtRepeatPassword; private List<ValidatorBinding> mBindingsBond; private EditText mTxtBondAansluitingsnr; private EditText mTxtBondCodeGerechtigde; private Button mBtnGeneral; private Button mBtnAddress; private Button mBtnPassword; private Button mBtnBond; private LinearLayout mLlyContentGeneral; private LinearLayout mLlyContentAddress; private LinearLayout mLlyContentPassword; private LinearLayout mLlyContentBond; private Button mBtnSaveGeneral; private Button mBtnSaveAddress; private Button mBtnSavePassword; private Button mBtnSaveBond; private User mUser; private Person mPerson; public static ProfileFragment newInstance() { ProfileFragment fragment = new ProfileFragment(); return fragment; } public ProfileFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); JoetzApplication.getContext().restartIfLoggedOut(); User u = JoetzApplication.getContext().getLoginManager().getUser(); Person p = u.getPerson(); //User only used to change password mUser = new User(); //Clone the person of the user, so changes to this object dont reflect in //the Person's object mPerson = p.clone(); //Initialize validation objects mUserValidator = new UserValidator(mUser); mPersonValidator = new PersonValidator(mPerson); //Lists of bindings per group of fields that can be edited mBindingsGeneral = new LinkedList<ValidatorBinding>(); mBindingsAddress = new LinkedList<ValidatorBinding>(); mBindingsBond = new LinkedList<ValidatorBinding>(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment rootView = inflater.inflate(R.layout.fragment_profile, container, false); mRootLinearLayout = (LinearLayout) rootView.findViewById(R.id.llyRootLinearLayout); //Clear focus when user scrolled (setError() annoying behavior) rootView.findViewById(R.id.scrRootScrollView) .getViewTreeObserver() .addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { clearFocus(); } }); //Show account type if user role is monitor or admin mTxtAccountType = (TextView) rootView.findViewById(R.id.txtAccountType); UserRole role = JoetzApplication.getContext().getLoginManager().getUser().getUserRole(); if(role.value() > UserRole.USER.value()){ mTxtAccountType.setVisibility(View.VISIBLE); mTxtAccountType.setText(getString(R.string.$_account_type_is, role.toString().toLowerCase())); } mBtnGeneral = (Button) rootView.findViewById(R.id.btnGeneral); mBtnAddress = (Button) rootView.findViewById(R.id.btnAddress); mBtnPassword = (Button) rootView.findViewById(R.id.btnPassword); mBtnBond = (Button) rootView.findViewById(R.id.btnBond); mBtnSaveGeneral = (Button) rootView.findViewById(R.id.btnSaveGeneral); mBtnSaveAddress = (Button) rootView.findViewById(R.id.btnSaveAddress); mBtnSavePassword = (Button) rootView.findViewById(R.id.btnSavePassword); mBtnSaveBond = (Button) rootView.findViewById(R.id.btnSaveBond); mLlyContentGeneral = (LinearLayout) rootView.findViewById(R.id.llyContentGeneral); mLlyContentAddress = (LinearLayout) rootView.findViewById(R.id.llyContentAddress); mLlyContentPassword = (LinearLayout) rootView.findViewById(R.id.llyContentPassword); mLlyContentBond = (LinearLayout) rootView.findViewById(R.id.llyContentBond); if (savedInstanceState != null) { if(savedInstanceState.containsKey(ARG_OPENED_BLOCKS)){ int[] vis = savedInstanceState.getIntArray(ARG_OPENED_BLOCKS); //these comments are required for android studio because it thinks im retarded //noinspection ResourceType mLlyContentGeneral.setVisibility(vis[0]); //noinspection ResourceType mLlyContentAddress.setVisibility(vis[1]); //noinspection ResourceType mLlyContentPassword.setVisibility(vis[2]); //noinspection ResourceType mLlyContentBond.setVisibility(vis[3]); } } initTextFields(); initValidators(); mBtnGeneral.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { toggleVisibility(mLlyContentGeneral); } }); mBtnAddress.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { toggleVisibility(mLlyContentAddress); } }); mBtnPassword.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { toggleVisibility(mLlyContentPassword); } }); mBtnBond.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { toggleVisibility(mLlyContentBond); } }); mBtnSaveGeneral.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { saveGeneral(); } }); mBtnSaveAddress.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { saveAddress(); } }); mBtnSavePassword.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { savePassword(); } }); mBtnSaveBond.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { saveBond(); } }); mBtnSaveGeneral.setEnabled(false); mBtnSaveAddress.setEnabled(false); mBtnSavePassword.setEnabled(false); mBtnSaveBond.setEnabled(false); return rootView; } /** * Toggle visibility of a view between GONE and VISIBLE * @param view View whose visibility should be toggled */ private void toggleVisibility(View view) { view.setVisibility( view.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if(outState != null){ if(mLlyContentAddress != null && mLlyContentBond != null && mLlyContentGeneral != null && mLlyContentPassword != null){ outState.putIntArray(ARG_OPENED_BLOCKS, new int[]{ mLlyContentGeneral.getVisibility(), mLlyContentAddress.getVisibility(), mLlyContentPassword.getVisibility(), mLlyContentBond.getVisibility() }); } } } /** * Clear the focus of the activity (helps with removing annoying effect of setError() on edittexts */ private void clearFocus() { View view = getActivity() == null ? null : getActivity().getCurrentFocus(); if (view == null || view == mRootLinearLayout) { return; } mRootLinearLayout.requestFocus(); } /** * Save general information */ private void saveGeneral() { if (hasErrors(mBindingsGeneral)) { showErrorBox(); return; } final ProgressDialog pgdProgress = new ProgressDialog(getActivity()); pgdProgress.setMessage(getString(R.string.changes_are_being_processed)); pgdProgress.setCancelable(false); pgdProgress.show(); mBtnSaveGeneral.setEnabled(false); setFieldsEnabled(mBindingsGeneral, false); SimpleRequestTask afterTask = new SimpleRequestTask() { @Override public void doTask(int statusCode) { pgdProgress.dismiss(); setFieldsEnabled(mBindingsGeneral, true); if (statusCode != ApiConnection.STATUS_OK) { showErrorRequestBox(); mBtnSaveGeneral.setEnabled(true); } else { showSuccessBox(); } } }; ProfileManager.changeStandardInformation(mPerson, afterTask); } /** * Save address information */ private void saveAddress() { if (hasErrors(mBindingsAddress)) { showErrorBox(); return; } final ProgressDialog pgdProgress = new ProgressDialog(getActivity()); pgdProgress.setMessage(getString(R.string.changes_are_being_processed)); pgdProgress.setCancelable(false); pgdProgress.show(); mBtnSaveAddress.setEnabled(false); setFieldsEnabled(mBindingsAddress, false); SimpleRequestTask afterTask = new SimpleRequestTask() { @Override public void doTask(int statusCode) { pgdProgress.dismiss(); setFieldsEnabled(mBindingsAddress, true); if (statusCode != ApiConnection.STATUS_OK) { showErrorRequestBox(); mBtnSaveAddress.setEnabled(true); } else { showSuccessBox(); } } }; ProfileManager.changeAddress(mPerson, afterTask); } /** * Send request to change the password of the user */ private void savePassword() { mBindingPassword.validate(); validateRepeatPassword(); if (mTxtPassword.getError() != null || mTxtRepeatPassword.getError() != null) { showErrorBox(); return; } final ProgressDialog pgdProgress = new ProgressDialog(getActivity()); pgdProgress.setMessage(getString(R.string.changes_are_being_processed)); pgdProgress.setCancelable(false); pgdProgress.show(); mBtnSavePassword.setEnabled(false); mTxtOldPassword.setEnabled(false); mTxtPassword.setEnabled(false); mTxtRepeatPassword.setEnabled(false); SimpleRequestTask afterTask = new SimpleRequestTask() { @Override public void doTask(int statusCode) { pgdProgress.dismiss(); mTxtOldPassword.setEnabled(true); mTxtPassword.setEnabled(true); mTxtRepeatPassword.setEnabled(true); if (statusCode != ApiConnection.STATUS_OK) { showErrorRequestBox(); mBtnSavePassword.setEnabled(true); } else { showSuccessBox(); } mTxtOldPassword.setText(""); mTxtPassword.setText(""); mTxtRepeatPassword.setText(""); } }; ProfileManager.changePassword(mTxtOldPassword.getText().toString(), mUser.getPassword(), afterTask); } /** * Save Bond information */ private void saveBond() { if (hasErrors(mBindingsBond)) { showErrorBox(); return; } final ProgressDialog pgdProgress = new ProgressDialog(getActivity()); pgdProgress.setMessage(getString(R.string.changes_are_being_processed)); pgdProgress.setCancelable(false); pgdProgress.show(); mBtnSaveBond.setEnabled(false); setFieldsEnabled(mBindingsBond, false); SimpleRequestTask afterTask = new SimpleRequestTask() { @Override public void doTask(int statusCode) { pgdProgress.dismiss(); setFieldsEnabled(mBindingsBond, true); if (statusCode != ApiConnection.STATUS_OK) { showErrorRequestBox(); mBtnSaveBond.setEnabled(true); } else { showSuccessBox(); } } }; ProfileManager.changeSocialSecurity(mPerson, afterTask); } /** * Show alert dialog if a request is successful */ private void showSuccessBox() { new AlertDialog.Builder(getActivity()) .setNeutralButton(R.string.ok, null) .setMessage(R.string.change_success) .create().show(); } /** * Error something went wrong with the request */ private void showErrorRequestBox() { new AlertDialog.Builder(getActivity()) .setNeutralButton(R.string.ok, null) .setMessage(R.string.error_unknown) .create().show(); } /** * Error that fields are not properly filled in */ private void showErrorBox() { new AlertDialog.Builder(getActivity()) .setNeutralButton(R.string.ok, null) .setMessage(R.string.error_errors_exist) .create().show(); } /** * Set the fields that are linked to the bindings in the parameter bindings, enabled or disabled * @param bindings The bindings that contain the edittexts to be enabled/disabled * @param enabled true or false */ private void setFieldsEnabled(List<ValidatorBinding> bindings, boolean enabled) { for (ValidatorBinding binding : bindings) { binding.getView().setEnabled(enabled); } } /** * Check if the bindings contain an error * @param bindings bindings to be checked for an error * @return true if any of the bindings contain an error */ private boolean hasErrors(List<ValidatorBinding> bindings) { for (ValidatorBinding binding : bindings) { binding.validate(); if (binding.getView().getError() != null) { return true; } } return false; } /** * Validate the repeatpassword field. Will set an error if its not equal to password or is empty */ private void validateRepeatPassword() { String pass = mTxtPassword.getText().toString(); String rPass = mTxtRepeatPassword.getText().toString(); if (rPass.isEmpty()) { mTxtRepeatPassword.setError(getString(R.string.$_need_filled_in, mTxtPassword.getHint().toString())); return; } if (!pass.equals(rPass)) { mTxtRepeatPassword.setError(getString(R.string.error_passwords_equal)); } } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * Open a dialog to choose a date for date of birth */ private void chooseDob() { ActivityUtils.chooseDateOfBirth(getActivity(), mPerson, new ActivityUtils.OnDateChosenListener() { @Override public void onDateChosen(int year, int monthOfYear, int dayOfMonth) { mPerson.setDateOfBirth(new DateTime(year, monthOfYear, dayOfMonth, 0, 0)); mDobValidator.fillValue(); mDobValidator.validate(); mTxtDob.requestFocus(); } }); } /** * Initialize textfields and textwatchers */ private void initTextFields() { abstract class MyWatcher implements TextWatcher { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } } MyWatcher watcherGeneral = new MyWatcher() { @Override public void afterTextChanged(Editable s) { mBtnSaveGeneral.setEnabled(true); } }; mTxtFirstName = (EditText) rootView.findViewById(R.id.txtFirstName); mTxtFirstName.addTextChangedListener(watcherGeneral); mTxtLastName = (EditText) rootView.findViewById(R.id.txtLastName); mTxtLastName.addTextChangedListener(watcherGeneral); mTxtEmail = (EditText) rootView.findViewById(R.id.txtEmail); mTxtEmail.addTextChangedListener(watcherGeneral); mTxtDob = (EditText) rootView.findViewById(R.id.txtDob); mTxtDob.addTextChangedListener(watcherGeneral); mTxtPhone = (EditText) rootView.findViewById(R.id.txtTelephone); mTxtPhone.addTextChangedListener(watcherGeneral); MyWatcher watcherAddress = new MyWatcher() { @Override public void afterTextChanged(Editable s) { mBtnSaveAddress.setEnabled(true); } }; mTxtStreet = (EditText) rootView.findViewById(R.id.txtStreet); mTxtStreet.addTextChangedListener(watcherAddress); mTxtHouseNumber = (EditText) rootView.findViewById(R.id.txtHouseNumber); mTxtHouseNumber.addTextChangedListener(watcherAddress); mTxtBus = (EditText) rootView.findViewById(R.id.txtBus); mTxtBus.addTextChangedListener(watcherAddress); mTxtCity = (EditText) rootView.findViewById(R.id.txtCity); mTxtCity.addTextChangedListener(watcherAddress); mTxtPostalCode = (EditText) rootView.findViewById(R.id.txtPostalCode); mTxtPostalCode.addTextChangedListener(watcherAddress); MyWatcher watcherPassword = new MyWatcher() { @Override public void afterTextChanged(Editable s) { checkEnablePasswordButton(); } }; mTxtOldPassword = (EditText) rootView.findViewById(R.id.txtOldPassword); mTxtOldPassword.addTextChangedListener(watcherPassword); mTxtPassword = (EditText) rootView.findViewById(R.id.txtPassword); mTxtPassword.addTextChangedListener(watcherPassword); mTxtRepeatPassword = (EditText) rootView.findViewById(R.id.txtRepeatPassword); mTxtRepeatPassword.addTextChangedListener(watcherPassword); MyWatcher watcherBond = new MyWatcher() { @Override public void afterTextChanged(Editable s) { mBtnSaveBond.setEnabled(true); } }; mTxtBondAansluitingsnr = (EditText) rootView.findViewById(R.id.txtBondAansluitingsnr); mTxtBondAansluitingsnr.addTextChangedListener(watcherBond); mTxtBondCodeGerechtigde = (EditText) rootView.findViewById(R.id.txtBondCodeGerechtigde); mTxtBondCodeGerechtigde.addTextChangedListener(watcherBond); //custom dob handling mTxtDob.setKeyListener(null); } /** * Check if the password save button should become enabled */ private void checkEnablePasswordButton() { if (mTxtOldPassword.getText().toString().isEmpty() || mTxtPassword.getText().toString().isEmpty() || mTxtRepeatPassword.getText().toString().isEmpty()) { return; } mBtnSavePassword.setEnabled(true); } /** * Initialize the validatorbindings */ private void initValidators() { mBindingsGeneral.clear(); mBindingsAddress.clear(); mBindingsBond.clear(); //General mBindingsGeneral.add(new PersonValidatorBinding(mPersonValidator, mTxtFirstName, PersonValidatorBinding.FIELD_FIRST_NAME, mTxtFirstName.getHint().toString())); mBindingsGeneral.add(new PersonValidatorBinding(mPersonValidator, mTxtLastName, PersonValidatorBinding.FIELD_LAST_NAME, mTxtLastName.getHint().toString())); mBindingsGeneral.add(new PersonValidatorBinding(mPersonValidator, mTxtEmail, PersonValidatorBinding.FIELD_EMAIL, mTxtEmail.getHint().toString())); mDobValidator = new PersonValidatorBinding(mPersonValidator, mTxtDob, PersonValidatorBinding.FIELD_DATE_OF_BIRTH, mTxtDob.getHint().toString()); mBindingsGeneral.add(mDobValidator); mDobValidator.addOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { chooseDob(); } } }); mBindingsGeneral.add(new PersonValidatorBinding(mPersonValidator, mTxtPhone, PersonValidatorBinding.FIELD_PHONE, mTxtPhone.getHint().toString())); //Address mBindingsAddress.add(new PersonValidatorBinding(mPersonValidator, mTxtStreet, PersonValidatorBinding.FIELD_STREET, mTxtStreet.getHint().toString())); mBindingsAddress.add(new PersonValidatorBinding(mPersonValidator, mTxtHouseNumber, PersonValidatorBinding.FIELD_HOUSE_NUMBER, mTxtHouseNumber.getHint().toString())); mBindingsAddress.add(new PersonValidatorBinding(mPersonValidator, mTxtBus, PersonValidatorBinding.FIELD_BUS, mTxtBus.getHint().toString(), false)); mBindingsAddress.add(new PersonValidatorBinding(mPersonValidator, mTxtCity, PersonValidatorBinding.FIELD_CITY, mTxtCity.getHint().toString())); mBindingsAddress.add(new PersonValidatorBinding(mPersonValidator, mTxtPostalCode, PersonValidatorBinding.FIELD_POSTAL_CODE, mTxtPostalCode.getHint().toString())); //Password mBindingPassword = new UserValidatorBinding(mUserValidator, mTxtPassword, UserValidatorBinding.FIELD_PASSWORD, mTxtPassword.getHint().toString()); //Bond mBindingsBond.add(new PersonValidatorBinding(mPersonValidator, mTxtBondAansluitingsnr, PersonValidatorBinding.FIELD_BOND_AANSLUITINGSNUMMER, mTxtBondAansluitingsnr.getHint().toString())); mBindingsBond.add(new PersonValidatorBinding(mPersonValidator, mTxtBondCodeGerechtigde, PersonValidatorBinding.FIELD_BOND_CODE_GERECHTIGDE, mTxtBondCodeGerechtigde.getHint().toString())); } public interface OnFragmentInteractionListener { } }