Migrate to gradle, appcompat as external dependency.
[mobilnebezpieczenstwo.git] / app / src / main / java / com / samsung / srpol / ui / drawer / CategoryArrayAdapter.java
1 /*
2    Copyright (C) 2014  Samsung Electronics Polska Sp. z o.o.
3
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU AFFERO General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8     You may obtain a copy of the License at
9
10                 http://www.gnu.org/licenses/agpl-3.0.txt
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 package com.samsung.srpol.ui.drawer;
22
23 import java.util.Collection;
24
25 import com.samsung.srpol.R;
26 import com.samsung.srpol.data.Category;
27
28 import android.content.Context;
29 import android.text.Spannable;
30 import android.text.SpannableString;
31 import android.text.style.ForegroundColorSpan;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.widget.ArrayAdapter;
36 import android.widget.ImageView;
37 import android.widget.TextView;
38
39 public class CategoryArrayAdapter extends ArrayAdapter<Category> {
40     private final Context mContext;
41     private String mTextPrefix ;
42     private String mCountTextPrefix;
43     private int mSpanTextColor;
44
45     private static class ViewHolder {
46         TextView textView1;
47         TextView textView2;
48         TextView textView3;
49         ImageView imageView;
50     }
51     
52     public CategoryArrayAdapter(Context context) {
53         super(context, R.layout.drawer_list_item);
54         mTextPrefix = context.getResources().getString(R.string.drawer_header_text_prefix);
55         mCountTextPrefix = context.getResources().getString(R.string.items_string_quantity);
56         mSpanTextColor = context.getResources().getColor(R.color.text_menu);
57         mContext = context;
58     }
59
60     @Override
61     public void addAll(Collection<? extends Category> collection) {
62         for (Category category : collection) {
63             super.add(category);
64         }
65     }
66     
67     @Override
68     public View getView(int position, View convertView, ViewGroup parent) {
69         ViewHolder holder = null;
70         if (convertView == null) {
71             LayoutInflater inflater = (LayoutInflater) mContext
72                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
73             convertView = inflater.inflate(R.layout.drawer_list_item, parent,
74                     false);
75             holder = new ViewHolder();
76             holder.textView1 = (TextView) convertView.findViewById(R.id.text1);
77             holder.textView2 = (TextView) convertView.findViewById(R.id.text2);
78             holder.textView3 = (TextView) convertView.findViewById(R.id.text3);
79             holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
80
81             convertView.setTag(holder);
82         } else {
83             holder = (ViewHolder) convertView.getTag();
84         }
85
86         Category category = getItem(position);
87         if (category.getIconRes() > 0)
88             holder.imageView.setImageResource(category.getIconRes());
89         holder.textView1.setText(mTextPrefix + category.getHeader());
90         holder.textView2.setText(category.getShortDescription());
91         holder.textView3.setText(createSpannableCountText(category.getCurrentlyVisible()));
92         return convertView;
93     }
94
95     private Spannable createSpannableCountText(int count){
96         
97         Spannable spannable = new SpannableString(mCountTextPrefix + count);
98         spannable.setSpan(new ForegroundColorSpan(mSpanTextColor),mCountTextPrefix.length(), spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
99         return spannable;
100     }
101 }