AllInOneActivity.java revision 9fc409f5a5f22d3faa42a4f58ec204686f5fe4c9
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 17package com.android.calendar; 18 19import com.android.calendar.CalendarController.EventHandler; 20import com.android.calendar.CalendarController.EventInfo; 21import com.android.calendar.CalendarController.EventType; 22import com.android.calendar.CalendarController.ViewType; 23import com.android.calendar.selectcalendars.SelectCalendarsFragment; 24 25import android.app.ActionBar; 26import android.app.Activity; 27import android.app.Fragment; 28import android.app.FragmentTransaction; 29import android.content.SharedPreferences; 30import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 31import android.content.res.Configuration; 32import android.os.Bundle; 33import android.text.format.DateUtils; 34import android.text.format.Time; 35import android.view.Menu; 36import android.view.MenuItem; 37import android.view.View; 38 39public class AllInOneActivity extends Activity implements EventHandler, 40 OnSharedPreferenceChangeListener { 41 private static final String TAG = "AllInOneActivity"; 42 private static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time"; 43 private static CalendarController mController; 44 private static boolean mIsMultipane; 45 46 @Override 47 protected void onCreate(Bundle icicle) { 48 super.onCreate(icicle); 49 50 // This needs to be created before setContentView 51 mController = CalendarController.getInstance(this); 52 53 mIsMultipane = (getResources().getConfiguration().screenLayout & 54 Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0; 55 56 // Must be the first to register so that this activity can modify the 57 // list the event handlers during dispatching. 58 mController.registerEventHandler(this); 59 60 setContentView(R.layout.all_in_one); 61 62 // Get time from intent or icicle 63 long timeMillis; 64 if (icicle != null) { 65 timeMillis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME); 66 } else { 67 timeMillis = Utils.timeFromIntentInMillis(getIntent()); 68 } 69 70 initFragments(timeMillis, Utils.getViewTypeFromIntentAndSharedPref(this)); 71 72 // Listen for changes that would require this to be refreshed 73 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this); 74 prefs.registerOnSharedPreferenceChangeListener(this); 75 } 76 77 @Override 78 protected void onPause() { 79 super.onPause(); 80 //FRAG_TODO save highlighted days of the week; 81 Utils.setDefaultView(this, mController.getViewType()); 82 } 83 84 @Override 85 public void onSaveInstanceState(Bundle outState) { 86 super.onSaveInstanceState(outState); 87 88 outState.putLong(BUNDLE_KEY_RESTORE_TIME, mController.getTime()); 89 } 90 91 @Override 92 protected void onDestroy() { 93 super.onDestroy(); 94 95 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this); 96 prefs.unregisterOnSharedPreferenceChangeListener(this); 97 } 98 99 private void initFragments(long timeMillis, int viewType) { 100 FragmentTransaction ft = openFragmentTransaction(); 101 102 if (mIsMultipane) { 103 Fragment miniMonthFrag = new MonthFragment(false, timeMillis, true); 104 ft.replace(R.id.mini_month, miniMonthFrag); 105 mController.registerEventHandler((EventHandler) miniMonthFrag); 106 107 Fragment selectCalendarsFrag = new SelectCalendarsFragment(); 108 ft.replace(R.id.calendar_list, selectCalendarsFrag); 109 } else { 110 findViewById(R.id.mini_month).setVisibility(View.GONE); 111 findViewById(R.id.calendar_list).setVisibility(View.GONE); 112 } 113 114 setMainPane(ft, R.id.main_pane, viewType, timeMillis, true); 115 116 ft.commit(); // this needs to be after setMainPane() 117 118 Time t = new Time(); 119 t.set(timeMillis); 120 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType); 121 } 122 123 @Override 124 public boolean onCreateOptionsMenu(Menu menu) { 125 super.onCreateOptionsMenu(menu); 126 127 getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu); 128 return true; 129 } 130 131 @Override 132 public boolean onOptionsItemSelected(MenuItem item) { 133 Time t = null; 134 int viewType = ViewType.CURRENT; 135 switch (item.getItemId()) { 136 case R.id.action_refresh: 137 mController.refreshCalendars(); 138 return true; 139 case R.id.action_day: 140 viewType = ViewType.DAY; 141 break; 142 case R.id.action_week: 143 viewType = ViewType.WEEK; 144 break; 145 case R.id.action_month: 146 viewType = ViewType.MONTH; 147 break; 148 case R.id.action_agenda: 149 viewType = ViewType.AGENDA; 150 break; 151 case R.id.action_today: 152 viewType = ViewType.CURRENT; 153 t = new Time(); 154 t.setToNow(); 155 break; 156 case R.id.action_search: 157 onSearchRequested(); 158 return true; 159 case R.id.action_create_event: 160 mController.sendEventRelatedEvent(this, EventType.CREATE_EVENT, -1, 0, 0, 0, 0); 161 return true; 162 case R.id.action_manage_calendars: 163 mController.sendEvent(this, EventType.LAUNCH_MANAGE_CALENDARS, null, null, 0, 0); 164 return true; 165 case R.id.action_settings: 166 mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0); 167 return true; 168 default: 169 return false; 170 } 171 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType); 172 return true; 173 } 174 175 @Override 176 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 177 if (key.equals(CalendarPreferenceActivity.KEY_WEEK_START_DAY)) { 178 initFragments(mController.getTime(), mController.getViewType()); 179 } 180 } 181 182 private void setMainPane(FragmentTransaction ft, int viewId, int viewType, 183 long timeMillis, boolean force) { 184 if(!force && mController.getPreviousViewType() == viewType) { 185 return; 186 } 187 188 // Deregister old view 189 Fragment frag = findFragmentById(viewId); 190 if (frag != null) { 191 mController.deregisterEventHandler((EventHandler) frag); 192 } 193 194 // Create new one 195 switch (viewType) { 196 case ViewType.AGENDA: 197 frag = new AgendaFragment(timeMillis); 198 break; 199 case ViewType.DAY: 200 frag = new DayFragment(timeMillis, 1); 201 break; 202 case ViewType.WEEK: 203 frag = new DayFragment(timeMillis, 7); 204 break; 205 case ViewType.MONTH: 206 frag = new MonthFragment(false, timeMillis, false); 207 break; 208 default: 209 throw new IllegalArgumentException( 210 "Must be Agenda, Day, Week, or Month ViewType, not " + viewType); 211 } 212 213 boolean doCommit = false; 214 if (ft == null) { 215 doCommit = true; 216 ft = openFragmentTransaction(); 217 } 218 219 ft.replace(viewId, frag); 220 mController.registerEventHandler((EventHandler) frag); 221 222 if (doCommit) { 223 ft.commit(); 224 } 225 } 226 227 private void setTitleInActionBar(EventInfo event) { 228 if (event.eventType != EventType.GO_TO) { 229 return; 230 } 231 232 long start = event.startTime.toMillis(false /* use isDst */); 233 long end = start; 234 235 if (event.endTime != null) { 236 end = event.endTime.toMillis(false /* use isDst */); 237 } 238 String msg = DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_SHOW_DATE); 239 240 ActionBar ab = getActionBar(); 241 if (ab != null) { 242 ab.setTitle(msg); 243 } 244 } 245 246 // EventHandler Interface 247 public long getSupportedEventTypes() { 248 return EventType.GO_TO; 249 } 250 251 // EventHandler Interface 252 public void handleEvent(EventInfo event) { 253 if (event.eventType == EventType.GO_TO) { 254 // Set title bar 255 setTitleInActionBar(event); 256 257 setMainPane(null, R.id.main_pane, event.viewType, 258 event.startTime.toMillis(false), false); 259 260 if (!mIsMultipane) { 261 return; 262 } 263 if (event.viewType == ViewType.MONTH) { 264 // hide minimonth and calendar frag 265 findViewById(R.id.mini_month).setVisibility(View.GONE); 266 findViewById(R.id.calendar_list).setVisibility(View.GONE); 267 } else { 268 // show minimonth and calendar frag 269 findViewById(R.id.mini_month).setVisibility(View.VISIBLE); 270 findViewById(R.id.calendar_list).setVisibility(View.VISIBLE); 271 } 272 } 273 } 274 275 // EventHandler Interface 276 public void eventsChanged() { 277 } 278 279 // EventHandler Interface 280 public boolean getAllDay() { 281 return false; 282 } 283 284 // EventHandler Interface 285 public long getSelectedTime() { 286 return 0; 287 } 288 289 // EventHandler Interface 290 public void goTo(Time time, boolean animate) { 291 } 292 293 // EventHandler Interface 294 public void goToToday() { 295 } 296} 297