OSDN Git Service

d4a2a03b56dcd8e2543c3e3baaeb3791a8c3ba93
[gokigen/JoggingTimer.git] / wear / src / main / java / net / osdn / gokigen / joggingtimer / utilities / ConfirmationDialog.java
1 package net.osdn.gokigen.joggingtimer.utilities;
2
3 import android.app.AlertDialog;
4 import android.app.Dialog;
5 import android.app.DialogFragment;
6 import android.content.Context;
7 import android.content.DialogInterface;
8 import android.os.Bundle;
9 import android.support.annotation.NonNull;
10 import android.util.Log;
11
12 import net.osdn.gokigen.joggingtimer.R;
13
14 /**
15  *   確認ダイアログの表示
16  *
17  *
18  */
19 public class ConfirmationDialog extends DialogFragment
20 {
21     private final String TAG = toString();
22     String title = "";
23     String message = "";
24     Callback callback = null;
25     Dialog myDialog = null;
26
27     public static ConfirmationDialog newInstance(String title, String message, @NonNull Callback callback)
28     {
29         ConfirmationDialog instance = new ConfirmationDialog();
30         instance.prepare(callback, title, message);
31
32         // パラメータはBundleにまとめておく
33         Bundle arguments = new Bundle();
34         arguments.putString("title", title);
35         arguments.putString("message", message);
36         instance.setArguments(arguments);
37
38         return (instance);
39     }
40
41     /**
42      *
43      *
44      */
45     private void prepare(Callback callback, String title, String message)
46     {
47         this.callback = callback;
48         this.title = title;
49         this.message = message;
50     }
51
52     /**
53      *
54      *
55      */
56     @Override
57     public @NonNull Dialog onCreateDialog(Bundle savedInstanceState)
58     {
59         String title = this.title;
60         String message = this.message;
61         if (savedInstanceState != null)
62         {
63             title = savedInstanceState.getString("title");
64             message = savedInstanceState.getString("message");
65         }
66         Context context = getContext();
67         AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
68         alertDialog.setTitle(title);
69         alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
70         alertDialog.setMessage(message);
71         alertDialog.setCancelable(true);
72         String positiveLabel = "OK";
73         String negativeLabel = "Cancel";
74         if (context != null)
75         {
76             positiveLabel = context.getString(R.string.dialog_positive_execute);
77             negativeLabel = context.getString(R.string.dialog_negative_cancel);
78         }
79
80         // ボタンを設定する(実行ボタン)
81         alertDialog.setPositiveButton(positiveLabel,
82                 new DialogInterface.OnClickListener() {
83                     public void onClick(DialogInterface dialog, int which)
84                     {
85                         Log.v(TAG, "ConfirmationDialog::OK");
86                         if (callback != null)
87                         {
88                             callback.confirm();
89                         }
90                         dialog.dismiss();
91                     }
92                 });
93
94         // ボタンを設定する (キャンセルボタン)
95         alertDialog.setNegativeButton(negativeLabel,
96                 new DialogInterface.OnClickListener() {
97                     public void onClick(DialogInterface dialog, int which)
98                     {
99                         dialog.cancel();
100                     }
101                 });
102
103         myDialog = alertDialog.create();
104         return (myDialog);
105     }
106
107     @Override
108     public void onPause()
109     {
110         super.onPause();
111         Log.v(TAG, "AlertDialog::onPause()");
112         if (myDialog != null)
113         {
114             myDialog.cancel();
115         }
116     }
117
118
119     // コールバックインタフェース
120     public interface Callback
121     {
122         void confirm(); // OKを選択したとき
123     }
124 }