/*
 * Copyright (c) 2011 Kurt Aaholst <kaaholst@gmail.com>
 *
 * 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 uk.org.ngo.squeezer.framework;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

import uk.org.ngo.squeezer.R;
import uk.org.ngo.squeezer.menu.MenuFragment;
import uk.org.ngo.squeezer.menu.SqueezerMenuFragment;
import uk.org.ngo.squeezer.service.SqueezeService;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

/**
 * <p>
 * This class defines the common minimum, which any activity browsing the SqueezeServer's database
 * must implement.
 * </p>
 * @author Kurt Aaholst
 */
public abstract class SqueezerItemListActivity extends SqueezerBaseActivity implements OnScrollListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MenuFragment.add(this, SqueezerMenuFragment.class);
    };

    /**
     * This is called when the service is connected.
     * <p>
     * You must register a callback for {@link SqueezeService} to call when the ordered items
     * from {@link #orderPage(int)} are received from SqueezeServer. This callback must pass
     * these items on to {@link SqueezerItemListAdapter#update(int, int, int, List)}.
     *
     * @throws RemoteException
     */
	protected abstract void registerCallback() throws RemoteException;

	/**
	 * This is called when the service is disconnected.
	 * @throws RemoteException
	 */
	protected abstract void unregisterCallback() throws RemoteException;

    /**
     * Implementations must start an asynchronous fetch of items, when this is called.
     * @throws RemoteException
     * @param start Position in list to start the fetch. Pass this on to {@link SqueezeService}
     */
	protected abstract void orderPage(int start) throws RemoteException;


	private final Set<Integer> orderedPages = new HashSet<Integer>();

	/**
	 * Order page at specified position, if it has not already been ordered.
	 * @param pagePosition
	 */
	public void maybeOrderPage(int pagePosition) {
		if (!listBusy && !orderedPages.contains(pagePosition)) {
			orderedPages.add(pagePosition);
			try {
				orderPage(pagePosition);
			} catch (RemoteException e) {
				Log.e(getTag(), "Error ordering items (" + pagePosition + "): " + e);
			}
		}
	}

	/**
	 * Clear all information about which pages has been ordered, and reorder the first page
	 */
	public void reorderItems() {
		orderedPages.clear();
		maybeOrderPage(0);
	}



	private boolean listBusy;
	public boolean isListBusy() { return listBusy; }

	public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
        case OnScrollListener.SCROLL_STATE_IDLE:
        	listBusy = false;

        	int pageSize = getResources().getInteger(R.integer.PageSize);
        	int pos = (view.getFirstVisiblePosition() / pageSize) * pageSize;
        	int end = view.getFirstVisiblePosition() + view.getChildCount();
        	while (pos < end) {
        		maybeOrderPage(pos);
        		pos += pageSize;
        	}
        	break;
        case OnScrollListener.SCROLL_STATE_FLING:
        case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
        	listBusy = true;
        	break;
        }
 	}

	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
	}

}

@@ -1088,23 +1088,16 @@
 r;%0A%0A/**%0A
- * %3Cp%3E%0A
  * This 
@@ -1161,16 +1161,19 @@
 sing the
+%0A *
  Squeeze
@@ -1189,19 +1189,16 @@
 database
-%0A *
  must im
@@ -1213,12 +1213,8 @@
 %0A * 
-%3C/p%3E
 %0A * 
@@ -3106,18 +3106,16 @@
 0);%0A%09%7D%0A%0A
-%0A%0A
 %09private

Format a comment, clean up whitespace.