com.nanostuffs.yurdriver.fragment.BankDetails.java Source code

Java tutorial

Introduction

Here is the source code for com.nanostuffs.yurdriver.fragment.BankDetails.java

Source

package com.nanostuffs.yurdriver.fragment;

import java.util.HashMap;

import org.json.JSONObject;

import com.androidquery.AQuery;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.nanostuffs.yurdriver.HideSoftKeyBoard;
import com.nanostuffs.yurdriver.R;
import com.nanostuffs.yurdriver.base.BaseRegisterFragment;
import com.nanostuffs.yurdriver.parse.AsyncTaskCompleteListener;
import com.nanostuffs.yurdriver.parse.MultiPartRequester;
import com.nanostuffs.yurdriver.parse.ParseContent;
import com.nanostuffs.yurdriver.utills.AndyConstants;
import com.nanostuffs.yurdriver.utills.AndyUtils;
import com.nanostuffs.yurdriver.utills.AppLog;
import com.nanostuffs.yurdriver.utills.PreferenceHelper;
import com.nanostuffs.yurdriver.widget.MyFontTextView;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BankDetails extends BaseRegisterFragment
        implements OnClickListener, ConnectionCallbacks, OnConnectionFailedListener, AsyncTaskCompleteListener {

    EditText edt_accno, edt_bankname, edt_routing, edit_paypal;
    MyFontTextView submit;
    private final String TAG = "RegisterFragment";
    private ParseContent parseContent;
    private AQuery aQuery;
    private PreferenceHelper preferenceHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view = inflater.inflate(R.layout.bank_details, container, false);

        submit = (MyFontTextView) view.findViewById(R.id.tvRegisterSubmit);
        edt_accno = (EditText) view.findViewById(R.id.accountnumber);
        edt_bankname = (EditText) view.findViewById(R.id.bankname);
        edt_routing = (EditText) view.findViewById(R.id.routing);
        edit_paypal = (EditText) view.findViewById(R.id.paypal);

        preferenceHelper = new PreferenceHelper(registerActivity);

        submit.setOnClickListener(this);

        LinearLayout lin = (LinearLayout) view.findViewById(R.id.mainLay);
        lin.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                HideSoftKeyBoard hid = new HideSoftKeyBoard(getActivity());
                hid.hideKeyboard(getActivity());
                return false;
            }
        });

        return view;

    }

    private void onRegisterButtonClick() {
        if (edt_accno.getText().length() == 0) {
            AndyUtils.showToast("Please Enter Account Number", registerActivity);
            return;
        } else if (edt_bankname.getText().length() == 0) {
            AndyUtils.showToast("Please Enter Bank Name", registerActivity);
            return;
        } else if (edt_routing.getText().length() == 0) {
            AndyUtils.showToast("Please Enter Routing", registerActivity);
            return;
        } else {
            register(VehicalRegistration.loginType, VehicalRegistration.socialId);
        }

    }

    private void register(String type, String id) {

        if (!AndyUtils.isNetworkAvailable(registerActivity)) {
            AndyUtils.showToast(getResources().getString(R.string.toast_no_internet), registerActivity);
            return;
        }

        AndyUtils.showCustomProgressDialog(registerActivity, "",
                getResources().getString(R.string.progress_dialog_register), false);

        if (type.equals(AndyConstants.MANUAL)) {
            AppLog.Log(TAG, "Simple Register method");
            //loginType,socialId,fname,lname,email,pass,proimg,mob,gender,address,zip,devtype,token,logtype
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(AndyConstants.URL, AndyConstants.ServiceType.REGISTER);
            map.put(AndyConstants.Params.FIRSTNAME, VehicalRegistration.fname);
            map.put(AndyConstants.Params.LAST_NAME, VehicalRegistration.lname);
            map.put(AndyConstants.Params.EMAIL, VehicalRegistration.email);
            map.put(AndyConstants.Params.CITY, RegisterFragment.city);
            map.put(AndyConstants.Params.STATE, RegisterFragment.state);
            map.put(AndyConstants.Params.COUNTRY, RegisterFragment.Country);
            map.put(AndyConstants.Params.PASSWORD, VehicalRegistration.pass);
            map.put("userfile", VehicalRegistration.proimg);
            map.put(AndyConstants.Params.PHONE, VehicalRegistration.mob);
            map.put(AndyConstants.Params.BIO, VehicalRegistration.gender);
            map.put(AndyConstants.Params.ADDRESS, VehicalRegistration.address);
            map.put("dob", RegisterFragment.dob);
            map.put(AndyConstants.Params.ZIPCODE, VehicalRegistration.zip);
            map.put(AndyConstants.Params.TYPE, VehicalRegistration.carType);

            map.put(AndyConstants.Params.PICTURE_VHC, RegisterFragment.vehicalImg);
            map.put(AndyConstants.Params.PICTURE_NOPL, RegisterFragment.noPlateImg);
            map.put(AndyConstants.Params.PICTURE_LIC, RegisterFragment.licenseImg);
            map.put(AndyConstants.Params.PICTURE_REGS, RegisterFragment.regstrationImg);

            map.put(AndyConstants.Params.CAR_MODEL, VehicalRegistration.carmodel);
            map.put(AndyConstants.Params.CAR_MAKE, VehicalRegistration.carmake);
            map.put(AndyConstants.Params.LICENCE_NO, VehicalRegistration.LicenseNO);
            map.put(AndyConstants.Params.RESTRATION_NO, VehicalRegistration.ReistrationNO);
            map.put(AndyConstants.Params.POLICY_NO, VehicalRegistration.PolicyNO);

            map.put(AndyConstants.Params.ACC_NO, edt_accno.getText().toString());
            map.put(AndyConstants.Params.BANK_NAME, edt_bankname.getText().toString());
            map.put(AndyConstants.Params.ROUTING, edt_routing.getText().toString());
            map.put(AndyConstants.Params.PAYPAL_EMAIL, edit_paypal.getText().toString());

            map.put(AndyConstants.Params.DEVICE_TYPE, AndyConstants.DEVICE_TYPE_ANDROID);
            Log.e("KJFEHKFWFLGGGGGGGGGGGGGGGGG", new PreferenceHelper(registerActivity).getDeviceToken());
            map.put(AndyConstants.Params.DEVICE_TOKEN, new PreferenceHelper(registerActivity).getDeviceToken());
            map.put(AndyConstants.Params.LOGIN_BY, AndyConstants.MANUAL);
            new MultiPartRequester(registerActivity, map, AndyConstants.ServiceCode.REGISTER, this);
        } else {
            registerSoicial(id, type);
        }
    }

    private void registerSoicial(String id, String loginType) {
        AppLog.Log(TAG, "Register social method");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(AndyConstants.URL, AndyConstants.ServiceType.REGISTER);
        map.put(AndyConstants.Params.FIRSTNAME, VehicalRegistration.fname);
        map.put(AndyConstants.Params.LAST_NAME, VehicalRegistration.lname);
        map.put(AndyConstants.Params.ADDRESS, VehicalRegistration.address);
        map.put(AndyConstants.Params.CITY, RegisterFragment.city);
        map.put(AndyConstants.Params.STATE, RegisterFragment.state);
        map.put(AndyConstants.Params.COUNTRY, RegisterFragment.Country);
        map.put(AndyConstants.Params.EMAIL, VehicalRegistration.email);
        map.put(AndyConstants.Params.PHONE, VehicalRegistration.mob);
        map.put("userfile", VehicalRegistration.proimg);

        map.put(AndyConstants.Params.TYPE, VehicalRegistration.carType);

        map.put("dob", RegisterFragment.dob);
        map.put(AndyConstants.Params.BIO, VehicalRegistration.gender);
        map.put(AndyConstants.Params.DEVICE_TYPE, AndyConstants.DEVICE_TYPE_ANDROID);

        map.put(AndyConstants.Params.PICTURE_VHC, RegisterFragment.vehicalImg);
        map.put(AndyConstants.Params.PICTURE_NOPL, RegisterFragment.noPlateImg);
        map.put(AndyConstants.Params.PICTURE_LIC, RegisterFragment.licenseImg);
        map.put(AndyConstants.Params.PICTURE_REGS, RegisterFragment.regstrationImg);

        map.put(AndyConstants.Params.CAR_MAKE, VehicalRegistration.carmake);
        map.put(AndyConstants.Params.CAR_MODEL, VehicalRegistration.carmodel);
        map.put(AndyConstants.Params.LICENCE_NO, VehicalRegistration.LicenseNO);
        map.put(AndyConstants.Params.RESTRATION_NO, VehicalRegistration.ReistrationNO);
        map.put(AndyConstants.Params.POLICY_NO, VehicalRegistration.PolicyNO);

        map.put(AndyConstants.Params.ACC_NO, edt_accno.getText().toString());
        map.put(AndyConstants.Params.BANK_NAME, edt_bankname.getText().toString());
        map.put(AndyConstants.Params.ROUTING, edt_routing.getText().toString());
        map.put(AndyConstants.Params.PAYPAL_EMAIL, edit_paypal.getText().toString());

        map.put(AndyConstants.Params.DEVICE_TOKEN, new PreferenceHelper(registerActivity).getDeviceToken());
        map.put(AndyConstants.Params.ZIPCODE, VehicalRegistration.zip);
        map.put(AndyConstants.Params.SOCIAL_UNIQUE_ID, id);
        map.put(AndyConstants.Params.LOGIN_BY, loginType);
        new MultiPartRequester(registerActivity, map, AndyConstants.ServiceCode.REGISTER, this);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        actionBarDetails();
        registerActivity.setActionBarTitle(getResources().getString(R.string.text_register));
        parseContent = new ParseContent(registerActivity);

        aQuery = new AQuery(registerActivity);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.tvRegisterSubmit:
            onRegisterButtonClick();
            break;
        }

    }

    private void actionBarDetails() {
        getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        final View addView = getActivity().getLayoutInflater().inflate(R.layout.my_custom_navigation, null);

        TextView title = (TextView) addView.findViewById(R.id.title);
        title.setText("REGISTRATION");
        ImageView back = (ImageView) addView.findViewById(R.id.back);
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                FragmentManager manager = getActivity().getSupportFragmentManager();
                manager.popBackStack();
            }
        });
        getActivity().getActionBar().setCustomView(addView);
    }

    @Override
    public void onTaskCompleted(String response, int serviceCode) {
        // TODO Auto-generated method stub
        switch (serviceCode) {
        case AndyConstants.ServiceCode.REGISTER:
            AppLog.Log(TAG, "Register response :" + response);
            try {
                AndyUtils.removeCustomProgressDialog();
                if (parseContent.isSuccess(response)) {

                    Toast.makeText(registerActivity,
                            registerActivity.getResources().getString(R.string.toast_register_success), 1).show();

                    JSONObject jsonObject = new JSONObject(response);

                    preferenceHelper.putUserId(jsonObject.getString(AndyConstants.Params.ID));
                    preferenceHelper.putSessionToken(jsonObject.getString(AndyConstants.Params.TOKEN));

                    registerActivity.addFragment(new AddReferal(), false, AndyConstants.LOGIN_FRAGMENT_TAG);

                    // registerActivity.finish();
                } else {

                    if (response != null) {
                        JSONObject j = new JSONObject(response);
                        if (j.has("error")) {
                            AndyUtils.showToast(j.getString("error"), registerActivity);
                        }

                    }

                }
            } catch (Exception e) {
                // TODO: handle exception
            }

            break;

        default:
            break;
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnected(Bundle arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub

    }

}