package com.growthpush.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import com.growthpush.handler.DefaultReceiveHandler;

/**
 * Created by Shigeru Ogawa on 13/08/12.
 */
public class AlertFragment extends DialogFragment implements DialogInterface.OnClickListener {

	public AlertFragment() {
		super();
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		Dialog dialog = generateAlertDialog();
		if (dialog == null)
			return super.onCreateDialog(savedInstanceState);

		return dialog;

	}

	private Dialog generateAlertDialog() {

		PackageManager packageManager = getActivity().getPackageManager();
		ApplicationInfo applicationInfo = null;
		try {
			applicationInfo = packageManager.getApplicationInfo(getActivity().getPackageName(), 0);
		} catch (NameNotFoundException e) {
			return null;
		}

		Dialog dialog = new AlertDialog.Builder(getActivity()).setIcon(packageManager.getApplicationIcon(applicationInfo))
				.setTitle(packageManager.getApplicationLabel(applicationInfo)).setMessage(getArguments().getString("message"))
				.setPositiveButton("OK", this).setNegativeButton("Cancel", this).create();

		dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		dialog.setCanceledOnTouchOutside(false);

		return dialog;
	}

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

		switch (which) {
		case DialogInterface.BUTTON_POSITIVE:

			dialog.dismiss();

			DefaultReceiveHandler.Callback callback = AlertActivity.getSharedCallback();
			if (callback != null)
				callback.onOpen(this.getActivity(), this.getActivity().getIntent());

			NotificationManager manager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
			manager.cancel("GrowthPush" + getActivity().getPackageName(), 1);
			break;

		case DialogInterface.BUTTON_NEGATIVE:

			dialog.dismiss();
			getActivity().finish();
			break;

		default:
			break;
		}

	}

}

@@ -23,16 +23,45 @@
 .view;%0A%0A
+import android.app.Activity;%0A
 import a
@@ -1867,26 +1867,227 @@
 N_POSITIVE:%0A
-%0A
 %09
+%09%09onClickPositive(dialog);%0A%09%09%09break;%0A%09%09case DialogInterface.BUTTON_NEGATIVE:%0A%09%09%09onClickNegative(dialog);%0A%09%09%09break;%0A%09%09default:%0A%09%09%09break;%0A%09%09%7D%0A%0A%09%7D%0A%0A%09private void onClickPositive(DialogInterface dialog) %7B%0A%0A
 %09%09dialog.dis
@@ -2095,17 +2095,16 @@
 iss();%0A%0A
-%09
 %09%09Defaul
@@ -2176,17 +2176,16 @@
 ck();%0A%09%09
-%09
 if (call
@@ -2198,17 +2198,16 @@
 = null)%0A
-%09
 %09%09%09callb
@@ -2271,16 +2271,79 @@
 nt());%0A%0A
+%09%09Activity activity = getActivity();%0A%09%09if (activity != null) %7B%0A
 %09%09%09Notif
@@ -2357,17 +2357,29 @@
 Manager 
-m
+notificationM
 anager =
@@ -2466,17 +2466,66 @@
 CE);%0A%09%09%09
-m
+if (notificationManager != null)%0A%09%09%09%09notificationM
 anager.c
@@ -2587,59 +2587,72 @@
 ;%0A%09%09
-%09break;%0A%0A%09%09case DialogInterface.BUTTON_NEGATIVE:
+%7D%0A%0A%09%7D%0A%0A%09private void onClickNegative(DialogInterface dialog) %7B
 %0A%0A
-%09
 %09%09di
@@ -2671,70 +2671,91 @@
 ();%0A
+%0A
 %09%09
-%09getActivity().finish();%0A%09%09%09break;%0A%0A%09%09default:%0A%09%09%09break;%0A%09%09%7D
+Activity activity = getActivity();%0A%09%09if (activity != null)%0A%09%09%09activity.finish();
 %0A%0A%09%7D

Refactor AlertFragment