blob: 12d7b10a37e38d175867fdfa540708c63a3c0e9c [file] [log] [blame]
The Android Open Source Project146de362009-03-03 19:32:18 -08001/*
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
Mason Tang23e7da32010-07-27 15:24:02 -070017package com.android.calendar.alerts;
The Android Open Source Project146de362009-03-03 19:32:18 -080018
The Android Open Source Project146de362009-03-03 19:32:18 -080019import android.app.Activity;
The Android Open Source Project146de362009-03-03 19:32:18 -080020import android.app.NotificationManager;
Isaac Katzenelson0ef732f2012-04-26 17:39:14 -070021import android.app.TaskStackBuilder;
Michael Chan5c7290c2009-07-16 17:41:59 -070022import android.content.ContentValues;
The Android Open Source Project146de362009-03-03 19:32:18 -080023import android.content.Context;
Erikdb4ce4a2010-10-27 12:50:59 -070024import android.content.Intent;
The Android Open Source Project146de362009-03-03 19:32:18 -080025import android.database.Cursor;
The Android Open Source Project146de362009-03-03 19:32:18 -080026import android.net.Uri;
27import android.os.Bundle;
Michael Chan8f7f1ab2012-05-11 07:03:26 -070028import android.provider.CalendarContract;
RoboErika7c03902011-06-14 11:06:44 -070029import android.provider.CalendarContract.CalendarAlerts;
Daisuke Miyakawa1a7fa282010-10-15 17:37:37 -070030import android.util.Log;
The Android Open Source Project146de362009-03-03 19:32:18 -080031import android.view.View;
Daisuke Miyakawa1a7fa282010-10-15 17:37:37 -070032import android.view.View.OnClickListener;
The Android Open Source Project146de362009-03-03 19:32:18 -080033import android.widget.AdapterView;
Daisuke Miyakawa1a7fa282010-10-15 17:37:37 -070034import android.widget.AdapterView.OnItemClickListener;
The Android Open Source Project146de362009-03-03 19:32:18 -080035import android.widget.Button;
36import android.widget.ListView;
The Android Open Source Project146de362009-03-03 19:32:18 -080037
Michael Chan8f7f1ab2012-05-11 07:03:26 -070038import com.android.calendar.AsyncQueryService;
39import com.android.calendar.EventInfoActivity;
40import com.android.calendar.R;
41import com.android.calendar.Utils;
42
The Android Open Source Project146de362009-03-03 19:32:18 -080043/**
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 */
Daisuke Miyakawa1a7fa282010-10-15 17:37:37 -070047public class AlertActivity extends Activity implements OnClickListener {
48 private static final String TAG = "AlertActivity";
Michael Chan5c7290c2009-07-16 17:41:59 -070049
Michael Chan5c7290c2009-07-16 17:41:59 -070050 private static final String[] PROJECTION = new String[] {
The Android Open Source Project146de362009-03-03 19:32:18 -080051 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
Isaac Katzenelson98819072012-03-01 15:58:30 -080058 CalendarAlerts.CALENDAR_COLOR, // 7
The Android Open Source Project146de362009-03-03 19:32:18 -080059 CalendarAlerts.RRULE, // 8
60 CalendarAlerts.HAS_ALARM, // 9
61 CalendarAlerts.STATE, // 10
62 CalendarAlerts.ALARM_TIME, // 11
63 };
Michael Chan5c7290c2009-07-16 17:41:59 -070064
65 public static final int INDEX_ROW_ID = 0;
The Android Open Source Project146de362009-03-03 19:32:18 -080066 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;
Michael Chan5c7290c2009-07-16 17:41:59 -070077
Sara Tingbb577102012-05-11 15:55:48 -070078 private static final String SELECTION = CalendarAlerts.STATE + "=?";
79 private static final String[] SELECTIONARG = new String[] {
80 Integer.toString(CalendarAlerts.STATE_FIRED)
81 };
Michael Chane2ae1ef2009-11-18 18:37:09 -080082
The Android Open Source Project146de362009-03-03 19:32:18 -080083 private AlertAdapter mAdapter;
84 private QueryHandler mQueryHandler;
85 private Cursor mCursor;
86 private ListView mListView;
The Android Open Source Project146de362009-03-03 19:32:18 -080087 private Button mDismissAllButton;
Michael Chan5c7290c2009-07-16 17:41:59 -070088
89
Sara Tingbb577102012-05-11 15:55:48 -070090 private void dismissFiredAlarms() {
Michael Chan5c7290c2009-07-16 17:41:59 -070091 ContentValues values = new ContentValues(1 /* size */);
RoboErikfa292a02011-06-30 12:49:42 -070092 values.put(PROJECTION[INDEX_STATE], CalendarAlerts.STATE_DISMISSED);
Sara Tingbb577102012-05-11 15:55:48 -070093 String selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED;
Michael Chan5c7290c2009-07-16 17:41:59 -070094 mQueryHandler.startUpdate(0, null, CalendarAlerts.CONTENT_URI, values,
Michael Chanbed02752010-04-27 10:56:53 -070095 selection, null /* selectionArgs */, Utils.UNDO_DELAY);
Michael Chan5c7290c2009-07-16 17:41:59 -070096 }
97
98 private void dismissAlarm(long id) {
99 ContentValues values = new ContentValues(1 /* size */);
RoboErikfa292a02011-06-30 12:49:42 -0700100 values.put(PROJECTION[INDEX_STATE], CalendarAlerts.STATE_DISMISSED);
Michael Chan5c7290c2009-07-16 17:41:59 -0700101 String selection = CalendarAlerts._ID + "=" + id;
102 mQueryHandler.startUpdate(0, null, CalendarAlerts.CONTENT_URI, values,
Michael Chanbed02752010-04-27 10:56:53 -0700103 selection, null /* selectionArgs */, Utils.UNDO_DELAY);
Michael Chan5c7290c2009-07-16 17:41:59 -0700104 }
105
Michael Chanbed02752010-04-27 10:56:53 -0700106 private class QueryHandler extends AsyncQueryService {
107 public QueryHandler(Context context) {
108 super(context);
The Android Open Source Project146de362009-03-03 19:32:18 -0800109 }
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);
Michael Chan10d9f112011-01-17 21:17:08 -0800117 mListView.setSelection(cursor.getCount() - 1);
Michael Chan5c7290c2009-07-16 17:41:59 -0700118
The Android Open Source Project146de362009-03-03 19:32:18 -0800119 // The results are in, enable the buttons
The Android Open Source Project146de362009-03-03 19:32:18 -0800120 mDismissAllButton.setEnabled(true);
121 } else {
122 cursor.close();
123 }
124 }
Michael Chan5c7290c2009-07-16 17:41:59 -0700125
126 @Override
Michael Chan5c7290c2009-07-16 17:41:59 -0700127 protected void onUpdateComplete(int token, Object cookie, int result) {
128 // Ignore
129 }
The Android Open Source Project146de362009-03-03 19:32:18 -0800130 }
Michael Chan5c7290c2009-07-16 17:41:59 -0700131
Isaac Katzenelson0ef732f2012-04-26 17:39:14 -0700132 private final OnItemClickListener mViewListener = new OnItemClickListener() {
The Android Open Source Project146de362009-03-03 19:32:18 -0800133
Isaac Katzenelson98819072012-03-01 15:58:30 -0800134 @Override
Christian Mehlmauer4ce500b2010-05-17 01:10:01 +0200135 public void onItemClick(AdapterView<?> parent, View view, int position,
The Android Open Source Project146de362009-03-03 19:32:18 -0800136 long i) {
137 AlertActivity alertActivity = AlertActivity.this;
138 Cursor cursor = alertActivity.getItemForView(view);
Michael Chan5c7290c2009-07-16 17:41:59 -0700139
The Android Open Source Project146de362009-03-03 19:32:18 -0800140 // Mark this alarm as DISMISSED
Michael Chan5c7290c2009-07-16 17:41:59 -0700141 dismissAlarm(cursor.getLong(INDEX_ROW_ID));
142
Isaac Katzenelson0ef732f2012-04-26 17:39:14 -0700143 // build an intent and task stack to start EventInfoActivity with AllInOneActivity
144 // as the parent activity rooted to home.
Michael Chan9e89dca2010-07-13 17:56:09 -0700145 long id = cursor.getInt(AlertActivity.INDEX_EVENT_ID);
146 long startMillis = cursor.getLong(AlertActivity.INDEX_BEGIN);
147 long endMillis = cursor.getLong(AlertActivity.INDEX_END);
Isaac Katzenelson98819072012-03-01 15:58:30 -0800148 Intent eventIntent = AlertUtils.buildEventViewIntent(AlertActivity.this, id,
149 startMillis, endMillis);
Isaac Katzenelson0ef732f2012-04-26 17:39:14 -0700150
Sara Tingfac2d152012-05-31 14:59:57 -0700151 if (Utils.isJellybeanOrLater()) {
Michael Chan693ca602012-05-31 12:24:11 -0700152 TaskStackBuilder.create(AlertActivity.this).addParentStack(EventInfoActivity.class)
153 .addNextIntent(eventIntent).startActivities();
Sara Tingfac2d152012-05-31 14:59:57 -0700154 } else {
155 alertActivity.startActivity(eventIntent);
Michael Chan693ca602012-05-31 12:24:11 -0700156 }
Michael Chan9e89dca2010-07-13 17:56:09 -0700157
The Android Open Source Project146de362009-03-03 19:32:18 -0800158 alertActivity.finish();
159 }
160 };
Michael Chan5c7290c2009-07-16 17:41:59 -0700161
The Android Open Source Project146de362009-03-03 19:32:18 -0800162 @Override
163 protected void onCreate(Bundle icicle) {
164 super.onCreate(icicle);
Michael Chan5c7290c2009-07-16 17:41:59 -0700165
The Android Open Source Project146de362009-03-03 19:32:18 -0800166 setContentView(R.layout.alert_activity);
Sara Tingbb577102012-05-11 15:55:48 -0700167 setTitle(R.string.alert_title);
Michael Chan5c7290c2009-07-16 17:41:59 -0700168
Michael Chanbed02752010-04-27 10:56:53 -0700169 mQueryHandler = new QueryHandler(this);
The Android Open Source Project146de362009-03-03 19:32:18 -0800170 mAdapter = new AlertAdapter(this, R.layout.alert_item);
Michael Chan5c7290c2009-07-16 17:41:59 -0700171
The Android Open Source Project146de362009-03-03 19:32:18 -0800172 mListView = (ListView) findViewById(R.id.alert_container);
173 mListView.setItemsCanFocus(true);
174 mListView.setAdapter(mAdapter);
175 mListView.setOnItemClickListener(mViewListener);
Michael Chan5c7290c2009-07-16 17:41:59 -0700176
The Android Open Source Project146de362009-03-03 19:32:18 -0800177 mDismissAllButton = (Button) findViewById(R.id.dismiss_all);
Daisuke Miyakawa1a7fa282010-10-15 17:37:37 -0700178 mDismissAllButton.setOnClickListener(this);
The Android Open Source Project146de362009-03-03 19:32:18 -0800179
180 // Disable the buttons, since they need mCursor, which is created asynchronously
The Android Open Source Project146de362009-03-03 19:32:18 -0800181 mDismissAllButton.setEnabled(false);
182 }
Michael Chan5c7290c2009-07-16 17:41:59 -0700183
The Android Open Source Project146de362009-03-03 19:32:18 -0800184 @Override
185 protected void onResume() {
186 super.onResume();
Michael Chan5c7290c2009-07-16 17:41:59 -0700187
The Android Open Source Project146de362009-03-03 19:32:18 -0800188 // If the cursor is null, start the async handler. If it is not null just requery.
189 if (mCursor == null) {
190 Uri uri = CalendarAlerts.CONTENT_URI_BY_INSTANCE;
Sara Tingbb577102012-05-11 15:55:48 -0700191 mQueryHandler.startQuery(0, null, uri, PROJECTION, SELECTION, SELECTIONARG,
Michael Chan8f7f1ab2012-05-11 07:03:26 -0700192 CalendarContract.CalendarAlerts.DEFAULT_SORT_ORDER);
The Android Open Source Project146de362009-03-03 19:32:18 -0800193 } else {
Daisuke Miyakawa1a7fa282010-10-15 17:37:37 -0700194 if (!mCursor.requery()) {
195 Log.w(TAG, "Cursor#requery() failed.");
196 mCursor.close();
197 mCursor = null;
198 }
The Android Open Source Project146de362009-03-03 19:32:18 -0800199 }
200 }
Michael Chan5c7290c2009-07-16 17:41:59 -0700201
Sara Tingc9129802012-07-26 13:59:25 -0700202 void closeActivityIfEmpty() {
Sara Ting50237502012-07-31 16:18:55 -0700203 if (mCursor != null && !mCursor.isClosed() && mCursor.getCount() == 0) {
Sara Tingc9129802012-07-26 13:59:25 -0700204 AlertActivity.this.finish();
205 }
206 }
207
The Android Open Source Project146de362009-03-03 19:32:18 -0800208 @Override
209 protected void onStop() {
210 super.onStop();
Sara Tingbb577102012-05-11 15:55:48 -0700211 AlertService.updateAlertNotification(this);
Michael Chan5c7290c2009-07-16 17:41:59 -0700212
The Android Open Source Project146de362009-03-03 19:32:18 -0800213 if (mCursor != null) {
214 mCursor.deactivate();
215 }
216 }
Michael Chan5c7290c2009-07-16 17:41:59 -0700217
The Android Open Source Project146de362009-03-03 19:32:18 -0800218 @Override
219 protected void onDestroy() {
220 super.onDestroy();
221 if (mCursor != null) {
222 mCursor.close();
223 }
224 }
Michael Chan5c7290c2009-07-16 17:41:59 -0700225
Daisuke Miyakawa1a7fa282010-10-15 17:37:37 -0700226 @Override
227 public void onClick(View v) {
Sara Ting42ba5ef2012-05-04 10:28:56 -0700228 if (v == mDismissAllButton) {
The Android Open Source Project146de362009-03-03 19:32:18 -0800229 NotificationManager nm =
230 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Sara Tingff230722012-05-30 12:31:27 -0700231 nm.cancelAll();
Michael Chane2ae1ef2009-11-18 18:37:09 -0800232
Sara Tingbb577102012-05-11 15:55:48 -0700233 dismissFiredAlarms();
Michael Chan5c7290c2009-07-16 17:41:59 -0700234
The Android Open Source Project146de362009-03-03 19:32:18 -0800235 finish();
236 }
Daisuke Miyakawa1a7fa282010-10-15 17:37:37 -0700237 }
Michael Chan5c7290c2009-07-16 17:41:59 -0700238
The Android Open Source Project146de362009-03-03 19:32:18 -0800239 public boolean isEmpty() {
Daisuke Miyakawa1a7fa282010-10-15 17:37:37 -0700240 return mCursor != null ? (mCursor.getCount() == 0) : true;
The Android Open Source Project146de362009-03-03 19:32:18 -0800241 }
Michael Chan5c7290c2009-07-16 17:41:59 -0700242
The Android Open Source Project146de362009-03-03 19:32:18 -0800243 public Cursor getItemForView(View view) {
Daisuke Miyakawa1a7fa282010-10-15 17:37:37 -0700244 final int index = mListView.getPositionForView(view);
The Android Open Source Project146de362009-03-03 19:32:18 -0800245 if (index < 0) {
246 return null;
247 }
248 return (Cursor) mListView.getAdapter().getItem(index);
249 }
250}