summaryrefslogtreecommitdiff
path: root/src/plugins/android.codeutils/templates/activity_samples/endless_list_pull_to_refresh/pull_to_refresh_activity.java
blob: 47d2a171f5f33bee2a251517b47942f22b61eb5e (plain)
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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package #package_name#;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.LauncherActivity.ListItem;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import #ManifestPackageName#.R;

/**
 * Class which holds an example of a endless list. It means a list will be
 * displayed and it will always have items to be displayed. <br>
 * New data is loaded asynchronously in order to provide a good user experience.
 */
public class #class_name# extends ListActivity {

	/**
	 * Adapter for endless list.
	 */
	private EndlessListAdapter arrayAdapter = null;

	/**
	 * The list header (Where is the loading and last updated labels)
	 */
	private LinearLayout listHeader = null;

	/**
	 * Last loaded item
	 */
	private TextView lastUpdated = null;

	/**
	 * Loading progress
	 */
	private ProgressBar loadingProgress = null;

	/**
	 * Just an integer to add items sequentially
	 */
	private int lastAdded = 0;

	/**
	 * Variable which controls when new items are being loaded. If this variable
	 * is true, it means items are being loaded, otherwise it is set to false.
	 */
	private boolean isLoading = false;

	/**
	 * The number of elements which are retrieved every time the service is
	 * called for retrieving elements.
	 */
	private static final int BLOCK_SIZE = 2;

	/**
	 * Property to save the number of items already loaded
	 */
	private static final String PROP_ITEM_COUNT = "item_count";

	/**
	 * Property to save the top most index of the list
	 */
	private static final String PROP_TOP_ITEM = "top_list_item";

	/**
	 * Property to save the time of the last update
	 */
	private static final String PROP_LAST_UPDATED = "last_updated";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		arrayAdapter = new EndlessListAdapter(this, R.layout.#layout_name#endless_list_pull_to_refresh/listrow.xml#,
				new ArrayList<ListElement>());

		listHeader = (LinearLayout) getLayoutInflater().inflate(
				R.layout.#layout_name#endless_list_pull_to_refresh/listheader.xml#, null);
		getListView().addHeaderView(listHeader);
		lastUpdated = (TextView) listHeader.findViewById(R.id.lastUpdated);
		loadingProgress = (ProgressBar) listHeader
				.findViewById(R.id.progressBar);
		loadingProgress.setVisibility(View.GONE);
		lastUpdated.setText(R.string.last_updated);
		lastUpdated.setText(lastUpdated.getText().toString() + DateFormat.format("EEEE, MMMM dd, yyyy",
				Calendar.getInstance()));
		if (savedInstanceState == null
				|| (savedInstanceState != null && savedInstanceState.getInt(
						PROP_ITEM_COUNT, 0) == 0)) {
			// download asynchronously initial list
			Downloaditems downloadAction = new Downloaditems();
			downloadAction.execute(new Integer[] { BLOCK_SIZE });
		}

		setListAdapter(arrayAdapter);
		getListView().setOnTouchListener(new PullEventListener());
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		/*
		 * Save instance loaded items to be restored after rotate the device OR
		 * after you have pressed home button
		 */
		outState.putInt(PROP_ITEM_COUNT, arrayAdapter.getCount());
		for (int i = 0; i < arrayAdapter.getCount(); i++) {
			outState.putSerializable(Integer.toString(i),
					arrayAdapter.getItemAt(i));
		}
		outState.putInt(PROP_TOP_ITEM, getListView().getFirstVisiblePosition());
		outState.putString(PROP_LAST_UPDATED, lastUpdated.getText().toString());
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle state) {
		/*
		 * Restore state. Also restore lastAdded value since this class is a new
		 * instance on restore
		 */
		super.onRestoreInstanceState(state);
		int count = state.getInt(PROP_ITEM_COUNT);
		for (int i = 0; i < count; i++) {
			arrayAdapter.add((ListElement) state.get(Integer.toString(i)));
		}
		lastAdded = count;
		getListView().setSelection(state.getInt(PROP_TOP_ITEM));
		lastUpdated.setText(state.getString(PROP_LAST_UPDATED));
	}

	@Override
	protected void onListItemClick(ListView lv, View v, int position, long id) {
	    int listIndex = position - 1;
	    if (arrayAdapter.getItemAt(listIndex) != null) {
	        //your action here
            Toast.makeText(
                    lv.getContext(),
                    getString(R.string.selected_element_message,
                            arrayAdapter.getItemAt(listIndex).text),
                    Toast.LENGTH_SHORT).show();
        }
	}

	/**
	 * This method represents a service which takes a long time to be executed.
	 * To simulate it, it is inserted a lag of 1 second. <br>
	 * This method basically creates a <i>cache</i> number of
	 * {@link ListElement} and returns it. It creates {@link ListElement}s with
	 * text higher than <i>itemNumber</i>.
	 * 
	 * @param itemNumber
	 *            Basic number to create other elements.
	 * @param numberOfItemsToBeCreated
	 *            Number of items to be created.
	 * 
	 * @return Returns the created list of {@link ListElement}s.
	 */
	private List<ListElement> retrieveItems(int numberOfItemsToBeCreated) {
		List<ListElement> results = new ArrayList<ListElement>();
		try {
			// wait for 2 seconds in order to simulate the long service
			Thread.sleep(2000);
			// create items
			for (int i = 0; i <= numberOfItemsToBeCreated; i++) {
			    String itemToBeAdded = getString(R.string.list_item_number,
                        (lastAdded++));
				results.add(new ListElement(itemToBeAdded, R.drawable.#drawable_name#endless_list_pull_to_refresh/listicon.png#));
			}
		} catch (InterruptedException e) {
			// treat exception here
		}
		return results;
	}

	/**
	 * Listener which handles the endless list. It is responsible for
	 * determining when the long service will be called asynchronously.
	 */

	/**
	 * Asynchronous job call. This class is responsible for calling the long
	 * service and managing the <i>isLoading</i> flag.
	 */
	class Downloaditems extends AsyncTask<Integer, Void, List<ListElement>> {

		// indexes constants
		private static final int NUMBER_OF_ITEMS_TO_BE_CREATED_INDEX = 0;

		@Override
		protected void onPreExecute() {
			// flag loading is being executed
			isLoading = true;
			loadingProgress.setVisibility(View.VISIBLE);
			lastUpdated.setText(R.string.loading_message);
		}

		@Override
		protected List<ListElement> doInBackground(Integer... params) {

			// execute the long service
			return retrieveItems(params[NUMBER_OF_ITEMS_TO_BE_CREATED_INDEX]);
		}

		@Override
		protected void onPostExecute(List<ListElement> result) {
			arrayAdapter.setNotifyOnChange(true);
			for (ListElement item : result) {
				// it is necessary to verify whether the item was already added
				// because this job is called many times asynchronously
				synchronized (arrayAdapter) {
					if (!arrayAdapter.contains(item)) {
						// Add items always in the beginning
						arrayAdapter.insert(item, 0);
					}
				}
			}
			
			loadingProgress.setVisibility(View.GONE);
			lastUpdated.setText(getString(R.string.last_updated,
			        DateFormat.format("EEEE, MMMM dd, yyyy",
                    Calendar.getInstance())));
			// flag the loading is finished
			isLoading = false;
		}
	}

	/**
	 * Adapter which handles the list be be displayed.
	 */
	class EndlessListAdapter extends ArrayAdapter<ListElement> {

		private final Activity context;
		private final List<ListElement> items;
		private final int rowViewId;

		/**
		 * Instantiate the Adapter for an Endless List Activity.
		 * 
		 * @param context
		 *            {@link Activity} which holds the endless list.
		 * @param rowviewId
		 *            Identifier of the View which holds each row of the List.
		 * @param items
		 *            Initial set of items which are added to list being
		 *            displayed.
		 */
		public EndlessListAdapter(Activity context, int rowviewId,
				List<ListElement> items) {
			super(context, rowviewId, items);
			this.context = context;
			this.items = items;
			this.rowViewId = rowviewId;
		}

		/**
		 * Check whether a {@link ListItem} is already in this adapter.
		 * 
		 * @param item
		 *            Item to be verified whether it is in the adapter.
		 * 
		 * @return Returns <code>true</code> in case the {@link ListElement} is
		 *         in the adapter, <code>false</code> otherwise.
		 */
		public boolean contains(ListElement item) {
			return items.contains(item);
		}

		/**
		 * Get a {@link ListElement} at a certain position.
		 * 
		 * @param index
		 *            Position where the {@link ListElement} is retrieved.
		 * 
		 * @return Returns the {@link ListElement} give a certain position.
		 */
		public ListElement getItemAt(int index) {
		    return index < items.size() ? items.get(index) : null;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ImageView imageView;
			TextView textView;

			View rowView = convertView;

			/*
			 * We inflate the row using the determined layout. Also, we fill the
			 * necessary data in the text and image views.
			 */
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(rowViewId, null, true);
			textView = (TextView) rowView.findViewById(R.id.text01);
			imageView = (ImageView) rowView.findViewById(R.id.img01);
			textView.setText(items.get(position).text);
			imageView.setImageResource(items.get(position).imageId);

			return rowView;
		}
	}

	class PullEventListener implements OnTouchListener {

        private float firstEventY;
		private CharSequence previousLastUpdatedText;
		private boolean shouldConsiderRefresh = false;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            // Save the first touch position
            case MotionEvent.ACTION_DOWN:
                shouldConsiderRefresh = getListView().getFirstVisiblePosition() == 0
                        && listHeader.getTop() == 0;
                previousLastUpdatedText = lastUpdated.getText();
                if (shouldConsiderRefresh) {
                    firstEventY = event.getY();
                }
                break;

            // Check if we can refresh with certain delta to update texts
            case MotionEvent.ACTION_MOVE:
                if (shouldConsiderRefresh) {
                    float currentEventY = event.getY();
                    if (currentEventY != firstEventY) {
                        lastUpdated.setText(R.string.pull_to_refresh);
                    }
                    if (shouldRefresh(firstEventY, currentEventY) && !isLoading) {
                        lastUpdated.setText(R.string.release_to_refresh);
                    }
                }
                break;

            // Check if we can refresh with certain delta and go back to the
            // original text because the touch event is finished
            case MotionEvent.ACTION_UP:
                lastUpdated.setText(previousLastUpdatedText);
                if (shouldConsiderRefresh) {
                    float currentEventY = event.getY();
                    if (shouldRefresh(firstEventY, currentEventY) && !isLoading) {
                        lastUpdated.setText(previousLastUpdatedText);
                        Downloaditems downloadAction = new Downloaditems();
                        downloadAction.execute(new Integer[] { BLOCK_SIZE });
                        event.setAction(MotionEvent.ACTION_CANCEL);
                        dispatchTouchEvent(event);
                        shouldConsiderRefresh = false;
                        return true;
                    }
                }
                break;
            }
            return false;
        }

        /**
         * Check if the difference between first and current touch positions are
         * enough to dispatch a refresh
         * 
         * @param firstTapPosition
         * @param currentPosition
         * @return true if the difference is big enough to refresh, false otherwise
         */
        private boolean shouldRefresh(float firstTapPosition,
                float currentPosition) {
            int threshold = getListView().getHeight() / 4;
            return ((currentPosition - firstTapPosition) / 2) > threshold;
        }

    }
}