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

Commit f1c92ca9 authored by Annie Chin's avatar Annie Chin Committed by Android (Google) Code Review
Browse files

Merge "Clean up open/close state for DragLayout." into ub-calculator-euler

parents 3f2211d0 9a21113a
Loading
Loading
Loading
Loading
+12 −6
Original line number Diff line number Diff line
@@ -87,6 +87,7 @@ public class Calculator extends Activity
        implements OnTextSizeChangeListener, OnLongClickListener,
        AlertDialogFragment.OnClickListener, Evaluator.EvaluationListener /* for main result */ {

    private static final String TAG = "Calculator";
    /**
     * Constant for an invalid resource id.
     */
@@ -221,9 +222,16 @@ public class Calculator extends Activity
        }
    };

    private final DragLayout.CloseCallback mCloseCallback = new DragLayout.CloseCallback() {
        @Override
        public void onClose() {
            popFragmentBackstack();
        }
    };

    private final DragLayout.DragCallback mDragCallback = new DragLayout.DragCallback() {
        @Override
        public void onStartDragging() {
        public void onStartDraggingOpen() {
            showHistoryFragment(FragmentTransaction.TRANSIT_NONE);
        }

@@ -232,11 +240,6 @@ public class Calculator extends Activity
            // no-op
        }

        @Override
        public void onClosed() {
            popFragmentBackstack();
        }

        @Override
        public boolean allowDrag(MotionEvent event) {
            return isViewTarget(mHistoryFrame, event) || isViewTarget(mDisplayView, event);
@@ -414,6 +417,7 @@ public class Calculator extends Activity
        mDragLayout = (DragLayout) findViewById(R.id.drag_layout);
        mDragLayout.removeDragCallback(mDragCallback);
        mDragLayout.addDragCallback(mDragCallback);
        mDragLayout.setCloseCallback(mCloseCallback);

        mHistoryFrame = (FrameLayout) findViewById(R.id.history_frame);

@@ -701,7 +705,9 @@ public class Calculator extends Activity
            return;
        }
        manager.popBackStack();
        manager.executePendingTransactions();
    }

    /**
     * Switch to INPUT from RESULT state in response to input of the specified button_id.
     * View.NO_ID is treated as an incomplete function id.
+25 −14
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ public class DragLayout extends RelativeLayout {
    private ViewDragHelper mDragHelper;

    private final List<DragCallback> mDragCallbacks = new ArrayList<>();
    private CloseCallback mCloseCallback;

    private int mDraggingState = ViewDragHelper.STATE_IDLE;
    private int mDraggingBorder;
@@ -158,7 +159,7 @@ public class DragLayout extends RelativeLayout {

    private void onStartDragging() {
        for (DragCallback c : mDragCallbacks) {
            c.onStartDragging();
            c.onStartDraggingOpen();
        }
        mHistoryFrame.setVisibility(VISIBLE);
    }
@@ -174,17 +175,15 @@ public class DragLayout extends RelativeLayout {

    public void setOpen() {
        mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, mVerticalRange);
        mIsOpen = true;
        mHistoryFrame.setVisibility(VISIBLE);
    }

    public void setClosed() {
        // Scroll the RecyclerView to the bottom.
        for (DragCallback c : mDragCallbacks) {
            c.onClosed();
        }
        mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, 0);
        mIsOpen = false;
    }

    public void setCloseCallback(CloseCallback callback) {
        mCloseCallback = callback;
    }

    public void addDragCallback(DragCallback callback) {
@@ -195,19 +194,27 @@ public class DragLayout extends RelativeLayout {
        mDragCallbacks.remove(callback);
    }

    /**
     * Callback when the layout is closed.
     * We use this to pop the HistoryFragment off the backstack.
     * We can't use a method in DragCallback because we get ConcurrentModificationExceptions on
     * mDragCallbacks when executePendingTransactions() is called for popping the fragment off the
     * backstack.
     */
    public interface CloseCallback {
        void onClose();
    }

    /**
     * Callbacks for coordinating with the RecyclerView or HistoryFragment.
     */
    public interface DragCallback {
        // Callback when a drag in any direction begins.
        void onStartDragging();
        // Callback when a drag to open begins.
        void onStartDraggingOpen();

        // Animate the RecyclerView text.
        void whileDragging(float yFraction);

        // Scroll the RecyclerView to the bottom before closing the frame.
        void onClosed();

        // Whether we should allow the drag to happen
        boolean allowDrag(MotionEvent event);

@@ -232,12 +239,16 @@ public class DragLayout extends RelativeLayout {
                // The view stopped moving.
                if (mDraggingBorder == 0) {
                    setClosed();
                    mIsOpen = false;
                    mHistoryFrame.setVisibility(GONE);
                    if (mCloseCallback != null) {
                        mCloseCallback.onClose();
                    }
                } else if (mDraggingBorder == mVerticalRange) {
                    setOpen();
                    mIsOpen = true;
                }
            }
            if (state == ViewDragHelper.STATE_DRAGGING) {
            } else if (state == ViewDragHelper.STATE_DRAGGING && !mIsOpen) {
                onStartDragging();
            }
            mDraggingState = state;
+5 −7
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ public class HistoryFragment extends Fragment {
    private final DragLayout.DragCallback mDragCallback =
            new DragLayout.DragCallback() {
                @Override
                public void onStartDragging() {
                public void onStartDraggingOpen() {
                    // no-op
                }

@@ -47,11 +47,6 @@ public class HistoryFragment extends Fragment {
                    mDragController.animateViews(yFraction, mRecyclerView);
                }

                @Override
                public void onClosed() {
                    mEvaluator.cancelNonMain();
                }

                @Override
                public boolean allowDrag(MotionEvent event) {
                    // Do not allow drag if the recycler view can move down more
@@ -200,8 +195,11 @@ public class HistoryFragment extends Fragment {
            dragLayout.removeDragCallback(mDragCallback);
        }

        // Note that the view is destroyed when the fragment backstack is popped, so
        // these are essentially called when the DragLayout is closed.
        mEvaluator.cancelNonMain();
        super.onDestroy();

        super.onDestroyView();
    }

    private void initializeController(boolean isResult) {