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

Commit 0b61ebde authored by Lucas Dupin's avatar Lucas Dupin
Browse files

Fix broadcast receiver race condition

Race would happen because receiver might not have been registered
before leaving doze. This is fixed by posting everything to the same
background Handler.

Test: manual
Change-Id: I7783651af79629dc4a817ff2e023cbdeb990320b
Fixes: 119506300
Fixes: 119436283
parent 9f557739
Loading
Loading
Loading
Loading
+4 −8
Original line number Original line Diff line number Diff line
@@ -33,8 +33,6 @@ import android.provider.Settings;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.Dependency;
import com.android.systemui.Dependency;


import java.util.concurrent.atomic.AtomicBoolean;

/**
/**
 * Controls the screen brightness when dozing.
 * Controls the screen brightness when dozing.
 */
 */
@@ -66,7 +64,6 @@ public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachi
     * --ei brightness_bucket 1}
     * --ei brightness_bucket 1}
     */
     */
    private int mDebugBrightnessBucket = -1;
    private int mDebugBrightnessBucket = -1;
    private AtomicBoolean mIsDestroyed = new AtomicBoolean();


    @VisibleForTesting
    @VisibleForTesting
    public DozeScreenBrightness(Context context, DozeMachine.Service service,
    public DozeScreenBrightness(Context context, DozeMachine.Service service,
@@ -89,9 +86,7 @@ public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachi
            Dependency.get(Dependency.BG_HANDLER).post(()-> {
            Dependency.get(Dependency.BG_HANDLER).post(()-> {
                IntentFilter filter = new IntentFilter();
                IntentFilter filter = new IntentFilter();
                filter.addAction(ACTION_AOD_BRIGHTNESS);
                filter.addAction(ACTION_AOD_BRIGHTNESS);
                if (!mIsDestroyed.get()) {
                mContext.registerReceiverAsUser(this, UserHandle.ALL, filter, null, handler);
                mContext.registerReceiverAsUser(this, UserHandle.ALL, filter, null, handler);
                }
            });
            });
        }
        }
    }
    }
@@ -129,10 +124,11 @@ public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachi
    }
    }


    private void onDestroy() {
    private void onDestroy() {
        mIsDestroyed.set(true);
        setLightSensorEnabled(false);
        setLightSensorEnabled(false);
        if (mDebuggable) {
        if (mDebuggable) {
            Dependency.get(Dependency.BG_HANDLER).post(()-> {
                mContext.unregisterReceiver(this);
                mContext.unregisterReceiver(this);
            });
        }
        }
    }
    }