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

Commit d3f4b586 authored by Michael Kwan's avatar Michael Kwan
Browse files

Catch exception where SwipeDismissLayout cannot monitor screen off.

Monitoring screen off uses a BroadcastReceiver. However, there are
occasionally uses where SwipeDismissLayout is used in a context where
it cannot use a BroadcastReceiver, notably when used inside of one,
therefore we need to catch and handle the case where it is not applicable.

Bug: 36034260
Change-Id: I253875623a28532dbc8a2dc96fc9468221b0b781
parent edb79fe7
Loading
Loading
Loading
Loading
+28 −20
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ReceiverCallNotAllowedException;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
@@ -86,24 +87,7 @@ public class SwipeDismissLayout extends FrameLayout {

    private OnDismissedListener mDismissedListener;
    private OnSwipeProgressChangedListener mProgressListener;
    private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
        private Runnable mRunnable = new Runnable() {
            @Override
            public void run() {
                if (mDismissed) {
                    dismiss();
                } else {
                    cancel();
                }
                resetMembers();
            }
        };

        @Override
        public void onReceive(Context context, Intent intent) {
            post(mRunnable);
        }
    };
    private BroadcastReceiver mScreenOffReceiver;
    private IntentFilter mScreenOffFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);


@@ -146,12 +130,36 @@ public class SwipeDismissLayout extends FrameLayout {
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        try {
            mScreenOffReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    post(() -> {
                        if (mDismissed) {
                            dismiss();
                        } else {
                            cancel();
                        }
                        resetMembers();
                    });
                }
            };
            getContext().registerReceiver(mScreenOffReceiver, mScreenOffFilter);
        } catch (ReceiverCallNotAllowedException e) {
            /* Exception is thrown if the context is a ReceiverRestrictedContext object. As
             * ReceiverRestrictedContext is not public, the context type cannot be checked before
             * calling registerReceiver. The most likely scenario in which the exception would be
             * thrown would be when a BroadcastReceiver creates a dialog to show the user. */
            mScreenOffReceiver = null; // clear receiver since it was not used.
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        if (mScreenOffReceiver != null) {
            getContext().unregisterReceiver(mScreenOffReceiver);
            mScreenOffReceiver = null;
        }
        super.onDetachedFromWindow();
    }