OSDN Git Service

660c1ae08ed02e9d97424ff79752c629cc0e6420
[gokigen/MeMoMa.git] / app / src / main / java / jp / sourceforge / gokigen / memoma / dialogs / ConfirmationDialog.java
1 package jp.sourceforge.gokigen.memoma.dialogs;
2
3 import android.app.AlertDialog;
4 import android.app.Dialog;
5 import android.content.Context;
6 import android.content.DialogInterface;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.widget.TextView;
10
11 import jp.sourceforge.gokigen.memoma.R;
12
13 /**
14  *   はい か いいえ を入力するダイアログを準備する
15  * 
16  * @author MRSa
17  *
18  */
19 public class ConfirmationDialog
20 {
21         private Context context;
22         private IResultReceiver resultReceiver = null;
23     private String  message = "";
24         private String  title = "";
25         private int    icon = 0;
26
27         public ConfirmationDialog(Context arg)
28         {
29                 context = arg;
30         }
31
32         /**
33          *  クラスの準備
34      *
35          */
36         public void prepare(IResultReceiver receiver, int titleIcon, String titleMessage, String confirmMessage)
37         {
38                 if (receiver != null)
39                 {
40                         resultReceiver = receiver;
41                 }
42                 icon = titleIcon;
43                 title = titleMessage;
44         message = confirmMessage;               
45         }
46
47     /**
48      *   確認ダイアログを応答する
49      *
50      */
51     public Dialog getDialog()
52     {
53         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
54         if (inflater == null)
55         {
56             return (null);
57         }
58         final View layout = inflater.inflate(R.layout.confirmationdialog, null);
59
60         AlertDialog.Builder builder = new AlertDialog.Builder(context);
61         final TextView  textView = layout.findViewById(R.id.confirm_message);
62
63         // 表示するデータ(アイコン、ダイアログタイトル、メッセージ)を準備する
64         if (icon != 0)
65         {
66             builder.setIcon(icon);
67         }
68         if (title != null)
69         {
70             builder.setTitle(title);
71         }
72         if (message != null)
73         {
74                 textView.setText(message);
75         }
76         builder.setView(layout);
77         builder.setCancelable(false);
78         builder.setPositiveButton(context.getString(R.string.confirmYes), new DialogInterface.OnClickListener()
79               {
80                    public void onClick(DialogInterface dialog, int id)
81                    {
82                            if (resultReceiver != null)
83                            {
84                                resultReceiver.acceptConfirmation();
85                            }
86                        dialog.cancel();
87                        System.gc();
88                    }
89                });
90         builder.setNegativeButton(context.getString(R.string.confirmNo), new DialogInterface.OnClickListener()
91                {
92                    public void onClick(DialogInterface dialog, int id)
93                    {
94                            if (resultReceiver != null)
95                            {
96                                resultReceiver.rejectConfirmation();
97                            }
98                        dialog.cancel();
99                        System.gc();
100                    }
101                });
102         return (builder.create());      
103     }
104
105     public interface IResultReceiver
106     {
107         void acceptConfirmation();
108         void rejectConfirmation();
109     }
110 }