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

Commit d0cca79f authored by Christopher Tate's avatar Christopher Tate
Browse files

Don't worry about an extra weak indirection with callback alarms

Bug 27995384

Change-Id: Ie9cf3e05dca4af7085b479fb65d4cf45ee0239ba
parent 665189f9
Loading
Loading
Loading
Loading
+7 −19
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ import android.util.Log;
import libcore.util.ZoneInfoDB;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

/**
@@ -245,12 +244,7 @@ public class AlarmManager {

    // Tracking of the OnAlarmListener -> wrapper mapping, for cancel() support.
    // Access is synchronized on the AlarmManager class object.
    //
    // These are weak references so that we don't leak listener references if, for
    // example, the pending-alarm messages are posted to a HandlerThread that is
    // disposed of prior to alarm delivery.  The underlying messages will be GC'd
    // but this static reference would still persist, orphaned, never deallocated.
    private static WeakHashMap<OnAlarmListener, WeakReference<ListenerWrapper>> sWrappers;
    private static WeakHashMap<OnAlarmListener, ListenerWrapper> sWrappers;

    /**
     * package private on purpose
@@ -637,16 +631,14 @@ public class AlarmManager {
        if (listener != null) {
            synchronized (AlarmManager.class) {
                if (sWrappers == null) {
                    sWrappers = new WeakHashMap<OnAlarmListener, WeakReference<ListenerWrapper>>();
                    sWrappers = new WeakHashMap<OnAlarmListener, ListenerWrapper>();
                }

                WeakReference<ListenerWrapper> wrapperRef = sWrappers.get(listener);
                // no existing wrapper *or* we've lost our weak ref to it => build a new one
                if (wrapperRef == null ||
                        (recipientWrapper = wrapperRef.get()) == null) {
                recipientWrapper = sWrappers.get(listener);
                // no existing wrapper => build a new one
                if (recipientWrapper == null) {
                    recipientWrapper = new ListenerWrapper(listener);
                    wrapperRef = new WeakReference<ListenerWrapper>(recipientWrapper);
                    sWrappers.put(listener, wrapperRef);
                    sWrappers.put(listener, recipientWrapper);
                }
            }

@@ -906,11 +898,7 @@ public class AlarmManager {
        ListenerWrapper wrapper = null;
        synchronized (AlarmManager.class) {
            if (sWrappers != null) {
                final WeakReference<ListenerWrapper> wrapperRef;
                wrapperRef = sWrappers.get(listener);
                if (wrapperRef != null) {
                    wrapper = wrapperRef.get();
                }
                wrapper = sWrappers.get(listener);
            }
        }