dtu.ds.warnme.app.dialog.WarningDialog.java Source code

Java tutorial

Introduction

Here is the source code for dtu.ds.warnme.app.dialog.WarningDialog.java

Source

/**
 * Project:   warnme-app
 * File:      WarningDialog.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright: Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * Date:      Apr 2, 2014
 */

package dtu.ds.warnme.app.dialog;

import org.apache.http.Header;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.loopj.android.http.AsyncHttpResponseHandler;

import dtu.ds.warnme.app.R;
import dtu.ds.warnme.app.activity.MainActivity;
import dtu.ds.warnme.app.model.impl.Event;
import dtu.ds.warnme.app.ws.client.restful.RestClientHolder;

/**
 * @author Bartosz Cichecki
 * 
 */
public class WarningDialog extends DialogFragment {

    private final static String TAG = "WarningDialog";

    private AlertDialog dialog;

    private Event event;

    public Event getEvent() {
        return event;
    }

    private int getMessage(Event event) {
        switch (event.getEventType()) {
        case ACCIDENT:
            return R.string.warning_message_accident;
        case POLICE:
            return R.string.warning_message_police;
        case SPEEDCAMERA:
            return R.string.warning_message_speed_camera;
        }
        throw new IllegalStateException("Unknown value of EventType.");

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Event event = (Event) getArguments().get(MainActivity.KEY_EVENT);

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
        dialogBuilder.setTitle(R.string.warning);
        dialogBuilder.setMessage(getMessage(event));
        dialogBuilder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Nothing to do here...
            }
        });
        dialogBuilder.setNegativeButton(R.string.decline, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Nothing to do here...

            }
        });

        dialog = dialogBuilder.create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialogInterface) {
                final Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
                final Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);

                positiveButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        RestClientHolder.getRestClient().upVoteEvent(dialog.getContext(), event,
                                new AsyncHttpResponseHandler() {

                                    @Override
                                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody,
                                            Throwable error) {
                                        Log.i(TAG, "Failed to upvote event " + event.getId() + " cause: " + error);
                                    }

                                    @Override
                                    public void onFinish() {
                                        dialog.dismiss();
                                    }

                                    @Override
                                    public void onStart() {
                                        positiveButton.setEnabled(false);
                                        negativeButton.setEnabled(false);
                                    }

                                    @Override
                                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                                        Log.i(TAG, "Event " + event.getId() + " upvoted with success");
                                    }

                                });
                    }
                });

                negativeButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        RestClientHolder.getRestClient().downVoteEvent(dialog.getContext(), event,
                                new AsyncHttpResponseHandler() {

                                    @Override
                                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody,
                                            Throwable error) {
                                        Log.i(TAG,
                                                "Failed to downvote event " + event.getId() + " cause: " + error);
                                    }

                                    @Override
                                    public void onFinish() {
                                        dialog.dismiss();
                                    }

                                    @Override
                                    public void onStart() {
                                        positiveButton.setEnabled(false);
                                        negativeButton.setEnabled(false);
                                    }

                                    @Override
                                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                                        Log.i(TAG, "Event " + event.getId() + " downvoted with success");
                                    }

                                });
                    }
                });

            }
        });

        return dialog;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

}