Clover Coverage Report - AnCal Coverage Report
Coverage timestamp: gio dic 18 2014 12:18:42 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
225   450   62   7,76
58   349   0,28   29
29     2,14  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  DateWidget       Line # 14 225 62 93,3% 0.9326923
 
No Tests
 
1    package pl.magot.vetch.widgets;
2   
3    import java.util.*;
4    import android.app.*;
5    import android.content.Intent;
6    import android.os.Bundle;
7    import android.widget.*;
8    import android.view.*;
9    import android.widget.Button;
10    import android.widget.LinearLayout;
11    import android.widget.LinearLayout.LayoutParams;
12    import java.text.SimpleDateFormat;
13   
 
14    public class DateWidget extends Activity {
15    // fields
16    private static String sStrSelect = "Select day";
17    private static String sStrSelected = "Selected day:";
18    private static String sStrNone = "none";
19   
20    // fields
21    private ArrayList<DateWidgetDayCell> days = new ArrayList<DateWidgetDayCell>();
22   
23    // fields
24    private SimpleDateFormat dateMonth = new SimpleDateFormat("MMMM yyyy");
25    private SimpleDateFormat dateFull = new SimpleDateFormat("d MMMM yyyy");
26   
27    // fields
28    private Calendar calStartDate = Calendar.getInstance();
29    private Calendar calToday = Calendar.getInstance();
30    private Calendar calCalendar = Calendar.getInstance();
31    private Calendar calSelected = Calendar.getInstance();
32   
33    // fields
34    LinearLayout layContent = null;
35    Button btnPrev = null;
36    Button btnToday = null;
37    Button btnNext = null;
38    Button btnNone = null;
39   
40    // fields
41    private boolean bNoneButton = true;
42    private int iFirstDayOfWeek = Calendar.MONDAY;
43   
44    // fields
45    private int iMonthViewCurrentMonth = 0;
46    private int iMonthViewCurrentYear = 0;
47   
48    // fields
49    public static final int SELECT_DATE_REQUEST = 111;
50    private static final int iDayCellSize = 38;
51    private static final int iDayHeaderHeight = 24;
52    private static final int iTotalWidth = (iDayCellSize * 7);
53    private static final int iSmallButtonWidth = 100;
54   
55    // methods
 
56  6 toggle @Override
57    public void onCreate(Bundle icicle) {
58   
59  6 super.onCreate(icicle);
60   
61    // init calendar to defaults
62  6 calSelected.setTimeInMillis(0);
63  6 iFirstDayOfWeek = Calendar.MONDAY;
64  6 bNoneButton = true;
65   
66    // get startup data
67  6 Bundle data = this.getIntent().getExtras();
68  6 if (data != null) {
69  6 if (data.containsKey("date"))
70  6 calSelected.setTimeInMillis(data.getLong("date"));
71  6 if (data.containsKey("firstDayOfWeek"))
72  6 iFirstDayOfWeek = data.getInt("firstDayOfWeek");
73  6 if (data.containsKey("noneButton"))
74  6 bNoneButton = data.getBoolean("noneButton");
75    }
76   
77  6 setContentView(generateContentView());
78   
79    // initialize
80  6 calStartDate = getCalendarStartDate();
81  6 DateWidgetDayCell daySelected = updateCalendar();
82  6 updateControlsState();
83   
84    // none button
85  6 if (bNoneButton) {
86  4 btnNone.requestFocus();
87  4 btnNone.setEnabled(true);
88  4 btnNone.setFocusable(true);
89    } else {
90  2 btnNone.setEnabled(false);
91  2 btnNone.setFocusable(false);
92    }
93   
94    // focus selected day
95  6 if (daySelected != null)
96  3 daySelected.requestFocus();
97    }
98   
 
99  6 toggle @Override
100    public void onStart() {
101  6 super.onStart();
102   
103    }
104   
 
105  1 toggle public static void setStrings(String strSelect, String strSelected, String strNone) {
106  1 sStrSelect = new String(strSelect);
107  1 sStrSelected = new String(strSelected);
108  1 sStrNone = new String(strNone);
109    }
110   
 
111  6 toggle public static void Open(Activity parentActivity, boolean bNoneButton, final Calendar calDate, int iFirstDayOfWeek) {
112  6 Intent it = new Intent("android.intent.action.AnCal.ACTION_MODE_EDIT_SELECT_DATE");
113  6 Bundle data = new Bundle();
114  6 data.putLong("date", calDate.getTimeInMillis());
115  6 data.putInt("firstDayOfWeek", iFirstDayOfWeek);
116  6 data.putBoolean("noneButton", bNoneButton);
117  6 it.putExtras(data);
118  6 parentActivity.startActivityForResult(it, SELECT_DATE_REQUEST);
119    }
120   
 
121  6 toggle public static long GetSelectedDateOnActivityResult(int requestCode, int resultCode, Bundle extras, Calendar outDate) {
122  6 if (requestCode == DateWidget.SELECT_DATE_REQUEST) {
123  6 if (resultCode == RESULT_OK) {
124  6 if (extras.containsKey("date")) {
125  6 final long lDate = extras.getLong("date");
126   
127  6 if (lDate == 0) {
128  0 outDate.setTimeInMillis(0);
129    } else {
130  6 Calendar calSelected = Calendar.getInstance();
131  6 calSelected.setTimeInMillis(lDate);
132  6 outDate.set(Calendar.YEAR, calSelected.get(Calendar.YEAR));
133  6 outDate.set(Calendar.MONTH, calSelected.get(Calendar.MONTH));
134  6 outDate.set(Calendar.DAY_OF_MONTH, calSelected.get(Calendar.DAY_OF_MONTH));
135    }
136   
137  6 return lDate;
138    }
139    }
140    }
141  0 return -1;
142    }
143   
 
144  78 toggle public LinearLayout createLayout(int iOrientation) {
145  78 LinearLayout lay = new LinearLayout(this);
146  78 lay.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
147    android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
148  78 lay.setOrientation(iOrientation);
149  78 return lay;
150    }
151   
 
152  12 toggle public Button createButton(String sText, int iWidth, int iHeight) {
153  12 Button btn = new Button(this);
154  12 btn.setText(sText);
155  12 btn.setLayoutParams(new LayoutParams(iWidth, iHeight));
156  12 return btn;
157    }
158   
 
159  0 toggle public TextView createLabel(String sText, int iWidth, int iHeight) {
160  0 TextView label = new TextView(this);
161  0 label.setText(sText);
162  0 label.setLayoutParams(new LayoutParams(iWidth, iHeight));
163  0 return label;
164    }
165   
 
166  6 toggle public void generateTopButtons(LinearLayout layTopControls) {
167  6 final int iHorPadding = 24;
168  6 final int iSmallButtonWidth = 60;
169   
170    // create buttons
171  6 btnToday = createButton("", iTotalWidth - iSmallButtonWidth - iSmallButtonWidth,
172    android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
173  6 btnToday.setPadding(iHorPadding, btnToday.getPaddingTop(), iHorPadding, btnToday.getPaddingBottom());
174  6 btnToday.setBackgroundResource(android.R.drawable.btn_default_small);
175   
176  6 SymbolButton btnPrev = new SymbolButton(this, SymbolButton.symbol.arrowLeft);
177  6 btnPrev.setLayoutParams(new LayoutParams(iSmallButtonWidth, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
178  6 btnPrev.setBackgroundResource(android.R.drawable.btn_default_small);
179   
180  6 SymbolButton btnNext = new SymbolButton(this, SymbolButton.symbol.arrowRight);
181  6 btnNext.setLayoutParams(new LayoutParams(iSmallButtonWidth, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
182  6 btnNext.setBackgroundResource(android.R.drawable.btn_default_small);
183   
184    // set events
185  6 btnPrev.setOnClickListener(new Button.OnClickListener() {
 
186  5 toggle public void onClick(View arg0) {
187  5 setPrevViewItem();
188    }
189    });
190  6 btnToday.setOnClickListener(new Button.OnClickListener() {
 
191  1 toggle public void onClick(View arg0) {
192  1 setTodayViewItem();
193    }
194    });
195  6 btnNext.setOnClickListener(new Button.OnClickListener() {
 
196  25 toggle public void onClick(View arg0) {
197  25 setNextViewItem();
198    }
199    });
200   
201  6 layTopControls.setGravity(Gravity.CENTER_HORIZONTAL);
202  6 layTopControls.addView(btnPrev);
203  6 layTopControls.addView(btnToday);
204  6 layTopControls.addView(btnNext);
205    }
206   
 
207  6 toggle public void generateBottomButtons(LinearLayout layBottomControls) {
208  6 btnNone = createButton(sStrNone, iSmallButtonWidth, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
209  6 btnNone.setBackgroundResource(android.R.drawable.btn_default_small);
210   
211    // set events
212  6 btnNone.setOnClickListener(new Button.OnClickListener() {
 
213  0 toggle public void onClick(View arg0) {
214  0 deselectAll();
215  0 updateControlsState();
216  0 OnClose();
217    }
218    });
219   
220  6 layBottomControls.setGravity(Gravity.CENTER_HORIZONTAL);
221  6 layBottomControls.addView(btnNone);
222    }
223   
 
224  6 toggle private View generateContentView() {
225  6 LinearLayout layMain = createLayout(LinearLayout.VERTICAL);
226  6 layMain.setPadding(8, 8, 8, 8);
227   
228  6 LinearLayout layTopControls = createLayout(LinearLayout.HORIZONTAL);
229  6 LinearLayout layContentTop = createLayout(LinearLayout.HORIZONTAL);
230  6 LinearLayout layContentBottom = createLayout(LinearLayout.HORIZONTAL);
231  6 LinearLayout layBottomControls = createLayout(LinearLayout.HORIZONTAL);
232   
233  6 layContent = createLayout(LinearLayout.VERTICAL);
234  6 layContent.setPadding(8, 0, 8, 0);
235   
236  6 generateTopButtons(layTopControls);
237  6 generateBottomButtons(layBottomControls);
238   
239  6 layContentTop.getLayoutParams().height = 12;
240  6 layContentBottom.getLayoutParams().height = 18;
241   
242  6 generateCalendar(layContent);
243   
244  6 if (!bNoneButton)
245  2 layBottomControls.getLayoutParams().height = 0;
246   
247  6 layMain.addView(layTopControls);
248  6 layMain.addView(layContentTop);
249  6 layMain.addView(layContent);
250  6 layMain.addView(layContentBottom);
251  6 layMain.addView(layBottomControls);
252   
253  6 return layMain;
254    }
255   
 
256  36 toggle private View generateCalendarRow() {
257  36 LinearLayout layRow = createLayout(LinearLayout.HORIZONTAL);
258  288 for (int iDay = 0; iDay < 7; iDay++) {
259  252 DateWidgetDayCell dayCell = new DateWidgetDayCell(this, iDayCellSize, iDayCellSize);
260  252 dayCell.setItemClick(mOnDayCellClick);
261  252 days.add(dayCell);
262  252 layRow.addView(dayCell);
263    }
264  36 return layRow;
265    }
266   
 
267  6 toggle private View generateCalendarHeader() {
268  6 LinearLayout layRow = createLayout(LinearLayout.HORIZONTAL);
269  48 for (int day = 0; day < 7; day++) {
270  42 DateWidgetDayHeader header = new DateWidgetDayHeader(this, iDayCellSize, iDayHeaderHeight);
271  42 header.setData((day + iFirstDayOfWeek) % 7);
272  42 layRow.addView(header);
273    }
274  6 return layRow;
275    }
276   
 
277  6 toggle private void generateCalendar(LinearLayout layContent) {
278    // generate days header
279  6 layContent.addView(generateCalendarHeader());
280    // generate days
281  6 days.clear();
282  42 for (int iRow = 0; iRow < 6; iRow++) {
283  36 layContent.addView(generateCalendarRow());
284    }
285    }
286   
 
287  6 toggle private Calendar getCalendarStartDate() {
288  6 calToday.setTimeInMillis(System.currentTimeMillis());
289  6 calToday.setFirstDayOfWeek(iFirstDayOfWeek);
290   
291  6 if (calSelected.getTimeInMillis() == 0) {
292  3 calStartDate.setTimeInMillis(System.currentTimeMillis());
293  3 calStartDate.setFirstDayOfWeek(iFirstDayOfWeek);
294    } else {
295  3 calStartDate.setTimeInMillis(calSelected.getTimeInMillis());
296  3 calStartDate.setFirstDayOfWeek(iFirstDayOfWeek);
297    }
298   
299  6 UpdateStartDateForMonth();
300   
301  6 return calStartDate;
302    }
303   
 
304  37 toggle private DateWidgetDayCell updateCalendar() {
305  37 DateWidgetDayCell daySelected = null;
306  37 boolean bSelected = false;
307   
308  37 final boolean bIsSelection = (calSelected.getTimeInMillis() != 0);
309  37 final int iSelectedYear = calSelected.get(Calendar.YEAR);
310  37 final int iSelectedMonth = calSelected.get(Calendar.MONTH);
311  37 final int iSelectedDay = calSelected.get(Calendar.DAY_OF_MONTH);
312   
313  37 calCalendar.setTimeInMillis(calStartDate.getTimeInMillis());
314   
315  1591 for (int i = 0; i < days.size(); i++) {
316  1554 final int iYear = calCalendar.get(Calendar.YEAR);
317  1554 final int iMonth = calCalendar.get(Calendar.MONTH);
318  1554 final int iDay = calCalendar.get(Calendar.DAY_OF_MONTH);
319  1554 final int iDayOfWeek = calCalendar.get(Calendar.DAY_OF_WEEK);
320   
321  1554 DateWidgetDayCell dayCell = days.get(i);
322   
323    // check today
324  1554 boolean bToday = false;
325  1554 if (calToday.get(Calendar.YEAR) == iYear)
326  277 if (calToday.get(Calendar.MONTH) == iMonth)
327  242 if (calToday.get(Calendar.DAY_OF_MONTH) == iDay)
328  7 bToday = true;
329   
330    // check holiday
331  1554 boolean bHoliday = false;
332  1554 if ((iDayOfWeek == Calendar.SATURDAY) || (iDayOfWeek == Calendar.SUNDAY))
333  444 bHoliday = true;
334  1554 if ((iMonth == Calendar.JANUARY) && (iDay == 1))
335  13 bHoliday = true;
336   
337  1554 dayCell.setData(iYear, iMonth, iDay, bToday, bHoliday, iMonthViewCurrentMonth);
338   
339    // check if selected day
340  1554 bSelected = false;
341  1554 if (bIsSelection)
342  756 if ((iSelectedDay == iDay) && (iSelectedMonth == iMonth) && (iSelectedYear == iYear))
343  3 bSelected = true;
344   
345  1554 dayCell.setSelected(bSelected);
346  1554 if (bSelected)
347  3 daySelected = dayCell;
348   
349  1554 calCalendar.add(Calendar.DAY_OF_MONTH, 1);
350    }
351   
352  37 layContent.invalidate();
353  37 return daySelected;
354    }
355   
 
356  37 toggle private void UpdateStartDateForMonth() {
357  37 iMonthViewCurrentMonth = calStartDate.get(Calendar.MONTH);
358  37 iMonthViewCurrentYear = calStartDate.get(Calendar.YEAR);
359  37 calStartDate.set(Calendar.DAY_OF_MONTH, 1);
360   
361  37 UpdateCurrentMonthDisplay();
362  37 int iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - iFirstDayOfWeek;
363  37 calStartDate.add(Calendar.DAY_OF_WEEK, -iDay);
364    }
365   
 
366  37 toggle private void UpdateCurrentMonthDisplay() {
367  37 String s = dateMonth.format(calStartDate.getTime());
368  37 btnToday.setText(s);
369    }
370   
 
371  5 toggle public void setPrevViewItem() {
372  5 iMonthViewCurrentMonth--;
373  5 if (iMonthViewCurrentMonth == -1) {
374  1 iMonthViewCurrentMonth = 11;
375  1 iMonthViewCurrentYear--;
376    }
377  5 calStartDate.set(Calendar.DAY_OF_MONTH, 1);
378  5 calStartDate.set(Calendar.MONTH, iMonthViewCurrentMonth);
379  5 calStartDate.set(Calendar.YEAR, iMonthViewCurrentYear);
380  5 UpdateStartDateForMonth();
381  5 updateCalendar();
382    }
383   
 
384  1 toggle public void setTodayViewItem() {
385  1 calToday.setTimeInMillis(System.currentTimeMillis());
386  1 calToday.setFirstDayOfWeek(iFirstDayOfWeek);
387   
388  1 calStartDate.setTimeInMillis(calToday.getTimeInMillis());
389  1 calStartDate.setFirstDayOfWeek(iFirstDayOfWeek);
390   
391  1 UpdateStartDateForMonth();
392  1 updateCalendar();
393    }
394   
 
395  25 toggle public void setNextViewItem() {
396  25 iMonthViewCurrentMonth++;
397  25 if (iMonthViewCurrentMonth == 12) {
398  5 iMonthViewCurrentMonth = 0;
399  5 iMonthViewCurrentYear++;
400    }
401  25 calStartDate.set(Calendar.DAY_OF_MONTH, 1);
402  25 calStartDate.set(Calendar.MONTH, iMonthViewCurrentMonth);
403  25 calStartDate.set(Calendar.YEAR, iMonthViewCurrentYear);
404  25 UpdateStartDateForMonth();
405  25 updateCalendar();
406    }
407   
408    public DateWidgetDayCell.OnItemClick mOnDayCellClick = new DateWidgetDayCell.OnItemClick() {
 
409  6 toggle public void OnClick(DateWidgetDayCell item) {
410  6 deselectAll();
411  6 calSelected.setTimeInMillis(item.getDate().getTimeInMillis());
412  6 item.setSelected(true);
413  6 updateControlsState();
414  6 OnClose();
415    }
416    };
417   
 
418  12 toggle public void updateControlsState() {
419  12 final boolean bDaySelected = (calSelected.getTimeInMillis() != 0);
420  12 btnNone.setEnabled(bDaySelected);
421  12 if (bDaySelected) {
422  9 String s = dateFull.format(calSelected.getTime());
423  9 setTitle(sStrSelected + " " + s);
424    } else {
425  3 setTitle(sStrSelect);
426    }
427    }
428   
 
429  6 toggle public void deselectAll() {
430  6 calSelected.setTimeInMillis(0);
431  258 for (int i = 0; i < days.size(); i++) {
432  252 DateWidgetDayCell dayCell = days.get(i);
433  252 if (dayCell.getSelected())
434  0 dayCell.setSelected(false);
435    }
436  6 layContent.invalidate();
437    }
438   
 
439  6 toggle public void OnClose() {
440  6 Bundle data = new Bundle();
441  6 data.putLong("date", calSelected.getTimeInMillis());
442   
443  6 Intent intentData = new Intent("");
444  6 intentData.putExtras(data);
445  6 setResult(RESULT_OK, intentData);
446   
447  6 this.finish();
448    }
449   
450    }