AlertActivity.java revision fac2d1594f7ecae8e50dc7790ce67de0c9b70d22
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.alerts;
18
19import android.app.Activity;
20import android.app.NotificationManager;
21import android.app.TaskStackBuilder;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.Intent;
25import android.database.Cursor;
26import android.net.Uri;
27import android.os.Bundle;
28import android.provider.CalendarContract;
29import android.provider.CalendarContract.CalendarAlerts;
30import android.util.Log;
31import android.view.View;
32import android.view.View.OnClickListener;
33import android.widget.AdapterView;
34import android.widget.AdapterView.OnItemClickListener;
35import android.widget.Button;
36import android.widget.ListView;
37
38import com.android.calendar.AsyncQueryService;
39import com.android.calendar.EventInfoActivity;
40import com.android.calendar.R;
41import com.android.calendar.Utils;
42
43/**
44 * The alert panel that pops up when there is a calendar event alarm.
45 * This activity is started by an intent that specifies an event id.
46  */
47public class AlertActivity extends Activity implements OnClickListener {
48    private static final String TAG = "AlertActivity";
49
50    private static final String[] PROJECTION = new String[] {
51        CalendarAlerts._ID,              // 0
52        CalendarAlerts.TITLE,            // 1
53        CalendarAlerts.EVENT_LOCATION,   // 2
54        CalendarAlerts.ALL_DAY,          // 3
55        CalendarAlerts.BEGIN,            // 4
56        CalendarAlerts.END,              // 5
57        CalendarAlerts.EVENT_ID,         // 6
58        CalendarAlerts.CALENDAR_COLOR,   // 7
59        CalendarAlerts.RRULE,            // 8
60        CalendarAlerts.HAS_ALARM,        // 9
61        CalendarAlerts.STATE,            // 10
62        CalendarAlerts.ALARM_TIME,       // 11
63    };
64
65    public static final int INDEX_ROW_ID = 0;
66    public static final int INDEX_TITLE = 1;
67    public static final int INDEX_EVENT_LOCATION = 2;
68    public static final int INDEX_ALL_DAY = 3;
69    public static final int INDEX_BEGIN = 4;
70    public static final int INDEX_END = 5;
71    public static final int INDEX_EVENT_ID = 6;
72    public static final int INDEX_COLOR = 7;
73    public static final int INDEX_RRULE = 8;
74    public static final int INDEX_HAS_ALARM = 9;
75    public static final int INDEX_STATE = 10;
76    public static final int INDEX_ALARM_TIME = 11;
77
78    private static final String SELECTION = CalendarAlerts.STATE + "=?";
79    private static final String[] SELECTIONARG = new String[] {
80        Integer.toString(CalendarAlerts.STATE_FIRED)
81    };
82
83    private AlertAdapter mAdapter;
84    private QueryHandler mQueryHandler;
85    private Cursor mCursor;
86    private ListView mListView;
87    private Button mDismissAllButton;
88
89
90    private void dismissFiredAlarms() {
91        ContentValues values = new ContentValues(1 /* size */);
92        values.put(PROJECTION[INDEX_STATE], CalendarAlerts.STATE_DISMISSED);
93        String selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED;
94        mQueryHandler.startUpdate(0, null, CalendarAlerts.CONTENT_URI, values,
95                selection, null /* selectionArgs */, Utils.UNDO_DELAY);
96    }
97
98    private void dismissAlarm(long id) {
99        ContentValues values = new ContentValues(1 /* size */);
100        values.put(PROJECTION[INDEX_STATE], CalendarAlerts.STATE_DISMISSED);
101        String selection = CalendarAlerts._ID + "=" + id;
102        mQueryHandler.startUpdate(0, null, CalendarAlerts.CONTENT_URI, values,
103                selection, null /* selectionArgs */, Utils.UNDO_DELAY);
104    }
105
106    private class QueryHandler extends AsyncQueryService {
107        public QueryHandler(Context context) {
108            super(context);
109        }
110
111        @Override
112        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
113            // Only set mCursor if the Activity is not finishing. Otherwise close the cursor.
114            if (!isFinishing()) {
115                mCursor = cursor;
116                mAdapter.changeCursor(cursor);
117                mListView.setSelection(cursor.getCount() - 1);
118
119                // The results are in, enable the buttons
120                mDismissAllButton.setEnabled(true);
121            } else {
122                cursor.close();
123            }
124        }
125
126        @Override
127        protected void onInsertComplete(int token, Object cookie, Uri uri) {
128            if (uri != null) {
129                Long alarmTime = (Long) cookie;
130
131                if (alarmTime != 0) {
132                    // Set a new alarm to go off after the snooze delay.
133                    // TODO make provider schedule this automatically when
134                    // inserting an alarm
135                    AlertUtils.scheduleAlarm(AlertActivity.this, null, alarmTime);
136                }
137            }
138        }
139
140        @Override
141        protected void onUpdateComplete(int token, Object cookie, int result) {
142            // Ignore
143        }
144    }
145
146
147
148    private final OnItemClickListener mViewListener = new OnItemClickListener() {
149
150        @Override
151        public void onItemClick(AdapterView<?> parent, View view, int position,
152                long i) {
153            AlertActivity alertActivity = AlertActivity.this;
154            Cursor cursor = alertActivity.getItemForView(view);
155
156            // Mark this alarm as DISMISSED
157            dismissAlarm(cursor.getLong(INDEX_ROW_ID));
158
159            // build an intent and task stack to start EventInfoActivity with AllInOneActivity
160            // as the parent activity rooted to home.
161            long id = cursor.getInt(AlertActivity.INDEX_EVENT_ID);
162            long startMillis = cursor.getLong(AlertActivity.INDEX_BEGIN);
163            long endMillis = cursor.getLong(AlertActivity.INDEX_END);
164            Intent eventIntent = AlertUtils.buildEventViewIntent(AlertActivity.this, id,
165                    startMillis, endMillis);
166
167            if (Utils.isJellybeanOrLater()) {
168                TaskStackBuilder.create(AlertActivity.this).addParentStack(EventInfoActivity.class)
169                        .addNextIntent(eventIntent).startActivities();
170            } else {
171                alertActivity.startActivity(eventIntent);
172            }
173
174            alertActivity.finish();
175        }
176    };
177
178    @Override
179    protected void onCreate(Bundle icicle) {
180        super.onCreate(icicle);
181
182        setContentView(R.layout.alert_activity);
183        setTitle(R.string.alert_title);
184
185        mQueryHandler = new QueryHandler(this);
186        mAdapter = new AlertAdapter(this, R.layout.alert_item);
187
188        mListView = (ListView) findViewById(R.id.alert_container);
189        mListView.setItemsCanFocus(true);
190        mListView.setAdapter(mAdapter);
191        mListView.setOnItemClickListener(mViewListener);
192
193        mDismissAllButton = (Button) findViewById(R.id.dismiss_all);
194        mDismissAllButton.setOnClickListener(this);
195
196        // Disable the buttons, since they need mCursor, which is created asynchronously
197        mDismissAllButton.setEnabled(false);
198    }
199
200    @Override
201    protected void onResume() {
202        super.onResume();
203
204        // If the cursor is null, start the async handler. If it is not null just requery.
205        if (mCursor == null) {
206            Uri uri = CalendarAlerts.CONTENT_URI_BY_INSTANCE;
207            mQueryHandler.startQuery(0, null, uri, PROJECTION, SELECTION, SELECTIONARG,
208                    CalendarContract.CalendarAlerts.DEFAULT_SORT_ORDER);
209        } else {
210            if (!mCursor.requery()) {
211                Log.w(TAG, "Cursor#requery() failed.");
212                mCursor.close();
213                mCursor = null;
214            }
215        }
216    }
217
218    @Override
219    protected void onStop() {
220        super.onStop();
221        AlertService.updateAlertNotification(this);
222
223        if (mCursor != null) {
224            mCursor.deactivate();
225        }
226    }
227
228    @Override
229    protected void onDestroy() {
230        super.onDestroy();
231        if (mCursor != null) {
232            mCursor.close();
233        }
234    }
235
236    @Override
237    public void onClick(View v) {
238        if (v == mDismissAllButton) {
239            NotificationManager nm =
240                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
241            nm.cancelAll();
242
243            dismissFiredAlarms();
244
245            finish();
246        }
247    }
248
249    public boolean isEmpty() {
250        return mCursor != null ? (mCursor.getCount() == 0) : true;
251    }
252
253    public Cursor getItemForView(View view) {
254        final int index = mListView.getPositionForView(view);
255        if (index < 0) {
256            return null;
257        }
258        return (Cursor) mListView.getAdapter().getItem(index);
259    }
260}
261