Skip to content

Commit

Permalink
Refactoring (get rid of private methods/params) to allow extending
Browse files Browse the repository at this point in the history
  • Loading branch information
shehabic committed Mar 3, 2015
1 parent c95da83 commit 262abd9
Showing 1 changed file with 32 additions and 45 deletions.
77 changes: 32 additions & 45 deletions library/src/main/java/com/shehabic/droppy/DroppyMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.support.v7.internal.view.menu.MenuBuilder;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
Expand All @@ -25,15 +22,17 @@
* Created by shehabic on 2/28/15.
*/
public class DroppyMenu {
private WindowManager mWindowManager;
private Context mContext;
private View anchor;
private List<DroppyMenuItemInterface> menuItems = new ArrayList<DroppyMenuItemInterface>();
private View mContentView;
private View mPopupView;
private DroppyClickCallbackInterface droppyClickCallbackInterface;
private int popupMenuLayoutResourceId;
private FrameLayout modalWindow;
protected Context mContext;
protected View anchor;
protected List<DroppyMenuItemInterface> menuItems = new ArrayList<DroppyMenuItemInterface>();
protected View mContentView;
protected View mPopupView;
protected DroppyClickCallbackInterface droppyClickCallbackInterface;
protected int popupMenuLayoutResourceId;
protected FrameLayout modalWindow;
protected int mPopupWidth;
protected int mPopupHeight;
protected int statusBarHeight = -1;

private DroppyMenu(
Context mContext,
Expand All @@ -51,7 +50,6 @@ private DroppyMenu(
popupMenuLayoutResourceId = R.layout.droppy_menu;
}
this.popupMenuLayoutResourceId = popupMenuLayoutResourceId;
mWindowManager = (WindowManager) mContext.getSystemService(mContext.WINDOW_SERVICE);
if (addTriggerOnAnchorClick) {
anchor.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -62,7 +60,7 @@ public void onClick(View v) {
}
}

private Activity getActivity() {
protected Activity getActivity() {
return (Activity) mContext;
}

Expand All @@ -80,7 +78,7 @@ public DroppyMenuItemInterface getMenuItemById(int id) {
return null;
}

public void addModal() {
protected void addModal() {
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
lp.leftMargin = 0;
lp.topMargin = 0;
Expand Down Expand Up @@ -119,12 +117,12 @@ public void notifyChange() {
render(true);
}

void render() {
protected void render() {
render(false);
}

void render(boolean forceRender) {
if (mPopupView == null || forceRender == true) {
protected void render(boolean forceRender) {
if (mPopupView == null || forceRender) {
if (mPopupView != null && ((ViewGroup) mPopupView).getChildCount() > 0) {
((ViewGroup) mPopupView).removeAllViews();
}
Expand All @@ -143,16 +141,14 @@ void render(boolean forceRender) {
mPopupHeight = mPopupView.getMeasuredHeight();
}

void callOnClick(final View v, final int id) {
protected void callOnClick(final View v, final int id) {
if (this.droppyClickCallbackInterface != null) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mPopupView.getLayoutParams();

droppyClickCallbackInterface.call(v, id);
dismiss();
}
}

void addMenuItemView(DroppyMenuItemInterface menuItem, final int id) {
protected void addMenuItemView(DroppyMenuItemInterface menuItem, final int id) {
final View menuItemView = menuItem.render(mContext);
if (menuItem.getId() == -1) {
menuItem.setId(id);
Expand All @@ -168,27 +164,22 @@ public void onClick(View v) {
((ViewGroup) mPopupView.findViewById(R.id.droppyMenu)).addView(menuItemView);
}

private int mPopupWidth;
private int mPopupHeight;
protected int statusBarHeight = -1;

protected Point getScreenSize() {
Point size = new Point();
((Activity) anchor.getContext()).getWindowManager().getDefaultDisplay().getSize(size);

return size;
}

protected boolean isTranslucentStatusBar() {
Window w = ((Activity) anchor.getContext()).getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
int flags = lp.flags;
if ((flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) == WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) {
return true;
}
return false;

return (flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) == WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
}

public int getStatusBarHeight() {
protected int getStatusBarHeight() {
if (statusBarHeight == -1 && isTranslucentStatusBar()) {
statusBarHeight = 0;
} else if (statusBarHeight == -1) {
Expand All @@ -209,11 +200,7 @@ protected Point getAnchorCoordinates() {
return new Point(coords[0], coords[1] - getStatusBarHeight());
}

private void adjustDropDownPosition(FrameLayout.LayoutParams params) {
adjustDropDownPosition(params, 0, 0);
}

private void adjustDropDownPosition(FrameLayout.LayoutParams params, int xOffset, int yOffset) {
protected void adjustDropDownPosition(FrameLayout.LayoutParams params, int xOffset, int yOffset) {
Point p = getAnchorCoordinates();
int finalX = p.x + xOffset;
final int anchorHeight = anchor.getHeight();
Expand All @@ -234,20 +221,18 @@ private void adjustDropDownPosition(FrameLayout.LayoutParams params, int xOffset
params.gravity = Gravity.LEFT | Gravity.TOP;
}


private class PopupViewContainer extends FrameLayout {
protected class PopupViewContainer extends FrameLayout {
public PopupViewContainer(Context context) {
super(context);
}
}


public static class Builder {
private Context ctx;
private View parentMenuItem;
private List<DroppyMenuItemInterface> menuItems = new ArrayList<DroppyMenuItemInterface>();
private DroppyClickCallbackInterface callbackInterface;
private boolean triggerOnAnchorClick = true;
protected Context ctx;
protected View parentMenuItem;
protected List<DroppyMenuItemInterface> menuItems = new ArrayList<DroppyMenuItemInterface>();
protected DroppyClickCallbackInterface callbackInterface;
protected boolean triggerOnAnchorClick = true;

public Builder(Context ctx, View parentMenuItem) {
this.ctx = ctx;
Expand Down Expand Up @@ -305,14 +290,16 @@ public Builder fromMenu(int menuResourceId) {
return this;
}

private Menu newMenuInstance(Context context) {
protected Menu newMenuInstance(Context context) {
try {
Class<?> menuBuilderClass = Class.forName("com.android.internal.view.menu.MenuBuilder");
Constructor<?> constructor = menuBuilderClass.getDeclaredConstructor(Context.class);

return (Menu) constructor.newInstance(context);
} catch (Exception e) {

}

return null;
}

Expand Down

0 comments on commit 262abd9

Please sign in to comment.