Browse Source

added custom popuptable

Julien Clauter 10 years ago
parent
commit
4ea7ee406c

+ 43 - 0
res/layout/simple_popup_item.xml

@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:padding="2dp"
+    android:gravity="top|center"
+    android:baselineAligned="false">
+
+    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:background="@color/dark_grey"
+        android:padding="10dp">
+
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:text="Title"
+        android:id="@+id/title_text_view"
+        android:textColor="#ffffff"
+        android:layout_alignParentTop="true"
+        android:layout_toLeftOf="@+id/isSelectedButton"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentStart="true"
+        android:layout_alignBottom="@+id/isSelectedButton"
+        android:textAlignment="textStart"
+        android:textStyle="bold" />
+
+    <RadioButton
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:id="@+id/isSelectedButton"
+        android:layout_alignParentTop="true"
+        android:layout_alignParentRight="true"
+        android:layout_alignParentEnd="true"
+        android:clickable="false"
+        android:checked="true"
+        android:enabled="false" />
+
+    </RelativeLayout>
+</RelativeLayout>

+ 20 - 0
res/layout/simple_popup_table.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    >
+
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:textAppearance="?android:attr/textAppearanceLarge"
+        android:text="Title"
+        android:padding="5dp"
+        android:textAlignment="center"
+        android:gravity="top|center"
+        android:id="@+id/title_text_view"
+        android:textColor="#ffffff"
+        android:textStyle="bold" />
+</LinearLayout>

+ 23 - 29
src/de/tudarmstadt/informatik/hostage/ui2/fragment/RecordOverviewFragment.java

@@ -4,21 +4,15 @@ import android.annotation.SuppressLint;
 import android.app.Fragment;
 import android.content.Context;
 import android.content.Intent;
-import android.graphics.Color;
 import android.os.Bundle;
-import android.text.SpannableString;
-import android.text.style.ForegroundColorSpan;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuInflater;
-import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.ExpandableListView;
 import android.widget.ImageButton;
-import android.widget.PopupMenu;
-import android.widget.PopupMenu.OnMenuItemClickListener;
 import android.widget.Toast;
 
 import com.google.android.gms.maps.model.LatLng;
@@ -43,16 +37,20 @@ import de.tudarmstadt.informatik.hostage.ui2.adapter.RecordListAdapter;
 import de.tudarmstadt.informatik.hostage.ui2.dialog.ChecklistDialog;
 import de.tudarmstadt.informatik.hostage.ui2.dialog.DateTimeDialogFragment;
 import de.tudarmstadt.informatik.hostage.ui2.model.ExpandableListItem;
+import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopup;
+import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopupItem;
+import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupItem;
+import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupTable;
 
 public class RecordOverviewFragment extends Fragment implements ChecklistDialog.ChecklistDialogListener, DateTimeDialogFragment.DateTimeDialogFragmentListener {
 	static final String SELECTED_KEY = "Selected";
 	static final String OTHERS_KEY = "Other";
 
-	static final String FILTER_MENU_TITLE_BSSID = "Filter by BSSID";
-	static final String FILTER_MENU_TITLE_ESSID = "Filter by ESSID";
-	static final String FILTER_MENU_TITLE_PROTOCOLS = "Filter by Protocol";
-	static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = "To Date";
-	static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = "From Date";
+	static final String FILTER_MENU_TITLE_BSSID = "BSSID";
+	static final String FILTER_MENU_TITLE_ESSID = "ESSID";
+	static final String FILTER_MENU_TITLE_PROTOCOLS = "Protocol";
+	static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = "Latest Time";
+	static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = "Earliest Time";
 	static final String FILTER_MENU_TITLE_SORTING = "Sort by";
 	static final String FILTER_MENU_TITLE_REMOVE = "Reset Filter";
     static final String FILTER_MENU_TITLE_GROUP = "Group by";
@@ -141,7 +139,7 @@ public class RecordOverviewFragment extends Fragment implements ChecklistDialog.
 		super.onCreateOptionsMenu(menu, inflater);
 	}
 
