AgendaFragment.java revision 00b8c1a39d75c1b4626dc987bd1a51cfaf7c9be1
1/*
2 * Copyright (C) 2007 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.agenda;
18
19import android.app.Fragment;
20import android.content.Context;
21import android.content.SharedPreferences;
22import android.os.Bundle;
23import android.text.format.Time;
24import android.util.Log;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28
29import com.android.calendar.CalendarController;
30import com.android.calendar.CalendarController.EventInfo;
31import com.android.calendar.CalendarController.EventType;
32import com.android.calendar.CalendarPreferenceActivity;
33
34import dalvik.system.VMRuntime;
35
36public class AgendaFragment extends Fragment implements CalendarController.EventHandler {
37
38    private static final String TAG = AgendaFragment.class.getSimpleName();
39    private static boolean DEBUG = false;
40
41    protected static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time";
42    private static final long INITIAL_HEAP_SIZE = 4*1024*1024;
43
44    private AgendaListView mAgendaListView;
45    private Time mTime;
46
47    private String mQuery;
48
49    public AgendaFragment() {
50        this(0);
51    }
52
53    public AgendaFragment(long timeMillis) {
54        mTime = new Time();
55        if (timeMillis == 0) {
56            mTime.setToNow();
57        } else {
58            mTime.set(timeMillis);
59        }
60    }
61
62    @Override
63    public void onCreate(Bundle icicle) {
64        super.onCreate(icicle);
65        // Eliminate extra GCs during startup by setting the initial heap size to 4MB.
66        // TODO: We should restore the old heap size once the activity reaches the idle state
67        VMRuntime.getRuntime().setMinimumHeapSize(INITIAL_HEAP_SIZE);
68    }
69
70    @Override
71    public View onCreateView(LayoutInflater inflater, ViewGroup container,
72            Bundle savedInstanceState) {
73        Context context = getActivity();
74        mAgendaListView = new AgendaListView(context);
75        mAgendaListView.goTo(mTime, mQuery, false);
76        return mAgendaListView;
77    }
78
79    @Override
80    public void onResume() {
81        super.onResume();
82        if (DEBUG) {
83            Log.v(TAG, "OnResume to " + mTime.toString());
84        }
85
86        SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(
87                getActivity());
88        boolean hideDeclined = prefs.getBoolean(
89                CalendarPreferenceActivity.KEY_HIDE_DECLINED, false);
90
91        mAgendaListView.setHideDeclinedEvents(hideDeclined);
92        mAgendaListView.goTo(mTime, mQuery, true);
93        mAgendaListView.onResume();
94
95//        // Register for Intent broadcasts
96//        IntentFilter filter = new IntentFilter();
97//        filter.addAction(Intent.ACTION_TIME_CHANGED);
98//        filter.addAction(Intent.ACTION_DATE_CHANGED);
99//        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
100//        registerReceiver(mIntentReceiver, filter);
101//
102//        mContentResolver.registerContentObserver(Events.CONTENT_URI, true, mObserver);
103    }
104
105    @Override
106    public void onSaveInstanceState(Bundle outState) {
107        super.onSaveInstanceState(outState);
108
109        long firstVisibleTime = mAgendaListView.getFirstVisibleTime();
110        if (firstVisibleTime > 0) {
111            mTime.set(firstVisibleTime);
112            outState.putLong(BUNDLE_KEY_RESTORE_TIME, firstVisibleTime);
113            if (DEBUG) {
114                Log.v(TAG, "onSaveInstanceState " + mTime.toString());
115            }
116        }
117    }
118
119    @Override
120    public void onPause() {
121        super.onPause();
122
123        mAgendaListView.onPause();
124//        mContentResolver.unregisterContentObserver(mObserver);
125//        unregisterReceiver(mIntentReceiver);
126
127        // Record Agenda View as the (new) default detailed view.
128//        Utils.setDefaultView(this, CalendarApplication.AGENDA_VIEW_ID);
129    }
130
131    /* Navigator interface methods */
132    @Override
133    public void goToToday() {
134        if (mAgendaListView == null) {
135         // The view hasn't been set yet. Just save the time and use it later.
136            mTime.setToNow();
137            return;
138        }
139        Time now = new Time();
140        now.setToNow();
141        mAgendaListView.goTo(now, mQuery, true); // Force refresh
142    }
143
144    @Override
145    public void goTo(Time time, boolean animate) {
146        if (mAgendaListView == null) {
147            // The view hasn't been set yet. Just save the time and use it
148            // later.
149            mTime.set(time);
150            return;
151        }
152        mAgendaListView.goTo(time, mQuery, false);
153    }
154
155    private void search(String query, Time time) {
156        mQuery = query;
157        if (time != null) {
158            mTime.set(time);
159        }
160        if (mAgendaListView == null) {
161            // The view hasn't been set yet. Just return.
162            return;
163        }
164        mAgendaListView.goTo(time, mQuery, true);
165    }
166
167    @Override
168    public long getSelectedTime() {
169        return mAgendaListView.getSelectedTime();
170    }
171
172    @Override
173    public boolean getAllDay() {
174        return false;
175    }
176
177    @Override
178    public void eventsChanged() {
179        mAgendaListView.refresh(true);
180    }
181
182    @Override
183    public long getSupportedEventTypes() {
184        return EventType.GO_TO | EventType.EVENTS_CHANGED | EventType.SEARCH;
185    }
186
187    @Override
188    public void handleEvent(EventInfo event) {
189        if (event.eventType == EventType.GO_TO) {
190            // TODO support a range of time
191            // TODO support event_id
192            // TODO figure out the animate bit
193            goTo(event.startTime, true);
194        } else if (event.eventType == EventType.SEARCH) {
195            search(event.query, event.startTime);
196        } else if (event.eventType == EventType.EVENTS_CHANGED) {
197            eventsChanged();
198        }
199    }
200}
201
202