OSDN Git Service

7703a107c9ab475d43a6369c2a443d37f8d2dbe5
[gokigen/JoggingTimer.git] / wear / src / main / java / net / osdn / gokigen / joggingtimer / utilities / CreateModelDataDialog.java
1 package net.osdn.gokigen.joggingtimer.utilities;
2
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.app.Dialog;
6 import android.app.DialogFragment;
7 import android.content.DialogInterface;
8 import android.os.Bundle;
9 import android.support.annotation.NonNull;
10 import android.util.Log;
11 import android.view.ContextThemeWrapper;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.widget.NumberPicker;
15 import android.widget.TextView;
16
17
18 import net.osdn.gokigen.joggingtimer.R;
19
20
21 /**
22  *
23  *
24  */
25 public class CreateModelDataDialog  extends DialogFragment
26 {
27     private final String TAG = toString();
28
29     private boolean isLap = true;
30     private String title = "";
31     private int lapCount = 0;
32     private Callback callback = null;
33     private long defaultValue = 0;
34     Dialog myDialog = null;
35
36     /**
37      *
38      *
39      */
40     public static CreateModelDataDialog newInstance(boolean isLap, String title, int lapCount, Callback callback, long defaultValue)
41     {
42         CreateModelDataDialog instance = new CreateModelDataDialog();
43         instance.prepare(isLap, title, lapCount, callback, defaultValue);
44
45         // パラメータはBundleにまとめておく
46         Bundle arguments = new Bundle();
47         arguments.putString("title", title);
48         //arguments.putString("message", message);
49         instance.setArguments(arguments);
50
51         return (instance);
52     }
53
54     /**
55      *
56      *
57      */
58     private void prepare(boolean isLap, String title, int lapCount, Callback callback, long defaultValue)
59     {
60         this.isLap = isLap;
61         this.title = title;
62         this.lapCount = lapCount;
63         this.callback = callback;
64         this.defaultValue = defaultValue;
65     }
66
67     /**
68      *
69      *
70      */
71     @Override
72     public @NonNull Dialog onCreateDialog(Bundle savedInstanceState)
73     {
74         Log.v(TAG, "show " + "def. : " + defaultValue);
75
76         Activity activity = getActivity();
77         // 確認ダイアログの生成
78         final AlertDialog.Builder alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.wear2_dialog_theme));
79
80         // Get the layout inflater
81         LayoutInflater inflater = activity.getLayoutInflater();
82         final View alertView = inflater.inflate(R.layout.time_model_picker, null, false);
83         alertDialog.setView(alertView);
84
85         final TextView titleText = alertView.findViewById(R.id.information_picker);
86         final TextView lapStartText = alertView.findViewById(R.id.lap_start);
87         final TextView lapEndText = alertView.findViewById(R.id.lap_end);
88         final NumberPicker lap = alertView.findViewById(R.id.number_picker_lap_count);
89         final NumberPicker hour = alertView.findViewById(R.id.number_picker_hours);
90         final NumberPicker minute = alertView.findViewById(R.id.number_picker_minutes);
91         final NumberPicker second = alertView.findViewById(R.id.number_picker_seconds);
92
93         try
94         {
95             if (title != null)
96             {
97                 titleText.setText(title);
98             }
99             if (isLap)
100             {
101                 lap.setVisibility(View.VISIBLE);
102                 lapStartText.setVisibility(View.VISIBLE);
103                 lapEndText.setVisibility(View.VISIBLE);
104                 lap.setMinValue(1);
105                 lap.setMaxValue(99);
106             }
107             else
108             {
109                 lap.setValue(lapCount);
110                 lap.setVisibility(View.GONE);
111                 lapStartText.setVisibility(View.GONE);
112                 lapEndText.setVisibility(View.GONE);
113             }
114
115             hour.setMinValue(0);
116             hour.setMaxValue(72);
117             minute.setMinValue(0);
118             minute.setMaxValue(59);
119             second.setMinValue(0);
120             second.setMaxValue(59);
121             second.setValue((int) (defaultValue / 1000) % 60);
122             minute.setValue((int) ((defaultValue / (1000 * 60)) % 60));
123             hour.setValue((int) ((defaultValue / (1000 * 60 * 60)) % 24));
124         }
125         catch (Exception e)
126         {
127             e.printStackTrace();
128         }
129
130         alertDialog.setCancelable(true);
131
132         // ボタンを設定する(実行ボタン)
133         alertDialog.setPositiveButton(activity.getString(R.string.dialog_positive_execute),
134                 new DialogInterface.OnClickListener() {
135                     public void onClick(DialogInterface dialog, int which)
136                     {
137                         try
138                         {
139                             Log.v(TAG, "ENTRY [" + lap.getValue() + "] " + hour.getValue() + ":" + minute.getValue() + ":" + second.getValue());
140                             int lapC = (isLap) ? lap.getValue() : lapCount;
141                             long newMillis = (hour.getValue() * 60 * 60 * 1000) + (minute.getValue() * 60 * 1000) + (second.getValue() * 1000);
142                             callback.dataCreated(isLap, lapC, defaultValue, newMillis);
143                         }
144                         catch (Exception e)
145                         {
146                             e.printStackTrace();
147                             callback.dataCreateCancelled();
148                         }
149                         dialog.dismiss();
150                     }
151                 });
152
153         // ボタンを設定する (キャンセルボタン)
154         alertDialog.setNegativeButton(activity.getString(R.string.dialog_negative_cancel),
155                 new DialogInterface.OnClickListener() {
156                     public void onClick(DialogInterface dialog, int which)
157                     {
158                         try
159                         {
160                             callback.dataCreateCancelled();
161                         }
162                         catch (Exception e)
163                         {
164                             e.printStackTrace();
165                         }
166                         dialog.cancel();
167                     }
168                 });
169
170         myDialog = alertDialog.create();
171         return (myDialog);
172     }
173
174
175     @Override
176     public void onPause()
177     {
178         super.onPause();
179         Log.v(TAG, "AlertDialog::onPause()");
180         if (myDialog != null)
181         {
182             myDialog.cancel();
183         }
184     }
185
186
187     /**
188      *  コールバックインタフェース
189      *
190      */
191     public interface Callback
192     {
193         void dataCreated(boolean isLap, int lap, long previousValue, long newValue); // OKを選択したとき
194         void dataCreateCancelled();  // キャンセルしたとき
195     }
196 }