-	public boolean onFilterMenuItemSelected(MenuItem item) {
+	public void onFilterMenuItemSelected(AbstractPopupItem item) {
 		String title = item.getTitle().toString();
 
 		if(title.equals(FILTER_MENU_TITLE_BSSID)){
@@ -166,7 +164,7 @@ public class RecordOverviewFragment extends Fragment implements ChecklistDialog.
 		if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
 			this.openTimestampFromFilterDialog();
 		}
-		return super.onOptionsItemSelected(item);
+		//return super.onOptionsItemSelected(item);
 	}
 
 
@@ -397,25 +395,21 @@ public class RecordOverviewFragment extends Fragment implements ChecklistDialog.
 	 * ***************************/
 
 	private void openFilterPopupMenuOnView(View v){
-        // Open FilterMenu
-        PopupMenu filterMenu = new PopupMenu(getBaseContext(), v);
 
-		for(String title : RecordOverviewFragment.this.filterMenuTitles()){
-			// Set a white Title
-			SpannableString styledMenuTitle = new SpannableString(title);
-			styledMenuTitle.setSpan(new ForegroundColorSpan(Color.parseColor("#FFFFFF")), 0, title.length(), 0);
-
-            MenuItem item = filterMenu.getMenu().add(styledMenuTitle);
-            item.setCheckable(this.isFilterSetForTitle(title));
-            if (item.isCheckable()) item.setChecked(true);
-		}
-		filterMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
-            public boolean onMenuItemClick(MenuItem item) {
+        SimplePopupTable filterMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
+            public void onItemClick(AbstractPopupItem item) {
                 RecordOverviewFragment.this.onFilterMenuItemSelected(item);
-                return true;
             }
         });
-			filterMenu.show();
+        filterMenu.setTitle("Filter by");
+		for(String title : RecordOverviewFragment.this.filterMenuTitles()){
+            SimplePopupItem item = new SimplePopupItem(this.getActivity());
+            item.setTitle(title);
+            item.setSelected(this.isFilterSetForTitle(title));
+
+            filterMenu.addItem(item);
+		}
+		filterMenu.showOnView(v);
 	}
 
     private boolean isFilterSetForTitle(String title){
@@ -498,7 +492,7 @@ public class RecordOverviewFragment extends Fragment implements ChecklistDialog.
 		boolean[] selected = new boolean[types.size()];
 		int i = 0;
 		for(String sorttype : types){
-			selected[i] =(this.filter.sorttype.toString() == sorttype);
+			selected[i] =(this.filter.sorttype.toString().equals(sorttype));
 			i++;
 		}
 		return selected;

+ 98 - 0
src/de/tudarmstadt/informatik/hostage/ui2/popup/AbstractPopup.java

@@ -0,0 +1,98 @@
+package de.tudarmstadt.informatik.hostage.ui2.popup;
+
+import android.app.Activity;
+import android.content.Context;
+import android.graphics.Rect;
+import android.view.Gravity;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.Window;
+import android.view.WindowManager;
+import android.widget.LinearLayout;
+import android.widget.PopupWindow;
+
+/**
+ * Created by Julien on 13.02.14.
+ */
+public abstract class AbstractPopup {
+
+    public interface OnPopupItemClickListener {
+        public void onItemClick(AbstractPopupItem item);
+    }
+
+    private PopupWindow popupWindow;
+    private Activity context;
+    private OnPopupItemClickListener onPopupItemClickListener;
+    private LinearLayout rootView;
+    private LayoutInflater lInf;
+
+    abstract public int getLayoutId();
+    abstract void configureView(View view);
+
+    public AbstractPopup(Context context, OnPopupItemClickListener listener) {
+        super();
+        this.context = (Activity) context;
+        this.onPopupItemClickListener = listener;
+        this.popupWindow = new PopupWindow(context);
+        this.popupWindow.setOutsideTouchable(true);
+        this.popupWindow.setFocusable(true);
+        this.lInf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+
+    }
+
+    public void addItem(final AbstractPopupItem item)	{
+        View view = item.getItemView();
+
+        if (this.rootView == null){
+            this.rootView = (LinearLayout) lInf.inflate(this.getLayoutId(), null);
+            this.configureView(this.rootView);
+        }
+        if (this.rootView != null){
+            this.rootView.addView(view);
+            view.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View view) {
+                    onPopupItemClickListener.onItemClick(item);
+                    popupWindow.dismiss();
+                }
+            });
+        }
+
+    }
+
+    public View getPopupView(){
+        return rootView;
+    }
+
+    public void showOnView(View view)	{
+        if (this.rootView == null){
+            this.rootView = (LinearLayout) lInf.inflate(this.getLayoutId(), null);
+        }
+
+        popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
+        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
+        popupWindow.setContentView(rootView);
+
+        Rect rectagle= new Rect();
+        Window window= this.context.getWindow();
+        window.getDecorView().getWindowVisibleDisplayFrame(rectagle);
+
+        int[] position = new int[2];
+        view.getLocationOnScreen(position);
+        rootView.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
+
+        int x = position[0] + (view.getWidth() / 2) - (rootView.getMeasuredWidth() / 2);
+        int y = position[1] - rootView.getMeasuredHeight();
+
+        x = Math.min(0,x);
+        x = Math.max(rectagle.width()-rootView.getMeasuredWidth(), x);
+
+        y = Math.min(0,y);
+        y = Math.max(rectagle.height()-rootView.getMeasuredHeight(), y);
+
+        this.configureView(rootView);
+
+        popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y);
+    }
+
+}

+ 76 - 0
src/de/tudarmstadt/informatik/hostage/ui2/popup/AbstractPopupItem.java

@@ -0,0 +1,76 @@
+package de.tudarmstadt.informatik.hostage.ui2.popup;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+
+import java.util.HashMap;
+
+/**
+ * Created by Julien on 13.02.14.
+ */
+public abstract class AbstractPopupItem {
+    private int itemId;
+    private String title;
+
+    private View itemView;
+
+    private LayoutInflater lInf;
+
+    public HashMap<Object, Object> data;
+
+
+    abstract public int getLayoutId();
+
+    abstract public void configureItemView(View view);
+
+
+    public void setValue(String key, Object value){
+        if (key != null && value != null){
+            this.data.put(key, value);
+            if (this.itemView != null) this.configureItemView(this.itemView);
+        }
+    }
+    public void setMultipleData(HashMap<Object, Object> map){
+        if (map != null){
+            for(Object key : map.keySet()){
+                this.data.put(key, map.get(key));
+            }
+            if (this.itemView != null) this.configureItemView(this.itemView);
+        }
+    }
+
+    public void setTitle(String title){
+        this.title = title;
+        if (this.itemView != null) this.configureItemView(this.itemView);
+    }
+    public String getTitle(){
+        return this.title;
+    }
+    public void setItemId(int id){
+        this.itemId = id;
+        if (this.itemView != null) this.configureItemView(this.itemView);
+    }
+    public int getItemId() {
+        return this.itemId;
+    }
+
+    public AbstractPopupItem(Context context) {
+        super();
+        lInf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+        this.data = new HashMap<Object, Object>();
+    }
+
+    public View getItemView(){
+        if (this.itemView == null){
+            this.itemView = lInf.inflate(this.getLayoutId(), null);
+        }
+        this.configureItemView(this.itemView);
+
+        return this.itemView;
+    }
+
+
+
+
+}

+ 46 - 0
src/de/tudarmstadt/informatik/hostage/ui2/popup/SimplePopupItem.java

@@ -0,0 +1,46 @@
+package de.tudarmstadt.informatik.hostage.ui2.popup;
+
+import android.content.Context;
+import android.view.View;
+import android.widget.RadioButton;
+import android.widget.TextView;
+
+import de.tudarmstadt.informatik.hostage.R;
+
+/**
+ * Created by Julien on 13.02.14.
+ */
+public class SimplePopupItem extends AbstractPopupItem {
+
+    public boolean selected;
+
+    public SimplePopupItem(Context context) {
+        super(context);
+    }
+
+    public int getLayoutId(){
+        return R.layout.simple_popup_item;
+    }
+
+    public void configureItemView(View view){
+        TextView titleView = (TextView) view.findViewById(R.id.title_text_view);
+        RadioButton cbox = (RadioButton) view.findViewById(R.id.isSelectedButton);
+        titleView.setText(this.getTitle());
+
+        if (this.isSelected()){
+            cbox.setVisibility(View.VISIBLE);
+        } else {
+            cbox.setVisibility(View.GONE);
+        }
+    }
+
+    public void setSelected(boolean selected){
+        this.selected = selected;
+        if (this.getItemView() != null) this.configureItemView(this.getItemView());
+    }
+
+    public boolean isSelected(){
+        return this.selected;
+    }
+
+}

+ 36 - 0
src/de/tudarmstadt/informatik/hostage/ui2/popup/SimplePopupTable.java

@@ -0,0 +1,36 @@
+package de.tudarmstadt.informatik.hostage.ui2.popup;
+
+import android.content.Context;
+import android.view.View;
+import android.widget.TextView;
+
+import de.tudarmstadt.informatik.hostage.R;
+
+/**
+ * Created by Julien on 13.02.14.
+ */
+public class SimplePopupTable extends AbstractPopup {
+
+    private String title;
+
+    public void setTitle(String title){
+        this.title = title;
+        if (this.getPopupView() != null) this.configureView(this.getPopupView());
+    }
+    public String getTitle(){
+        return this.title;
+    }
+
+    public SimplePopupTable(Context context, OnPopupItemClickListener listener){
+        super(context, listener);
+    }
+
+    public int getLayoutId(){
+        return R.layout.simple_popup_table;
+    }
+
+    void configureView(View view){
+        TextView titleView = (TextView) view.findViewById(R.id.title_text_view);
+        titleView.setText(this.title);
+    }
+}