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