1 package net.osdn.gokigen.joggingtimer.utilities;
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;
18 import net.osdn.gokigen.joggingtimer.R;
25 public class CreateModelDataDialog extends DialogFragment
27 private final String TAG = toString();
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;
40 public static CreateModelDataDialog newInstance(boolean isLap, String title, int lapCount, Callback callback, long defaultValue)
42 CreateModelDataDialog instance = new CreateModelDataDialog();
43 instance.prepare(isLap, title, lapCount, callback, defaultValue);
45 // パラメータはBundleにまとめておく
46 Bundle arguments = new Bundle();
47 arguments.putString("title", title);
48 //arguments.putString("message", message);
49 instance.setArguments(arguments);
58 private void prepare(boolean isLap, String title, int lapCount, Callback callback, long defaultValue)
62 this.lapCount = lapCount;
63 this.callback = callback;
64 this.defaultValue = defaultValue;
72 public @NonNull Dialog onCreateDialog(Bundle savedInstanceState)
74 Log.v(TAG, "show " + "def. : " + defaultValue);
76 Activity activity = getActivity();
78 final AlertDialog.Builder alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.wear2_dialog_theme));
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);
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);
97 titleText.setText(title);
101 lap.setVisibility(View.VISIBLE);
102 lapStartText.setVisibility(View.VISIBLE);
103 lapEndText.setVisibility(View.VISIBLE);
109 lap.setValue(lapCount);
110 lap.setVisibility(View.GONE);
111 lapStartText.setVisibility(View.GONE);
112 lapEndText.setVisibility(View.GONE);
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));
130 alertDialog.setCancelable(true);
133 alertDialog.setPositiveButton(activity.getString(R.string.dialog_positive_execute),
134 new DialogInterface.OnClickListener() {
135 public void onClick(DialogInterface dialog, int which)
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);
147 callback.dataCreateCancelled();
153 // ボタンを設定する (キャンセルボタン)
154 alertDialog.setNegativeButton(activity.getString(R.string.dialog_negative_cancel),
155 new DialogInterface.OnClickListener() {
156 public void onClick(DialogInterface dialog, int which)
160 callback.dataCreateCancelled();
170 myDialog = alertDialog.create();
176 public void onPause()
179 Log.v(TAG, "AlertDialog::onPause()");
180 if (myDialog != null)
191 public interface Callback
193 void dataCreated(boolean isLap, int lap, long previousValue, long newValue); // OKを選択したとき
194 void dataCreateCancelled(); // キャンセルしたとき