com.auto.venerisauto.aftersales.services.various.KteoActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.auto.venerisauto.aftersales.services.various.KteoActivity.java

Source

package com.auto.venerisauto.aftersales.services.various;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import com.auto.venerisauto.R;
import com.auto.venerisauto.fragments.DatePickerFragment;

import android.app.ActionBar;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class KteoActivity extends FragmentActivity {

    private Button submitButton;
    private EditText name;
    private EditText phone;
    private EditText emailText;
    private EditText carModel;
    private EditText carNumber;
    private Button changeDateButton;
    private Button licenseDateButton;
    private Button lastcheckButton;
    private TextView licenseDate;
    private TextView lastCheckDate;
    private EditText carVendor;
    private EditText comments;
    private TextView date;
    private String email_recipient;

    private final static Map<String, Integer> CAR_VENDORS = new HashMap<String, Integer>() {
        private static final long serialVersionUID = 2896441016065748354L;
        {
            put("Chevrolet", Integer.valueOf(R.drawable.top_bar_chevrolet));
            put("Nissan", Integer.valueOf(R.drawable.top_bar_nissan));
            put("Seat", Integer.valueOf(R.drawable.top_bar_seat));
            put("Ssangyong", Integer.valueOf(R.drawable.top_bar_ssangyong));

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kteo);
        addButtonsListeners();
        setCurrentDateOnView();

        ActionBar actionBar = getActionBar();
        actionBar.setBackgroundDrawable(
                getResources().getDrawable(CAR_VENDORS.get(getIntent().getStringExtra("carVendor")).intValue()));

        if ("Nissan".equals(getIntent().getStringExtra("carVendor"))) {
            email_recipient = "eveneris@nissan.gr";
        } else if ("Seat".equals(getIntent().getStringExtra("carVendor"))) {
            email_recipient = "info@seat-veneris.gr";
        } else if ("Chevrolet".equals(getIntent().getStringExtra("carVendor"))) {
            email_recipient = "veneris@chevroletnet.gr";
        } else {
            email_recipient = " info@venerisauto.gr";
        }

    }

    public void addButtonsListeners() {

        submitButton = (Button) findViewById(R.id.ButtonSend);
        submitButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sendEmail();
                name.setText("");
                emailText.setText("");
                phone.setText("");
                comments.setText("");
                name.setText("");
                emailText.setText("");
                phone.setText("");
                carModel.setText("");
                carVendor.setText("");
                carNumber.setText("");
                licenseDate.setText("");
                lastCheckDate.setText("");
                date.setText("");
                comments.setText("");

            }
        });

        changeDateButton = (Button) findViewById(R.id.date);
        changeDateButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showDatePicker();
            }
        });

        licenseDateButton = (Button) findViewById(R.id.date_licence);
        licenseDateButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showDatePicker();
            }
        });

        lastcheckButton = (Button) findViewById(R.id.lastdate);
        lastcheckButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showDatePicker();
            }
        });

    }

    private void sendEmail() {
        // info@venerisauto.gr new Intent(Intent.ACTION_VIEW); Uri data =
        // Uri.parse("mailto:?subject=" + "blah blah subject" + "&body=" +
        // "blah blah body" + "&to=" + "sendme@me.com");
        Intent myIntent = getIntent();
        String[] recipients = { "info@venerisauto.gr" };
        Intent email = new Intent(Intent.ACTION_VIEW);
        recipients[0] = email_recipient;
        // prompts email clients only
        email.setType("message/rfc822");
        name = (EditText) findViewById(R.id.EditTextName1);
        emailText = (EditText) findViewById(R.id.EditTextName3);
        phone = (EditText) findViewById(R.id.EditTextName2);
        carModel = (EditText) findViewById(R.id.EditTextName4);
        carNumber = (EditText) findViewById(R.id.EditTextName5);
        date = (TextView) findViewById(R.id.selectedDate);
        carVendor = (EditText) findViewById(R.id.car_vendor);
        licenseDate = (TextView) findViewById(R.id.date_licence_text);
        lastCheckDate = (TextView) findViewById(R.id.last_date_text);
        comments = (EditText) findViewById(R.id.EditTextComments);
        String subject = new StringBuilder().append("  ")
                .append("  ??  ").append("  ")
                .append(myIntent.getStringExtra("carVendor")).toString();
        String body = new StringBuilder().append(": ").append(name.getText().toString())
                .append("\n : ").append(phone.getText().toString()).append("\n E-mail: ")
                .append(emailText.getText().toString()).append("\n ? : ")
                .append(carVendor.getText().toString()).append("\n  : ")
                .append(carModel.getText().toString()).append("\n ? 1 : ")
                .append(licenseDate.getText().toString()).append("\n ? ?: ")
                .append(carNumber.getText().toString())
                .append("\n  ? : ")
                .append(lastCheckDate.getText().toString())
                .append("\n  ? KTEO: ").append(date.getText().toString())
                .append("\n : ").append(comments.getText().toString()).toString();
        Uri data = Uri.parse("mailto:?subject=" + subject + "&body=" + body + "&to=" + recipients[0]);
        email.setData(data);
        try {
            // the user can choose the email client
            startActivity(Intent.createChooser(email, "Choose an email client from..."));

        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(KteoActivity.this, "No email client installed.", Toast.LENGTH_LONG).show();
        }

    }

    private void showDatePicker() {
        DatePickerFragment date = new DatePickerFragment();
        /**
         * Set Up Current Date Into dialog
         */
        Calendar calender = Calendar.getInstance();
        Bundle args = new Bundle();
        args.putInt("year", calender.get(Calendar.YEAR));
        args.putInt("month", calender.get(Calendar.MONTH));
        args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
        date.setArguments(args);
        /**
         * Set Call back to capture selected date
         */
        date.setCallBack(ondate);
        date.show(getSupportFragmentManager(), "Date Picker");
    }

    OnDateSetListener ondate = new OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            date.setText(new StringBuilder().append(dayOfMonth).append("-")
                    // Month is 0 based, just add 1
                    .append(monthOfYear + 1).append("-").append(year).append(" "));

        }

    };

    private void setCurrentDateOnView() {

        date = (TextView) findViewById(R.id.selectedDate);
        licenseDate = (TextView) findViewById(R.id.date_licence_text);
        lastCheckDate = (TextView) findViewById(R.id.last_date_text);
        Calendar c = Calendar.getInstance();

        // set current date into textview
        date.setText(new StringBuilder()
                // Month is 0 based, just add 1

                .append(c.get(Calendar.DAY_OF_MONTH)).append("-").append(c.get(Calendar.MONTH) + 1).append("-")
                .append(c.get(Calendar.YEAR)).append(" "));

        licenseDate.setText(new StringBuilder()
                // Month is 0 based, just add 1

                .append(c.get(Calendar.DAY_OF_MONTH)).append("-").append(c.get(Calendar.MONTH) + 1).append("-")
                .append(c.get(Calendar.YEAR)).append(" "));

        lastCheckDate.setText(new StringBuilder()
                // Month is 0 based, just add 1

                .append(c.get(Calendar.DAY_OF_MONTH)).append("-").append(c.get(Calendar.MONTH) + 1).append("-")
                .append(c.get(Calendar.YEAR)).append(" "));

    }

}