HandleApiCalls.java revision 04d0eaaf489d08c56104d9b71d0d2edecb4e27a5
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
17package com.android.deskclock;
18
19import static android.provider.AlarmClock.ACTION_SET_ALARM;
20import static android.provider.AlarmClock.ACTION_SET_TIMER;
21import static android.provider.AlarmClock.EXTRA_DELETE_AFTER_USE;
22import static android.provider.AlarmClock.EXTRA_HOUR;
23import static android.provider.AlarmClock.EXTRA_LENGTH;
24import static android.provider.AlarmClock.EXTRA_MESSAGE;
25import static android.provider.AlarmClock.EXTRA_MINUTES;
26import static android.provider.AlarmClock.EXTRA_SKIP_UI;
27
28import android.app.Activity;
29import android.content.ContentResolver;
30import android.content.Intent;
31import android.content.SharedPreferences;
32import android.database.Cursor;
33import android.net.Uri;
34import android.os.Bundle;
35import android.preference.PreferenceManager;
36import android.text.TextUtils;
37
38import com.android.deskclock.provider.Alarm;
39import com.android.deskclock.provider.DaysOfWeek;
40import com.android.deskclock.timer.TimerFragment;
41import com.android.deskclock.timer.TimerObj;
42import com.android.deskclock.timer.Timers;
43
44import java.util.ArrayList;
45import java.util.Calendar;
46import java.util.List;
47
48public class HandleApiCalls extends Activity {
49
50    public static final long TIMER_MIN_LENGTH = 1000;
51    public static final long TIMER_MAX_LENGTH = 24 * 60 * 60 * 1000;
52
53    @Override
54    protected void onCreate(Bundle icicle) {
55        try {
56            super.onCreate(icicle);
57            Intent intent = getIntent();
58            if (intent != null) {
59                if (ACTION_SET_ALARM.equals(intent.getAction())) {
60                    handleSetAlarm(intent);
61                } else if (ACTION_SET_TIMER.equals(intent.getAction())) {
62                    handleSetTimer(intent);
63                }
64            }
65        } finally {
66            finish();
67        }
68    }
69
70    /***
71     * Processes the SET_ALARM intent
72     * @param intent
73     */
74    private void handleSetAlarm(Intent intent) {
75
76        // Intent has no time , open the alarm creation UI
77        if (!intent.hasExtra(EXTRA_HOUR)) {
78            Intent createAlarm = new Intent(this, DeskClock.class);
79            createAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
80            createAlarm.putExtra(Alarms.ALARM_CREATE_NEW, true);
81            createAlarm.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.ALARM_TAB_INDEX);
82
83            startActivity(createAlarm);
84            finish();
85            return;
86        }
87
88        final Calendar calendar = Calendar.getInstance();
89        calendar.setTimeInMillis(System.currentTimeMillis());
90        final int hour = intent.getIntExtra(EXTRA_HOUR, calendar.get(Calendar.HOUR_OF_DAY));
91        final int minutes = intent.getIntExtra(EXTRA_MINUTES, calendar.get(Calendar.MINUTE));
92        final boolean skipUi = intent.getBooleanExtra(EXTRA_SKIP_UI, false);
93        String message = intent.getStringExtra(EXTRA_MESSAGE);
94        if (message == null) {
95            message = "";
96        }
97
98        // Check if the alarm already exists and handle it
99        ContentResolver cr = getContentResolver();
100        List<Alarm> alarms = Alarm.getAlarms(cr,
101                Alarm.HOUR + "=" + hour + " AND " +
102                Alarm.MINUTES + "=" + minutes + " AND " +
103                Alarm.DAYS_OF_WEEK + "=" + DaysOfWeek.NO_DAYS_SET + " AND " +
104                Alarm.MESSAGE + "=?",
105                new String[] { message });
106        if (!alarms.isEmpty()) {
107            enableAlarm(alarms.get(0), true, skipUi);
108            finish();
109            return;
110        }
111
112        // Otherwise insert it and handle it
113        Alarm alarm = new Alarm(hour, minutes);
114        alarm.enabled = true;
115        alarm.label = message;
116
117        Uri result = cr.insert(Alarm.CONTENT_URI, Alarm.createContentValues(alarm));
118        enableAlarm(Alarm.getAlarm(cr, Alarm.getId(result)), false, skipUi);
119        finish();
120    }
121
122    private void handleSetTimer(Intent intent) {
123        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
124        // If no length is supplied , show the timer setup view
125        if (!intent.hasExtra(EXTRA_LENGTH)) {
126            startActivity(new Intent(this, DeskClock.class)
127                  .putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.TIMER_TAB_INDEX)
128                  .putExtra(TimerFragment.GOTO_SETUP_VIEW, true));
129            return;
130        }
131
132        final long length = 1000l * intent.getIntExtra(EXTRA_LENGTH, 0);
133        if (length < TIMER_MIN_LENGTH || length > TIMER_MAX_LENGTH) {
134            Log.i("Invalid timer length requested: " + length);
135            return;
136        }
137        String label = intent.getStringExtra(EXTRA_MESSAGE);
138        if (label == null) {
139            label = "";
140        }
141        final boolean deleteAfterUse = intent.getBooleanExtra(EXTRA_DELETE_AFTER_USE, false);
142
143        TimerObj timer = null;
144        // Do not delete existing timers by reusing them and deleting them after use
145        if (!deleteAfterUse) {
146            // Find an existing matching timer
147            final ArrayList<TimerObj> timers = new ArrayList<TimerObj>();
148            TimerObj.getTimersFromSharedPrefs(prefs, timers);
149            for (TimerObj t : timers) {
150                if (t.mSetupLength == length && (TextUtils.equals(label, t.mLabel))
151                        && t.mState == TimerObj.STATE_RESTART) {
152                    timer = t;
153                    break;
154                }
155            }
156        }
157
158        if (timer == null) {
159            // Use a new timer
160            timer = new TimerObj(length, label);
161        }
162
163        timer.mState = TimerObj.STATE_RUNNING;
164        timer.mStartTime = Utils.getTimeNow();
165        timer.mDeleteAfterUse = deleteAfterUse;
166        timer.writeToSharedPref(prefs);
167
168        // Tell TimerReceiver that the timer was started
169        sendBroadcast(new Intent().setAction(Timers.START_TIMER)
170                .putExtra(Timers.TIMER_INTENT_EXTRA, timer.mTimerId));
171
172        if (intent.getBooleanExtra(EXTRA_SKIP_UI, false)) {
173            Utils.showInUseNotifications(this);
174        } else {
175            startActivity(new Intent(this, DeskClock.class)
176                    .putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.TIMER_TAB_INDEX));
177        }
178    }
179
180    private void enableAlarm(Alarm alarm, boolean enable, boolean skipUi) {
181        if (enable) {
182            Alarms.enableAlarm(this, (int)alarm.id, true);
183            alarm.enabled = true;
184        }
185        Alarms.setAlarm(this, alarm);
186        AlarmUtils.popAlarmSetToast(this, alarm.calculateAlarmTime());
187        if (!skipUi) {
188            Intent createdAlarm = new Intent(this, DeskClock.class);
189            createdAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
190            createdAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
191            createdAlarm.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.ALARM_TAB_INDEX);
192            createdAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
193            startActivity(createdAlarm);
194        }
195    }
196}
197