AllInOneActivity.java revision 9a3cb14e28536e4133dddbe952f47189fe344ec1
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
45    @Override
46    protected void onCreate(Bundle icicle) {
47        super.onCreate(icicle);
48
49        // This needs to be created before setContentView
50        mController = CalendarController.getInstance(this);
51
52        // Must be the first to register so that this activity can modify the
53        // list the event handlers during dispatching.
54        mController.registerEventHandler(this);
55
56        setContentView(R.layout.all_in_one);
57
58        // Get time from intent or icicle
59        long timeMillis;
60        if (icicle != null) {
61            timeMillis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME);
62        } else {
63            timeMillis = Utils.timeFromIntentInMillis(getIntent());
64        }
65
66        initFragments(timeMillis, Utils.getViewTypeFromIntentAndSharedPref(this));
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
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
88    protected void onDestroy() {
89        super.onDestroy();
90
91        SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this);
92        prefs.unregisterOnSharedPreferenceChangeListener(this);
93    }
94
95    private void initFragments(long timeMillis, int viewType) {
96        FragmentTransaction ft = openFragmentTransaction();
97
98        boolean multipane = (getResources().getConfiguration().screenLayout &
99                Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0;
100
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        }
112
113        setMainPane(ft, R.id.main_pane, viewType, timeMillis, true);
114
115        ft.commit(); // this needs to be after setMainPane()
116
117        Time t = new Time();
118        t.set(timeMillis);
119        mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
120    }
121
122    @Override
123    public boolean onCreateOptionsMenu(Menu menu) {
124        super.onCreateOptionsMenu(menu);
125
126        getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);
127        return true;
128    }
129
130    @Override
131    public boolean onOptionsItemSelected(MenuItem item) {
132        Time t = null;
133        int viewType = ViewType.CURRENT;
134        switch (item.getItemId()) {
135            case R.id.action_refresh:
136                mController.refreshCalendars();
137                return true;
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;
147            case R.id.action_agenda:
148                viewType = ViewType.AGENDA;
149                break;
150            case R.id.action_today:
151                viewType = ViewType.CURRENT;
152                t = new Time();
153                t.setToNow();
154                break;
155            case R.id.action_search:
156                onSearchRequested();
157                return true;
158            case R.id.action_create_event:
159                mController.sendEventRelatedEvent(this, EventType.CREATE_EVENT, -1, 0, 0, 0, 0);
160                return true;
161            case R.id.action_manage_calendars:
162                mController.sendEvent(this, EventType.LAUNCH_MANAGE_CALENDARS, null, null, 0, 0);
163                return true;
164            case R.id.action_settings:
165                mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0);
166                return true;
167            default:
168                return false;
169        }
170        mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
171        return true;
172    }
173
174    @Override
175    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
176        if (key.equals(CalendarPreferenceActivity.KEY_WEEK_START_DAY)) {
177            initFragments(mController.getTime(), mController.getViewType());
178        }
179    }
180
181    private void setMainPane(FragmentTransaction ft, int viewId, int viewType,
182            long timeMillis, boolean force) {
183        if(!force && mController.getPreviousViewType() == viewType) {
184            return;
185        }
186
187        // Deregister old view
188        Fragment frag = findFragmentById(viewId);
189        if (frag != null) {
190            mController.deregisterEventHandler((EventHandler) frag);
191        }
192
193        // Create new one
194        switch (viewType) {
195            case ViewType.AGENDA:
196                frag = new AgendaFragment(timeMillis);
197                break;
198            case ViewType.DAY:
199                frag = new DayFragment(timeMillis, 1);
200                break;
201            case ViewType.WEEK:
202                frag = new DayFragment(timeMillis, 7);
203                break;
204            case ViewType.MONTH:
205                frag = new MonthFragment(false, timeMillis);
206                break;
207            default:
208                throw new IllegalArgumentException(
209                        "Must be Agenda, Day, Week, or Month ViewType, not " + viewType);
210        }
211
212        boolean doCommit = false;
213        if (ft == null) {
214            doCommit = true;
215            ft = openFragmentTransaction();
216        }
217
218        ft.replace(viewId, frag);
219        mController.registerEventHandler((EventHandler) frag);
220
221        if (doCommit) {
222            ft.commit();
223        }
224    }
225
226    private void setTitleInActionBar(EventInfo event) {
227        if (event.eventType != EventType.GO_TO) {
228            return;
229        }
230
231        long start = event.startTime.toMillis(false /* use isDst */);
232        long end = start;
233
234        if (event.endTime != null) {
235            end = event.endTime.toMillis(false /* use isDst */);
236        }
237        String msg = DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_SHOW_DATE);
238
239        ActionBar ab = getActionBar();
240        if (ab != null) {
241            ab.setTitle(msg);
242        }
243    }
244
245    // EventHandler Interface
246    public long getSupportedEventTypes() {
247        return EventType.GO_TO;
248    }
249
250    // EventHandler Interface
251    public void handleEvent(EventInfo event) {
252        if (event.eventType == EventType.GO_TO) {
253            // Set title bar
254            setTitleInActionBar(event);
255
256            setMainPane(null, R.id.main_pane, event.viewType,
257                    event.startTime.toMillis(false), false);
258
259            // FRAG_TODO only for XL screen
260            if (event.viewType == ViewType.MONTH) {
261                // hide minimonth and calendar frag
262                // show agenda view
263            } else {
264                // show minimonth and calendar frag
265            }
266        }
267    }
268
269    // EventHandler Interface
270    public void eventsChanged() {
271    }
272
273    // EventHandler Interface
274    public boolean getAllDay() {
275        return false;
276    }
277
278    // EventHandler Interface
279    public long getSelectedTime() {
280        return 0;
281    }
282
283    // EventHandler Interface
284    public void goTo(Time time, boolean animate) {
285    }
286
287    // EventHandler Interface
288    public void goToToday() {
289    }
290}
291