Add history feature; group some functionality into subpackages
[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   private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
39   private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
40
41   private static final int[] buttons = {
42       R.string.button_add_calendar
43   };
44
45   public CalendarResultHandler(Activity activity, ParsedResult result) {
46     super(activity, result);
47   }
48
49   @Override
50   public int getButtonCount() {
51     return buttons.length;
52   }
53
54   @Override
55   public int getButtonText(int index) {
56     return buttons[index];
57   }
58
59   @Override
60   public void handleButtonPress(int index) {
61     CalendarParsedResult calendarResult = (CalendarParsedResult) getResult();
62     switch (index) {
63       case 0:
64         addCalendarEvent(calendarResult.getSummary(), calendarResult.getStart(),
65             calendarResult.getEnd());
66         break;
67     }
68   }
69
70   @Override
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);
76
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();
81     }
82     appendTime(endString, result);
83
84     ParsedResult.maybeAppend(calResult.getLocation(), result);
85     ParsedResult.maybeAppend(calResult.getAttendee(), result);
86     ParsedResult.maybeAppend(calResult.getTitle(), result);
87     return result.toString();
88   }
89
90   private static void appendTime(String when, StringBuffer result) {
91     if (when.length() == 8) {
92       // Show only year/month/day
93       Date date;
94       synchronized (DATE_FORMAT) {
95         date = DATE_FORMAT.parse(when, new ParsePosition(0));
96       }
97       ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result);
98     } else {
99       // The when string can be local time, or UTC if it ends with a Z
100       Date date;
101       synchronized (DATE_TIME_FORMAT) {
102        date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
103       }
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;
109       }
110       ParsedResult.maybeAppend(DateFormat.getDateTimeInstance().format(milliseconds), result);
111     }
112   }
113
114   @Override
115   public int getDisplayTitle() {
116     return R.string.result_calendar;
117   }
118 }