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

Commit 55fd70af authored by Raj Yengisetty's avatar Raj Yengisetty Committed by Rajesh Yengisetty
Browse files

DeskClock: fix behavior with alarms when device is off

The current behavior causes an NPE and prevents the alarm
from firing. Determine if this a boot alarm and if the
alarm needs to be fired immediately. If not, proceed with
normal alarm handling.

Also, adding some checks to prevent NPEs and FCs when
handling an intent without data() and querying for an
AlarmInstance with an invalid id.

Change-Id: I0d5ee998c43207905875a0272fe70596a3877587
parent d3c7e722
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
@@ -67,6 +68,8 @@ public class AlarmActivity extends Activity implements View.OnClickListener, Vie

    private static final String LOGTAG = AlarmActivity.class.getSimpleName();

    private static final String PROP_BOOT_MODE = "ro.alarm_boot";

    private static final Interpolator PULSE_INTERPOLATOR =
            new PathInterpolator(0.4f, 0.0f, 0.2f, 1.0f);
    private static final Interpolator REVEAL_INTERPOLATOR =
@@ -137,8 +140,21 @@ public class AlarmActivity extends Activity implements View.OnClickListener, Vie
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final long instanceId = AlarmInstance.getId(getIntent().getData());
        mAlarmInstance = AlarmInstance.getInstance(getContentResolver(), instanceId);
        Uri intentData = getIntent().getData();

        boolean isAlarmBoot = getIntent().getBooleanExtra(PROP_BOOT_MODE, false);

        LogUtils.i(LOGTAG, "AlarmBoot start alarm activity, "
                + "alarm boot = " + isAlarmBoot + " | getIntent() " + getIntent());

        if (isAlarmBoot) {
            AlarmStateManager.setRtcPowerUp(this, isAlarmBoot);
            mAlarmInstance = AlarmInstance.getClosestFiredAlarmInstance(getContentResolver());
        } else if (intentData != null) {
            long instanceId = AlarmInstance.getId(intentData);
            mAlarmInstance = AlarmInstance.getInstance(this.getContentResolver(), instanceId);
        }

        if (mAlarmInstance == null) {
            // The alarm got deleted before the activity got created, so just finish()
            LogUtils.e(LOGTAG, "Error displaying alarm for intent: %s", getIntent());
+33 −1
Original line number Diff line number Diff line
@@ -139,7 +139,11 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
    }

    public static long getId(Uri contentUri) {
        if (contentUri != null) {
            return ContentUris.parseId(contentUri);
        } else {
            return INVALID_ID;
        }
    }

    public static Uri getUri(long instanceId) {
@@ -154,6 +158,10 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
     * @return instance if found, null otherwise
     */
    public static AlarmInstance getInstance(ContentResolver contentResolver, long instanceId) {
        if (instanceId == INVALID_ID) {
            return null;
        }

        Cursor cursor = contentResolver.query(getUri(instanceId), QUERY_COLUMNS, null, null, null);
        AlarmInstance result = null;
        if (cursor == null) {
@@ -253,6 +261,30 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
        return deletedRows == 1;
    }

    public static AlarmInstance getClosestFiredAlarmInstance(ContentResolver contentResolver) {
        List<AlarmInstance> alertAlarms = getInstances(contentResolver, null);
        long currentTime = System.currentTimeMillis();

        AlarmInstance firstAlarm = null;
        long closestMissAlarmElapse = currentTime;

        for (AlarmInstance ai : alertAlarms) {
            if (ai.mAlarmState != FIRED_STATE) {
                continue;
            }

            long time = Math.abs(currentTime - ai.getAlarmTime().getTimeInMillis());

            if (closestMissAlarmElapse > time) {
                firstAlarm = ai;
                closestMissAlarmElapse = time;
            }
        }

        return firstAlarm;
    }


    // Public fields
    public long mId;
    public int mYear;