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 KIN'D, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.setupwizardlib.util;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.drawable.Drawable;
22import android.os.Bundle;
23import android.support.v7.widget.LinearLayoutManager;
24import android.support.v7.widget.RecyclerView;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.ImageView;
29import android.widget.TextView;
30
31import com.android.setupwizardlib.R;
32import com.android.setupwizardlib.view.HeaderRecyclerView;
33
34/**
35 * A helper delegate to integrate GLIF theme with PreferenceFragment v14. To use this, create an
36 * instance and delegate {@code PreferenceFragment#onCreateRecyclerView} to it. Then call
37 * {@code PreferenceFragment#setDivider} to {@link #getDividerDrawable(android.content.Context)} in
38 * order to make sure the correct inset is applied to the dividers.
39 *
40 * @deprecated Use {@link com.android.setupwizardlib.GlifPreferenceLayout}
41 */
42@Deprecated
43public class GlifPreferenceDelegate {
44
45    public static final int[] ATTRS_LIST_DIVIDER = new int[]{ android.R.attr.listDivider };
46
47    private HeaderRecyclerView mRecyclerView;
48    private boolean mHasIcons;
49
50    public GlifPreferenceDelegate(boolean hasIcons) {
51        mHasIcons = hasIcons;
52    }
53
54    public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent,
55            Bundle savedInstanceState) {
56        final Context inflaterContext = inflater.getContext();
57        mRecyclerView = new HeaderRecyclerView(inflaterContext);
58        mRecyclerView.setLayoutManager(new LinearLayoutManager(inflaterContext));
59        final View header = inflater.inflate(R.layout.suw_glif_header, mRecyclerView, false);
60        mRecyclerView.setHeader(header);
61        return mRecyclerView;
62    }
63
64    public Drawable getDividerDrawable(Context context) {
65        final TypedArray a = context.obtainStyledAttributes(ATTRS_LIST_DIVIDER);
66        final Drawable defaultDivider = a.getDrawable(0);
67        a.recycle();
68
69        final int dividerInset = context.getResources().getDimensionPixelSize(
70                mHasIcons ? R.dimen.suw_items_glif_icon_divider_inset
71                        : R.dimen.suw_items_glif_text_divider_inset);
72        return DrawableLayoutDirectionHelper.createRelativeInsetDrawable(defaultDivider,
73                dividerInset /* start */, 0 /* top */, 0 /* end */, 0 /* bottom */,
74                context);
75    }
76
77    public void setHeaderText(CharSequence text) {
78        final View header = mRecyclerView.getHeader();
79
80        final View titleView = header.findViewById(R.id.suw_layout_title);
81        if (titleView instanceof TextView)  {
82            ((TextView) titleView).setText(text);
83        }
84    }
85
86    public void setIcon(Drawable icon) {
87        final View header = mRecyclerView.getHeader();
88
89        final View iconView = header.findViewById(R.id.suw_layout_icon);
90        if (iconView instanceof ImageView) {
91            ((ImageView) iconView).setImageDrawable(icon);
92        }
93    }
94}
95