The new Android client, featuring:
[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 class CalendarResultHandler extends ResultHandler {
32
33   private static final int[] mButtons = {
34       R.string.button_add_calendar
35   };
36
37   public CalendarResultHandler(Activity activity, ParsedResult result) {
38     super(activity, result);
39   }
40
41   public int getButtonCount() {
42     return mButtons.length;
43   }
44
45   public int getButtonText(int index) {
46     return mButtons[index];
47   }
48
49   public void handleButtonPress(int index) {
50     CalendarParsedResult calendarResult = (CalendarParsedResult) mResult;
51     switch (index) {
52       case 0:
53         addCalendarEvent(calendarResult.getSummary(), calendarResult.getStart(),
54             calendarResult.getEnd());
55         break;
56     }
57   }
58
59   @Override
60   public CharSequence getDisplayContents() {
61     CalendarParsedResult calResult = (CalendarParsedResult) mResult;
62     StringBuffer result = new StringBuffer();
63     ParsedResult.maybeAppend(calResult.getSummary(), result);
64     appendTime(calResult.getStart(), result);
65
66     // The end can be null if the event has no duration, so use the start time.
67     String endString = calResult.getEnd();
68     if (endString == null) endString = calResult.getStart();
69     appendTime(endString, result);
70
71     ParsedResult.maybeAppend(calResult.getLocation(), result);
72     ParsedResult.maybeAppend(calResult.getAttendee(), result);
73     ParsedResult.maybeAppend(calResult.getTitle(), result);
74     return result.toString();
75   }
76
77   private void appendTime(String when, StringBuffer result) {
78     if (when.length() == 8) {
79       // Show only year/month/day
80       SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
81       Date date = format.parse(when, new ParsePosition(0));
82       ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result);
83     } else {
84       // The when string can be local time, or UTC if it ends with a Z
85       SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
86       Date date = format.parse(when.substring(0, 15), new ParsePosition(0));
87       long milliseconds = date.getTime();
88       if (when.length() == 16 && when.charAt(15) == 'Z') {
89         GregorianCalendar calendar = new GregorianCalendar();
90         int offset = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET));
91         milliseconds += offset;
92       }
93       ParsedResult.maybeAppend(DateFormat.getDateTimeInstance().format(milliseconds), result);
94     }
95   }
96
97   public int getDisplayTitle() {
98     return R.string.result_calendar;
99   }
100
101 }