/* * Copyright 2012 Tallinn University Centre for Educational Technology * * 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 ee.htk.dippler.app; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.content.Context; import android.database.DataSetObserver; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ListView; import ee.htk.dippler.app.ArrayAdapters.EventArrayAdapter; import ee.htk.dippler.app.entities.Course; import ee.htk.dippler.app.entities.Event; import ee.htk.dippler.app.service.ListingManager; import ee.htk.dippler.app.service.SessionObject; public class ActivitesActivity extends ListActivity { private ListingManager listingManager; private Course course; private List events = new ArrayList(); private EventArrayAdapter mAdapter; private View footerView; private ListView listView; private View progress; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Bundle extras = getIntent().getExtras(); course = extras.getParcelable("dippler-course"); SessionObject session = extras.getParcelable("dippler-session"); listingManager = new ListingManager(getApplicationContext(), session.getId()); setContentView(R.layout.course_listing); progress = findViewById(R.id.progress_wrapper); listView = getListView(); listView.setOnScrollListener(new EndlessScrollListener()); footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false); listView.addFooterView(footerView); mAdapter = new EventArrayAdapter(this, R.layout.list_news, events); setListAdapter(mAdapter); mAdapter.registerDataSetObserver(new DataSetObserver() { @Override public void onChanged() { super.onChanged(); listView.removeFooterView(footerView); if ( progress.isShown() ) { progress.setVisibility(View.GONE); } } }); //Init first set of activites - page 1 new fetchEvents(course.getId()).execute(1); } private class fetchEvents extends AsyncTask> { private int course_id = 0; private int base_limit = 10; public fetchEvents(int course_id) { this.course_id = course_id; } @Override protected List doInBackground(Integer... pages) { int offset = ( (base_limit * pages[0]) - 10 ); int limit = (base_limit * pages[0]); Log.i("dippler-app", "Offset "+ offset+ " limit " +limit); return listingManager.getEvents(course_id, offset, limit ); } @Override protected void onPostExecute(List events_in) { super.onPostExecute(events_in); for (Event event : events_in) { if ( !events.contains(event) ) { events.add(event); } } mAdapter.notifyDataSetChanged(); } } class EndlessScrollListener implements OnScrollListener { private int visibleThreshold = 5; private int currentPage = 1; private int previousTotal = 0; private boolean loading = true; public EndlessScrollListener() { } public EndlessScrollListener(int visibleThreshold) { this.visibleThreshold = visibleThreshold; } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (loading) { if (totalItemCount > previousTotal) { loading = false; previousTotal = totalItemCount; currentPage++; } } if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { //If no footerViews present, add one if ( listView.getFooterViewsCount() == 0) { listView.addFooterView(footerView); } new fetchEvents(course.getId()).execute(currentPage + 1); loading = true; } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } } }