package com.actionbarsherlock.internal.widget;
import com.actionbarsherlock.R;
import android.content.Context;
import android.content.res.Resources;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
public class {
private static final int EXPAND_LIST_TIMEOUT = 250;
private Context mContext;
private PopupWindow mPopup;
private ListAdapter mAdapter;
private DropDownListView mDropDownList;
private int mDropDownHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
private int mDropDownWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
private int mDropDownHorizontalOffset;
private int mDropDownVerticalOffset;
private boolean mDropDownVerticalOffsetSet;
private int mListItemExpandMaximum = Integer.MAX_VALUE;
private View mPromptView;
private int mPromptPosition = POSITION_PROMPT_ABOVE;
private DataSetObserver mObserver;
private View mDropDownAnchorView;
private Drawable mDropDownListHighlight;
private AdapterView.OnItemClickListener mItemClickListener;
private AdapterView.OnItemSelectedListener mItemSelectedListener;
private final ResizePopupRunnable mResizePopupRunnable = new ResizePopupRunnable();
private final PopupTouchInterceptor mTouchInterceptor = new PopupTouchInterceptor();
private final PopupScrollListener mScrollListener = new PopupScrollListener();
private final ListSelectorHider mHideSelector = new ListSelectorHider();
private Handler mHandler = new Handler();
private Rect mTempRect = new Rect();
private boolean mModal;
public static final int POSITION_PROMPT_ABOVE = 0;
public static final int POSITION_PROMPT_BELOW = 1;
public (Context context) {
this(context, null, R.attr.listPopupWindowStyle);
}
public (Context context, AttributeSet attrs, int defStyleAttr) {
mContext = context;
mPopup = new PopupWindow(context, attrs, defStyleAttr);
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
}
public (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
mContext = context;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
Context wrapped = new ContextThemeWrapper(context, defStyleRes);
mPopup = new PopupWindow(wrapped, attrs, defStyleAttr);
} else {
mPopup = new PopupWindow(context, attrs, defStyleAttr, defStyleRes);
}
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
}
if (mObserver == null) {
mObserver = new PopupDataSetObserver();
} else if (mAdapter != null) {
mAdapter.unregisterDataSetObserver(mObserver);
}
mAdapter = adapter;
if (mAdapter != null) {
adapter.registerDataSetObserver(mObserver);
}
if (mDropDownList != null) {
mDropDownList.setAdapter(mAdapter);
}
}
mPromptPosition = position;
}
mModal = true;
mPopup.setFocusable(modal);
}
mPopup.setBackgroundDrawable(d);
}
mDropDownAnchorView = anchor;
}
mDropDownHorizontalOffset = offset;
}
mDropDownVerticalOffset = offset;
mDropDownVerticalOffsetSet = true;
}
Drawable popupBackground = mPopup.getBackground();
if (popupBackground != null) {
popupBackground.getPadding(mTempRect);
mDropDownWidth = mTempRect.left + mTempRect.right + width;
} else {
mDropDownWidth = width;
}
}
mItemClickListener = clickListener;
}
int height = buildDropDown();
int widthSpec = 0;
int heightSpec = 0;
boolean noInputMethod = isInputMethodNotNeeded();
if (mPopup.isShowing()) {
if (mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT) {
widthSpec = -1;
} else if (mDropDownWidth == ViewGroup.LayoutParams.WRAP_CONTENT) {
widthSpec = mDropDownAnchorView.getWidth();
} else {
widthSpec = mDropDownWidth;
}
if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
heightSpec = noInputMethod ? height : ViewGroup.LayoutParams.MATCH_PARENT;
if (noInputMethod) {
mPopup.setWindowLayoutMode(
mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT ?
ViewGroup.LayoutParams.MATCH_PARENT : 0, 0);
} else {
mPopup.setWindowLayoutMode(
mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT ?
ViewGroup.LayoutParams.MATCH_PARENT : 0,
ViewGroup.LayoutParams.MATCH_PARENT);
}
} else if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
heightSpec = height;
} else {
heightSpec = mDropDownHeight;
}
mPopup.setOutsideTouchable(true);
mPopup.update(mDropDownAnchorView, mDropDownHorizontalOffset,
mDropDownVerticalOffset, widthSpec, heightSpec);
} else {
if (mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT) {
widthSpec = ViewGroup.LayoutParams.MATCH_PARENT;
} else {
if (mDropDownWidth == ViewGroup.LayoutParams.WRAP_CONTENT) {
mPopup.setWidth(mDropDownAnchorView.getWidth());
} else {
mPopup.setWidth(mDropDownWidth);
}
}
if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
heightSpec = ViewGroup.LayoutParams.MATCH_PARENT;
} else {
if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
mPopup.setHeight(height);
} else {
mPopup.setHeight(mDropDownHeight);
}
}
mPopup.setWindowLayoutMode(widthSpec, heightSpec);
mPopup.setOutsideTouchable(true);
mPopup.setTouchInterceptor(mTouchInterceptor);
mPopup.showAsDropDown(mDropDownAnchorView,
mDropDownHorizontalOffset, mDropDownVerticalOffset);
mDropDownList.setSelection(ListView.INVALID_POSITION);
if (!mModal || mDropDownList.isInTouchMode()) {
clearListSelection();
}
if (!mModal) {
mHandler.post(mHideSelector);
}
}
}
mPopup.dismiss();
if (mPromptView != null) {
final ViewParent parent = mPromptView.getParent();
if (parent instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) parent;
group.removeView(mPromptView);
}
}
mPopup.setContentView(null);
mDropDownList = null;
mHandler.removeCallbacks(mResizePopupRunnable);
}
public void (PopupWindow.OnDismissListener listener) {
mPopup.setOnDismissListener(listener);
}
mPopup.setInputMethodMode(mode);
}
final DropDownListView list = mDropDownList;
if (list != null) {
list.mListSelectionHidden = true;
list.requestLayout();
}
}
return mPopup.isShowing();
}
return mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED;
}
return mDropDownList;
}
ViewGroup dropDownView;
int otherHeights = 0;
if (mDropDownList == null) {
Context context = mContext;
mDropDownList = new DropDownListView(context, !mModal);
if (mDropDownListHighlight != null) {
mDropDownList.setSelector(mDropDownListHighlight);
}
mDropDownList.setAdapter(mAdapter);
mDropDownList.setOnItemClickListener(mItemClickListener);
mDropDownList.setFocusable(true);
mDropDownList.setFocusableInTouchMode(true);
mDropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
int position, long id) {
if (position != -1) {
DropDownListView dropDownList = mDropDownList;
if (dropDownList != null) {
dropDownList.mListSelectionHidden = false;
}
}
}
}
});
mDropDownList.setOnScrollListener(mScrollListener);
if (mItemSelectedListener != null) {
mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
}
dropDownView = mDropDownList;
View hintView = mPromptView;
if (hintView != null) {
LinearLayout hintContainer = new LinearLayout(context);
hintContainer.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f
);
switch (mPromptPosition) {
case POSITION_PROMPT_BELOW:
hintContainer.addView(dropDownView, hintParams);
hintContainer.addView(hintView);
break;
case POSITION_PROMPT_ABOVE:
hintContainer.addView(hintView);
hintContainer.addView(dropDownView, hintParams);
break;
default:
break;
}
int widthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.AT_MOST);
int heightSpec = MeasureSpec.UNSPECIFIED;
hintView.measure(widthSpec, heightSpec);
hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin
+ hintParams.bottomMargin;
dropDownView = hintContainer;
}
mPopup.setContentView(dropDownView);
} else {
dropDownView = (ViewGroup) mPopup.getContentView();
final View view = mPromptView;
if (view != null) {
LinearLayout.LayoutParams hintParams =
(LinearLayout.LayoutParams) view.getLayoutParams();
otherHeights = view.getMeasuredHeight() + hintParams.topMargin
+ hintParams.bottomMargin;
}
}
int padding = 0;
Drawable background = mPopup.getBackground();
if (background != null) {
background.getPadding(mTempRect);
padding = mTempRect.top + mTempRect.bottom;
if (!mDropDownVerticalOffsetSet) {
mDropDownVerticalOffset = -mTempRect.top;
}
}
boolean ignoreBottomDecorations =
mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED;
final int maxHeight = getMaxAvailableHeight(
mDropDownAnchorView, mDropDownVerticalOffset, ignoreBottomDecorations);
if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
return maxHeight + padding;
}
final int listContent = measureHeightOfChildren(MeasureSpec.UNSPECIFIED,
0, -1, maxHeight - otherHeights, -1);
if (listContent > 0) otherHeights += padding;
return listContent + otherHeights;
}
final Rect displayFrame = new Rect();
anchor.getWindowVisibleDisplayFrame(displayFrame);
final int[] anchorPos = new int[2];
anchor.getLocationOnScreen(anchorPos);
int bottomEdge = displayFrame.bottom;
if (ignoreBottomDecorations) {
Resources res = anchor.getContext().getResources();
bottomEdge = res.getDisplayMetrics().heightPixels;
}
final int distanceToBottom = bottomEdge - (anchorPos[1] + anchor.getHeight()) - yOffset;
final int distanceToTop = anchorPos[1] - displayFrame.top + yOffset;
int returnedHeight = Math.max(distanceToBottom, distanceToTop);
if (mPopup.getBackground() != null) {
mPopup.getBackground().getPadding(mTempRect);
returnedHeight -= mTempRect.top + mTempRect.bottom;
}
return returnedHeight;
}
final int maxHeight, int disallowPartialChildPosition) {
final ListAdapter adapter = mAdapter;
if (adapter == null) {
return mDropDownList.getListPaddingTop() + mDropDownList.getListPaddingBottom();
}
int returnedHeight = mDropDownList.getListPaddingTop() + mDropDownList.getListPaddingBottom();
final int dividerHeight = ((mDropDownList.getDividerHeight() > 0) && mDropDownList.getDivider() != null) ? mDropDownList.getDividerHeight() : 0;
int prevHeightWithoutPartialChild = 0;
int i;
View child;
endPosition = (endPosition == -1) ? adapter.getCount() - 1 : endPosition;
for (i = startPosition; i <= endPosition; ++i) {
child = mAdapter.getView(i, null, mDropDownList);
if (mDropDownList.getCacheColorHint() != 0) {
child.setDrawingCacheBackgroundColor(mDropDownList.getCacheColorHint());
}
measureScrapChild(child, i, widthMeasureSpec);
if (i > 0) {
returnedHeight += dividerHeight;
}
returnedHeight += child.getMeasuredHeight();
if (returnedHeight >= maxHeight) {
return (disallowPartialChildPosition >= 0)
&& (i > disallowPartialChildPosition)
&& (prevHeightWithoutPartialChild > 0)
&& (returnedHeight != maxHeight)
? prevHeightWithoutPartialChild
: maxHeight;
}
if ((disallowPartialChildPosition >= 0) && (i >= disallowPartialChildPosition)) {
prevHeightWithoutPartialChild = returnedHeight;
}
}
return returnedHeight;
}
ListView.LayoutParams p = (ListView.LayoutParams) child.getLayoutParams();
if (p == null) {
p = new ListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0);
child.setLayoutParams(p);
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthMeasureSpec,
mDropDownList.getPaddingLeft() + mDropDownList.getPaddingRight(), p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
child.measure(childWidthSpec, childHeightSpec);
}
private boolean mListSelectionHidden;
private boolean mHijackFocus;
super(context, null, R.attr.dropDownListViewStyle);
mHijackFocus = hijackFocus;
setCacheColorHint(0);
}
@Override
return (mHijackFocus && mListSelectionHidden) || super.isInTouchMode();
}
@Override
return mHijackFocus || super.hasWindowFocus();
}
@Override
return mHijackFocus || super.isFocused();
}
@Override
return mHijackFocus || super.hasFocus();
}
}
private class extends DataSetObserver {
@Override
if (isShowing()) {
show();
}
}
@Override
dismiss();
}
}
clearListSelection();
}
}
private class implements Runnable {
if (mDropDownList != null && mDropDownList.getCount() > mDropDownList.getChildCount() &&
mDropDownList.getChildCount() <= mListItemExpandMaximum) {
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
show();
}
}
}
private class implements OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
final int x = (int) event.getX();
final int y = (int) event.getY();
if (action == MotionEvent.ACTION_DOWN &&
mPopup != null && mPopup.isShowing() &&
(x >= 0 && x < mPopup.getWidth() && y >= 0 && y < mPopup.getHeight())) {
mHandler.postDelayed(mResizePopupRunnable, EXPAND_LIST_TIMEOUT);
} else if (action == MotionEvent.ACTION_UP) {
mHandler.removeCallbacks(mResizePopupRunnable);
}
return false;
}
}
private class implements ListView.OnScrollListener {
public void onScroll(AbsListView view,
int firstVisibleItem,
int visibleItemCount,
int totalItemCount) {
}
if (scrollState == SCROLL_STATE_TOUCH_SCROLL &&
!isInputMethodNotNeeded() && mPopup.getContentView() != null) {
mHandler.removeCallbacks(mResizePopupRunnable);
mResizePopupRunnable.run();
}
}
}
}