1 /* 2 * Copyright (C) 2024 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.systemui.tv.media; 18 19 import android.content.Context; 20 import android.graphics.drawable.Drawable; 21 import android.view.View; 22 import android.view.ViewGroup; 23 24 import androidx.annotation.Nullable; 25 import androidx.recyclerview.widget.RecyclerView; 26 27 import com.android.systemui.tv.res.R; 28 29 public class FadingEdgeUtil { 30 31 @Nullable getForegroundDrawable(RecyclerView recyclerView, Context context)32 public static Drawable getForegroundDrawable(RecyclerView recyclerView, Context context) { 33 if (shouldShowTopFadingEdge(recyclerView) && shouldShowBottomFadingEdge(recyclerView)) { 34 return context.getDrawable(R.drawable.both_end_fading_edge); 35 } else if (shouldShowBottomFadingEdge(recyclerView)) { 36 return context.getDrawable(R.drawable.bottom_fading_edge); 37 } else if (shouldShowTopFadingEdge(recyclerView)) { 38 return context.getDrawable(R.drawable.top_fading_edge); 39 } 40 return null; 41 } 42 43 @SuppressWarnings("nullness") shouldShowTopFadingEdge(RecyclerView recyclerView)44 private static boolean shouldShowTopFadingEdge(RecyclerView recyclerView) { 45 RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); 46 View firstVisibleChildView = layoutManager.getChildAt(0); 47 int positionOfCurrentFistView = layoutManager.getPosition(firstVisibleChildView); 48 boolean isFirstAdapterItemVisible = (positionOfCurrentFistView == 0); 49 if (!isFirstAdapterItemVisible) { 50 return true; 51 } 52 int top = firstVisibleChildView.getTop(); 53 return top < 0; 54 } 55 56 @SuppressWarnings("nullness") shouldShowBottomFadingEdge(RecyclerView recyclerView)57 private static boolean shouldShowBottomFadingEdge(RecyclerView recyclerView) { 58 // Get adapter's last child View. 59 View lastChildView = null; 60 RecyclerView.ViewHolder holder = 61 recyclerView.findViewHolderForAdapterPosition( 62 recyclerView.getAdapter().getItemCount() - 1); 63 if (holder != null) { 64 lastChildView = holder.itemView; 65 } 66 if (lastChildView == null) { 67 // If last child is not inflated yet, then it definitely is not visible. 68 return true; 69 } 70 if (lastChildView.getTop() >= recyclerView.getBottom()) { 71 // If last child is below dashboard's bottom edge, then it should be invisible 72 // currently. 73 // Sometimes even if the last item is not visible, it still get counted in 74 // layoutManager's 75 // children. So use the coordinates would be more precise. 76 return true; 77 } 78 int rvBottom = recyclerView.getBottom(); 79 int bottom = lastChildView.getBottom(); 80 int bottomMargin = 81 ((ViewGroup.MarginLayoutParams) lastChildView.getLayoutParams()).bottomMargin; 82 return bottom + bottomMargin > rvBottom; 83 } 84 85 } 86