<commit_before>package cz.vutbr.fit.iha.menu;

import java.util.List;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;

import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;

import cz.vutbr.fit.iha.Constants;
import cz.vutbr.fit.iha.R;
import cz.vutbr.fit.iha.activity.AdapterUsersActivity;
import cz.vutbr.fit.iha.activity.AddAdapterActivity;
import cz.vutbr.fit.iha.activity.MainActivity;
import cz.vutbr.fit.iha.activity.SettingsMainActivity;
import cz.vutbr.fit.iha.activity.dialog.InfoDialogFragment;
import cz.vutbr.fit.iha.activity.menuItem.AdapterMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.CustomViewMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.EmptyMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.GroupImageMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.LocationMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.MenuItem;
import cz.vutbr.fit.iha.activity.menuItem.ProfileMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.SeparatorMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.SettingMenuItem;
import cz.vutbr.fit.iha.adapter.Adapter;
import cz.vutbr.fit.iha.adapter.location.Location;
import cz.vutbr.fit.iha.arrayadapter.MenuListAdapter;
import cz.vutbr.fit.iha.asynctask.CallbackTask.CallbackTaskListener;
import cz.vutbr.fit.iha.asynctask.SwitchAdapterTask;
import cz.vutbr.fit.iha.asynctask.UnregisterAdapterTask;
import cz.vutbr.fit.iha.controller.Controller;
import cz.vutbr.fit.iha.household.ActualUser;
import cz.vutbr.fit.iha.persistence.Persistence;
import cz.vutbr.fit.iha.thread.ToastMessageThread;
import cz.vutbr.fit.iha.util.Log;

public class NavDrawerMenu {
	private static final String TAG = "NavDrawerMenu";
	private final static String TAG_INFO = "tag_info";

	private MainActivity mActivity;
	private Controller mController;

	private DrawerLayout mDrawerLayout;
	private StickyListHeadersListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;
	private String mDrawerTitle = "IHA";

	private String mActiveLocationId;
	private String mActiveCustomViewId;
	private String mActiveAdapterId;
	private String mAdaterIdUnregist;

	private boolean mIsDrawerOpen;

	private boolean backPressed = false;

	private SwitchAdapterTask mSwitchAdapterTask;
	private UnregisterAdapterTask mUnregisterAdapterTask;

	private MenuListAdapter mMenuAdapter;

	//
	private ActionMode mMode;
	private MenuItem mSelectedMenuItem;

	public NavDrawerMenu(MainActivity activity) {
		// Set activity
		mActivity = activity;

		backPressed = mActivity.getBackPressed();
		// Get controller
		mController = Controller.getInstance(mActivity);

		// Get GUI element for menu
		getGUIElements();
		// Set all of listener for (listview) menu items
		settingsMenu();
	}

	private void getGUIElements() {
		// Locate DrawerLayout in activity_location_screen.xml
		mDrawerLayout = (DrawerLayout) mActivity.findViewById(R.id.drawer_layout);
		// Locate ListView in activity_location_screen.xml
		mDrawerList = (StickyListHeadersListView) mActivity.findViewById(R.id.listview_drawer);
	}

