package org.mtransit.android.ui.widget;

import java.lang.ref.WeakReference;

import org.mtransit.android.commons.MTLog;

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;

public class ListViewSwipeRefreshLayout extends SwipeRefreshLayout implements MTLog.Loggable {

	private static final String TAG = ListViewSwipeRefreshLayout.class.getSimpleName();

	@Override
	public String getLogTag() {
		return TAG;
	}

	private boolean refreshEnabled = true;
	private WeakReference<AbsListView> listViewWR;
	private WeakReference<View> loadingViewWR;
	private WeakReference<View> emptyViewWR;

	public ListViewSwipeRefreshLayout(Context context) {
		super(context);
	}

	public ListViewSwipeRefreshLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setListViewWR(AbsListView listView) {
		this.listViewWR = new WeakReference<AbsListView>(listView);
	}

	public void setLoadingViewWR(View loadingView) {
		this.loadingViewWR = new WeakReference<View>(loadingView);
	}

	public void setEmptyViewWR(View emptyView) {
		this.emptyViewWR = new WeakReference<View>(emptyView);
	}

	public void setRefreshEnabled(boolean refreshEnabled) {
		this.refreshEnabled = refreshEnabled;
	}

	@Override
	public boolean canChildScrollUp() {
		if (!this.refreshEnabled) {
			return true;
		}
		AbsListView listView = this.listViewWR == null ? null : this.listViewWR.get();
		if (listView != null && listView.getVisibility() == View.VISIBLE) {
			return canListViewScrollUp(listView);
		}
		View loadingView = this.loadingViewWR == null ? null : this.loadingViewWR.get();
		if (loadingView != null && loadingView.getVisibility() == View.VISIBLE) {
			return true;
		}
		View emptyView = this.emptyViewWR == null ? null : this.emptyViewWR.get();
		if (emptyView != null && emptyView.getVisibility() == View.VISIBLE) {
			return canViewScrollUp(emptyView);
		}
		return super.canChildScrollUp();
	}

	private static boolean canListViewScrollUp(AbsListView listView) {
		if (android.os.Build.VERSION.SDK_INT >= 14) {
			return ViewCompat.canScrollVertically(listView, -1);
		} else {
			return listView.getChildCount() > 0 && (listView.getFirstVisiblePosition() > 0 || listView.getChildAt(0).getTop() < listView.getPaddingTop());
		}
	}

	public boolean canViewScrollUp(View view) {
		if (android.os.Build.VERSION.SDK_INT < 14) {
			if (view instanceof AbsListView) {
				AbsListView absListView = (AbsListView) view;
				return absListView.getChildCount() > 0
						&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0).getTop() < absListView.getPaddingTop());
			} else {
				return view.getScrollY() > 0;
			}
		} else {
			return ViewCompat.canScrollVertically(view, -1);
		}
	}
}

@@ -151,51 +151,8 @@
 xt;%0A
-import android.support.v4.view.ViewCompat;%0A
 impo
@@ -2107,702 +2107,111 @@
 %7B%0A%09%09
-if (android.os.Build.VERSION.SDK_INT %3E= 14) %7B%0A%09%09%09return ViewCompat.canScrollVertically(listView, -1);%0A%09%09%7D else %7B%0A%09%09%09return listView.getChildCount() %3E 0 && (listView.getFirstVisiblePosition() %3E 0 %7C%7C listView.getChildAt(0).getTop() %3C listView.getPaddingTop());%0A%09%09%7D%0A%09%7D%0A%0A%09public boolean canViewScrollUp(View view) %7B%0A%09%09if (android.os.Build.VERSION.SDK_INT %3C 14) %7B%0A%09%09%09if (view instanceof AbsListView) %7B%0A%09%09%09%09AbsListView absListView = (AbsListView) view;%0A%09%09%09%09return absListView.getChildCount() %3E 0%0A%09%09%09%09%09%09&& (absListView.getFirstVisiblePosition() %3E 0 %7C%7C absListView.getChildAt(0).getTop() %3C absListView.getPaddingTop());%0A%09%09%09%7D else %7B%0A%09%09%09%09return view.getScrollY() %3E 0;%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09return ViewCompat
+return listView.canScrollVertically(-1);%0A%09%7D%0A%0A%09public boolean canViewScrollUp(View view) %7B%0A%09%09return view
 .can
@@ -2231,22 +2231,12 @@
 lly(
-view, -1);%0A%09%09%7D
+-1);
 %0A%09%7D%0A

Fix lint: ObsoleteSdkInt