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 
17 package com.android.email.activity;
18 
19 import android.app.Activity;
20 import android.content.ContentUris;
21 import android.content.Intent;
22 import android.database.Cursor;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.provider.CalendarContract;
26 
27 import com.android.emailcommon.mail.MeetingInfo;
28 import com.android.emailcommon.mail.PackedString;
29 import com.android.emailcommon.provider.EmailContent.Message;
30 import com.android.emailcommon.utility.Utility;
31 
32 import java.text.ParseException;
33 
34 public class EventViewer extends Activity {
35     @Override
onCreate(Bundle savedInstanceState)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