com.df.app.procedures.CarRecogniseLayout.java Source code

Java tutorial

Introduction

Here is the source code for com.df.app.procedures.CarRecogniseLayout.java

Source

package com.df.app.procedures;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.df.app.MainActivity;
import com.df.app.R;
import com.df.app.service.util.AppCommon;
import com.df.library.entries.Brand;
import com.df.library.entries.CarSettings;
import com.df.library.entries.Country;
import com.df.library.entries.Manufacturer;
import com.df.library.entries.Model;
import com.df.library.entries.Series;
import com.df.library.entries.VehicleModel;
import com.df.library.asyncTask.GetCarSettingsByVinTask;
import com.df.library.asyncTask.GetCarSettingsTask;
import com.df.library.asyncTask.UpdateAuthorizeCodeStatusTask;
import com.df.library.service.LicenseRecognise;
import com.df.library.util.Helper;
import com.df.library.util.MyAlertDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import wintone.idcard.android.AuthParameterMessage;
import wintone.idcard.android.AuthService;
import wintone.idcard.android.IDCardCfg;

import static com.df.library.util.Helper.enableView;
import static com.df.library.util.Helper.getEditViewText;
import static com.df.library.util.Helper.isVin;
import static com.df.library.util.Helper.setEditViewText;
import static com.df.library.util.Helper.setTextView;
import static com.df.library.util.Helper.showView;

/**
 * Created by  on 13-12-20.
 *
 * 
 */
public class CarRecogniseLayout extends LinearLayout {
    OnShowContent mShowContentCallback;
    OnHideContent mHideContentCallback;

    static View rootView;

    // ?6?
    public static EditText vehicleTypeEdit;
    public static EditText useCharacterEdit;

    // ?
    private VehicleModel vehicleModel;

    // spinner??
    private int lastCountryIndex = 0;
    private int lastBrandIndex = 0;
    private int lastManufacturerIndex = 0;
    private int lastSeriesIndex = -50;
    private int lastModelIndex = 0;

    // ??
    private CarSettings mCarSettings;

    // ?
    private boolean modify;
    private int carId;
    private EditText countryEdit;
    private EditText brandEdit;
    private EditText manufacturerEdit;
    private EditText seriesEdit;
    private EditText modelEdit;

    private Button recogniseButton;
    private ImageView licenseImageView;

    private boolean cut = true;

    private LicenseRecognise licenseRecognise;
    public static int nMainID = 6;

    private AuthService.authBinder authBinder;
    public static int ReturnAuthority = -1;
    private String authCode = "";

