blob: 094dc860d46a5fa53efe4562e6a1e4c60fded236 [file] [log] [blame]
Michael Chan49701592010-06-30 11:04:03 -07001/*
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
Michael Chan83b0fe32010-07-08 16:46:26 -070019import static android.provider.Calendar.EVENT_BEGIN_TIME;
20
21import com.android.calendar.CalendarController.EventHandler;
Michael Chan83b0fe32010-07-08 16:46:26 -070022import com.android.calendar.CalendarController.EventType;
Michael Chan3458a172010-07-13 17:58:21 -070023import com.android.calendar.CalendarController.ViewType;
24import com.android.calendar.SelectCalendars.SelectCalendarsFragment;
Michael Chan83b0fe32010-07-08 16:46:26 -070025
Michael Chan49701592010-06-30 11:04:03 -070026import android.app.Activity;
Michael Chan83b0fe32010-07-08 16:46:26 -070027import android.app.Fragment;
Michael Chan3458a172010-07-13 17:58:21 -070028import android.app.FragmentTransaction;
Mason Tang8e3d4302010-07-12 17:39:30 -070029import android.content.SharedPreferences;
30import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Michael Chan49701592010-06-30 11:04:03 -070031import android.os.Bundle;
Michael Chan3458a172010-07-13 17:58:21 -070032import android.text.format.DateUtils;
Michael Chan83b0fe32010-07-08 16:46:26 -070033import android.text.format.Time;
Michael Chan3458a172010-07-13 17:58:21 -070034import android.util.Log;
Erik2051f122010-07-02 13:45:45 -070035import android.view.Menu;
Michael Chan3458a172010-07-13 17:58:21 -070036import android.view.MenuItem;
Michael Chan49701592010-06-30 11:04:03 -070037
Mason Tang8e3d4302010-07-12 17:39:30 -070038public class AllInOneActivity extends Activity implements OnSharedPreferenceChangeListener {
Michael Chan3458a172010-07-13 17:58:21 -070039 private static String TAG = "AllInOneActivity";
Michael Chan83b0fe32010-07-08 16:46:26 -070040 public static CalendarController mController; // FRAG_TODO make private
41
Michael Chan49701592010-06-30 11:04:03 -070042 @Override
Michael Chan83b0fe32010-07-08 16:46:26 -070043 protected void onCreate(Bundle icicle) {
44 super.onCreate(icicle);
45
46 // This needs to be created before setContentView
47 mController = new CalendarController(this);
48
Michael Chan49701592010-06-30 11:04:03 -070049 setContentView(R.layout.all_in_one);
Michael Chan83b0fe32010-07-08 16:46:26 -070050
Michael Chan3458a172010-07-13 17:58:21 -070051 // Get time from intent or icicle
Michael Chan83b0fe32010-07-08 16:46:26 -070052 long timeMillis;
53 if (icicle != null) {
54 timeMillis = icicle.getLong(EVENT_BEGIN_TIME);
55 } else {
56 timeMillis = Utils.timeFromIntentInMillis(getIntent());
57 }
58
Mason Tang8e3d4302010-07-12 17:39:30 -070059 initFragments(timeMillis);
60
61 // Listen for changes that would require this to be refreshed
62 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this);
63 prefs.registerOnSharedPreferenceChangeListener(this);
64 }
65
66 @Override
67 protected void onDestroy() {
68 super.onDestroy();
69
70 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this);
71 prefs.unregisterOnSharedPreferenceChangeListener(this);
72 }
73
74 private void initFragments(long timeMillis) {
Michael Chan3458a172010-07-13 17:58:21 -070075 FragmentTransaction ft = openFragmentTransaction();
76
77 Fragment miniMonthFrag = new MonthFragment(false, timeMillis);
78 ft.replace(R.id.mini_month, miniMonthFrag);
Mason Tang8e3d4302010-07-12 17:39:30 -070079 mController.registerEventHandler((EventHandler) miniMonthFrag);
Michael Chan3458a172010-07-13 17:58:21 -070080
81 Fragment selectCalendarsFrag = new SelectCalendarsFragment();
82 ft.replace(R.id.calendar_list, selectCalendarsFrag);
83
Michael Chan83b0fe32010-07-08 16:46:26 -070084 // FRAG_TODO restore event.viewType from icicle
Mason Tang8e3d4302010-07-12 17:39:30 -070085 mController.setMainPane(ft, R.id.main_pane, ViewType.WEEK,
86 timeMillis, true);
Michael Chan3458a172010-07-13 17:58:21 -070087
88 ft.commit(); // this needs to be after setMainPane()
89
90 // Set title
91 String msg = DateUtils.formatDateRange(this, timeMillis, timeMillis,
92 DateUtils.FORMAT_SHOW_DATE);
93 Log.d(TAG, "################# onCreate " + timeMillis);
94 setTitle(msg);
Michael Chan49701592010-06-30 11:04:03 -070095 }
Erik2051f122010-07-02 13:45:45 -070096
97 @Override
98 public boolean onCreateOptionsMenu(Menu menu) {
99 super.onCreateOptionsMenu(menu);
100
Erik2051f122010-07-02 13:45:45 -0700101 getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);
102 return true;
103 }
Michael Chan3458a172010-07-13 17:58:21 -0700104
105 @Override
106 public boolean onOptionsItemSelected(MenuItem item) {
107 Time t = null;
108 int viewType = ViewType.CURRENT;
109 switch (item.getItemId()) {
110 case R.id.action_day:
111 viewType = ViewType.DAY;
112 break;
113 case R.id.action_week:
114 viewType = ViewType.WEEK;
115 break;
116 case R.id.action_month:
117 viewType = ViewType.MONTH;
118 break;
119 case R.id.action_today:
120 viewType = ViewType.CURRENT;
121 t = new Time();
122 t.setToNow();
123 break;
124 case R.id.action_create_event:
125 mController.sendEventRelatedEvent(this, EventType.CREATE_EVENT, -1, 0, 0, 0, 0);
126 return true;
127 case R.id.action_settings:
128 mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0);
129 return true;
130 default:
131 return false;
132 }
133 mController.sendEvent(this, EventType.SELECT, t, null, -1, viewType);
134 return true;
135 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700136
137 @Override
138 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
139 if (key.equals(CalendarPreferenceActivity.KEY_WEEK_START_DAY)) {
140 initFragments(mController.getTime());
141 }
142 }
Michael Chan49701592010-06-30 11:04:03 -0700143}