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