Revert "New setting: "Small screen" to avoid ellipsizing on small screens"
[f-droid:fdroidclient.git] / src / org / fdroid / fdroid / views / AppListAdapter.java
1 package org.fdroid.fdroid.views;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.content.Context;
7 import android.graphics.Typeface;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.*;
12 import android.text.SpannableString;
13 import android.text.style.StyleSpan;
14 import android.graphics.Bitmap;
15
16 import org.fdroid.fdroid.DB;
17 import org.fdroid.fdroid.Preferences;
18 import org.fdroid.fdroid.R;
19 import org.fdroid.fdroid.compat.LayoutCompat;
20
21 import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
22 import com.nostra13.universalimageloader.core.DisplayImageOptions;
23 import com.nostra13.universalimageloader.core.ImageLoader;
24 import com.nostra13.universalimageloader.core.assist.ImageScaleType;
25
26 abstract public class AppListAdapter extends BaseAdapter {
27
28     private List<DB.App> items = new ArrayList<DB.App>();
29     private Context mContext;
30     private LayoutInflater mInflater;
31     private DisplayImageOptions displayImageOptions;
32
33     public AppListAdapter(Context context) {
34         mContext = context;
35         mInflater = (LayoutInflater) mContext.getSystemService(
36                 Context.LAYOUT_INFLATER_SERVICE);
37
38         displayImageOptions = new DisplayImageOptions.Builder()
39             .cacheInMemory(true)
40             .cacheOnDisc(true)
41             .imageScaleType(ImageScaleType.NONE)
42             .resetViewBeforeLoading(true)
43             .showImageOnLoading(R.drawable.ic_repo_app_default)
44             .showImageForEmptyUri(R.drawable.ic_repo_app_default)
45             .displayer(new FadeInBitmapDisplayer(200, true, true, false))
46             .bitmapConfig(Bitmap.Config.RGB_565)
47             .build();
48
49     }
50
51     abstract protected boolean showStatusUpdate();
52
53     abstract protected boolean showStatusInstalled();
54
55     public void addItem(DB.App app) {
56         items.add(app);
57     }
58
59     public void addItems(List<DB.App> apps) {
60         items.addAll(apps);
61     }
62
63     public void clear() {
64         items.clear();
65     }
66
67     @Override
68     public int getCount() {
69         return items.size();
70     }
71
72     @Override
73     public Object getItem(int position) {
74         return items.get(position);
75     }
76
77     @Override
78     public long getItemId(int position) {
79         return position;
80     }
81
82     private static class ViewHolder {
83         TextView name;
84         TextView summary;
85         TextView status;
86         TextView license;
87         ImageView icon;
88     }
89
90     @Override
91     public View getView(int position, View convertView, ViewGroup parent) {
92
93         boolean compact = Preferences.get().hasCompactLayout();
94         DB.App app = items.get(position);
95         ViewHolder holder;
96
97         if (convertView == null) {
98             convertView = mInflater.inflate(R.layout.applistitem, null);
99
100             holder = new ViewHolder();
101             holder.name = (TextView) convertView.findViewById(R.id.name);
102             holder.summary = (TextView) convertView.findViewById(R.id.summary);
103             holder.status = (TextView) convertView.findViewById(R.id.status);
104             holder.license = (TextView) convertView.findViewById(R.id.license);
105             holder.icon = (ImageView) convertView.findViewById(R.id.icon);
106
107             convertView.setTag(holder);
108         } else {
109             holder = (ViewHolder) convertView.getTag();
110         }
111
112         holder.name.setText(app.name);
113         holder.summary.setText(app.summary);
114
115         layoutIcon(holder.icon, compact);
116         ImageLoader.getInstance().displayImage(app.iconUrl, holder.icon,
117             displayImageOptions);
118
119         holder.status.setText(getVersionInfo(app));
120         holder.license.setText(app.license);
121
122         // Disable it all if it isn't compatible...
123         View[] views = {
124             convertView,
125             holder.status,
126             holder.summary,
127             holder.license,
128             holder.name
129         };
130
131         for (View view : views) {
132             view.setEnabled(app.compatible && !app.filtered);
133         }
134
135         return convertView;
136     }
137
138     private String ellipsize(String input, int maxLength) {
139         if (input == null || input.length() < maxLength+1) {
140             return input;
141         }
142         return input.substring(0, maxLength) + "…";
143     }
144
145     private String getVersionInfo(DB.App app) {
146
147         if (app.curApk == null) {
148             return null;
149         }
150
151         if (app.installedVersion == null) {
152             return ellipsize(app.curApk.version, 12);
153         }
154
155         if (app.toUpdate && showStatusUpdate()) {
156             return ellipsize(app.installedVersion, 8) +
157                 " → " + ellipsize(app.curApk.version, 8);
158         }
159
160         if (app.installedVerCode > 0 && showStatusInstalled()) {
161             return ellipsize(app.installedVersion, 12) + " ✔";
162         }
163
164         return app.installedVersion;
165     }
166
167     private void layoutIcon(ImageView icon, boolean compact) {
168         int size = (int)mContext.getResources().getDimension((compact
169             ? R.dimen.applist_icon_compact_size
170             : R.dimen.applist_icon_normal_size));
171
172         RelativeLayout.LayoutParams params =
173             (RelativeLayout.LayoutParams)icon.getLayoutParams();
174
175         params.height = size;
176         params.width = size;
177
178         icon.setLayoutParams(params);
179     }
180
181 }