content: package dev.dworks.apps.anexplorer.directory;
  
  import android.content.Context;
  import android.content.res.TypedArray;
  import android.graphics.Canvas;
  import android.graphics.Rect;
  import android.graphics.drawable.Drawable;
  import androidx.annotation.NonNull;
  import androidx.recyclerview.widget.LinearLayoutManager;
  import androidx.recyclerview.widget.RecyclerView;
  import android.util.Log;
  import android.view.View;
  import android.widget.LinearLayout;
  
  /**
   * Adds interior dividers to a RecyclerView with a LinearLayoutManager or its
   * subclass.
   */
  public class DividerItemDecoration extends RecyclerView.ItemDecoration {
  
      public static final int HORIZONTAL = LinearLayout.HORIZONTAL;
      public static final int VERTICAL = LinearLayout.VERTICAL;
  
      private static final String TAG = "DividerItem";
      private static final int[] ATTRS = new int[]{ android.R.attr.listDivider };
  
      private Drawable mDivider;
  
      /**
       * Current orientation. Either {@link #HORIZONTAL} or {@link #VERTICAL}.
       */
      private int mOrientation;
  
      private int mLeftInset;
      private int mRightInset;
  
      private final Rect mBounds = new Rect();
  
      /**
       * Creates a divider {@link RecyclerView.ItemDecoration} that can be used with a
       * {@link LinearLayoutManager}.
       *
       * @param context Current context, it will be used to access resources.
       * @param orientation Divider orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}.
       */
      public DividerItemDecoration(Context context, int orientation) {
          final TypedArray a = context.obtainStyledAttributes(ATTRS);
          mDivider = a.getDrawable(0);
          if (mDivider == null) {
              Log.w(TAG, "@android:attr/listDivider was not set in the theme used for this "
                      + "DividerItemDecoration. Please set that attribute all call setDrawable()");
          }
          a.recycle();
          setOrientation(orientation);
          mLeftInset = 0;
          mRightInset = 0;
      }
  
      /**
       * Sets the orientation for this divider. This should be called if
       * {@link RecyclerView.LayoutManager} changes orientation.
       *
       * @param orientation {@link #HORIZONTAL} or {@link #VERTICAL}
       */
      public void setOrientation(int orientation) {
          if (orientation != HORIZONTAL && orientation != VERTICAL) {
              throw new IllegalArgumentException(
                      "Invalid orientation. It should be either HORIZONTAL or VERTICAL");
          }
          mOrientation = orientation;
      }
  
      /**
       * Sets the {@link Drawable} for this divider.
       *
       * @param drawable Drawable that should be used as a divider.
       */
      public void setDrawable(@NonNull Drawable drawable) {
          if (drawable == null) {
              throw new IllegalArgumentException("Drawable cannot be null.");
          }
          mDivider = drawable;
      }
  
      /**
       * @param leftInset Left inset of a divider
       * @param rightInset Right inset of a divider
       */
      public void setInset(int leftInset, int rightInset) {
          mLeftInset = leftInset;
          mRightInset = rightInset;
      }
  
      /**
       * Draws horizontal or vertical dividers onto the parent RecyclerView.
       *
       * @param canvas The {@link Canvas} onto which dividers will be drawn
       * @param parent The RecyclerView onto which dividers are being added
       * @param state The current RecyclerView.State of the RecyclerView
       */
      @Override
      public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
          if (mOrientation == LinearLayoutManager.HORIZONTAL) {
              drawHorizontal(canvas, parent);
          } else if (mOrientation == LinearLayoutManager.VERTICAL) {
              drawVertical(canvas, parent);
          }
      }
  
      /**
       * Determines the size and location of offsets between items in the parent
       * RecyclerView.
       *
       * @param outRect The {@link Rect} of offsets to be added around the child
       *                view
       * @param view The child view to be decorated with an offset
       * @param parent The RecyclerView onto which dividers are being added
       * @param state The current RecyclerView.State of the RecyclerView
       */
      @Override
      public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
          if (mDivider == null) {
              outRect.set(0, 0, 0, 0);
              return;
          }
          if (mOrientation == VERTICAL) {
              outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
          } else {
              outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
          }
      }
  
      /**
       * Adds dividers to a RecyclerView with a LinearLayoutManager or its
       * subclass oriented horizontally.
       *
       * @param canvas The {@link Canvas} onto which horizontal dividers will be
       *               drawn
       * @param parent The RecyclerView onto which horizontal dividers are being
       *               added
       */
      private void drawHorizontal(Canvas canvas, RecyclerView parent) {
          canvas.save();
          final int top;
          final int bottom;
          //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
          if (parent.getClipToPadding()) {
              top = parent.getPaddingTop();
              bottom = parent.getHeight() - parent.getPaddingBottom();
              canvas.clipRect(parent.getPaddingLeft(), top,
                      parent.getWidth() - parent.getPaddingRight(), bottom);
          } else {
              top = 0;
              bottom = parent.getHeight();
          }
  
          final int childCount = parent.getChildCount();
          for (int i = 0; i < childCount; i++) {
              final View child = parent.getChildAt(i);
              parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
              final int right = mRightInset + mBounds.right + Math.round(child.getTranslationX());
              final int left = mLeftInset + right - mDivider.getIntrinsicWidth();
              mDivider.setBounds(left, top, right, bottom);
              mDivider.draw(canvas);
          }
          canvas.restore();
      }
  
      /**
       * Adds dividers to a RecyclerView with a LinearLayoutManager or its
       * subclass oriented vertically.
       *
       * @param canvas The {@link Canvas} onto which vertical dividers will be
       *               drawn
       * @param parent The RecyclerView onto which vertical dividers are being
       *               added
       */
      private void drawVertical(Canvas canvas, RecyclerView parent) {
          canvas.save();
          final int left;
          final int right;
          //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
          if (parent.getClipToPadding()) {
              left = mLeftInset + parent.getPaddingLeft();
              right = mRightInset + parent.getWidth() - parent.getPaddingRight();
              canvas.clipRect(left, parent.getPaddingTop(), right,
                      parent.getHeight() - parent.getPaddingBottom());
          } else {
              left = mLeftInset;
              right = mRightInset + parent.getWidth();
          }
  
          final int childCount = parent.getChildCount();
          for (int i = 0; i < childCount; i++) {
              final View child = parent.getChildAt(i);
              parent.getDecoratedBoundsWithMargins(child, mBounds);
              final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
              final int top = bottom - mDivider.getIntrinsicHeight();
              mDivider.setBounds(left, top, right, bottom);
              mDivider.draw(canvas);
          }
          canvas.restore();
      }
  }