	private void settingsMenu() {
		// Set a custom shadow that overlays the main content when the drawer opens
		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

		mDrawerLayout.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
					backPressed = ((MainActivity) mActivity).getBackPressed();
					Log.d(TAG, "BackPressed = " + String.valueOf(backPressed));
					if (mDrawerLayout == null) {
						return false;
					}
					if (mDrawerLayout.isDrawerOpen(mDrawerList) && !backPressed) {
						firstTapBack();
						return true;
					} else if (mDrawerLayout.isDrawerOpen(mDrawerList) && backPressed) {
						secondTapBack();
						return true;
					}
				}
				return false;
			}
		});

		// Capture listview menu item click
		mDrawerList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mSelectedMenuItem = (MenuItem) mMenuAdapter.getItem(position);
				switch (mSelectedMenuItem.getType()) {
				case ADAPTER:
					// if it is not chosen, switch to selected adapter
					if (!mController.getActiveAdapter().getId().equals(mSelectedMenuItem.getId())) {
						doSwitchAdapter(mSelectedMenuItem.getId());
					}
					break;

				case CUSTOM_VIEW:
					// TODO: otevrit custom view, jeste nedelame s customView, takze pozdeji

					// TEMP
					mActiveLocationId = null;
					mActiveCustomViewId = mSelectedMenuItem.getId();

					changeCustomView(true);
					redrawMenu();
					break;

				case SETTING:
					if (mSelectedMenuItem.getId().equals(cz.vutbr.fit.iha.activity.menuItem.MenuItem.ID_ABOUT)) {
						InfoDialogFragment dialog = new InfoDialogFragment();
						dialog.show(mActivity.getSupportFragmentManager(), TAG_INFO);
					} else if (mSelectedMenuItem.getId().equals(cz.vutbr.fit.iha.activity.menuItem.MenuItem.ID_SETTINGS)) {
						Intent intent = new Intent(mActivity, SettingsMainActivity.class);
						mActivity.startActivity(intent);
					}
					break;

				case LOCATION:
					// Get the title followed by the position
					Adapter adapter = mController.getActiveAdapter();
					if (adapter != null) {
						mActiveCustomViewId = null;
						changeLocation(mController.getLocation(adapter.getId(), mSelectedMenuItem.getId()), true);
						redrawMenu();
					}
					break;

				default:
					break;
				}
			}
		});
		mDrawerList.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				Log.d(TAG, "Item Long press");
				mSelectedMenuItem = (MenuItem) mMenuAdapter.getItem(position);
				switch (mSelectedMenuItem.getType()) {
				case LOCATION:
					if(!mSelectedMenuItem.getId().equals(Constants.GUI_MENU_ALL_SENSOR_ID))
						mMode = mActivity.startActionMode(new ActionModeLocations());
					break;
				case ADAPTER:
					Log.i(TAG, "Long press - adapter");
					//mAdaterIdUnregist = item.getId();
					mMode = mActivity.startActionMode(new ActionModeAdapters());
					mSelectedMenuItem.setIsSelected();
					break;
				default:
					// do nothing
					break;
				}<commit_msg>Fix switching action mode for adapter<commit_after>package cz.vutbr.fit.iha.menu;

import java.util.List;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;

import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;

import cz.vutbr.fit.iha.Constants;
import cz.vutbr.fit.iha.R;
import cz.vutbr.fit.iha.activity.AdapterUsersActivity;
import cz.vutbr.fit.iha.activity.AddAdapterActivity;
import cz.vutbr.fit.iha.activity.MainActivity;
import cz.vutbr.fit.iha.activity.SettingsMainActivity;
import cz.vutbr.fit.iha.activity.dialog.InfoDialogFragment;
import cz.vutbr.fit.iha.activity.menuItem.AdapterMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.CustomViewMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.EmptyMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.GroupImageMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.LocationMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.MenuItem;
import cz.vutbr.fit.iha.activity.menuItem.ProfileMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.SeparatorMenuItem;
import cz.vutbr.fit.iha.activity.menuItem.SettingMenuItem;
import cz.vutbr.fit.iha.adapter.Adapter;
import cz.vutbr.fit.iha.adapter.location.Location;
import cz.vutbr.fit.iha.arrayadapter.MenuListAdapter;
import cz.vutbr.fit.iha.asynctask.CallbackTask.CallbackTaskListener;
import cz.vutbr.fit.iha.asynctask.SwitchAdapterTask;
import cz.vutbr.fit.iha.asynctask.UnregisterAdapterTask;
import cz.vutbr.fit.iha.controller.Controller;
import cz.vutbr.fit.iha.household.ActualUser;
import cz.vutbr.fit.iha.persistence.Persistence;
import cz.vutbr.fit.iha.thread.ToastMessageThread;
import cz.vutbr.fit.iha.util.Log;

public class NavDrawerMenu {
	private static final String TAG = "NavDrawerMenu";
	private final static String TAG_INFO = "tag_info";

	private MainActivity mActivity;
	private Controller mController;

	private DrawerLayout mDrawerLayout;
	private StickyListHeadersListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;
	private String mDrawerTitle = "IHA";

	private String mActiveLocationId;
	private String mActiveCustomViewId;
	private String mActiveAdapterId;
	private String mAdaterIdUnregist;

	private boolean mIsDrawerOpen;

	private boolean backPressed = false;

	private SwitchAdapterTask mSwitchAdapterTask;
	private UnregisterAdapterTask mUnregisterAdapterTask;

