Small speedups in time-related code
[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   public int getButtonCount() {
45     return mButtons.length;
46   }
47
48   public int getButtonText(int index) {
49     return mButtons[index];
50   }
51
52   public void handleButtonPress(int index) {
53     CalendarParsedResult calendarResult = (CalendarParsedResult) mResult;
54     switch (index) {
55       case 0:
56         addCalendarEvent(calendarResult.getSummary(), calendarResult.getStart(),
57             calendarResult.getEnd());
58         break;
59     }
60   }
61
62   @Override
63   public CharSequence getDisplayContents() {
64     CalendarParsedResult calResult = (CalendarParsedResult) mResult;
65     StringBuffer result = new StringBuffer();
66     ParsedResult.maybeAppend(calResult.getSummary(), result);
67     appendTime(calResult.getStart(), result);
68
69     // The end can be null if the event has no duration, so use the start time.
70     String endString = calResult.getEnd();
71     if (endString == null) endString = calResult.getStart();
72     appendTime(endString, result);
73
74     ParsedResult.maybeAppend(calResult.getLocation(), result);
75     ParsedResult.maybeAppend(calResult.getAttendee(), result);
76     ParsedResult.maybeAppend(calResult.getTitle(), result);
77     return result.toString();
78   }
79
80   private void appendTime(String when, StringBuffer result) {
81     if (when.length() == 8) {
82       // Show only year/month/day
83       Date date;
84       synchronized (DATE_FORMAT) {
85         date = DATE_FORMAT.parse(when, new ParsePosition(0));
86       }
87       ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result);
88     } else {
89       // The when string can be local time, or UTC if it ends with a Z
90       Date date;
91       synchronized (DATE_TIME_FORMAT) {
92        date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
93       }
94       long milliseconds = date.getTime();
95       if (when.length() == 16 && when.charAt(15) == 'Z') {
96         Calendar calendar = new GregorianCalendar();
97         int offset = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET));
98         milliseconds += offset;
99       }
100       ParsedResult.maybeAppend(DateFormat.getDateTimeInstance().format(milliseconds), result);
101     }
102   }
103
104   public int getDisplayTitle() {
105     return R.string.result_calendar;
106   }
107
108 }