package org.adaptlab.chpir.android.survey.QuestionFragments;

import org.adaptlab.chpir.android.survey.QuestionFragment;

import android.view.ViewGroup;
import android.widget.TimePicker;

public class TimeQuestionFragment extends QuestionFragment {

    @Override
    protected void createQuestionComponent(ViewGroup questionComponent) {
        TimePicker timePicker = new TimePicker(getActivity());
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                getResponse().setResponse(formatTime(hourOfDay, minute));
                getResponse().save();
            }
        });
        
        getResponse().setResponse(formatTime(timePicker.getCurrentHour(), timePicker.getCurrentMinute()));
        getResponse().save();
        
        questionComponent.addView(timePicker);
    }
    
    // Format: HH:MM AM/PM
    private String formatTime(int hour, int minute) {
        String period;
        if (hour <= 12) {
            period = "AM";
        } else {
            period = "PM";
            hour = hour - 12;
        }
        return hour + ":" + formatMinute(minute) + " " + period;
    }

    // Add a 0 to minute values less than 10 to look more natural
    private String formatMinute(int minute) {
        if (minute < 10) {
            return "0" + minute;
        } else {
            return ""+minute;
        }
    }
}

@@ -1055,28 +1055,18 @@
 riod
-;%0A        if
+ =
  (hour %3C
 = 12
@@ -1065,41 +1065,26 @@
 ur %3C
-=
  12
-) %7B%0A            period = %22A
+ ? %22AM%22 : %22P
 M%22
+)
 ;%0A  
@@ -1093,42 +1093,45 @@
     
-%7D else %7B%0A            period = %22PM%22
+hour = (hour %3E 13 ? hour - 12 : hour)
 ;%0A  
@@ -1140,40 +1140,34 @@
     
-    
+if (
 hour =
+= 0)
  hour 
--
+=
  12;%0A
-        %7D%0A
     

Fix time format bug