1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * 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 License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package com.android.tv.quicksettings;
15
16import android.support.v7.widget.RecyclerView;
17import android.view.LayoutInflater;
18import android.view.View;
19import android.view.ViewGroup;
20import android.widget.TextView;
21
22import java.util.ArrayList;
23
24public class DialogAdapter extends RecyclerView.Adapter<DialogAdapter.ViewHolder> {
25
26    private static final float FOCUSED_SCALE = 1f;
27    private static final float UNFOCUSED_SCALE = 0.5f;
28
29    private final ArrayList<Setting> mSettings;
30    private final int mPivotX;
31    private final int mPivotY;
32    private final SettingClickedListener mSettingClickedListener;
33
34    public DialogAdapter(ArrayList<Setting> settings, int pivotX, int pivotY,
35            SettingClickedListener settingClickedListener) {
36        mSettings = settings;
37        mPivotX = pivotX;
38        mPivotY = pivotY;
39        mSettingClickedListener = settingClickedListener;
40    }
41
42    @Override
43    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
44        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.dialog_setting, parent,
45                false);
46        final ViewHolder vh = new ViewHolder(v);
47        v.setPivotX(mPivotX);
48        v.setPivotY(mPivotY);
49        v.setScaleX(UNFOCUSED_SCALE);
50        v.setScaleY(UNFOCUSED_SCALE);
51        v.setOnClickListener(new View.OnClickListener() {
52            @Override
53            public void onClick(View v) {
54                Setting s = vh.mSetting;
55                if (s != null) {
56                    mSettingClickedListener.onSettingClicked(s);
57                }
58            }
59        });
60        v.setOnFocusChangeListener(new View.OnFocusChangeListener() {
61            @Override
62            public void onFocusChange(View v, boolean focusGained) {
63                float scale = focusGained ? FOCUSED_SCALE : UNFOCUSED_SCALE;
64                v.animate().cancel();
65                v.animate().scaleX(scale).scaleY(scale).setDuration(
66                        v.getContext().getResources()
67                                .getInteger(android.R.integer.config_shortAnimTime))
68                        .start();
69            }
70        });
71        return vh;
72    }
73
74    @Override
75    public void onBindViewHolder(ViewHolder holder, int position) {
76        Setting s = mSettings.get(position);
77        holder.setSetting(s);
78        holder.mTitle.setText(s.getTitle());
79    }
80
81    @Override
82    public void onViewRecycled(ViewHolder holder) {
83        holder.setSetting(null);
84    }
85
86    @Override
87    public int getItemCount() {
88        return mSettings.size();
89    }
90
91    public static class ViewHolder extends RecyclerView.ViewHolder {
92        private final TextView mTitle;
93        private Setting mSetting;
94
95        public ViewHolder(View itemView) {
96            super(itemView);
97            mTitle = (TextView) itemView.findViewById(R.id.setting_title);
98        }
99
100        public void setSetting(Setting setting) {
101            mSetting = setting;
102        }
103    }
104}
105