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 {
38 private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
39 private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
41 private static final int[] buttons = {
42 R.string.button_add_calendar
45 public CalendarResultHandler(Activity activity, ParsedResult result) {
46 super(activity, result);
50 public int getButtonCount() {
51 return buttons.length;
55 public int getButtonText(int index) {
56 return buttons[index];
60 public void handleButtonPress(int index) {
61 CalendarParsedResult calendarResult = (CalendarParsedResult) getResult();
64 addCalendarEvent(calendarResult.getSummary(), calendarResult.getStart(),
65 calendarResult.getEnd());
71 public CharSequence getDisplayContents() {
72 CalendarParsedResult calResult = (CalendarParsedResult) getResult();
73 StringBuffer result = new StringBuffer();
74 ParsedResult.maybeAppend(calResult.getSummary(), result);
75 appendTime(calResult.getStart(), result);
77 // The end can be null if the event has no duration, so use the start time.
78 String endString = calResult.getEnd();
79 if (endString == null) {
80 endString = calResult.getStart();
82 appendTime(endString, result);
84 ParsedResult.maybeAppend(calResult.getLocation(), result);
85 ParsedResult.maybeAppend(calResult.getAttendee(), result);
86 ParsedResult.maybeAppend(calResult.getTitle(), result);
87 return result.toString();
90 private static void appendTime(String when, StringBuffer result) {
91 if (when.length() == 8) {
92 // Show only year/month/day
94 synchronized (DATE_FORMAT) {
95 date = DATE_FORMAT.parse(when, new ParsePosition(0));
97 ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result);
99 // The when string can be local time, or UTC if it ends with a Z
101 synchronized (DATE_TIME_FORMAT) {
102 date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
104 long milliseconds = date.getTime();
105 if (when.length() == 16 && when.charAt(15) == 'Z') {
106 Calendar calendar = new GregorianCalendar();
107 int offset = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET));
108 milliseconds += offset;
110 ParsedResult.maybeAppend(DateFormat.getDateTimeInstance().format(milliseconds), result);
115 public int getDisplayTitle() {
116 return R.string.result_calendar;