1 /* 2 * Copyright (C) 2010 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 17 package com.android.deskclock; 18 19 import static android.provider.AlarmClock.ACTION_SET_ALARM; 20 import static android.provider.AlarmClock.EXTRA_HOUR; 21 import static android.provider.AlarmClock.EXTRA_MESSAGE; 22 import static android.provider.AlarmClock.EXTRA_MINUTES; 23 import static android.provider.AlarmClock.EXTRA_SKIP_UI; 24 25 import android.app.Activity; 26 import android.content.ContentResolver; 27 import android.content.ContentValues; 28 import android.content.Intent; 29 import android.database.Cursor; 30 import android.net.Uri; 31 import android.os.Bundle; 32 33 import java.util.Calendar; 34 35 public class HandleSetAlarm extends Activity { 36 37 @Override 38 protected void onCreate(Bundle icicle) { 39 super.onCreate(icicle); 40 Intent intent = getIntent(); 41 if (intent == null || !ACTION_SET_ALARM.equals(intent.getAction())) { 42 finish(); 43 return; 44 } else if (!intent.hasExtra(EXTRA_HOUR)) { 45 startActivity(new Intent(this, AlarmClock.class)); 46 finish(); 47 return; 48 } 49 50 final Calendar calendar = Calendar.getInstance(); 51 calendar.setTimeInMillis(System.currentTimeMillis()); 52 final int hour = intent.getIntExtra(EXTRA_HOUR, 53 calendar.get(Calendar.HOUR_OF_DAY)); 54 final int minutes = intent.getIntExtra(EXTRA_MINUTES, 55 calendar.get(Calendar.MINUTE)); 56 final boolean skipUi = intent.getBooleanExtra(EXTRA_SKIP_UI, false); 57 String message = intent.getStringExtra(EXTRA_MESSAGE); 58 if (message == null) { 59 message = ""; 60 } 61 62 Cursor c = null; 63 long timeInMillis = Alarms.calculateAlarm(hour, minutes, 64 new Alarm.DaysOfWeek(0)).getTimeInMillis(); 65 try { 66 c = getContentResolver().query( 67 Alarm.Columns.CONTENT_URI, 68 Alarm.Columns.ALARM_QUERY_COLUMNS, 69 Alarm.Columns.HOUR + "=" + hour + " AND " + 70 Alarm.Columns.MINUTES + "=" + minutes + " AND " + 71 Alarm.Columns.DAYS_OF_WEEK + "=0 AND " + 72 Alarm.Columns.MESSAGE + "=?", 73 new String[] { message }, null); 74 if (handleCursorResult(c, timeInMillis, true, skipUi)) { 75 finish(); 76 return; 77 } 78 } finally { 79 if (c != null) c.close(); 80 // Reset for use below. 81 c = null; 82 } 83 84 ContentValues values = new ContentValues(); 85 values.put(Alarm.Columns.HOUR, hour); 86 values.put(Alarm.Columns.MINUTES, minutes); 87 values.put(Alarm.Columns.MESSAGE, message); 88 values.put(Alarm.Columns.ENABLED, 1); 89 values.put(Alarm.Columns.VIBRATE, 1); 90 values.put(Alarm.Columns.DAYS_OF_WEEK, 0); 91 values.put(Alarm.Columns.ALARM_TIME, timeInMillis); 92 93 ContentResolver cr = getContentResolver(); 94 Uri result = cr.insert(Alarm.Columns.CONTENT_URI, values); 95 if (result != null) { 96 try { 97 c = cr.query(result, Alarm.Columns.ALARM_QUERY_COLUMNS, null, 98 null, null); 99 handleCursorResult(c, timeInMillis, false, skipUi); 100 } finally { 101 if (c != null) c.close(); 102 } 103 } 104 105 finish(); 106 } 107 108 private boolean handleCursorResult(Cursor c, long timeInMillis, 109 boolean enable, boolean skipUi) { 110 if (c != null && c.moveToFirst()) { 111 Alarm alarm = new Alarm(c); 112 if (enable) { 113 Alarms.enableAlarm(this, alarm.id, true); 114 alarm.enabled = true; 115 } 116 AlarmUtils.popAlarmSetToast(this, timeInMillis); 117 if (skipUi) { 118 Alarms.setAlarm(this, alarm); 119 } else { 120 Intent i = new Intent(this, AlarmClock.class); 121 i.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm); 122 startActivity(i); 123 } 124 return true; 125 } 126 return false; 127 } 128 } 129