Home | History | Annotate | Download | only in selection
      1 /*
      2  * Copyright (C) 2017 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 package com.android.documentsui.selection;
     17 
     18 import static com.android.internal.util.Preconditions.checkArgument;
     19 
     20 import android.annotation.DrawableRes;
     21 import android.graphics.Point;
     22 import android.graphics.Rect;
     23 import android.graphics.drawable.Drawable;
     24 import android.support.v7.widget.GridLayoutManager;
     25 import android.support.v7.widget.RecyclerView;
     26 import android.view.View;
     27 
     28 import com.android.documentsui.selection.BandSelectionHelper.BandHost;
     29 
     30 /**
     31  * RecyclerView backed {@link BandHost}.
     32  */
     33 public final class DefaultBandHost extends BandHost {
     34 
     35     private final RecyclerView mRecView;
     36     private final Drawable mBand;
     37 
     38     private boolean mIsOverlayShown;
     39 
     40     public DefaultBandHost(RecyclerView recView, @DrawableRes int bandOverlayId) {
     41 
     42         checkArgument(recView != null);
     43 
     44         mRecView = recView;
     45         mBand = mRecView.getContext().getTheme().getDrawable(bandOverlayId);
     46 
     47         checkArgument(mBand != null);
     48     }
     49 
     50     @Override
     51     public int getAdapterPositionAt(int index) {
     52         return mRecView.getChildAdapterPosition(mRecView.getChildAt(index));
     53     }
     54 
     55     @Override
     56     public void addOnScrollListener(RecyclerView.OnScrollListener listener) {
     57         mRecView.addOnScrollListener(listener);
     58     }
     59 
     60     @Override
     61     public void removeOnScrollListener(RecyclerView.OnScrollListener listener) {
     62         mRecView.removeOnScrollListener(listener);
     63     }
     64 
     65     @Override
     66     public Point createAbsolutePoint(Point relativePoint) {
     67         return new Point(relativePoint.x + mRecView.computeHorizontalScrollOffset(),
     68                 relativePoint.y + mRecView.computeVerticalScrollOffset());
     69     }
     70 
     71     @Override
     72     public Rect getAbsoluteRectForChildViewAt(int index) {
     73         final View child = mRecView.getChildAt(index);
     74         final Rect childRect = new Rect();
     75         child.getHitRect(childRect);
     76         childRect.left += mRecView.computeHorizontalScrollOffset();
     77         childRect.right += mRecView.computeHorizontalScrollOffset();
     78         childRect.top += mRecView.computeVerticalScrollOffset();
     79         childRect.bottom += mRecView.computeVerticalScrollOffset();
     80         return childRect;
     81     }
     82 
     83     @Override
     84     public int getChildCount() {
     85         return mRecView.getAdapter().getItemCount();
     86     }
     87 
     88     @Override
     89     public int getVisibleChildCount() {
     90         return mRecView.getChildCount();
     91     }
     92 
     93     @Override
     94     public int getColumnCount() {
     95         RecyclerView.LayoutManager layoutManager = mRecView.getLayoutManager();
     96         if (layoutManager instanceof GridLayoutManager) {
     97             return ((GridLayoutManager) layoutManager).getSpanCount();
     98         }
     99 
    100         // Otherwise, it is a list with 1 column.
    101         return 1;
    102     }
    103 
    104     @Override
    105     public int getHeight() {
    106         return mRecView.getHeight();
    107     }
    108 
    109     @Override
    110     public void invalidateView() {
    111         mRecView.invalidate();
    112     }
    113 
    114     @Override
    115     public void runAtNextFrame(Runnable r) {
    116         mRecView.postOnAnimation(r);
    117     }
    118 
    119     @Override
    120     public void removeCallback(Runnable r) {
    121         mRecView.removeCallbacks(r);
    122     }
    123 
    124     @Override
    125     public void scrollBy(int dy) {
    126         mRecView.scrollBy(0, dy);
    127     }
    128 
    129     @Override
    130     public void showBand(Rect rect) {
    131         mBand.setBounds(rect);
    132 
    133         if (!mIsOverlayShown) {
    134             mRecView.getOverlay().add(mBand);
    135         }
    136     }
    137 
    138     @Override
    139     public void hideBand() {
    140         mRecView.getOverlay().remove(mBand);
    141     }
    142 
    143     @Override
    144     public boolean hasView(int pos) {
    145         return mRecView.findViewHolderForAdapterPosition(pos) != null;
    146     }
    147 }