Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6c528aa0 authored by Patrick Scott's avatar Patrick Scott
Browse files

Handle the new set alarm intent.

Enable a few options by default and check for duplicates.

Change-Id: Ib7d1537b1fce398f2d52114da245658f40a59954
parent da9528b9
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -15,7 +15,9 @@
    <application android:label="@string/app_label"
                 android:icon="@drawable/ic_launcher_alarmclock">

        <provider android:name="AlarmProvider" android:authorities="com.android.deskclock" />
        <provider android:name="AlarmProvider"
                android:authorities="com.android.deskclock"
                android:exported="false" />

        <activity android:name="DeskClock"
                android:label="@string/app_label"
@@ -85,6 +87,16 @@
            </intent-filter>
        </receiver>

        <activity android:name="HandleSetAlarm"
                android:theme="@android:style/Theme.NoDisplay"
                android:excludeFromRecents="true"
                android:permission="com.android.alarm.permission.SET_ALARM">
            <intent-filter>
                <action android:name="android.intent.action.SET_ALARM" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <!-- This service receives the same intent as AlarmReceiver but it does
             not respond to the same broadcast. The AlarmReceiver will receive
             the alert broadcast and will start this service with the same
+91 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.deskclock;

import android.app.Activity;
import android.content.Context;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import static android.provider.AlarmClock.ACTION_SET_ALARM;
import static android.provider.AlarmClock.EXTRA_HOUR;
import static android.provider.AlarmClock.EXTRA_MESSAGE;
import static android.provider.AlarmClock.EXTRA_MINUTES;

import java.util.Calendar;

public class HandleSetAlarm extends Activity {

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Intent intent = getIntent();
        if (intent == null || !ACTION_SET_ALARM.equals(intent.getAction())) {
            finish();
            return;
        }

        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        final int hour = intent.getIntExtra(EXTRA_HOUR,
                calendar.get(Calendar.HOUR_OF_DAY));
        final int minutes = intent.getIntExtra(EXTRA_MINUTES,
                calendar.get(Calendar.MINUTE));
        String message = intent.getStringExtra(EXTRA_MESSAGE);
        if (message == null) {
            message = "";
        }

        Cursor c = null;
        try {
            c = getContentResolver().query(
                    Alarm.Columns.CONTENT_URI,
                    new String[] { Alarm.Columns._ID },
                    Alarm.Columns.HOUR + "=" + hour + " AND " +
                    Alarm.Columns.MINUTES + "=" + minutes + " AND " +
                    Alarm.Columns.MESSAGE + "=?",
                    new String[] { message }, null);
            if (c != null && c.moveToFirst()) {
                do {
                    Alarms.enableAlarm(this, c.getInt(0), true);
                } while (c.moveToNext());
                finish();
                return;
            }
        } finally {
            if (c != null) c.close();
        }

        ContentValues values = new ContentValues();
        values.put(Alarm.Columns.HOUR, hour);
        values.put(Alarm.Columns.MINUTES, minutes);
        values.put(Alarm.Columns.MESSAGE, message);
        values.put(Alarm.Columns.ENABLED, 1);
        values.put(Alarm.Columns.VIBRATE, 1);
        values.put(Alarm.Columns.ALARM_TIME,
                Alarms.calculateAlarm(hour, minutes,
                        new Alarm.DaysOfWeek(0)).getTimeInMillis());

        if (getContentResolver().insert(
                Alarm.Columns.CONTENT_URI, values) != null) {
            Alarms.setNextAlert(this);
        }

        finish();
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -17,8 +17,17 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.deskclock.tests">

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <application>
        <uses-library android:name="android.test.runner" />

        <activity android:name="TestAddAlarm" android:label="Test Adding Alarm">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <instrumentation android:name="com.android.deskclock.DeskClockLaunchPerformance"
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.deskclock.tests;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.AlarmClock;

public class TestAddAlarm extends Activity {

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
        i.putExtra(AlarmClock.EXTRA_MESSAGE, "New Alarm!");
        i.putExtra(AlarmClock.EXTRA_HOUR, 12);
        i.putExtra(AlarmClock.EXTRA_MINUTES, 27);

        startActivity(i);

        // Should not see a duplicate
        startActivity(i);

        // No message, alarm set for now.
        startActivity(new Intent(AlarmClock.ACTION_SET_ALARM));

        i.removeExtra(AlarmClock.EXTRA_MESSAGE);
        startActivity(i);
        // No dup of null message.
        startActivity(i);

        // Enable default 8:30 alarm.
        i.putExtra(AlarmClock.EXTRA_HOUR, 8);
        i.putExtra(AlarmClock.EXTRA_MINUTES, 30);
        startActivity(i);

        finish();
    }
}