OSDN Git Service

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