1be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk/*
2be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk * Copyright (C) 2016 The Android Open Source Project
3be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk *
4be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk * except in compliance with the License. You may obtain a copy of the License at
6be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk *
7be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk *      http://www.apache.org/licenses/LICENSE-2.0
8be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk *
9be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk * Unless required by applicable law or agreed to in writing, software distributed under the
10be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk * KIND, either express or implied. See the License for the specific language governing
12be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk * permissions and limitations under the License.
13be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk */
14be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk
15be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkpackage com.android.settings.dashboard;
16be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk
17be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkimport android.content.Context;
18be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkimport android.graphics.Canvas;
19be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkimport android.graphics.drawable.Drawable;
20be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkimport android.support.v4.view.ViewCompat;
21be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkimport android.support.v7.widget.RecyclerView;
22be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkimport android.support.v7.widget.RecyclerView.State;
23be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkimport android.support.v7.widget.RecyclerView.ViewHolder;
24be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkimport android.util.TypedValue;
25be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkimport android.view.View;
26be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkimport com.android.settings.R;
27be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk
28be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monkpublic class DashboardDecorator extends RecyclerView.ItemDecoration {
29be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk
30be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk    private final Context mContext;
31be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk    private final Drawable mDivider;
32be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk
33be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk    public DashboardDecorator(Context context) {
34be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk        mContext = context;
35be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk        TypedValue value = new TypedValue();
36be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk        mContext.getTheme().resolveAttribute(android.R.attr.listDivider, value, true);
37be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk        mDivider = mContext.getDrawable(value.resourceId);
38be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk    }
39be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk
40be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk    @Override
41be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk    public void onDrawOver(Canvas c, RecyclerView parent, State state) {
42be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk        final int childCount = parent.getChildCount();
43be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk        for (int i = 1; i < childCount; i++) {
44be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk            final View child = parent.getChildAt(i);
45be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk            final ViewHolder holder = parent.getChildViewHolder(child);
46be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk            if (holder.getItemViewType() == R.layout.dashboard_category) {
47be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk                if (parent.getChildViewHolder(parent.getChildAt(i - 1)).getItemViewType()
48be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk                        != R.layout.dashboard_tile) {
49be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk                    continue;
50be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk                }
51be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk            } else if (holder.getItemViewType() != R.layout.condition_card) {
52be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk                continue;
53be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk            }
54be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk
55be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk            int top = getChildTop(child);
5679f370678523206e88787b211f09483be32ccbddJason Monk            mDivider.setBounds(child.getLeft(), top, child.getRight(),
5779f370678523206e88787b211f09483be32ccbddJason Monk                    top + mDivider.getIntrinsicHeight());
58be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk            mDivider.draw(c);
59be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk        }
60be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk    }
61be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk
62be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk    private int getChildTop(View child) {
63be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
64be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk                .getLayoutParams();
65be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk        return child.getTop() + params.topMargin + Math.round(ViewCompat.getTranslationY(child));
66be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk    }
67be8d5aa2b9ead06803a47c12a672ad124a2a4bb3Jason Monk}
68