92ca1c1a09bd212add51e73fd267f917b1a2ee6d
[mobilnebezpieczenstwo.git] / src / com / samsung / srpol / ui / tabpager / AppListArrayAdapter.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.tabpager;
22
23 import java.util.ArrayList;
24
25 import com.samsung.srpol.R;
26 import com.samsung.srpol.data.Category;
27 import com.samsung.srpol.data.Subcategory;
28 import com.samsung.srpol.loader.AppDetails;
29 import com.samsung.srpol.loader.AppListLoader;
30 import com.samsung.srpol.utils.Utils;
31
32 import android.annotation.TargetApi;
33 import android.content.Context;
34 import android.content.SharedPreferences;
35 import android.graphics.drawable.Drawable;
36 import android.os.Build;
37 import android.preference.PreferenceManager;
38 import android.view.LayoutInflater;
39 import android.view.View;
40 import android.view.ViewGroup;
41 import android.widget.ArrayAdapter;
42 import android.widget.ImageView;
43 import android.widget.LinearLayout;
44 import android.widget.TextView;
45
46 public class AppListArrayAdapter extends ArrayAdapter<AppDetails> {
47
48     private static SharedPreferences mSp;
49     
50     private final Context mContext;
51     private final Category mCategory;
52     private ArrayList<AppDetails> mDisplayedList;
53
54     private static class ViewHolder {
55         public TextView appName;
56         public TextView appPackageName;
57         public ImageView appIcon;
58         public ImageView systemAppIcon;
59         public LinearLayout groupIcons;
60         public ImageView[] groupIconsView;
61     }
62
63     public AppListArrayAdapter(Context context, Category category) {
64         super(context, R.layout.app_list_item_view);
65         mContext = context;
66         mCategory = category;
67         mDisplayedList = new ArrayList<AppDetails>();
68         if (mSp == null)
69             mSp = PreferenceManager.getDefaultSharedPreferences(mContext);
70         refreshVisibleList();
71     }
72
73     private void refreshVisibleList() {
74         boolean includeSystemApps = mSp.getBoolean(
75                 AppListLoader.PREF_INCLUDE_SYSTEM_APPS, true);
76         
77         mDisplayedList.clear();
78         for (AppDetails app : mCategory.getRelatedApps()) {
79             if (isVisible(app, includeSystemApps))
80                 mDisplayedList.add(app);
81         }
82         mCategory.updateVisibleCount(mDisplayedList.size());
83     }
84
85     @Override
86     public void notifyDataSetChanged() {
87         refreshVisibleList();
88         super.notifyDataSetChanged();
89     }
90     
91     @Override
92     public AppDetails getItem(int position) {
93         return mDisplayedList.get(position);
94     }
95     
96     @Override
97     public int getCount() {
98         return mDisplayedList.size();
99     }
100     
101     private boolean isVisible(AppDetails item, boolean includeSystemApps) {
102
103         return !item.isSystemApp() 
104                 || (includeSystemApps && item.isSystemApp());
105     }
106     
107     @SuppressWarnings("deprecation")
108     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
109     @Override
110     public View getView(int position, View convertView, ViewGroup parent) {
111         ViewHolder holder;
112         if (convertView == null) {
113             LayoutInflater inflater = (LayoutInflater) mContext
114                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
115             convertView = inflater.inflate(R.layout.app_list_item_view, parent,
116                     false);
117             holder = new ViewHolder();
118             holder.appName = (TextView) convertView
119                     .findViewById(R.id.list_app_name);
120             holder.appPackageName = (TextView) convertView
121                     .findViewById(R.id.list_app_package);
122             holder.appIcon = (ImageView) convertView
123                     .findViewById(R.id.list_app_icon);
124             holder.systemAppIcon = (ImageView) convertView
125                     .findViewById(R.id.list_system_app_icon);
126             holder.groupIcons = (LinearLayout) convertView
127                     .findViewById(R.id.app_list_group_icons);
128             
129             int i = 0;
130             holder.groupIconsView = new ImageView[mCategory.getSubCategories().size()];
131             for (Subcategory category : mCategory.getSubCategories()){
132                 ImageView icon = new ImageView(mContext);
133                 icon.setImageDrawable(category.getDarkIcon());
134                 holder.groupIconsView[i++] = icon;
135                 holder.groupIcons.addView(icon);
136             }
137             convertView.setTag(holder);
138         } else
139             holder = (ViewHolder) convertView.getTag();
140
141         AppDetails details = getItem(position);
142         holder.appName.setText(details.getAppName());
143         holder.appPackageName.setText(details.getAppPackageName());
144         holder.appIcon.setImageDrawable(details.getAppIcon());
145
146         // disabling and enabling apps in system application manager is available since API 4.0
147         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
148             if (details.isEnabled()) {
149                 holder.appName.setTextColor(mContext.getResources().getColor(android.R.color.black));
150             } else {
151                 holder.appName.setTextColor(mContext.getResources().getColor(R.color.disabled_app_name));
152             }
153         }
154         if(details.isSystemApp()){
155             holder.systemAppIcon.setVisibility(View.VISIBLE);
156             if(details.isEnabled()){
157                 holder.systemAppIcon.setImageDrawable(Utils.getmSystemIcon(mContext));
158             } else {
159                 holder.systemAppIcon.setImageDrawable(Utils.getmSystemIconDisable(mContext));
160             }
161         } else {
162             holder.systemAppIcon.setVisibility(View.INVISIBLE);
163         }
164
165         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
166             convertView.setBackgroundDrawable(mContext
167                     .getResources()
168                     .getDrawable(
169                             details.isSystemApp() ? R.color.grayout_list_item_bg
170                                     : android.R.color.white));
171         else {
172             convertView.setBackground(mContext
173                     .getResources()
174                     .getDrawable(
175                             details.isSystemApp() ? R.color.grayout_list_item_bg
176                                     : android.R.color.white));
177         }
178         for (int i = 0; i < mCategory.getSubCategories().size(); ++i) {
179             if (details.isInSubcategory(mCategory.getSubCategories().get(i).getId())) {
180                 // disabling and enabling apps in system application manager is available since API 4.0
181                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
182                     if (details.isEnabled())
183                         holder.groupIconsView[i].setImageDrawable(mCategory.getSubCategories().get(i).getDarkIcon());
184                     else
185                         holder.groupIconsView[i].setImageDrawable(mCategory.getSubCategories().get(i).getDisabledIcon());
186                 }
187                 holder.groupIconsView[i].setVisibility(View.VISIBLE);
188             } else {
189                 holder.groupIconsView[i].setVisibility(View.GONE);
190             }
191         }
192         return convertView;
193     }
194     
195 }