1 /* 2 * Copyright (C) 2010 The Android Open Source Project 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 package com.android.calendar; 17 18 import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME; 19 import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME; 20 import static android.provider.CalendarContract.Attendees.ATTENDEE_STATUS; 21 22 import android.app.ActionBar; 23 import android.app.Activity; 24 import android.app.FragmentManager; 25 import android.app.FragmentTransaction; 26 import android.content.Intent; 27 import android.content.res.Resources; 28 import android.database.ContentObserver; 29 import android.net.Uri; 30 import android.os.Bundle; 31 import android.os.Handler; 32 import android.provider.CalendarContract; 33 import android.provider.CalendarContract.Attendees; 34 import android.util.Log; 35 import android.widget.Toast; 36 37 import java.util.ArrayList; 38 import java.util.List; 39 40 public class EventInfoActivity extends Activity { 41 private static final String TAG = "EventInfoActivity"; 42 private EventInfoFragment mInfoFragment; 43 private long mStartMillis, mEndMillis; 44 private long mEventId; 45 46 // Create an observer so that we can update the views whenever a 47 // Calendar event changes. 48 private final ContentObserver mObserver = new ContentObserver(new Handler()) { 49 @Override 50 public boolean deliverSelfNotifications() { 51 return false; 52 } 53 54 @Override 55 public void onChange(boolean selfChange) { 56 if (selfChange) return; 57 if (mInfoFragment != null) { 58 mInfoFragment.reloadEvents(); 59 } 60 } 61 }; 62 63 @Override onCreate(Bundle icicle)64 protected void onCreate(Bundle icicle) { 65 super.onCreate(icicle); 66 67 // Get the info needed for the fragment 68 Intent intent = getIntent(); 69 int attendeeResponse = 0; 70 mEventId = -1; 71 boolean isDialog = false; 72 73 if (icicle != null) { 74 mEventId = icicle.getLong(EventInfoFragment.BUNDLE_KEY_EVENT_ID); 75 mStartMillis = icicle.getLong(EventInfoFragment.BUNDLE_KEY_START_MILLIS); 76 mEndMillis = icicle.getLong(EventInfoFragment.BUNDLE_KEY_END_MILLIS); 77 attendeeResponse = icicle.getInt(EventInfoFragment.BUNDLE_KEY_ATTENDEE_RESPONSE); 78 isDialog = icicle.getBoolean(EventInfoFragment.BUNDLE_KEY_IS_DIALOG); 79 } else if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) { 80 mStartMillis = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, 0); 81 mEndMillis = intent.getLongExtra(EXTRA_EVENT_END_TIME, 0); 82 attendeeResponse = intent.getIntExtra(ATTENDEE_STATUS, 83 Attendees.ATTENDEE_STATUS_NONE); 84 Uri data = intent.getData(); 85 if (data != null) { 86 try { 87 List<String> pathSegments = data.getPathSegments(); 88 int size = pathSegments.size(); 89 if (size > 2 && "EventTime".equals(pathSegments.get(2))) { 90 // Support non-standard VIEW intent format: 91 //dat = content://com.android.calendar/events/[id]/EventTime/[start]/[end] 92 mEventId = Long.parseLong(pathSegments.get(1)); 93 if (size > 4) { 94 mStartMillis = Long.parseLong(pathSegments.get(3)); 95 mEndMillis = Long.parseLong(pathSegments.get(4)); 96 } 97 } else { 98 mEventId = Long.parseLong(data.getLastPathSegment()); 99 } 100 } catch (NumberFormatException e) { 101 if (mEventId == -1) { 102 // do nothing here , deal with it later 103 } else if (mStartMillis == 0 || mEndMillis ==0) { 104 // Parsing failed on the start or end time , make sure the times were not 105 // pulled from the intent's extras and reset them. 106 mStartMillis = 0; 107 mEndMillis = 0; 108 } 109 } 110 } 111 } 112 113 if (mEventId == -1) { 114 Log.w(TAG, "No event id"); 115 Toast.makeText(this, R.string.event_not_found, Toast.LENGTH_SHORT).show(); 116 finish(); 117 } 118 119 // If we do not support showing full screen event info in this configuration, 120 // close the activity and show the event in AllInOne. 121 Resources res = getResources(); 122 if (!res.getBoolean(R.bool.agenda_show_event_info_full_screen) 123 && !res.getBoolean(R.bool.show_event_info_full_screen)) { 124 CalendarController.getInstance(this) 125 .launchViewEvent(mEventId, mStartMillis, mEndMillis, attendeeResponse); 126 finish(); 127 return; 128 } 129 130 setContentView(R.layout.simple_frame_layout); 131 132 // Get the fragment if exists 133 mInfoFragment = (EventInfoFragment) 134 getFragmentManager().findFragmentById(R.id.main_frame); 135 136 137 // Remove the application title 138 ActionBar bar = getActionBar(); 139 if (bar != null) { 140 bar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME); 141 } 142 143 // Create a new fragment if none exists 144 if (mInfoFragment == null) { 145 FragmentManager fragmentManager = getFragmentManager(); 146 FragmentTransaction ft = fragmentManager.beginTransaction(); 147 mInfoFragment = new EventInfoFragment(this, mEventId, mStartMillis, mEndMillis, 148 attendeeResponse, isDialog, (isDialog ? 149 EventInfoFragment.DIALOG_WINDOW_STYLE : 150 EventInfoFragment.FULL_WINDOW_STYLE)); 151 ft.replace(R.id.main_frame, mInfoFragment); 152 ft.commit(); 153 } 154 } 155 156 @Override onNewIntent(Intent intent)157 protected void onNewIntent(Intent intent) { 158 // From the Android Dev Guide: "It's important to note that when 159 // onNewIntent(Intent) is called, the Activity has not been restarted, 160 // so the getIntent() method will still return the Intent that was first 161 // received with onCreate(). This is why setIntent(Intent) is called 162 // inside onNewIntent(Intent) (just in case you call getIntent() at a 163 // later time)." 164 setIntent(intent); 165 } 166 167 168 @Override onSaveInstanceState(Bundle outState)169 public void onSaveInstanceState(Bundle outState) { 170 super.onSaveInstanceState(outState); 171 } 172 173 @Override onResume()174 protected void onResume() { 175 super.onResume(); 176 getContentResolver().registerContentObserver(CalendarContract.Events.CONTENT_URI, 177 true, mObserver); 178 } 179 180 @Override onPause()181 protected void onPause() { 182 super.onPause(); 183 getContentResolver().unregisterContentObserver(mObserver); 184 } 185 186 @Override onDestroy()187 protected void onDestroy() { 188 super.onDestroy(); 189 } 190 } 191