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