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

Java tutorial

Introduction

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

Source

package com.nanostuffs.yurdriver.fragment;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
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.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

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.MyFontEdittextView;
import com.nanostuffs.yurdriver.widget.MyFontTextView;
import com.soundcloud.android.crop.Crop;

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

    public static Uri uri = null;
    private final String TAG = "RegisterFragment";
    public static String loginType, socialId, fname, lname, email, pass, proimg, mob, gender, address, zip, devtype,
            token, logtype;
    public static String carmake, carmodel, LicenseNO, ReistrationNO, PolicyNO, carType;
    private MyFontEdittextView carMake, carModel, LicenseNo, ReistrationNo, PolicyNo;
    private MyFontTextView clickSubmit;
    private Spinner typeCar;

    private String profileImageData = null, socialProPicUrl;

    public static ImageView licimg, vehimg, numimg, regimg;

    public int licgal = 503;
    public int liccam = 504;
    public int vehgal = 501;
    public int vehcam = 502;
    public int numgal = 505;
    public int numcam = 506;
    public int reggal = 507;
    public int regcam = 508;

    public static int vehflag = 0;
    public int code = 0;

    private ImageView carImg;
    private ParseContent parseContent;
    private AQuery aQuery;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.vehical_detail, container, false);

        carMake = (MyFontEdittextView) view.findViewById(R.id.carMake);
        carModel = (MyFontEdittextView) view.findViewById(R.id.carModel);
        LicenseNo = (MyFontEdittextView) view.findViewById(R.id.LicenceNo);
        ReistrationNo = (MyFontEdittextView) view.findViewById(R.id.RegistrationNo);
        PolicyNo = (MyFontEdittextView) view.findViewById(R.id.policyNo);
        clickSubmit = (MyFontTextView) view.findViewById(R.id.tvRegisterSubmit);
        typeCar = (Spinner) view.findViewById(R.id.cartype);
        clickSubmit.setOnClickListener(this);

        vehimg = (ImageView) view.findViewById(R.id.vehicalpic);
        licimg = (ImageView) view.findViewById(R.id.licensepic);
        numimg = (ImageView) view.findViewById(R.id.drRegistration);
        regimg = (ImageView) view.findViewById(R.id.insuranseCard);

        vehimg.setOnClickListener(this);
        licimg.setOnClickListener(this);
        numimg.setOnClickListener(this);
        regimg.setOnClickListener(this);

        Bundle bundle = getArguments();
        loginType = bundle.getString("logInType");
        socialId = bundle.getString("socialId");

        fname = bundle.getString("fname");
        lname = bundle.getString("lname");
        email = bundle.getString("email");
        pass = bundle.getString("pass");
        proimg = bundle.getString("proimg");
        mob = bundle.getString("mob");
        gender = bundle.getString("gend");
        address = bundle.getString("addr");

        zip = bundle.getString("zip");
        devtype = bundle.getString("devicetype");
        token = bundle.getString("token");
        logtype = bundle.getString("logintype");

        if (RegisterFragment.vehicalImg != null) {
            File imgFileC = new File(RegisterFragment.vehicalImg);

            if (imgFileC.exists()) {
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFileC.getAbsolutePath());
                vehimg.setImageBitmap(myBitmap);
            }
        }

        if (RegisterFragment.licenseImg != null) {
            File imgFileL = new File(RegisterFragment.licenseImg);

            if (imgFileL.exists()) {
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFileL.getAbsolutePath());
                licimg.setImageBitmap(myBitmap);
            }
        }

        if (RegisterFragment.noPlateImg != null) {
            File imgFileI = new File(RegisterFragment.noPlateImg);

            if (imgFileI.exists()) {
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFileI.getAbsolutePath());
                numimg.setImageBitmap(myBitmap);
            }
        }

        if (RegisterFragment.regstrationImg != null) {
            File imgFileR = new File(RegisterFragment.regstrationImg);

            if (imgFileR.exists()) {
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFileR.getAbsolutePath());
                regimg.setImageBitmap(myBitmap);
            }
        }

        typeCar.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                carType = String.valueOf(position + 1);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });

        ScrollView v = (ScrollView) view.findViewById(R.id.scrollView1);
        v.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 (carMake.getText().length() == 0) {
            AndyUtils.showToast(getResources().getString(R.string.error_empty_carmake), registerActivity);
            return;
        } else if (carModel.getText().length() == 0) {
            AndyUtils.showToast(getResources().getString(R.string.error_empty_carmodel), registerActivity);
            return;
        } else if (LicenseNo.getText().length() == 0) {
            AndyUtils.showToast(getResources().getString(R.string.error_empty_licenceno), registerActivity);
            return;
        } else if (ReistrationNo.getText().length() == 0) {
            AndyUtils.showToast(getResources().getString(R.string.error_empty_registration), registerActivity);
            return;
        } else if (PolicyNo.getText().length() == 0) {
            AndyUtils.showToast(getResources().getString(R.string.error_empty_policyno), registerActivity);
            return;
        } else if (RegisterFragment.vehicalImg == null || RegisterFragment.vehicalImg.equals("")) {
            AndyUtils.showToast(getResources().getString(R.string.error_empty_image_vehical), registerActivity);
            return;
        } else if (RegisterFragment.noPlateImg == null || RegisterFragment.noPlateImg.equals("")) {
            AndyUtils.showToast(getResources().getString(R.string.error_empty_image_insurance), registerActivity);
            return;
        } else if (RegisterFragment.regstrationImg == null || RegisterFragment.regstrationImg.equals("")) {
            AndyUtils.showToast(getResources().getString(R.string.error_empty_image_registration),
                    registerActivity);
            return;
        } else if (RegisterFragment.licenseImg == null || RegisterFragment.licenseImg.equals("")) {
            AndyUtils.showToast(getResources().getString(R.string.error_empty_image_licence), registerActivity);
            return;
        } else {
            // register(loginType, socialId);
            registerActivity.addFragment(new BankDetails(), true, AndyConstants.LOGIN_FRAGMENT_TAG);

            carmake = carMake.getText().toString();
            carmodel = carModel.getText().toString();
            LicenseNO = LicenseNo.getText().toString();
            ReistrationNO = ReistrationNo.getText().toString();
            PolicyNO = PolicyNo.getText().toString();
        }

    }

    @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);

            if (parseContent.isSuccess(response)) {
                AndyUtils.showToast(registerActivity.getResources().getString(R.string.toast_register_success),
                        registerActivity);
                // parseContent.parseUserAndStoreToDb(response);
                // new PreferenceHelper(getActivity())
                // .putPassword(etRegisterPassword.getText().toString());
                // Intent intent = new Intent(registerActivity,
                // MapActivity.class);
                // startActivity(intent);
                AndyUtils.removeCustomProgressDialog();
                registerActivity.addFragment(new BankDetails(), false, AndyConstants.LOGIN_FRAGMENT_TAG);

                // registerActivity.finish();
            }
            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

    }

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

        case R.id.vehicalpic:
            code = 1;
            showPictureDialog();
            break;

        case R.id.licensepic:
            code = 2;
            showPictureDialog();
            break;

        case R.id.drRegistration:
            code = 3;
            showPictureDialog();
            break;

        case R.id.insuranseCard:
            code = 4;
            showPictureDialog();
            break;

        default:
            break;
        }
    }

    private void showPictureDialog() {
        AlertDialog.Builder pictureDialog = new AlertDialog.Builder(registerActivity);
        pictureDialog.setTitle(getResources().getString(R.string.dialog_chhose_photo));
        String[] pictureDialogItems = { getResources().getString(R.string.dialog_from_gallery),
                getResources().getString(R.string.dialog_from_camera) };

        pictureDialog.setItems(pictureDialogItems, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {

                case 0:

                    if (code == 1) {
                        choosePhotoFromGallary(vehgal);
                    }
                    if (code == 2) {
                        choosePhotoFromGallary(licgal);
                    }
                    if (code == 3) {
                        choosePhotoFromGallary(numgal);
                    }
                    if (code == 4) {
                        choosePhotoFromGallary(reggal);
                    }

                    break;

                case 1:

                    if (code == 1) {
                        takePhotoFromCamera(vehcam);
                    }
                    if (code == 2) {
                        takePhotoFromCamera(liccam);
                    }
                    if (code == 3) {
                        takePhotoFromCamera(numcam);
                    }
                    if (code == 4) {
                        takePhotoFromCamera(regcam);
                    }

                    break;

                }
            }
        });
        pictureDialog.show();
    }

    private void choosePhotoFromGallary(int code) {

        // Intent intent = new Intent();
        // intent.setType("image/*");
        // intent.setAction(Intent.ACTION_GET_CONTENT);
        // intent.addCategory(Intent.CATEGORY_OPENABLE);

        vehflag = 2;
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        registerActivity.startActivityForResult(galleryIntent, code, AndyConstants.REGISTER_FRAGMENT_TAG);

    }

    private void takePhotoFromCamera(int code) {

        vehflag = 2;
        Calendar cal = Calendar.getInstance();
        File file = new File(Environment.getExternalStorageDirectory(), (cal.getTimeInMillis() + ".jpg"));

        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {

            file.delete();
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        uri = Uri.fromFile(file);
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        registerActivity.startActivityForResult(cameraIntent, code, AndyConstants.REGISTER_FRAGMENT_TAG);
    }

    @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);
    }

    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);
    }

}