Advertisement
Guest User

Untitled

a guest
Sep 4th, 2012
126
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None
  1. package com.fps.iHealthFirst;
  2.  
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.Set;
  6. import java.util.TreeSet;
  7.  
  8. import android.app.Activity;
  9. import android.os.Bundle;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.view.Window;
  15. import android.widget.AdapterView;
  16. import android.widget.AdapterView.OnItemSelectedListener;
  17. import android.widget.ArrayAdapter;
  18. import android.widget.Button;
  19. import android.widget.Spinner;
  20. import android.widget.TextView;
  21.  
  22. public class iSleep extends Activity implements OnItemSelectedListener, OnClickListener {
  23.  
  24. private Spinner spHour, spMinute, spTime;
  25. private Button calculate, cancel;
  26. private TextView answer;
  27.  
  28. private String[] shour = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
  29. private String[] sminute = { "00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55" };
  30. private String[] stime = { "AM", "PM" };
  31.  
  32. /** Date param for extra */
  33. public static final String DATE_PARAM = "date";
  34. /** Choice param for extra */
  35. public static final String CHOICE_PARAM = "choice";
  36.  
  37.  
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. // TODO Auto-generated method stub
  41. super.onCreate(savedInstanceState);
  42. requestWindowFeature(Window.FEATURE_LEFT_ICON);
  43. setContentView(R.layout.isleep);
  44. getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.sleep32);
  45.  
  46. // initializes controls
  47. initControls();
  48.  
  49. // initializes arrayAdapter
  50. initArrayAdapter();
  51.  
  52.  
  53.  
  54. } // end of onCreate method
  55.  
  56. private void initArrayAdapter()
  57. {
  58.  
  59. ArrayAdapter<String> aa = new ArrayAdapter<String>( this,android.R.layout.simple_spinner_item, shour );
  60. aa.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
  61. spHour.setAdapter( aa );
  62.  
  63. ArrayAdapter<String> aa2 = new ArrayAdapter<String>( this,android.R.layout.simple_spinner_item, sminute );
  64. aa2.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
  65. spMinute.setAdapter( aa2 );
  66.  
  67. ArrayAdapter<String> aa3 = new ArrayAdapter<String>( this,android.R.layout.simple_spinner_item, stime );
  68. aa3.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
  69. spTime.setAdapter( aa3 );
  70.  
  71. }
  72.  
  73.  
  74. private void initControls(){
  75.  
  76. spHour = (Spinner) findViewById (R.id.spSleepHour);
  77. spMinute = (Spinner) findViewById (R.id.spSleepMinute);
  78. spTime = (Spinner) findViewById (R.id.spSleepTime);
  79. calculate = (Button) findViewById (R.id.btSleepCalculate);
  80. cancel = (Button) findViewById (R.id.btSleepCancel);
  81. answer = (TextView) findViewById (R.id.tvSleepResult);
  82.  
  83. // providing listeners for each spinner
  84. spHour.setOnItemSelectedListener(this);
  85. spMinute.setOnItemSelectedListener(this);
  86. spTime.setOnItemSelectedListener(this);
  87.  
  88. // providing listener for button
  89. calculate.setOnClickListener(this);
  90. cancel.setOnClickListener(this);
  91.  
  92. } // end of initControls method
  93.  
  94.  
  95. public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
  96. // TODO Auto-generated method stub
  97.  
  98. switch( parent.getId() )
  99. {
  100. case R.id.spSleepHour:
  101.  
  102. break;
  103. case R.id.spSleepMinute:
  104.  
  105. break;
  106. case R.id.spSleepTime:
  107.  
  108. break;
  109. }
  110.  
  111.  
  112. } // end of onItemSelected method
  113.  
  114. public void onNothingSelected(AdapterView<?> arg0) {
  115. // TODO Auto-generated method stub
  116.  
  117. }
  118.  
  119.  
  120. public void onClick(View v) {
  121. // TODO Auto-generated method stub
  122.  
  123. switch( v.getId() )
  124. {
  125. case R.id.btSleepCalculate:
  126. getWakingTimes();
  127. break;
  128. case R.id.btSleepCancel:
  129. resetSpinnerValues();
  130. break;
  131. } // end of switch statement
  132.  
  133. } // end of onClick method
  134.  
  135. /**
  136. * Get times to wake. We add 14 minutes to fall asleep, and calculate the
  137. * sleep cycles.
  138. *
  139. * @return list of times when to get up
  140. */
  141. public static Set<Date> getWakingTimes() {
  142. return getWakingTime(new Date());
  143. }
  144.  
  145. /**
  146. * Get times to wake. We add 14 minutes to fall asleep, and calculate the
  147. * sleep cycles.
  148. *
  149. * @param sleepTime
  150. * bed time
  151. *
  152. * @return list of times when to get up
  153. */
  154. public static Set<Date> getWakingTime(Date sleepTime) {
  155. Calendar fallAsleep = Calendar.getInstance();
  156. fallAsleep.setTime(sleepTime);
  157. fallAsleep.add(Calendar.MINUTE, 14);
  158. Set<Date> result = new TreeSet<Date>();
  159. for (int i = 1; i <= 6; i++) {
  160. fallAsleep.add(Calendar.MINUTE, 90);
  161. result.add(fallAsleep.getTime());
  162. }
  163. return result;
  164. }
  165.  
  166. /**
  167. * Get sleeping times corresponding to a local time
  168. *
  169. * @param wakingTime
  170. * time to wake up !
  171. * @return list of times one should go to bed to
  172. */
  173. public static Set<Date> getSleepingTimes(Date wakingTime) {
  174.  
  175. Set<Date> result = new TreeSet<Date>();
  176. Calendar calendar = Calendar.getInstance();
  177. calendar.setTime(wakingTime);
  178. calendar.add(Calendar.MINUTE, -14);
  179. calendar.add(Calendar.MINUTE, -(2 * 90));
  180. for (int i = 3; i <= 6; i++) {
  181. calendar.add(Calendar.MINUTE, -90);
  182. result.add(calendar.getTime());
  183. }
  184. return result;
  185.  
  186. } // end of getSleepingTimes method
  187.  
  188.  
  189. private void resetSpinnerValues()
  190. {
  191. spHour.setSelection(0);
  192. spMinute.setSelection(0);
  193. spTime.setSelection(0);
  194.  
  195. } // end of resetSpinnerValues method
  196.  
  197.  
  198. // MenuInflater
  199.  
  200. @Override
  201. public boolean onCreateOptionsMenu(Menu menu) {
  202. getMenuInflater().inflate(R.menu.isleep_menu, menu);
  203. return true;
  204. }
  205.  
  206. @Override
  207. public boolean onOptionsItemSelected(MenuItem item) {
  208. // TODO Auto-generated method stub
  209. switch (item.getItemId()) {
  210. case R.id.isleep_home:
  211. finish();
  212. break;
  213. }
  214.  
  215. return super.onOptionsItemSelected(item);
  216. }
  217.  
  218.  
  219.  
  220.  
  221. }
Advertisement
RAW Paste Data Copied
Advertisement