AlertActivity.java revision 1881a896a82c425845e3ef2a1a716d329f0e45f1
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 com.android.calendar.AsyncQueryService; 20import com.android.calendar.R; 21import com.android.calendar.Utils; 22 23import android.app.Activity; 24import android.app.AlarmManager; 25import android.app.NotificationManager; 26import android.content.ContentValues; 27import android.content.Context; 28import android.content.Intent; 29import android.database.Cursor; 30import android.net.Uri; 31import android.net.Uri.Builder; 32import android.os.Bundle; 33import android.provider.Calendar; 34import android.provider.Calendar.CalendarAlerts; 35import android.provider.Calendar.CalendarAlertsColumns; 36import android.util.Log; 37import android.view.View; 38import android.view.View.OnClickListener; 39import android.view.ViewGroup; 40import android.view.WindowManager; 41import android.widget.AdapterView; 42import android.widget.AdapterView.OnItemClickListener; 43import android.widget.Button; 44import android.widget.ListView; 45 46/** 47 * The alert panel that pops up when there is a calendar event alarm. 48 * This activity is started by an intent that specifies an event id. 49 */ 50public class AlertActivity extends Activity implements OnClickListener { 51 private static final String TAG = "AlertActivity"; 52 53 // The default snooze delay: 5 minutes 54 public static final long SNOOZE_DELAY = 5 * 60 * 1000L; 55 56 private static final String[] PROJECTION = new String[] { 57 CalendarAlerts._ID, // 0 58 CalendarAlerts.TITLE, // 1 59 CalendarAlerts.EVENT_LOCATION, // 2 60 CalendarAlerts.ALL_DAY, // 3 61 CalendarAlerts.BEGIN, // 4 62 CalendarAlerts.END, // 5 63 CalendarAlerts.EVENT_ID, // 6 64 CalendarAlerts.COLOR, // 7 65 CalendarAlerts.RRULE, // 8 66 CalendarAlerts.HAS_ALARM, // 9 67 CalendarAlerts.STATE, // 10 68 CalendarAlerts.ALARM_TIME, // 11 69 }; 70 71 public static final int INDEX_ROW_ID = 0; 72 public static final int INDEX_TITLE = 1; 73 public static final int INDEX_EVENT_LOCATION = 2; 74 public static final int INDEX_ALL_DAY = 3; 75 public static final int INDEX_BEGIN = 4; 76 public static final int INDEX_END = 5; 77 public static final int INDEX_EVENT_ID = 6; 78 public static final int INDEX_COLOR = 7; 79 public static final int INDEX_RRULE = 8; 80 public static final int INDEX_HAS_ALARM = 9; 81 public static final int INDEX_STATE = 10; 82 public static final int INDEX_ALARM_TIME = 11; 83 84 private static final String SELECTION = CalendarAlerts.STATE + "=?"; 85 private static final String[] SELECTIONARG = new String[] { 86 Integer.toString(CalendarAlerts.FIRED) 87 }; 88 89 // We use one notification id for all events so that we don't clutter 90 // the notification screen. It doesn't matter what the id is, as long 91 // as it is used consistently everywhere. 92 public static final int NOTIFICATION_ID = 0; 93 94 private AlertAdapter mAdapter; 95 private QueryHandler mQueryHandler; 96 private Cursor mCursor; 97 private ListView mListView; 98 private Button mSnoozeAllButton; 99 private Button mDismissAllButton; 100 101 102 private void dismissFiredAlarms() { 103 ContentValues values = new ContentValues(1 /* size */); 104 values.put(PROJECTION[INDEX_STATE], CalendarAlerts.DISMISSED); 105 String selection = CalendarAlerts.STATE + "=" + CalendarAlerts.FIRED; 106 mQueryHandler.startUpdate(0, null, CalendarAlerts.CONTENT_URI, values, 107 selection, null /* selectionArgs */, Utils.UNDO_DELAY); 108 } 109 110 private void dismissAlarm(long id) { 111 ContentValues values = new ContentValues(1 /* size */); 112 values.put(PROJECTION[INDEX_STATE], CalendarAlerts.DISMISSED); 113 String selection = CalendarAlerts._ID + "=" + id; 114 mQueryHandler.startUpdate(0, null, CalendarAlerts.CONTENT_URI, values, 115 selection, null /* selectionArgs */, Utils.UNDO_DELAY); 116 } 117 118 private class QueryHandler extends AsyncQueryService { 119 public QueryHandler(Context context) { 120 super(context); 121 } 122 123 @Override 124 protected void onQueryComplete(int token, Object cookie, Cursor cursor) { 125 // Only set mCursor if the Activity is not finishing. Otherwise close the cursor. 126 if (!isFinishing()) { 127 mCursor = cursor; 128 mAdapter.changeCursor(cursor); 129 mListView.setSelection(cursor.getCount() - 1); 130 131 // The results are in, enable the buttons 132 mSnoozeAllButton.setEnabled(true); 133 mDismissAllButton.setEnabled(true); 134 } else { 135 cursor.close(); 136 } 137 } 138 139 @Override 140 protected void onInsertComplete(int token, Object cookie, Uri uri) { 141 if (uri != null) { 142 Long alarmTime = (Long) cookie; 143 144 if (alarmTime != 0) { 145 // Set a new alarm to go off after the snooze delay. 146 AlarmManager alarmManager = 147 (AlarmManager) getSystemService(Context.ALARM_SERVICE); 148 CalendarAlerts.scheduleAlarm(AlertActivity.this, alarmManager, alarmTime); 149 } 150 } 151 } 152 153 @Override 154 protected void onUpdateComplete(int token, Object cookie, int result) { 155 // Ignore 156 } 157 } 158 159 private static ContentValues makeContentValues(long eventId, long begin, long end, 160 long alarmTime, int minutes) { 161 ContentValues values = new ContentValues(); 162 values.put(CalendarAlerts.EVENT_ID, eventId); 163 values.put(CalendarAlerts.BEGIN, begin); 164 values.put(CalendarAlerts.END, end); 165 values.put(CalendarAlerts.ALARM_TIME, alarmTime); 166 long currentTime = System.currentTimeMillis(); 167 values.put(CalendarAlerts.CREATION_TIME, currentTime); 168 values.put(CalendarAlerts.RECEIVED_TIME, 0); 169 values.put(CalendarAlerts.NOTIFY_TIME, 0); 170 values.put(CalendarAlerts.STATE, CalendarAlertsColumns.SCHEDULED); 171 values.put(CalendarAlerts.MINUTES, minutes); 172 return values; 173 } 174 175 private OnItemClickListener mViewListener = new OnItemClickListener() { 176 177 public void onItemClick(AdapterView<?> parent, View view, int position, 178 long i) { 179 AlertActivity alertActivity = AlertActivity.this; 180 Cursor cursor = alertActivity.getItemForView(view); 181 182 // Mark this alarm as DISMISSED 183 dismissAlarm(cursor.getLong(INDEX_ROW_ID)); 184 185 long id = cursor.getInt(AlertActivity.INDEX_EVENT_ID); 186 long startMillis = cursor.getLong(AlertActivity.INDEX_BEGIN); 187 long endMillis = cursor.getLong(AlertActivity.INDEX_END); 188 Intent eventIntent = new Intent(Intent.ACTION_VIEW); 189 Builder builder = Calendar.CONTENT_URI.buildUpon(); 190 builder.appendEncodedPath("time/" + startMillis); 191 eventIntent.setData(builder.build()); 192 alertActivity.startActivity(eventIntent); 193 194 alertActivity.finish(); 195 } 196 }; 197 198 @Override 199 protected void onCreate(Bundle icicle) { 200 super.onCreate(icicle); 201 202 setContentView(R.layout.alert_activity); 203 setTitle(R.string.alert_title); 204 205 mQueryHandler = new QueryHandler(this); 206 mAdapter = new AlertAdapter(this, R.layout.alert_item); 207 208 mListView = (ListView) findViewById(R.id.alert_container); 209 mListView.setItemsCanFocus(true); 210 mListView.setAdapter(mAdapter); 211 mListView.setOnItemClickListener(mViewListener); 212 213 mSnoozeAllButton = (Button) findViewById(R.id.snooze_all); 214 mSnoozeAllButton.setOnClickListener(this); 215 mDismissAllButton = (Button) findViewById(R.id.dismiss_all); 216 mDismissAllButton.setOnClickListener(this); 217 218 // Disable the buttons, since they need mCursor, which is created asynchronously 219 mSnoozeAllButton.setEnabled(false); 220 mDismissAllButton.setEnabled(false); 221 } 222 223 @Override 224 protected void onResume() { 225 super.onResume(); 226 227 // If the cursor is null, start the async handler. If it is not null just requery. 228 if (mCursor == null) { 229 Uri uri = CalendarAlerts.CONTENT_URI_BY_INSTANCE; 230 mQueryHandler.startQuery(0, null, uri, PROJECTION, SELECTION, 231 SELECTIONARG, CalendarAlerts.DEFAULT_SORT_ORDER); 232 } else { 233 if (!mCursor.requery()) { 234 Log.w(TAG, "Cursor#requery() failed."); 235 mCursor.close(); 236 mCursor = null; 237 } 238 } 239 } 240 241 @Override 242 protected void onStop() { 243 super.onStop(); 244 AlertService.updateAlertNotification(this); 245 246 if (mCursor != null) { 247 mCursor.deactivate(); 248 } 249 } 250 251 @Override 252 protected void onDestroy() { 253 super.onDestroy(); 254 if (mCursor != null) { 255 mCursor.close(); 256 } 257 } 258 259 @Override 260 public void onClick(View v) { 261 if (v == mSnoozeAllButton) { 262 long alarmTime = System.currentTimeMillis() + SNOOZE_DELAY; 263 264 NotificationManager nm = 265 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 266 nm.cancel(NOTIFICATION_ID); 267 268 if (mCursor != null) { 269 long scheduleAlarmTime = 0; 270 mCursor.moveToPosition(-1); 271 while (mCursor.moveToNext()) { 272 long eventId = mCursor.getLong(INDEX_EVENT_ID); 273 long begin = mCursor.getLong(INDEX_BEGIN); 274 long end = mCursor.getLong(INDEX_END); 275 276 // Set the "minutes" to zero to indicate this is a snoozed 277 // alarm. There is code in AlertService.java that checks 278 // this field. 279 ContentValues values = 280 makeContentValues(eventId, begin, end, alarmTime, 0 /* minutes */); 281 282 // Create a new alarm entry in the CalendarAlerts table 283 if (mCursor.isLast()) { 284 scheduleAlarmTime = alarmTime; 285 } 286 mQueryHandler.startInsert(0, 287 scheduleAlarmTime, CalendarAlerts.CONTENT_URI, values, 288 Utils.UNDO_DELAY); 289 } 290 } else { 291 Log.d(TAG, "Cursor object is null. Ignore the Snooze request."); 292 } 293 294 dismissFiredAlarms(); 295 finish(); 296 } else if (v == mDismissAllButton) { 297 NotificationManager nm = 298 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 299 nm.cancel(NOTIFICATION_ID); 300 301 dismissFiredAlarms(); 302 303 finish(); 304 } 305 } 306 307 public boolean isEmpty() { 308 return mCursor != null ? (mCursor.getCount() == 0) : true; 309 } 310 311 public Cursor getItemForView(View view) { 312 final int index = mListView.getPositionForView(view); 313 if (index < 0) { 314 return null; 315 } 316 return (Cursor) mListView.getAdapter().getItem(index); 317 } 318} 319