More reckless refactoring and code style tweaks -- mostly adding braces around condit...
[zxing.git] / android / src / com / google / zxing / client / android / result / CalendarResultHandler.java
1 /*
2  * Copyright (C) 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.android.result;
18
19 import android.app.Activity;
20 import com.google.zxing.client.android.R;
21 import com.google.zxing.client.result.CalendarParsedResult;
22 import com.google.zxing.client.result.ParsedResult;
23
24 import java.text.DateFormat;
25 import java.text.ParsePosition;
26 import java.text.SimpleDateFormat;
27 import java.util.Calendar;
28 import java.util.Date;
29 import java.util.GregorianCalendar;
30
31 public final class CalendarResultHandler extends ResultHandler {
32
33   private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
34   private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
35
36   private static final int[] mButtons = {
37       R.string.button_add_calendar
38   };
39
40   public CalendarResultHandler(Activity activity, ParsedResult result) {
41     super(activity, result);
42   }
43
44   @Override
45   public int getButtonCount() {
46     return mButtons.length;
47   }
48
49   @Override
50   public int getButtonText(int index) {
51     return mButtons[index];
52   }
53
54   @Override
55   public void handleButtonPress(int index) {
56     CalendarParsedResult calendarResult = (CalendarParsedResult) mResult;
57     switch (index) {
58       case 0:
59         addCalendarEvent(calendarResult.getSummary(), calendarResult.getStart(),
60             calendarResult.getEnd());
61         break;
62     }
63   }
64
65   @Override
66   public CharSequence getDisplayContents() {
67     CalendarParsedResult calResult = (CalendarParsedResult) mResult;
68     StringBuffer result = new StringBuffer();
69     ParsedResult.maybeAppend(calResult.getSummary(), result);
70     appendTime(calResult.getStart(), result);
71
72     // The end can be null if the event has no duration, so use the start time.
73     String endString = calResult.getEnd();
74     if (endString == null) {
75       endString = calResult.getStart();
76     }
77     appendTime(endString, result);
78
79     ParsedResult.maybeAppend(calResult.getLocation(), result);
80     ParsedResult.maybeAppend(calResult.getAttendee(), result);
81     ParsedResult.maybeAppend(calResult.getTitle(), result);
82     return result.toString();
83   }
84
85   private static void appendTime(String when, StringBuffer result) {
86     if (when.length() == 8) {
87       // Show only year/month/day
88       Date date;
89       synchronized (DATE_FORMAT) {
90         date = DATE_FORMAT.parse(when, new ParsePosition(0));
91       }
92       ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result);
93     } else {
94       // The when string can be local time, or UTC if it ends with a Z
95       Date date;
96       synchronized (DATE_TIME_FORMAT) {
97        date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
98       }
99       long milliseconds = date.getTime();
100       if (when.length() == 16 && when.charAt(15) == 'Z') {
101         Calendar calendar = new GregorianCalendar();
102         int offset = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET));
103         milliseconds += offset;
104       }
105       ParsedResult.maybeAppend(DateFormat.getDateTimeInstance().format(milliseconds), result);
106     }
107   }
108
109   @Override
110   public int getDisplayTitle() {
111     return R.string.result_calendar;
112   }
113
114 }