1 /*
2  * Copyright (C) 2015 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.google.android.setupdesign.view;
18 
19 import android.annotation.TargetApi;
20 import android.content.Context;
21 import android.content.res.TypedArray;
22 import android.graphics.Canvas;
23 import android.graphics.RectF;
24 import android.os.Build;
25 import android.util.AttributeSet;
26 import android.view.LayoutInflater;
27 import android.view.MotionEvent;
28 import android.view.View;
29 import android.view.WindowInsets;
30 import android.view.accessibility.AccessibilityEvent;
31 import android.widget.ListView;
32 import com.google.android.setupdesign.R;
33 
34 /**
35  * This class provides sticky header functionality in a list view, to use with
36  * SetupWizardIllustration. To use this, add a header tagged with "sticky", or a header tagged with
37  * "stickyContainer" and one of its child tagged as "sticky". The sticky container will be drawn
38  * when the sticky element hits the top of the view.
39  *
40  * <p>There are a few things to note:
41  *
42  * <ol>
43  *   <li>The two supported scenarios are StickyHeaderListView -> Header (stickyContainer) -> sticky,
44  *       and StickyHeaderListView -> Header (sticky). The arrow (->) represents parent/child
45  *       relationship and must be immediate child.
46  *   <li>The view does not work well with padding. b/16190933
47  *   <li>If fitsSystemWindows is true, then this will offset the sticking position by the height of
48  *       the system decorations at the top of the screen.
49  * </ol>
50  *
51  * @see StickyHeaderScrollView
52  */
53 public class StickyHeaderListView extends ListView {
54 
55   private View sticky;
56   private View stickyContainer;
57   private int statusBarInset = 0;
58   private final RectF stickyRect = new RectF();
59 
StickyHeaderListView(Context context)60   public StickyHeaderListView(Context context) {
61     super(context);
62     init(null, android.R.attr.listViewStyle);
63   }
64 
StickyHeaderListView(Context context, AttributeSet attrs)65   public StickyHeaderListView(Context context, AttributeSet attrs) {
66     super(context, attrs);
67     init(attrs, android.R.attr.listViewStyle);
68   }
69 
StickyHeaderListView(Context context, AttributeSet attrs, int defStyleAttr)70   public StickyHeaderListView(Context context, AttributeSet attrs, int defStyleAttr) {
71     super(context, attrs, defStyleAttr);
72     init(attrs, defStyleAttr);
73   }
74 
init(AttributeSet attrs, int defStyleAttr)75   private void init(AttributeSet attrs, int defStyleAttr) {
76     if (isInEditMode()) {
77       return;
78     }
79 
80     final TypedArray a =
81         getContext()
82             .obtainStyledAttributes(attrs, R.styleable.SudStickyHeaderListView, defStyleAttr, 0);
83     int headerResId = a.getResourceId(R.styleable.SudStickyHeaderListView_sudHeader, 0);
84     if (headerResId != 0) {
85       LayoutInflater inflater = LayoutInflater.from(getContext());
86       View header = inflater.inflate(headerResId, this, false);
87       addHeaderView(header, null, false);
88     }
89     a.recycle();
90   }
91 
92   @Override
onLayout(boolean changed, int l, int t, int r, int b)93   protected void onLayout(boolean changed, int l, int t, int r, int b) {
94     super.onLayout(changed, l, t, r, b);
95     if (sticky == null) {
96       updateStickyView();
97     }
98   }
99 
updateStickyView()100   public void updateStickyView() {
101     sticky = findViewWithTag("sticky");
102     stickyContainer = findViewWithTag("stickyContainer");
103   }
104 
105   @Override
dispatchTouchEvent(MotionEvent ev)106   public boolean dispatchTouchEvent(MotionEvent ev) {
107     if (stickyRect.contains(ev.getX(), ev.getY())) {
108       ev.offsetLocation(-stickyRect.left, -stickyRect.top);
109       return stickyContainer.dispatchTouchEvent(ev);
110     } else {
111       return super.dispatchTouchEvent(ev);
112     }
113   }
114 
115   @Override
draw(Canvas canvas)116   public void draw(Canvas canvas) {
117     super.draw(canvas);
118     if (sticky != null) {
119       final int saveCount = canvas.save();
120       // The view to draw when sticking to the top
121       final View drawTarget = stickyContainer != null ? stickyContainer : sticky;
122       // The offset to draw the view at when sticky
123       final int drawOffset = stickyContainer != null ? sticky.getTop() : 0;
124       // Position of the draw target, relative to the outside of the scrollView
125       final int drawTop = drawTarget.getTop();
126       if (drawTop + drawOffset < statusBarInset || !drawTarget.isShown()) {
127         // ListView does not translate the canvas, so we can simply draw at the top
128         stickyRect.set(
129             0,
130             -drawOffset + statusBarInset,
131             drawTarget.getWidth(),
132             drawTarget.getHeight() - drawOffset + statusBarInset);
133         canvas.translate(0, stickyRect.top);
134         canvas.clipRect(0, 0, drawTarget.getWidth(), drawTarget.getHeight());
135         drawTarget.draw(canvas);
136       } else {
137         stickyRect.setEmpty();
138       }
139       canvas.restoreToCount(saveCount);
140     }
141   }
142 
143   @Override
144   @TargetApi(Build.VERSION_CODES.LOLLIPOP)
onApplyWindowInsets(WindowInsets insets)145   public WindowInsets onApplyWindowInsets(WindowInsets insets) {
146     if (getFitsSystemWindows()) {
147       statusBarInset = insets.getSystemWindowInsetTop();
148       insets.replaceSystemWindowInsets(
149           insets.getSystemWindowInsetLeft(),
150           0, /* top */
151           insets.getSystemWindowInsetRight(),
152           insets.getSystemWindowInsetBottom());
153     }
154     return insets;
155   }
156 
157   @Override
onInitializeAccessibilityEvent(AccessibilityEvent event)158   public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
159     super.onInitializeAccessibilityEvent(event);
160 
161     // Decoration-only headers should not count as an item for accessibility, adjust the
162     // accessibility event to account for that.
163     final int numberOfHeaders = sticky != null ? 1 : 0;
164     event.setItemCount(event.getItemCount() - numberOfHeaders);
165     event.setFromIndex(Math.max(event.getFromIndex() - numberOfHeaders, 0));
166     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
167       event.setToIndex(Math.max(event.getToIndex() - numberOfHeaders, 0));
168     }
169   }
170 }
171