-
Notifications
You must be signed in to change notification settings - Fork 0
/
GDMentionsList.java
420 lines (343 loc) · 13.6 KB
/
GDMentionsList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package com.alcubier.skoltialert.mentions;
import greendroid.app.GDActivity;
import greendroid.app.GDListActivity;
import greendroid.image.ChainImageProcessor;
import greendroid.image.ImageProcessor;
import greendroid.image.MaskImageProcessor;
import greendroid.image.ScaleImageProcessor;
import greendroid.widget.ActionBarItem;
import greendroid.widget.ActionBarItem.Type;
import greendroid.widget.AsyncImageView;
import greendroid.widget.LoaderActionBarItem;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import com.alcubier.skoltialert.R;
import com.alcubier.skoltialert.db.DatabaseHelper;
import com.alcubier.skoltialert.service.RefreshService;
import com.alcubier.skoltialert.tracks.Track;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;
public class GDMentionsList extends GDListActivity implements OnScrollListener, OnClickListener {
Track track;
final String TAG = getClass().getName();
private final int REFRESH = 0;
private final int COMPOSE = 1;
final int MAX_MENTIONS = 100;
boolean haymasmentions;
String type;
String dateAlertFrom;
String dateAlertTo;
DatabaseHelper dbHelper;
ArrayList<Mention> mentions;
protected MyAdapter m_adapter;
static long lastIdMention;
//the ViewSwitcher
private ViewSwitcher switcher;
final GDActivity thisActivity = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mentions= new ArrayList<Mention>();
track= new Track();
Bundle bundle = this.getIntent().getExtras();
track.setIdTrack(bundle.getInt("idTrack"));
track.setSearchTrack(bundle.getString("searchTrack"));
track.setTopTrack(bundle.getInt("topTrack"));
track.setLastId(bundle.getLong("lastId"));
type = bundle.getString("type");
if (!(type.equals("LastMentions"))) {
dateAlertFrom = bundle.getString("dateAlertFrom");
dateAlertTo = bundle.getString("dateAlertTo");
}
dbHelper=new DatabaseHelper(this);
lastIdMention = Long.MAX_VALUE;
mentions = LoadMentions(lastIdMention);
initListView();
getListView().setOnScrollListener(this);
getListView().setDivider(null);
getListView().setDividerHeight(0);
addActionBarItem(Type.Refresh, REFRESH);
addActionBarItem(Type.Compose, COMPOSE);
setTitle(track.getSearchTrack());
GoogleAnalyticsTracker.getInstance().trackPageView("GDMentionsList");
}
public void initListView() {
m_adapter = new MyAdapter(this, R.layout.listrowmention, mentions);
if (haymasmentions) {
getListView().removeFooterView(switcher);
}
haymasmentions = false;
if (mentions.size()==MAX_MENTIONS) {
//create the ViewSwitcher in the current context
switcher = new ViewSwitcher(this);
//footer Button: see XML1
Button footer = (Button)View.inflate(this, R.layout.loadmore, null);
//progress View: see XML2
View progress = View.inflate(this, R.layout.progreso, null);
//add the views (first added will show first)
switcher.addView(footer);
switcher.addView(progress);
//add the ViewSwitcher to the footer
getListView().addFooterView(switcher);
haymasmentions = true;
}
setListAdapter(m_adapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.close();
}
@Override /* Load More Button Was Clicked */
public void onClick(View arg0) {
//Ojo que cuando se ponga alguna otra view que soporte Onclick habrá que discernir entre uno y otro
//first view is showing, show the second progress view
switcher.showNext();
//and start background work
new getMoreItems().execute();
}
private static class MyAdapter extends ArrayAdapter<Mention> {
private ArrayList<Mention> mentions;
Date dataNow;
Context context;
static class ViewHolder {
public AsyncImageView imageView;
public TextView hFrom;
public TextView hText;
public TextView hDate;
public LinearLayout hMention;
}
private LayoutInflater mInflater;
private ImageProcessor mImageProcessor;
public MyAdapter(Context context, int textViewResourceId, ArrayList<Mention> mentions) {
super(context, textViewResourceId, mentions);
this.mentions = mentions;
this.context = context;
dataNow = new Date();
mInflater = LayoutInflater.from(context);
// mImageForPosition = context.getString(R.string.image_for_position);
prepareImageProcessor(context);
}
private void prepareImageProcessor(Context context) {
final int thumbnailSize = context.getResources().getDimensionPixelSize(R.dimen.thumbnail_size);
final int thumbnailRadius = context.getResources().getDimensionPixelSize(R.dimen.thumbnail_radius);
//REDONDEADO
//@formatter:off
mImageProcessor = new ChainImageProcessor(
new ScaleImageProcessor(thumbnailSize, thumbnailSize, ScaleType.FIT_XY),
new MaskImageProcessor(thumbnailRadius));
//@formatter:on
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Date dataMention;
SimpleDateFormat dateFormat;
String horaString;
String diaNow;
String diaString;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listrowmention, parent, false);
holder = new ViewHolder();
holder.imageView = (AsyncImageView) convertView.findViewById(R.id.async_image);
holder.imageView.setImageProcessor(mImageProcessor);
holder.hFrom = (TextView) convertView.findViewById(R.id.colWhoMention);
holder.hDate = (TextView) convertView.findViewById(R.id.colDateMention);
holder.hText = (TextView) convertView.findViewById(R.id.colTxtMention);
holder.hMention = (LinearLayout) convertView.findViewById(R.id.one_mention);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Mention mention = mentions.get(position);
if (lastIdMention > mention.getLastId()) {
lastIdMention = mention.getLastId();
}
holder.imageView.setUrl(mention.getGifo());
holder.hFrom.setText(mention.getFromUser());
// holder.hDate.setText(mention.getDateMentions());
dataMention = Utils.String2Date(mention.getDateMentions(),"yyyy-MM-dd HH:mm:ss");
dateFormat = new SimpleDateFormat("HH:mm");
horaString = dateFormat.format(dataMention);
dateFormat = new SimpleDateFormat("dd");
diaNow = dateFormat.format(dataNow);
diaString = dateFormat.format(dataMention);
Integer difer = Integer.parseInt(diaNow) - Integer.parseInt(diaString);
if (difer==0) {
holder.hDate.setText(context.getString(R.string.today_date) + " " + horaString);
} else {
if (difer==1) {
holder.hDate.setText(context.getString(R.string.yesterday_date) + " " + horaString);
} else {
dateFormat = new SimpleDateFormat("dd-MMM HH:mm");
holder.hDate.setText(dateFormat.format(dataMention));
}
}
holder.hText.setText(mention.getText().replaceAll(""","\\\"").replaceAll("&","&").replaceAll(">",">").replaceAll("<","<"));
holder.hMention.setOnClickListener(new VerTweet(mention));
return convertView;
}
public class VerTweet implements View.OnClickListener
{
Mention mention;
public VerTweet ( Mention mention) {
this.mention = mention;
}
public void onClick( View view ){
Bundle bundle = new Bundle();
//Le ponemos la variable parametro con el contenido test
bundle.putInt("idTrack", mention.getIdTrack());
bundle.putString("Gifo", mention.getGifo());
bundle.putString("From", mention.getFromUser());
bundle.putString("Date", mention.getDateMentions());
bundle.putString("Text", mention.getText());
bundle.putLong("Status", mention.getLastId());
Intent i = new Intent(getContext(), Tweet.class);
i.putExtras(bundle);
getContext().startActivity(i);
}
}
}
public void onScroll(AbsListView arg0, int firstVisible, int visibleCount, int totalCount) {
// boolean loadMore = /* maybe add a padding */
// firstVisible + visibleCount >= totalCount;
// if(loadMore) {
// Toast.makeText(this, "Hemos llegado al final1",Toast.LENGTH_SHORT).show();
// }
}
public void onScrollStateChanged(AbsListView listView, int scrollState) {
if (getListView() == listView) {
searchAsyncImageViews(listView, scrollState == OnScrollListener.SCROLL_STATE_FLING);
}
}
private void searchAsyncImageViews(ViewGroup viewGroup, boolean pause) {
final int childCount = viewGroup.getChildCount();
for (int i = 0; i < childCount; i++) {
AsyncImageView image = (AsyncImageView) viewGroup.getChildAt(i).findViewById(R.id.async_image);
if (image != null) {
image.setPaused(pause);
}
}
}
public ArrayList<Mention> LoadMentions(long NextMention)
{
try
{
if (type.equals("LastMentions")) {
mentions = dbHelper.ListAllMentions(track.getIdTrack(), NextMention, MAX_MENTIONS);
} else {
mentions = dbHelper.ListAlertMentions(track.getIdTrack(), dateAlertFrom, dateAlertTo, NextMention, MAX_MENTIONS);
}
return mentions;
}
catch(Exception ex)
{
Log.i(TAG,"excepcion loadMentions: "+ex);
return null;
}
}
/** Background Task To Get More Items**/
private class getMoreItems extends AsyncTask<Void, Void, Object> {
@Override
protected Object doInBackground(Void... arg0) {
//code to add more items
//...
if (haymasmentions) {
mentions = LoadMentions(lastIdMention);
}
else {
mentions = null;
}
return null;
}
@Override /* Background Task is Done */
protected void onPostExecute(Object result) {
//go back to the first view
if (haymasmentions) {
if(mentions != null && mentions.size() > 0) {
for(int i=0;i<mentions.size();i++) m_adapter.add(mentions.get(i));
m_adapter.notifyDataSetChanged();
}
if ((mentions != null && mentions.size() < MAX_MENTIONS) || mentions == null) {
haymasmentions = false;
getListView().removeFooterView(switcher);
} else {
switcher.showPrevious();
}
}
//update the ListView
}
}
@Override
public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
Bundle bundle;
switch (item.getItemId()) {
case REFRESH:
// new getMoreMentions().execute();
bundle = new Bundle();
bundle.putString("type", "refreshone");
bundle.putString("track", track.getSearchTrack());
Intent refresh = new Intent(this, RefreshService.class);
refresh.putExtras(bundle);
refresh.putExtra(RefreshService.EXTRA_MESSENGER, new Messenger(handler));
startService(refresh);
break;
case COMPOSE:
bundle = new Bundle();
bundle.putString("type", "newTweet");
Intent i = new Intent(this, WriteTweet.class);
i.putExtras(bundle);
startActivity(i);
break;
default:
return super.onHandleActionBarItemClick(item, position);
}
return true;
}
private Handler handler=new Handler() {
@Override
public void handleMessage(Message msg) {
new getMoreMentions().execute();
}
};
/** Background Task To Get More Items**/
private class getMoreMentions extends AsyncTask<Void, Void, Object> {
@Override
protected Object doInBackground(Void... arg0) {
//code to add more items
//...
lastIdMention = Long.MAX_VALUE;
mentions = LoadMentions(lastIdMention);
return null;
}
@Override /* Background Task is Done */
protected void onPostExecute(Object result) {
//go back to the first view
initListView();
m_adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), getString(R.string.download_done), Toast.LENGTH_SHORT).show();
((LoaderActionBarItem) getActionBar().getItem(REFRESH)).setLoading(false);
//update the ListView
}
}
}