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

Commit 187d05e0 authored by kaiyiz's avatar kaiyiz Committed by Brint E. Kriebel
Browse files

DeskClock: handle call events on alarm

Alarm clock ringing while incoming call received.

When call ringing,alarm clock time arrived.Only Call ringing and no alarm
clock ringing.and after hang up the phone,alarm start ring.
When alarm ringing,receive a call,the alarm is stopped,and it can answer
or hang up the phone.after hang up the phone ,there has some reminder.

Change-Id: Id1bdf689f709e78ced30cec06cbc93a7a06a5b3b
CRs-Fixed: 638868
parent 6f6d51f6
Loading
Loading
Loading
Loading
+87 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@
package com.android.deskclock.alarms;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
@@ -26,6 +28,7 @@ import android.net.Uri;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.widget.Toast;
import android.telephony.TelephonyManager;

import com.android.deskclock.AlarmAlertWakeLock;
import com.android.deskclock.AlarmClockFragment;
@@ -423,6 +426,31 @@ public final class AlarmStateManager extends BroadcastReceiver {

    }

    /**
     * This will set the alarm instance to the one minute late and update
     * the application notifications and schedule any state changes that need
     * to occur in the future.
     *
     * @param context application context
     * @param instance to set state to
     */
    public static void setOneminutelate(Context context, AlarmInstance instance) {
        AlarmService.stopAlarm(context, instance);
        int snoozeMinutes = 1;

        // Set alarm time to next minute.Update alarm state and new alarm time in db.
        Calendar newAlarmTime = Calendar.getInstance();
        newAlarmTime.add(Calendar.MINUTE, snoozeMinutes);
        instance.setAlarmTime(newAlarmTime);
        instance.mAlarmState = AlarmInstance.SNOOZE_STATE;
        AlarmInstance.updateInstance(context.getContentResolver(), instance);
        scheduleInstanceStateChange(context, instance.getAlarmTime(),
                instance, AlarmInstance.FIRED_STATE);

        // Instance time changed, so find next alarm that will fire and notify system
        updateNextAlarm(context);
    }

    /**
     * This will set the alarm instance to the MISSED_STATE and update
     * the application notifications and schedule any state changes that need
@@ -712,6 +740,19 @@ public final class AlarmStateManager extends BroadcastReceiver {
                return;
            }

            // If the phone is busy, keep the alarm snoozing.When the call is ended,
            // the new coming alarm or the alarm which wakes from sooze,will skip the codes here
            // and continue show the alarm as normal.
            if (context.getResources().getBoolean(R.bool.config_delayalarm)) {
                TelephonyManager mTelephonyManager = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);
                if ((mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE)
                        && (alarmState == AlarmInstance.FIRED_STATE)) {
                    snooze(context, intent, instance);
                    return;
                }
            }

            if (alarmState >= 0) {
                setAlarmState(context, instance, alarmState);
            } else {
@@ -738,4 +779,50 @@ public final class AlarmStateManager extends BroadcastReceiver {
            setDismissState(context, instance);
        }
    }

    /**
     * Make the alarm snooze for one second.If the phone is still busy in call,
     * this method will be called again to snooze for another one second.
     * If the phone is not busy in call anymore, this method will not be
     * called, and the alarm will wake up within one second.
     */
    private void snooze(Context context, Intent intent, AlarmInstance instance) {
        Calendar currentTime = Calendar.getInstance();
        if ((currentTime.get(Calendar.SECOND) < 57)
                && (currentTime.get(Calendar.MINUTE) == instance.mMinute)) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {}
            Uri uri = intent.getData();
            AlarmInstance newInstance = AlarmInstance.getInstance(context.getContentResolver(),
                    AlarmInstance.getId(uri));
            if (newInstance == null) {
                // If AlarmInstance is turn to null,return.
                return;
            }

            // Notify the user that the alarm has been snoozed.
            Intent cancelSnooze = createStateChangeIntent(context, ALARM_MANAGER_TAG, newInstance,
                    AlarmInstance.DISMISSED_STATE);
            PendingIntent broadcast = PendingIntent.getBroadcast(context, instance.hashCode(),
                    cancelSnooze, 0);
            String label = newInstance.getLabelOrDefault(context);
            label = context.getString(R.string.alarm_notify_snooze_label, label);
            NotificationManager nm = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Notification n = new Notification(R.drawable.stat_notify_alarm, label, 0);
            n.setLatestEventInfo(context, label,
                    context.getString(R.string.alarm_notify_snooze_text,
                            AlarmUtils.getFormattedTime(context, instance.getAlarmTime())),
                    broadcast);
            n.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
            nm.notify(instance.hashCode(), n);

            scheduleInstanceStateChange(context, newInstance.getAlarmTime(), newInstance,
                    AlarmInstance.FIRED_STATE);
        } else {
            // when alarm is about to overtime,let it snooze to next minute
            setOneminutelate(context, instance);
        }
    }
}