Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.calendar; |
| 18 | |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 19 | import com.android.calendar.CalendarController.EventHandler; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 20 | import com.android.calendar.CalendarController.EventInfo; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 21 | import com.android.calendar.CalendarController.EventType; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 22 | import com.android.calendar.CalendarController.ViewType; |
Mason Tang | 9a3cb14 | 2010-07-27 12:16:41 -0700 | [diff] [blame] | 23 | import com.android.calendar.selectcalendars.SelectCalendarsFragment; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 24 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 25 | import android.app.ActionBar; |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 26 | import android.app.Activity; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 27 | import android.app.Fragment; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 28 | import android.app.FragmentTransaction; |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 29 | import android.content.SharedPreferences; |
| 30 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 31 | import android.content.res.Configuration; |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 33 | import android.text.format.DateUtils; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 34 | import android.text.format.Time; |
Erik | 2051f12 | 2010-07-02 13:45:45 -0700 | [diff] [blame] | 35 | import android.view.Menu; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 36 | import android.view.MenuItem; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 37 | import android.view.View; |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 38 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 39 | public class AllInOneActivity extends Activity implements EventHandler, |
| 40 | OnSharedPreferenceChangeListener { |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 41 | private static final String TAG = "AllInOneActivity"; |
| 42 | private static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time"; |
Michael Chan | 0558def | 2010-07-22 21:30:32 -0700 | [diff] [blame] | 43 | private static CalendarController mController; |
Erik | 9fc409f | 2010-07-28 11:40:47 -0700 | [diff] [blame^] | 44 | private static boolean mIsMultipane; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 45 | |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 46 | @Override |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 47 | protected void onCreate(Bundle icicle) { |
| 48 | super.onCreate(icicle); |
| 49 | |
| 50 | // This needs to be created before setContentView |
Michael Chan | 0558def | 2010-07-22 21:30:32 -0700 | [diff] [blame] | 51 | mController = CalendarController.getInstance(this); |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 52 | |
Erik | 9fc409f | 2010-07-28 11:40:47 -0700 | [diff] [blame^] | 53 | mIsMultipane = (getResources().getConfiguration().screenLayout & |
| 54 | Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0; |
| 55 | |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 56 | // Must be the first to register so that this activity can modify the |
| 57 | // list the event handlers during dispatching. |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 58 | mController.registerEventHandler(this); |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 59 | |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 60 | setContentView(R.layout.all_in_one); |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 61 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 62 | // Get time from intent or icicle |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 63 | long timeMillis; |
| 64 | if (icicle != null) { |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 65 | timeMillis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME); |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 66 | } else { |
| 67 | timeMillis = Utils.timeFromIntentInMillis(getIntent()); |
| 68 | } |
| 69 | |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 70 | initFragments(timeMillis, Utils.getViewTypeFromIntentAndSharedPref(this)); |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 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 |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 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 |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 92 | protected void onDestroy() { |
| 93 | super.onDestroy(); |
| 94 | |
| 95 | SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this); |
| 96 | prefs.unregisterOnSharedPreferenceChangeListener(this); |
| 97 | } |
| 98 | |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 99 | private void initFragments(long timeMillis, int viewType) { |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 100 | FragmentTransaction ft = openFragmentTransaction(); |
| 101 | |
Erik | 9fc409f | 2010-07-28 11:40:47 -0700 | [diff] [blame^] | 102 | if (mIsMultipane) { |
Erik | c7003b4 | 2010-07-27 13:25:20 -0700 | [diff] [blame] | 103 | Fragment miniMonthFrag = new MonthFragment(false, timeMillis, true); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 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 | } |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 113 | |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 114 | setMainPane(ft, R.id.main_pane, viewType, timeMillis, true); |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 115 | |
| 116 | ft.commit(); // this needs to be after setMainPane() |
| 117 | |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 118 | Time t = new Time(); |
| 119 | t.set(timeMillis); |
| 120 | mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType); |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 121 | } |
Erik | 2051f12 | 2010-07-02 13:45:45 -0700 | [diff] [blame] | 122 | |
| 123 | @Override |
| 124 | public boolean onCreateOptionsMenu(Menu menu) { |
| 125 | super.onCreateOptionsMenu(menu); |
| 126 | |
Erik | 2051f12 | 2010-07-02 13:45:45 -0700 | [diff] [blame] | 127 | getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu); |
| 128 | return true; |
| 129 | } |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 130 | |
| 131 | @Override |
| 132 | public boolean onOptionsItemSelected(MenuItem item) { |
| 133 | Time t = null; |
| 134 | int viewType = ViewType.CURRENT; |
| 135 | switch (item.getItemId()) { |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 136 | case R.id.action_refresh: |
| 137 | mController.refreshCalendars(); |
| 138 | return true; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 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; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 148 | case R.id.action_agenda: |
| 149 | viewType = ViewType.AGENDA; |
| 150 | break; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 151 | case R.id.action_today: |
| 152 | viewType = ViewType.CURRENT; |
| 153 | t = new Time(); |
| 154 | t.setToNow(); |
| 155 | break; |
Mason Tang | 0948785 | 2010-07-26 15:55:36 -0700 | [diff] [blame] | 156 | case R.id.action_search: |
| 157 | onSearchRequested(); |
| 158 | return true; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 159 | case R.id.action_create_event: |
| 160 | mController.sendEventRelatedEvent(this, EventType.CREATE_EVENT, -1, 0, 0, 0, 0); |
| 161 | return true; |
Michael Chan | 2c477fc | 2010-07-15 18:26:50 -0700 | [diff] [blame] | 162 | case R.id.action_manage_calendars: |
| 163 | mController.sendEvent(this, EventType.LAUNCH_MANAGE_CALENDARS, null, null, 0, 0); |
| 164 | return true; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 165 | case R.id.action_settings: |
| 166 | mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0); |
| 167 | return true; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 168 | default: |
| 169 | return false; |
| 170 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 171 | mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType); |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 172 | return true; |
| 173 | } |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 174 | |
| 175 | @Override |
| 176 | public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { |
| 177 | if (key.equals(CalendarPreferenceActivity.KEY_WEEK_START_DAY)) { |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 178 | initFragments(mController.getTime(), mController.getViewType()); |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 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: |
Mason Tang | 31df426 | 2010-07-19 17:51:46 -0700 | [diff] [blame] | 197 | frag = new AgendaFragment(timeMillis); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 198 | break; |
| 199 | case ViewType.DAY: |
Michael Chan | 75d9b56 | 2010-07-26 16:54:25 -0700 | [diff] [blame] | 200 | frag = new DayFragment(timeMillis, 1); |
| 201 | break; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 202 | case ViewType.WEEK: |
Michael Chan | 75d9b56 | 2010-07-26 16:54:25 -0700 | [diff] [blame] | 203 | frag = new DayFragment(timeMillis, 7); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 204 | break; |
| 205 | case ViewType.MONTH: |
Erik | c7003b4 | 2010-07-27 13:25:20 -0700 | [diff] [blame] | 206 | frag = new MonthFragment(false, timeMillis, false); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 207 | break; |
| 208 | default: |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 209 | throw new IllegalArgumentException( |
| 210 | "Must be Agenda, Day, Week, or Month ViewType, not " + viewType); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 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 | |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 235 | if (event.endTime != null) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 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 | |
Erik | 9fc409f | 2010-07-28 11:40:47 -0700 | [diff] [blame^] | 260 | if (!mIsMultipane) { |
| 261 | return; |
| 262 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 263 | if (event.viewType == ViewType.MONTH) { |
| 264 | // hide minimonth and calendar frag |
Erik | 9fc409f | 2010-07-28 11:40:47 -0700 | [diff] [blame^] | 265 | findViewById(R.id.mini_month).setVisibility(View.GONE); |
| 266 | findViewById(R.id.calendar_list).setVisibility(View.GONE); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 267 | } else { |
| 268 | // show minimonth and calendar frag |
Erik | 9fc409f | 2010-07-28 11:40:47 -0700 | [diff] [blame^] | 269 | findViewById(R.id.mini_month).setVisibility(View.VISIBLE); |
| 270 | findViewById(R.id.calendar_list).setVisibility(View.VISIBLE); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 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 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 296 | } |