1/**
2 * Copyright (c) 2012, Google Inc.
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
17package com.android.email.activity;
18
19import android.app.Activity;
20import android.content.ContentUris;
21import android.content.Intent;
22import android.database.Cursor;
23import android.net.Uri;
24import android.os.Bundle;
25import android.provider.CalendarContract;
26
27import com.android.emailcommon.mail.MeetingInfo;
28import com.android.emailcommon.mail.PackedString;
29import com.android.emailcommon.provider.EmailContent.Message;
30import com.android.emailcommon.utility.Utility;
31
32import java.text.ParseException;
33
34public class EventViewer extends Activity {
35    @Override
36    public void onCreate(Bundle savedInstanceState) {
37        super.onCreate(savedInstanceState);
38        Uri uri = getIntent().getData();
39        long messageId = Long.parseLong(uri.getLastPathSegment());
40        Message msg = Message.restoreMessageWithId(this, messageId);
41        if (msg == null) {
42            finish();
43        } else {
44            PackedString info = new PackedString(msg.mMeetingInfo);
45            String uid = info.get(MeetingInfo.MEETING_UID);
46            long eventId = -1;
47            if (uid != null) {
48                Cursor c = getContentResolver().query(CalendarContract.Events.CONTENT_URI,
49                        new String[] {CalendarContract.Events._ID},
50                        CalendarContract.Events.SYNC_DATA2 + "=?",
51                        new String[] {uid}, null);
52                if (c != null) {
53                    try {
54                        if (c.getCount() == 1) {
55                            c.moveToFirst();
56                            eventId = c.getLong(0);
57                        }
58                    } finally {
59                        c.close();
60                    }
61                }
62            }
63            Intent intent = new Intent(Intent.ACTION_VIEW);
64            if (eventId != -1) {
65                uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
66            } else {
67                long time;
68                try {
69                    time = Utility.parseEmailDateTimeToMillis(info.get(MeetingInfo.MEETING_DTSTART));
70                } catch (ParseException e) {
71                    finish();
72                    return;
73                }
74                uri = Uri.parse("content://com.android.calendar/time/" + time);
75                intent.putExtra("VIEW", "DAY");
76            }
77            intent.setData(uri);
78            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
79            startActivity(intent);
80            finish();
81        }
82    }
83}
84