• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
14 package com.android.tv.quicksettings;
15 
16 import android.content.Intent;
17 import android.support.v7.widget.RecyclerView;
18 import android.text.TextUtils;
19 import android.util.Log;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.TextView;
24 
25 import java.util.ArrayList;
26 
27 public class PanelAdapter extends RecyclerView.Adapter<PanelAdapter.ViewHolder> {
28 
29     static class ViewHolder extends RecyclerView.ViewHolder {
30         private final TextView mTitle;
31         private final TextView mValue;
32         private Setting mSetting;
33 
ViewHolder(View itemView)34         public ViewHolder(View itemView) {
35             super(itemView);
36             mTitle = (TextView) itemView.findViewById(R.id.setting_title);
37             mValue = (TextView) itemView.findViewById(R.id.setting_value);
38         }
39 
setSetting(Setting setting)40         public void setSetting(Setting setting) {
41             mSetting = setting;
42         }
43     }
44 
45     private static final String TAG = "QuickSettings";
46 
47     private final SettingClickedListener mSettingClickedListener;
48     private ArrayList<Setting> mSettings;
49 
PanelAdapter(ArrayList<Setting> settings, SettingClickedListener settingClickedListener)50     public PanelAdapter(ArrayList<Setting> settings, SettingClickedListener settingClickedListener) {
51         mSettings = settings;
52         mSettingClickedListener = settingClickedListener;
53     }
54 
55     @Override
onCreateViewHolder(final ViewGroup parent, int viewType)56     public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
57         View v = LayoutInflater.from(parent.getContext())
58                 .inflate(R.layout.setting, parent, false);
59         final ViewHolder vh = new ViewHolder(v);
60         v.setOnClickListener(new View.OnClickListener() {
61             @Override
62             public void onClick(View v) {
63                 Setting s = vh.mSetting;
64                 if (s != null) {
65                     mSettingClickedListener.onSettingClicked(s);
66                 }
67             }
68         });
69         return vh;
70     }
71 
72     @Override
onBindViewHolder(ViewHolder holder, int position)73     public void onBindViewHolder(ViewHolder holder, int position) {
74         Setting s = mSettings.get(position);
75         holder.setSetting(s);
76         holder.mTitle.setText(s.getTitle());
77         switch (s.getType()) {
78             case Setting.TYPE_INT:
79                 holder.mValue.setText(Integer.toString(s.getIntValue()));
80                 break;
81             case Setting.TYPE_STRING:
82                 holder.mValue.setText(s.getStringValue());
83                 break;
84             default:
85                 holder.mValue.setText("");
86                 break;
87         }
88         if (TextUtils.isEmpty(holder.mValue.getText())) {
89             holder.mValue.setVisibility(View.GONE);
90         } else {
91             holder.mValue.setVisibility(View.VISIBLE);
92         }
93     }
94 
95     @Override
onViewRecycled(ViewHolder holder)96     public void onViewRecycled(ViewHolder holder) {
97         holder.setSetting(null);
98     }
99 
100     @Override
getItemCount()101     public int getItemCount() {
102         return mSettings.size();
103     }
104 
setSettings(ArrayList<Setting> settings)105     public void setSettings(ArrayList<Setting> settings) {
106         mSettings = settings;
107         notifyDataSetChanged();
108     }
109 }
110