1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.wallpaper.widget; 18 19 import static com.android.wallpaper.picker.individual.IndividualPickerFragment2.IndividualAdapter.ITEM_VIEW_TYPE_HEADER; 20 import static com.android.wallpaper.picker.individual.IndividualPickerFragment2.IndividualAdapter.ITEM_VIEW_TYPE_HEADER_TOP; 21 import static com.android.wallpaper.picker.individual.IndividualPickerFragment2.IndividualAdapter.ITEM_VIEW_TYPE_INDIVIDUAL_WALLPAPER; 22 23 import android.graphics.Rect; 24 import android.os.Build; 25 import android.view.View; 26 27 import androidx.recyclerview.widget.GridLayoutManager; 28 import androidx.recyclerview.widget.RecyclerView; 29 30 /** 31 * RecyclerView ItemDecorator that adds a horizontal space and bottom space of the given size 32 * between items of type creative category 33 */ 34 public class GridPaddingDecorationCreativeCategory extends RecyclerView.ItemDecoration { 35 private final int mPaddingHorizontal; 36 private final int mPaddingBottom; 37 private int mEdgePadding; 38 GridPaddingDecorationCreativeCategory(int paddingHorizontal, int paddingBottom, int edgePadding)39 public GridPaddingDecorationCreativeCategory(int paddingHorizontal, int paddingBottom, 40 int edgePadding) { 41 mPaddingHorizontal = paddingHorizontal; 42 mPaddingBottom = paddingBottom; 43 mEdgePadding = edgePadding; 44 } 45 46 @Override getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)47 public void getItemOffsets(Rect outRect, View view, RecyclerView parent, 48 RecyclerView.State state) { 49 int position = parent.getChildAdapterPosition(view); 50 int spanCount = getSpanCount(parent); 51 boolean isRtl = isRtlLayout(parent); 52 53 if (position >= 0) { 54 if (parent.getAdapter().getItemViewType(position) 55 == ITEM_VIEW_TYPE_INDIVIDUAL_WALLPAPER) { 56 outRect.bottom = mPaddingBottom; 57 // Calculate the total horizontal space available for items in a row 58 int totalHorizontalSpace = parent.getWidth() 59 - parent.getPaddingLeft() 60 - parent.getPaddingRight() 61 - (mEdgePadding * 2); // Account for the gaps on both sides 62 63 // Calculate the width of each item in the row 64 int itemWidth = totalHorizontalSpace / spanCount; 65 66 // Calculate the left and right padding for all items 67 int padding = (totalHorizontalSpace - (itemWidth * spanCount)) / (spanCount - 1); 68 69 boolean isFirstItem = position % spanCount == 0; 70 // This needs to be done since the emoji wallpapers creation tile is at position 1 71 // which is the first element in the row 72 if (position == 1) { 73 isFirstItem = true; 74 } 75 if (isFirstItem) { 76 if (isRtl) { 77 outRect.right = mEdgePadding; 78 outRect.left = padding; 79 } else { 80 outRect.left = mEdgePadding; 81 outRect.right = padding; 82 } 83 } 84 boolean isLastItem = (position + 1) % spanCount == 0; 85 if (isLastItem) { 86 if (isRtl) { 87 outRect.right = padding; 88 outRect.left = mEdgePadding; 89 } else { 90 outRect.left = padding; 91 outRect.right = mEdgePadding; 92 } 93 } 94 if (!isFirstItem && !isLastItem) { 95 // Middle items 96 outRect.left = outRect.left + mEdgePadding / 2; 97 outRect.right = outRect.right + mEdgePadding / 2; 98 } 99 } else if ((parent.getAdapter().getItemViewType(position) == ITEM_VIEW_TYPE_HEADER) 100 || (parent.getAdapter().getItemViewType(position) 101 == ITEM_VIEW_TYPE_HEADER_TOP)) { 102 // Header items 103 outRect.left = mPaddingHorizontal; 104 outRect.right = mPaddingHorizontal; 105 outRect.bottom = mPaddingBottom; 106 } 107 } 108 } 109 getSpanCount(RecyclerView parent)110 private int getSpanCount(RecyclerView parent) { 111 RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 112 if (layoutManager instanceof GridLayoutManager) { 113 GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager; 114 return gridLayoutManager.getSpanCount(); 115 } 116 return 1; 117 } 118 isRtlLayout(RecyclerView parent)119 private boolean isRtlLayout(RecyclerView parent) { 120 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 121 int layoutDirection = parent.getLayoutDirection(); 122 return layoutDirection == View.LAYOUT_DIRECTION_RTL; 123 } 124 return false; 125 } 126 127 } 128