	private MenuListAdapter mMenuAdapter;

	//
	private ActionMode mMode;
	private MenuItem mSelectedMenuItem;

	public NavDrawerMenu(MainActivity activity) {
		// Set activity
		mActivity = activity;

		backPressed = mActivity.getBackPressed();
		// Get controller
		mController = Controller.getInstance(mActivity);

		// Get GUI element for menu
		getGUIElements();
		// Set all of listener for (listview) menu items
		settingsMenu();
	}

	private void getGUIElements() {
		// Locate DrawerLayout in activity_location_screen.xml
		mDrawerLayout = (DrawerLayout) mActivity.findViewById(R.id.drawer_layout);
		// Locate ListView in activity_location_screen.xml
		mDrawerList = (StickyListHeadersListView) mActivity.findViewById(R.id.listview_drawer);
	}

	private void settingsMenu() {
		// Set a custom shadow that overlays the main content when the drawer opens
		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

		mDrawerLayout.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
					backPressed = ((MainActivity) mActivity).getBackPressed();
					Log.d(TAG, "BackPressed = " + String.valueOf(backPressed));
					if (mDrawerLayout == null) {
						return false;
					}
					if (mDrawerLayout.isDrawerOpen(mDrawerList) && !backPressed) {
						firstTapBack();
						return true;
					} else if (mDrawerLayout.isDrawerOpen(mDrawerList) && backPressed) {
						secondTapBack();
						return true;
					}
				}
				return false;
			}
		});

		// Capture listview menu item click
		mDrawerList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mSelectedMenuItem = (MenuItem) mMenuAdapter.getItem(position);
				switch (mSelectedMenuItem.getType()) {
				case ADAPTER:
					// if it is not chosen, switch to selected adapter
					if (!mController.getActiveAdapter().getId().equals(mSelectedMenuItem.getId())) {
						doSwitchAdapter(mSelectedMenuItem.getId());
					}
					break;

				case CUSTOM_VIEW:
					// TODO: otevrit custom view, jeste nedelame s customView, takze pozdeji

					// TEMP
					mActiveLocationId = null;
					mActiveCustomViewId = mSelectedMenuItem.getId();

					changeCustomView(true);
					redrawMenu();
					break;

				case SETTING:
					if (mSelectedMenuItem.getId().equals(cz.vutbr.fit.iha.activity.menuItem.MenuItem.ID_ABOUT)) {
						InfoDialogFragment dialog = new InfoDialogFragment();
						dialog.show(mActivity.getSupportFragmentManager(), TAG_INFO);
					} else if (mSelectedMenuItem.getId().equals(cz.vutbr.fit.iha.activity.menuItem.MenuItem.ID_SETTINGS)) {
						Intent intent = new Intent(mActivity, SettingsMainActivity.class);
						mActivity.startActivity(intent);
					}
					break;

				case LOCATION:
					// Get the title followed by the position
					Adapter adapter = mController.getActiveAdapter();
					if (adapter != null) {
						mActiveCustomViewId = null;
						changeLocation(mController.getLocation(adapter.getId(), mSelectedMenuItem.getId()), true);
						redrawMenu();
					}
					break;

				default:
					break;
				}
			}
		});
		mDrawerList.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				if(mMode != null) {
					// Action Mode is active and I want change
					mSelectedMenuItem.setNotSelected();
					mMode = null;
				}
				
				Log.d(TAG, "Item Long press");
				mSelectedMenuItem = (MenuItem) mMenuAdapter.getItem(position);
				switch (mSelectedMenuItem.getType()) {
				case LOCATION:
					if(!mSelectedMenuItem.getId().equals(Constants.GUI_MENU_ALL_SENSOR_ID))
						mMode = mActivity.startActionMode(new ActionModeLocations());
					break;
				case ADAPTER:
					Log.i(TAG, "Long press - adapter");
					//mAdaterIdUnregist = item.getId();
					mMode = mActivity.startActionMode(new ActionModeAdapters());
					mSelectedMenuItem.setIsSelected();
					break;
				default:
					// do nothing
					break;
				}
{'fraction_non_alphanumeric': 0.10093352986593669, 'fraction_numerical': 0.0004211412929037692, 'mean_word_length': 4.5203409531189465, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '363770', 'n_tokens_mistral': 5110, 'n_tokens_neox': 4434, 'n_words': 852}