    public ServiceConnection authConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            authBinder = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            authBinder = (AuthService.authBinder) service;
            try {
                AuthParameterMessage apm = new AuthParameterMessage();

                apm.sn = authCode;
                apm.authfile = "";

                ReturnAuthority = authBinder.getIDCardAuth(apm);

                Log.d(AppCommon.TAG, ReturnAuthority == 0 ? "???" : "?");

                if (ReturnAuthority != 0) {

                } else {
                    UpdateAuthorizeCodeStatusTask updateAuthorizeCodeStatusTask = new UpdateAuthorizeCodeStatusTask(
                            getContext(), new UpdateAuthorizeCodeStatusTask.OnUpdateFinished() {
                                @Override
                                public void onFinished(String result) {
                                    // TODO nothing
                                }

                                @Override
                                public void onFailed(String result) {
                                    // TODO nothing

                                }
                            });
                    updateAuthorizeCodeStatusTask.execute();

                    createSnFile(authCode);
                }
            } catch (Exception e) {
                Toast.makeText(getContext(), "??", Toast.LENGTH_LONG).show();
            } finally {
                if (authBinder != null) {
                    getContext().unbindService(authConn);
                }
            }
        }
    };

    /**
     * 
     * @param context
     * @param sListener
     */
    public CarRecogniseLayout(Context context, OnShowContent sListener, OnHideContent hListener) {
        super(context);
        mShowContentCallback = sListener;
        mHideContentCallback = hListener;
        init(context);
    }

    private void init(final Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.car_recognise_layout, this);

        deleteLastLicensePhoto();

        mCarSettings = CarSettings.getInstance();

        licenseRecognise = new LicenseRecognise(context, AppCommon.licensePhotoPath);

        licenseImageView = (ImageView) findViewById(R.id.licenseImage);
        licenseImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                licenseRecognise.takePhoto();
            }
        });

        // 
        recogniseButton = (Button) rootView.findViewById(R.id.recognise_button);
        recogniseButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // ???
                if (!mCarSettings.getBrandString().equals("")) {
                    reRecognise(R.string.reRecognise1);
                } else if (!getEditViewText(rootView, R.id.plateNumber_edit).equals("")) {
                    reRecognise(R.string.reRecognise);
                } else {
                    //fillInDummyData();
                    if (licensePhotoExist())
                        recogniseLicense();
                    else
                        Toast.makeText(context, "???", Toast.LENGTH_SHORT).show();
                }
            }
        });

        // vin
        Button vinConfirmButton = (Button) rootView.findViewById(R.id.vinConfirm_button);
        vinConfirmButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // VIN??
                if (getEditViewText(rootView, R.id.vin_edit).equals("")) {
                    Toast.makeText(rootView.getContext(), "VIN?", Toast.LENGTH_SHORT).show();
                } else {
                    checkVinAndGetCarSettings();
                }
            }
        });

        // ?
        Button brandSelectButton = (Button) rootView.findViewById(R.id.brand_select_button);
        brandSelectButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                selectCarManually();
            }
        });

        vehicleTypeEdit = (EditText) rootView.findViewById(R.id.vehicleType_edit);
        vehicleTypeEdit.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_UP:
                    choose(R.array.vehicleType_items, R.id.vehicleType_edit);
                    break;
                }
                return false;
            }
        });

        useCharacterEdit = (EditText) rootView.findViewById(R.id.useCharacter_edit);
        useCharacterEdit.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_UP:
                    choose(R.array.useCharacter_items, R.id.useCharacter_edit);
                    break;
                }
                return false;
            }
        });

        // vin???
        InputFilter alphaNumericFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence arg0, int arg1, int arg2, Spanned arg3, int arg4, int arg5) {
                for (int k = arg1; k < arg2; k++) {
                    if (!Character.isLetterOrDigit(arg0.charAt(k))) {
                        return "";
                    }
                }
                return null;
            }
        };

        EditText vin_edit = (EditText) findViewById(R.id.vin_edit);
        vin_edit.setFilters(new InputFilter[] { alphaNumericFilter, new InputFilter.AllCaps(),
                new InputFilter.LengthFilter(17) });

        EditText plateNumberEdit = (EditText) findViewById(R.id.plateNumber_edit);
        plateNumberEdit
                .setFilters(new InputFilter[] { new InputFilter.AllCaps(), new InputFilter.LengthFilter(10) });

        EditText licenseModelEdit = (EditText) findViewById(R.id.licenseModel_edit);
        licenseModelEdit
                .setFilters(new InputFilter[] { new InputFilter.AllCaps(), new InputFilter.LengthFilter(22) });

        EditText engineSerialEdit = (EditText) findViewById(R.id.engineSerial_edit);
        engineSerialEdit
                .setFilters(new InputFilter[] { new InputFilter.AllCaps(), new InputFilter.LengthFilter(17) });

        vehicleModel = MainActivity.vehicleModel;
    }

    private void deleteLastLicensePhoto() {
        File file = new File(AppCommon.licensePhotoPath);
        file.delete();
    }

    private boolean licensePhotoExist() {
        File file = new File(AppCommon.licensePhotoPath);
        return file.exists();
    }

    private void createSnFile(String editsString) {
        if (editsString != null && !editsString.equals("")) {
            File file = new File(AppCommon.licenseUtilPath);
            if (!file.exists()) {
                file.mkdir();
            }
            String filePATH = AppCommon.licenseUtilPath + "/idcard.sn";
            File newFile = new File(filePATH);
            try {
                newFile.delete();
                newFile.createNewFile();
                FileOutputStream fos = new FileOutputStream(newFile);
                StringBuffer sBuffer = new StringBuffer();
                sBuffer.append(editsString);
                fos.write(sBuffer.toString().toUpperCase().getBytes());
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * ??
     */
    private void recogniseLicense() {
        //
        String selectPath = AppCommon.licensePhotoPath;
        boolean cutBoolean = cut;

        try {
            Intent intent = new Intent("wintone.idcard");
            Bundle bundle = new Bundle();
            int nSubID[] = null;
            bundle.putInt("nTypeInitIDCard", 0); //?0??
            bundle.putString("lpFileName", selectPath);//?
            bundle.putInt("nTypeLoadImageToMemory", 0);//0???1??24
            bundle.putInt("nMainID", nMainID); //?6?2????????ID?????
            bundle.putIntArray("nSubID", nSubID); //????ID????????nSubID[0]=nullnMainID?

            //sn
            File file = new File(AppCommon.licenseUtilPath);
            String snString = null;
            if (file.exists()) {
                String filePATH = AppCommon.licenseUtilPath + "/IdCard.sn";
                File newFile = new File(filePATH);
                if (newFile.exists()) {
                    BufferedReader bfReader = new BufferedReader(new FileReader(newFile));
                    snString = bfReader.readLine().toUpperCase();
                    bfReader.close();
                } else {
                    bundle.putString("sn", "");
                }
                if (snString != null && !snString.equals("")) {
                    bundle.putString("sn", snString);
                } else {
                    bundle.putString("sn", "");
                }
            } else {
                bundle.putString("sn", "");
            }

            bundle.putString("authfile", ""); //?  /mnt/sdcard/AndroidWT/357816040594713_zj.txt
            bundle.putString("logo", ""); //logologo??
            bundle.putBoolean("isCut", cutBoolean); //??
            bundle.putString("returntype", "withvalue");//?withvalue???
            intent.putExtras(bundle);
            ((Activity) getContext()).startActivityForResult(intent, 8);
        } catch (Exception e) {
            Toast.makeText(getContext(), "?" + "wintone.idcard", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * ???
     */
    private void reRecognise(final int text) {
        MyAlertDialog.showAlert(getContext(), text, R.string.alert, MyAlertDialog.BUTTON_STYLE_OK_CANCEL,
                new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(Message message) {
                        switch (message.what) {
                        case MyAlertDialog.POSITIVE_PRESSED:
                            //fillInDummyData();
                            recogniseLicense();
                            if (text == R.string.reRecognise1)
                                mHideContentCallback.hideContent();
                            break;
                        case MyAlertDialog.NEGATIVE_PRESSED:
                            break;
                        }

                        return true;
                    }
                }));
    }

    /**
     * 
     * @param arrayId
     * @param editViewId
     */
    private void choose(final int arrayId, final int editViewId) {
        View view1 = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.popup_layout, null);

        final AlertDialog dialog = new AlertDialog.Builder(getContext()).setView(view1).create();

        TableLayout contentArea = (TableLayout) view1.findViewById(R.id.contentArea);

        final ListView listView = new ListView(view1.getContext());

        listView.setAdapter(new ArrayAdapter<String>(view1.getContext(), android.R.layout.simple_list_item_1,
                view1.getResources().getStringArray(arrayId)));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                dialog.dismiss();
                String temp = (String) listView.getItemAtPosition(i);
                setEditViewText(rootView, editViewId, temp);
            }
        });

        contentArea.addView(listView);

        setTextView(view1, R.id.title, getResources().getString(R.string.alert));

        dialog.show();
    }

    /**
     * VIN??
     */
    private void checkVinAndGetCarSettings() {
        InputMethodManager imm = (InputMethodManager) rootView.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(findViewById(R.id.vin_edit).getWindowToken(), 0);

        final String vinString = getEditViewText(rootView, R.id.vin_edit);

        // ?
        if (vinString.equals("")) {
            Toast.makeText(rootView.getContext(), "VIN?", Toast.LENGTH_SHORT).show();
            findViewById(R.id.vin_edit).requestFocus();
            return;
        }

        // VIN?
        if (!isVin(vinString)) {
            View view1 = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.popup_layout, null);
            TableLayout contentArea = (TableLayout) view1.findViewById(R.id.contentArea);
            TextView content = new TextView(view1.getContext());
            content.setText("VIN?: " + vinString + "\n"
                    + "VIN?????\n");
            content.setTextSize(20f);
            contentArea.addView(content);

            setTextView(view1, R.id.title, getResources().getString(R.string.alert));

            AlertDialog dialog = new AlertDialog.Builder(rootView.getContext()).setView(view1)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            setEditViewText(rootView, R.id.brand_edit, "");
                            findViewById(R.id.brand_select_button).setEnabled(false);

                            // ???VIN
                            getCarSettingsFromServer();
                        }
                    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            findViewById(R.id.vin_edit).requestFocus();
                        }
                    }).create();

            dialog.show();
            return;
        }

        setEditViewText(rootView, R.id.brand_edit, "");
        findViewById(R.id.brand_select_button).setEnabled(false);

        // ???VIN
        getCarSettingsFromServer();
    }

    /**
     * ???(seriesId + modelId)
     * @param seriesId seriesId + modelId
     */
    private void getCarSettingsFromServer(String seriesId) {
        GetCarSettingsTask getCarSettingsTask = new GetCarSettingsTask(getContext(), seriesId,
                new GetCarSettingsTask.OnGetCarSettingsFinished() {
                    @Override
                    public void onFinished(String result) {
                        // ?
                        updateCarSettings();

                        // UI
                        updateUi();
                    }

                    @Override
                    public void onFailed(String result) {
                        // ??
                        Toast.makeText(rootView.getContext(), result, Toast.LENGTH_SHORT).show();
                        Log.d("DFCarChecker", "???" + result);
                    }
                });
        getCarSettingsTask.execute();
    }

    /**
     * ??? (vin)
     */
    private void getCarSettingsFromServer() {
        String vin = getEditViewText(rootView, R.id.vin_edit);

        GetCarSettingsByVinTask getCarSettingsByVinTask = new GetCarSettingsByVinTask(getContext(), vin,
                new GetCarSettingsByVinTask.OnGetCarSettingsFinished() {
                    @Override
                    public void onFinished(String result) {
                        final List<String> modelNames;
                        final List<JSONObject> jsonObjects;

                        try {
                            JSONArray jsonArray = new JSONArray(result);
                            jsonObjects = new ArrayList<JSONObject>();

                            // ??string list
                            modelNames = new ArrayList<String>();

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                                Country country;
                                Brand brand;
                                Manufacturer manufacturer;
                                Series series;
                                Model model;

                                // ?ID?
                                if (jsonObject.has("countryId"))
                                    country = vehicleModel.getCountryById(jsonObject.getString("countryId"));
                                else
                                    country = vehicleModel.countries.get(0);

                                if (jsonObject.has("brandId"))
                                    brand = country.getBrandById(jsonObject.getString("brandId"));
                                else
                                    brand = country.brands.get(0);

                                if (jsonObject.has("manufacturerId"))
                                    manufacturer = brand
                                            .getManufacturerById(jsonObject.getString("manufacturerId"));
                                else
                                    manufacturer = brand.manufacturers.get(0);

                                if (jsonObject.has("seriesId"))
                                    series = manufacturer.getSeriesById(jsonObject.getString("seriesId"));
                                else
                                    series = manufacturer.serieses.get(0);

                                if (jsonObject.has("modelId"))
                                    model = series.getModelById(jsonObject.getString("modelId"));
                                else
                                    model = series.models.get(0);

                                // ??list??
                                jsonObjects.add(jsonObject);
                                modelNames.add(manufacturer.name + " " + series.name + " " + model.name);
                            }

                            String[] tempArray = new String[modelNames.size() + 1];

                            for (int i = 0; i < modelNames.size(); i++) {
                                tempArray[i] = modelNames.get(i);
                            }

                            showModelChooseDialog(tempArray, new OnChooseModelFinished() {
                                @Override
                                public void onFinished(int index) throws JSONException {
                                    // ?????
                                    if (index == modelNames.size()) {
                                        selectCarManually();
                                    }
                                    // ?
                                    else {
                                        JSONObject jsonObject = jsonObjects.get(index);

                                        Country country = vehicleModel
                                                .getCountryById(jsonObject.getString("countryId"));
                                        Brand brand = country.getBrandById(jsonObject.getString("brandId"));
                                        Manufacturer manufacturer = brand
                                                .getManufacturerById(jsonObject.getString("manufacturerId"));
                                        Series series = manufacturer
                                                .getSeriesById(jsonObject.getString("seriesId"));
                                        Model model = series.getModelById(jsonObject.getString("modelId"));

                                        // ?idspinner?
                                        lastCountryIndex = vehicleModel.getCountryNames().indexOf(country.name);
                                        lastBrandIndex = country.getBrandNames().indexOf(brand.name);
                                        lastManufacturerIndex = brand.getManufacturerNames()
                                                .indexOf(manufacturer.name);
                                        lastSeriesIndex = manufacturer.getSeriesNames().indexOf(series.name);
                                        lastModelIndex = series.getModelNames().indexOf(model.name);

                                        // ??
                                        updateCarSettings();

                                        // UI
                                        updateUi();
                                    }
                                }
                            });
                        } catch (JSONException e) {
                            Log.d("DFCarChecker", "Json?" + e.getMessage());
                        }
                    }

                    @Override
                    public void onFailed(String result) {
                        // ??
                        Log.d("DFCarChecker", "???" + result);

                        if (result.equals("VIN??")) {
                            View view1 = ((Activity) getContext()).getLayoutInflater()
                                    .inflate(R.layout.popup_layout, null);
                            TableLayout contentArea = (TableLayout) view1.findViewById(R.id.contentArea);
                            TextView content = new TextView(view1.getContext());
                            content.setText("?");
                            content.setTextSize(20f);
                            contentArea.addView(content);

                            setTextView(view1, R.id.title, getResources().getString(R.string.alert));

                            AlertDialog dialog = new AlertDialog.Builder(getContext()).setView(view1)
                                    .setPositiveButton(R.string.ok, null)
                                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
                                        @Override
                                        public void onDismiss(DialogInterface dialogInterface) {
                                            // ?
                                            selectCarManually();
                                            showView(rootView, R.id.brand_input, true);
                                        }
                                    }).create();

                            dialog.show();

                            // ??
                            updateUi();
                        } else {
                            // ??
                            Log.d("DFCarChecker", "???" + result);
                            Toast.makeText(getContext(), result, Toast.LENGTH_LONG).show();
                        }
                    }
                });
        getCarSettingsByVinTask.execute();
    }

    /**
     * ?
     * @param array
     * @param mCallback
     * @throws JSONException
     */
    private void showModelChooseDialog(String[] array, final OnChooseModelFinished mCallback) throws JSONException {
        array[array.length - 1] = "";

        View view1 = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.popup_layout, null);

        final AlertDialog dialog = new AlertDialog.Builder(getContext()).setView(view1).create();

        TableLayout contentArea = (TableLayout) view1.findViewById(R.id.contentArea);
        final ListView listView = new ListView(view1.getContext());
        listView.setAdapter(
                new ArrayAdapter<String>(view1.getContext(), android.R.layout.simple_list_item_1, array));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                try {
                    mCallback.onFinished(i);
                    dialog.dismiss();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        contentArea.addView(listView);

        setTextView(view1, R.id.title, getResources().getString(R.string.select_model));

        dialog.show();
    }

    /**
     * 
     */
    private void selectCarManually() {
        if (!mCarSettings.getBrandString().equals("")) {
            View view1 = ((Activity) rootView.getContext()).getLayoutInflater().inflate(R.layout.popup_layout,
                    null);
            TableLayout contentArea = (TableLayout) view1.findViewById(R.id.contentArea);
            TextView content = new TextView(view1.getContext());
            content.setText(R.string.reMatch);
            content.setTextSize(20f);
            contentArea.addView(content);

            setTextView(view1, R.id.title, getResources().getString(R.string.alert));

            AlertDialog dialog = new AlertDialog.Builder(rootView.getContext()).setView(view1)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            mHideContentCallback.hideContent();
                            showSelectCarDialog();
                        }
                    }).setNegativeButton(R.string.cancel, null).create();

            dialog.show();
        } else {
            showSelectCarDialog();
        }
    }

    /**
     * ?
     */
    private void showSelectCarDialog() {
        View view = LayoutInflater.from(rootView.getContext()).inflate(R.layout.vehicle_model_select, null);

        TextView title = (TextView) view.findViewById(R.id.title);
        title.setText(R.string.select_model);

        initModelSelectEdits(view);

        AlertDialog dialog = new AlertDialog.Builder(rootView.getContext()).setView(view)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // ?Spinner?
                        if (lastCountryIndex == 0 || lastBrandIndex == 0 || lastManufacturerIndex == 0
                                || lastSeriesIndex == 0 || lastModelIndex == 0) {
                            Toast.makeText(rootView.getContext(), "", Toast.LENGTH_SHORT)
                                    .show();

                            return;
                        }

                        Country country = vehicleModel.countries.get(lastCountryIndex - 1);
                        Brand brand = country.brands.get(lastBrandIndex - 1);
                        Manufacturer manufacturer = brand.manufacturers.get(lastManufacturerIndex - 1);
                        Series series = manufacturer.serieses.get(lastSeriesIndex - 1);
                        Model model = series.models.get(lastModelIndex - 1);

                        // ?seriesIdmodelId????  config:powerWindows,powerSeats...
                        getCarSettingsFromServer(series.id + "," + model.id);
                    }
                }).setNegativeButton(R.string.cancel, null).create();

        dialog.show();
    }

    /**
     * UI
     */
    private void updateUi() {
        showView(rootView, R.id.brand_input, true);
        showView(rootView, R.id.recognise_button, false);

        // ?EditText
        setEditViewText(rootView, R.id.brand_edit, mCarSettings.getBrandString());

        // ???????
        enableView(rootView, R.id.plateNumber_edit, false);
        enableView(rootView, R.id.licenseModel_edit, false);
        enableView(rootView, R.id.vehicleType_edit, false);
        enableView(rootView, R.id.useCharacter_edit, false);
        enableView(rootView, R.id.engineSerial_edit, false);

        if (mCarSettings.getSeries() != null)
            // 
            if (!modify) {
                // ?ui
                mShowContentCallback.showContent(getEditViewText(rootView, R.id.vin_edit),
                        getEditViewText(rootView, R.id.plateNumber_edit),
                        getEditViewText(rootView, R.id.licenseModel_edit),
                        getEditViewText(rootView, R.id.vehicleType_edit),
                        getEditViewText(rootView, R.id.useCharacter_edit),
                        getEditViewText(rootView, R.id.engineSerial_edit), mCarSettings.getSeries().id,
                        mCarSettings.getModel().id);
            }
            // 
            else {
                mShowContentCallback.modify(Integer.toString(carId));
            }

    }

    /**
     * ??
     */
    private void updateCarSettings() {
        Country country = vehicleModel.countries.get(lastCountryIndex - 1);
        Brand brand = country.brands.get(lastBrandIndex - 1);
        Manufacturer manufacturer = brand.manufacturers.get(lastManufacturerIndex - 1);
        Series series = manufacturer.serieses.get(lastSeriesIndex - 1);
        Model model = series.models.get(lastModelIndex - 1);

        // 
        String brandString = manufacturer.name + " " + series.name + " " + model.name;

        // ??
        mCarSettings.setBrandString(brandString);
        mCarSettings.setCountry(country);
        mCarSettings.setBrand(brand);
        mCarSettings.setManufacturer(manufacturer);
        mCarSettings.setSeries(series);
        mCarSettings.setModel(model);

        enableView(rootView, R.id.brand_select_button, true);
    }

    /**
     * ??
     * @param seriesId seriesId
     * @param modelId modelId
     */
    private void modifyCarSettings(String seriesId, String modelId) {
        // ??
        Country country = null;
        Brand brand = null;
        Manufacturer manufacturer = null;
        Series series = null;
        Model model = null;

        boolean found = false;

        for (Country country1 : vehicleModel.getCountries()) {
            for (Brand brand1 : country1.brands) {
                for (Manufacturer manufacturer1 : brand1.manufacturers) {
                    for (Series series1 : manufacturer1.serieses) {
                        if (series1.id.equals(seriesId)) {
                            manufacturer = manufacturer1;
                            brand = brand1;
                            country = country1;
                            series = series1;
                            model = series.getModelById(modelId);

                            found = true;
                            break;
                        }
                    }
                    if (found)
                        break;
                }
                if (found)
                    break;
            }
            if (found)
                break;
        }

        mCarSettings.setCountry(country);
        mCarSettings.setBrand(brand);
        mCarSettings.setManufacturer(manufacturer);
        mCarSettings.setSeries(series);
        mCarSettings.setModel(model);

        // 
        String brandString = manufacturer.name + " " + series.name + " " + model.name;

        // ??
        mCarSettings.setBrandString(brandString);

        updateUi();
    }

    /**
     * ?
     * @param view
     */
    private void initModelSelectEdits(View view) {
        TextView title = (TextView) view.findViewById(R.id.title);
        title.setText(R.string.select_model);

        countryEdit = (EditText) view.findViewById(R.id.country_edit);
        brandEdit = (EditText) view.findViewById(R.id.brand_edit);
        manufacturerEdit = (EditText) view.findViewById(R.id.manufacturer_edit);
        seriesEdit = (EditText) view.findViewById(R.id.series_edit);
        modelEdit = (EditText) view.findViewById(R.id.model_edit);

        setCountryEdit();

        if (mCarSettings.getCountry() != null) {
            countryEdit.setText(mCarSettings.getCountry().name);
            brandEdit.setText(mCarSettings.getBrand().name);
            manufacturerEdit.setText(mCarSettings.getManufacturer().name);
            seriesEdit.setText(mCarSettings.getSeries().name);
            modelEdit.setText(mCarSettings.getModel().name);
        }
    }

    /**
     * edit
     */
    private void setCountryEdit() {
        final ArrayAdapter<String> adapter;

        if (vehicleModel == null) {
            adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1,
                    Helper.getEmptyStringList());
        } else {
            adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1,
                    vehicleModel.getCountryNames());
        }

        countryEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                showListDialog(R.string.chooseCountry, adapter, new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(Message message) {
                        if (message.what == 0) {
                            countryEdit.setText("");
                            brandEdit.setText("");
                            manufacturerEdit.setText("");
                            seriesEdit.setText("");
                            modelEdit.setText("");
                        } else {
                            Country country = vehicleModel.getCountries().get(message.what - 1);
                            countryEdit.setText(country.name);

                            // ??edit
                            if (lastCountryIndex != message.what) {
                                brandEdit.setText("");
                                manufacturerEdit.setText("");
                                seriesEdit.setText("");
                                modelEdit.setText("");
                            }

                            setBrandEdit(country);
                        }

                        lastCountryIndex = message.what;

                        return true;
                    }
                }));
            }
        });
    }

    /**
     * ?edit
     */
    private void setBrandEdit(final Country country) {
        // adaptercountrybrand??
        final ArrayAdapter<String> adapter;

        if (country == null) {
            adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1,
                    Helper.getEmptyStringList());
        } else {
            adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1,
                    country.getBrandNames());
        }

        final Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what == 0) {
                    brandEdit.setText("");
                    manufacturerEdit.setText("");
                    seriesEdit.setText("");
                    modelEdit.setText("");
                } else {
                    Brand brand = country.brands.get(message.what - 1);
                    brandEdit.setText(brand.name);

                    // ??edit
                    if (lastBrandIndex != message.what) {
                        manufacturerEdit.setText("");
                        seriesEdit.setText("");
                        modelEdit.setText("");
                    }

                    setManufacturerEdit(brand);
                }

                lastBrandIndex = message.what;

                return true;
            }
        });

        // ????manufacturer
        if (country.getBrandNames().size() == 2) {
            lastBrandIndex = 1;
            brandEdit.setText(country.getBrandNames().get(1));
            setManufacturerEdit(country.brands.get(0));
            return;
        }
        // ?brand
        else {
            showListDialog(R.string.chooseBrand, adapter, handler);
        }

        // brandEdit
        brandEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                showListDialog(R.string.chooseBrand, adapter, handler);
            }
        });
    }

    /**
     * edit
     */
    private void setManufacturerEdit(final Brand brand) {
        // adapterbrandmanufacturer??
        final ArrayAdapter<String> adapter;

        if (brand == null) {
            adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1,
                    Helper.getEmptyStringList());
        } else {
            adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1,
                    brand.getManufacturerNames());
        }

        final Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what == 0) {
                    manufacturerEdit.setText("");
                    seriesEdit.setText("");
                    modelEdit.setText("");
                } else {
                    Manufacturer manufacturer = brand.manufacturers.get(message.what - 1);
                    manufacturerEdit.setText(manufacturer.name);

                    // ??edit
                    if (lastManufacturerIndex != message.what) {
                        seriesEdit.setText("");
                        modelEdit.setText("");
                    }

                    setSeriesEdit(manufacturer);
                }

                lastManufacturerIndex = message.what;

                return true;
            }
        });

        // ????series
        if (brand.getManufacturerNames().size() == 2) {
            lastManufacturerIndex = 1;
            manufacturerEdit.setText(brand.getManufacturerNames().get(1));
            setSeriesEdit(brand.manufacturers.get(0));
            return;
        }
        // ?manufactuer
        else {
            showListDialog(R.string.chooseManufacturer, adapter, handler);
        }

        // manufacturerEdit
        manufacturerEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                showListDialog(R.string.chooseManufacturer, adapter, handler);
            }
        });
    }

    /**
     * edit
     */
    private void setSeriesEdit(final Manufacturer manufacturer) {
        // adaptermanufacturerseries??
        final ArrayAdapter<String> adapter;

        if (manufacturer == null) {
            adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1,
                    Helper.getEmptyStringList());
        } else {
            adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1,
                    manufacturer.getSeriesNames());
        }

        final Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what == 0) {
                    seriesEdit.setText("");
                    modelEdit.setText("");
                } else {
                    Series series = manufacturer.serieses.get(message.what - 1);
                    seriesEdit.setText(series.name);

                    // ??edit
                    if (lastSeriesIndex != message.what) {
                        modelEdit.setText("");
                    }

                    setModelEdit(series);
                }

                lastSeriesIndex = message.what;

                return true;
            }
        });

        // ????series
        if (manufacturer.getSeriesNames().size() == 2) {
            lastSeriesIndex = 1;
            seriesEdit.setText(manufacturer.getSeriesNames().get(1));
            setModelEdit(manufacturer.serieses.get(0));
            return;
        }
        // ?series
        else {
            showListDialog(R.string.chooseSeries, adapter, handler);
        }

        // manufacturerEdit
        seriesEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                showListDialog(R.string.chooseSeries, adapter, handler);
            }
        });
    }

    /**
     * edit
     */
    private void setModelEdit(final Series series) {
        // adapterseriesmodel??
        final ArrayAdapter<String> adapter;

        if (series == null) {
            adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1,
                    Helper.getEmptyStringList());
        } else {
            adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1,
                    series.getModelNames());
        }

        final Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                lastModelIndex = message.what;

                if (message.what == 0) {
                    modelEdit.setText("");
                } else {
                    Model model = series.models.get(message.what - 1);
                    modelEdit.setText(model.name);
                }

                return true;
            }
        });

        // ???
        if (series.getModelNames().size() == 2) {
            lastModelIndex = 1;
            modelEdit.setText(series.getModelNames().get(1));
            return;
        }
        // ?model
        else {
            showListDialog(R.string.chooseModel, adapter, handler);
        }

        // manufacturerEdit
        modelEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                showListDialog(R.string.chooseModel, adapter, handler);
            }
        });
    }

    /**
     * 
     */
    private void showListDialog(int titleId, ArrayAdapter<String> adapter, final Handler handler) {
        View view1 = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.popup_layout, null);

        final AlertDialog dialog = new AlertDialog.Builder(getContext()).setView(view1).create();

        TableLayout contentArea = (TableLayout) view1.findViewById(R.id.contentArea);
        final ListView listView = new ListView(view1.getContext());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                handler.sendEmptyMessage(i);
                dialog.dismiss();
            }
        });
        contentArea.addView(listView);

        setTextView(view1, R.id.title, getResources().getString(titleId));

        dialog.show();
    }

    /**
     * ?
     */
    private void fillInDummyData() {
        // ???
        enableView(rootView, R.id.plateNumber_edit, true);
        enableView(rootView, R.id.licenseModel_edit, true);
        enableView(rootView, R.id.vehicleType_edit, true);
        enableView(rootView, R.id.useCharacter_edit, true);
        enableView(rootView, R.id.engineSerial_edit, true);

        setEditViewText(rootView, R.id.plateNumber_edit, "M26332");
        setEditViewText(rootView, R.id.licenseModel_edit, "SQR7160");
        setEditViewText(rootView, R.id.vehicleType_edit, "?");
        setEditViewText(rootView, R.id.useCharacter_edit, "???");
        setEditViewText(rootView, R.id.engineSerial_edit, "K1H00875");
        setEditViewText(rootView, R.id.vin_edit, "LSJDA11A21D012476");
    }

    /**
     * ??
     * @param jsonString
     */
    public void fillInData(String jsonString) {
        modify = true;

        try {
            JSONObject jsonObject = new JSONObject(jsonString);

            carId = jsonObject.getInt("carId");

            setEditViewText(rootView, R.id.plateNumber_edit, jsonObject.getString("plateNumber"));
            setEditViewText(rootView, R.id.licenseModel_edit, jsonObject.getString("licenseModel"));
            setEditViewText(rootView, R.id.vehicleType_edit, jsonObject.getString("vehicleType"));
            setEditViewText(rootView, R.id.useCharacter_edit, jsonObject.getString("useCharacter"));
            setEditViewText(rootView, R.id.engineSerial_edit, jsonObject.getString("engineSerial"));
            setEditViewText(rootView, R.id.vin_edit, jsonObject.getString("vin"));

            modifyCarSettings(jsonObject.getString("seriesId"), jsonObject.getString("modelId"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void fillInData(int carId) {
        mShowContentCallback.modify(Integer.toString(carId));
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            fillInRecogData((String[]) message.obj);
            return false;
        }
    });

    public static void fillInRecogData(String[] fields) {
        setEditViewText(rootView, R.id.plateNumber_edit, fields[0]);
        setEditViewText(rootView, R.id.licenseModel_edit, fields[1]);
        setEditViewText(rootView, R.id.vehicleType_edit, fields[2]);
        setEditViewText(rootView, R.id.useCharacter_edit, fields[3]);
        setEditViewText(rootView, R.id.engineSerial_edit, fields[4]);
        setEditViewText(rootView, R.id.vin_edit, fields[5]);
    }

    public void startAuthService(String authCode) {
        this.authCode = authCode;
        Intent authIntent = new Intent(getContext(), AuthService.class);
        getContext().bindService(authIntent, authConn, Service.BIND_AUTO_CREATE);
    }

    public void updateLicensePhoto(boolean cut) {
        this.cut = cut;

        Bitmap bitmap = BitmapFactory.decodeFile(AppCommon.licensePhotoPath);

        licenseImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        licenseImageView.setImageBitmap(bitmap);

        recogniseButton.setVisibility(VISIBLE);
    }

    /**
     * InputProceduresLayout?
     * ??
     */
    public interface OnShowContent {
        public void showContent(String vin, String plateNumber, String licenseModel, String vehicleType,
                String useCharacter, String engineSerial, String seriesId, String modelId);

        public void modify(String carId);
    }

    /**
     * InputProceduresLayout?
     * ????
     */
    public interface OnHideContent {
        public void hideContent();
    }

    /**
     * getCarSettingsFromServer()?
     */
    public interface OnChooseModelFinished {
        public void onFinished(int index) throws JSONException;
    }
}