76c9cd312413f10f66966375ed606a5be890b2ab
[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 com.google.zxing.client.android.R;
20 import com.google.zxing.client.result.CalendarParsedResult;
21 import com.google.zxing.client.result.ParsedResult;
22
23 import android.app.Activity;
24
25 import java.text.DateFormat;
26 import java.text.ParsePosition;
27 import java.text.SimpleDateFormat;
28 import java.util.Calendar;
29 import java.util.Date;
30 import java.util.GregorianCalendar;
31
32 /**
33  * Handles calendar entries encoded in QR Codes.
34  *
35  * @author dswitkin@google.com (Daniel Switkin)
36  */
37 public final class CalendarResultHandler extends ResultHandler {
38
39   private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
40   private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
41
42   private static final int[] buttons = {
43       R.string.button_add_calendar
44   };
45
46   public CalendarResultHandler(Activity activity, ParsedResult result) {
47     super(activity, result);
48   }
49
50   @Override
51   public int getButtonCount() {
52     return buttons.length;
53   }
54
55   @Override
56   public int getButtonText(int index) {
57     return buttons[index];
58   }
59
60   @Override
61   public void handleButtonPress(int index) {
62     CalendarParsedResult calendarResult = (CalendarParsedResult) getResult();
63     if (index == 0) {
64       addCalendarEvent(calendarResult.getSummary(),
65                        calendarResult.getStart(),
66                        calendarResult.getEnd(),
67                        calendarResult.getLocation(),
68                        calendarResult.getDescription());
69     }
70   }
71
72   @Override
73   public CharSequence getDisplayContents() {
74     CalendarParsedResult calResult = (CalendarParsedResult) getResult();
75     StringBuffer result = new StringBuffer();
76     ParsedResult.maybeAppend(calResult.getSummary(), result);
77     appendTime(calResult.getStart(), result);
78
79     // The end can be null if the event has no duration, so use the start time.
80     String endString = calResult.getEnd();
81     if (endString == null) {
82       endString = calResult.getStart();
83     }
84     appendTime(endString, result);
85
86     ParsedResult.maybeAppend(calResult.getLocation(), result);
87     ParsedResult.maybeAppend(calResult.getAttendee(), result);
88     ParsedResult.maybeAppend(calResult.getDescription(), result);
89     return result.toString();
90   }
91
92   private static void appendTime(String when, StringBuffer result) {
93     if (when.length() == 8) {
94       // Show only year/month/day
95       Date date;
96       synchronized (DATE_FORMAT) {
97         date = DATE_FORMAT.parse(when, new ParsePosition(0));
98       }
99       ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result);
100     } else {
101       // The when string can be local time, or UTC if it ends with a Z
102       Date date;
103       synchronized (DATE_TIME_FORMAT) {
104        date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
105       }
106       long milliseconds = date.getTime();
107       if (when.length() == 16 && when.charAt(15) == 'Z') {
108         Calendar calendar = new GregorianCalendar();
109         int offset = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET));
110         milliseconds += offset;
111       }
112       ParsedResult.maybeAppend(DateFormat.getDateTimeInstance().format(milliseconds), result);
113     }
114   }
115
116   @Override
117   public int getDisplayTitle() {
118     return R.string.result_calendar;
119   }
120 }