2 * Copyright (C) 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android.result;
19 import com.google.zxing.client.android.R;
20 import com.google.zxing.client.result.CalendarParsedResult;
21 import com.google.zxing.client.result.ParsedResult;
23 import android.app.Activity;
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;
33 * Handles calendar entries encoded in QR Codes.
35 * @author dswitkin@google.com (Daniel Switkin)
37 public final class CalendarResultHandler extends ResultHandler {
39 private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
40 private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
42 private static final int[] buttons = {
43 R.string.button_add_calendar
46 public CalendarResultHandler(Activity activity, ParsedResult result) {
47 super(activity, result);
51 public int getButtonCount() {
52 return buttons.length;
56 public int getButtonText(int index) {
57 return buttons[index];
61 public void handleButtonPress(int index) {
62 CalendarParsedResult calendarResult = (CalendarParsedResult) getResult();
64 addCalendarEvent(calendarResult.getSummary(),
65 calendarResult.getStart(),
66 calendarResult.getEnd(),
67 calendarResult.getLocation(),
68 calendarResult.getDescription());
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);
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();
84 appendTime(endString, result);
86 ParsedResult.maybeAppend(calResult.getLocation(), result);
87 ParsedResult.maybeAppend(calResult.getAttendee(), result);
88 ParsedResult.maybeAppend(calResult.getDescription(), result);
89 return result.toString();
92 private static void appendTime(String when, StringBuffer result) {
93 if (when.length() == 8) {
94 // Show only year/month/day
96 synchronized (DATE_FORMAT) {
97 date = DATE_FORMAT.parse(when, new ParsePosition(0));
99 ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result);
101 // The when string can be local time, or UTC if it ends with a Z
103 synchronized (DATE_TIME_FORMAT) {
104 date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
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;
112 ParsedResult.maybeAppend(DateFormat.getDateTimeInstance().format(milliseconds), result);
117 public int getDisplayTitle() {
118 return R.string.result_calendar;