AllInOneActivity.java revision 2c477fc6410f4da175d8a9856561aab230ff1c19
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 static android.provider.Calendar.EVENT_BEGIN_TIME;
20
21import com.android.calendar.CalendarController.EventHandler;
22import com.android.calendar.CalendarController.EventType;
23import com.android.calendar.CalendarController.ViewType;
24import com.android.calendar.SelectCalendars.SelectCalendarsFragment;
25
26import android.app.Activity;
27import android.app.Fragment;
28import android.app.FragmentTransaction;
29import android.content.SharedPreferences;
30import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
31import android.os.Bundle;
32import android.text.format.DateUtils;
33import android.text.format.Time;
34import android.util.Log;
35import android.view.Menu;
36import android.view.MenuItem;
37
38public class AllInOneActivity extends Activity implements OnSharedPreferenceChangeListener {
39    private static String TAG = "AllInOneActivity";
40    public static CalendarController mController; // FRAG_TODO make private
41
42    @Override
43    protected void onCreate(Bundle icicle) {
44        super.onCreate(icicle);
45
46        // This needs to be created before setContentView
47        mController = new CalendarController(this);
48
49        setContentView(R.layout.all_in_one);
50
51        // Get time from intent or icicle
52        long timeMillis;
53        if (icicle != null) {
54            timeMillis = icicle.getLong(EVENT_BEGIN_TIME);
55        } else {
56            timeMillis = Utils.timeFromIntentInMillis(getIntent());
57        }
58
59        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) {
75        FragmentTransaction ft = openFragmentTransaction();
76
77        Fragment miniMonthFrag = new MonthFragment(false, timeMillis);
78        ft.replace(R.id.mini_month, miniMonthFrag);
79        mController.registerEventHandler((EventHandler) miniMonthFrag);
80
81        Fragment selectCalendarsFrag = new SelectCalendarsFragment();
82        ft.replace(R.id.calendar_list, selectCalendarsFrag);
83
84        // FRAG_TODO restore event.viewType from icicle
85        mController.setMainPane(ft, R.id.main_pane, ViewType.WEEK,
86                timeMillis, true);
87
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);
95    }
96
97    @Override
98    public boolean onCreateOptionsMenu(Menu menu) {
99        super.onCreateOptionsMenu(menu);
100
101        getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);
102        return true;
103    }
104
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            case R.id.action_manage_calendars:
131                mController.sendEvent(this, EventType.LAUNCH_MANAGE_CALENDARS, null, null, 0, 0);
132                return true;
133            default:
134                return false;
135        }
136        mController.sendEvent(this, EventType.SELECT, t, null, -1, viewType);
137        return true;
138    }
139
140    @Override
141    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
142        if (key.equals(CalendarPreferenceActivity.KEY_WEEK_START_DAY)) {
143            initFragments(mController.getTime());
144        }
145    }
146}
147