<commit_before>package com.starksoft.material_activity;

import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ActionMode;
import android.support.v7.view.ActionMode.Callback;
import android.text.TextUtils;
import android.text.style.ReplacementSpan;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class StarksoftActivityNewDrawer extends AppCompatActivity
{
	private DrawerLayout mDrawerLayout;
	private NavigationView mNavigationView;
	private ActionBarDrawerToggle mDrawerToggle;
	private CharSequence mDrawerTitle;
	private CharSequence mTitle;

	//	private StarksoftActivity mActivity;
	private Fragment activeFragment;
	// Одновременно может быть только один ActionMode в пределах одной Activity
	private ActionMode mActionMode;

	/////////////////////////////////////////////////////////////////////////////////////////////////
	// ActionMode related methods
	/////////////////////////////////////////////////////////////////////////////////////////////////
	public ActionMode startMyActionMode(Callback mActionModeCallback)
	{
		if (mActionModeCallback == null)
			return mActionMode = null;

		return mActionMode = startSupportActionMode(mActionModeCallback);
	}

	/**
	 * returns true if mode was closed
	 */
	public boolean closeActionMode()
	{
		boolean isModeEnabled = mActionMode != null;
		if (isModeEnabled)
			mActionMode.finish();

		return isModeEnabled;
	}

	public ActionMode getActionMode()
	{
		return mActionMode;
	}

	/**
	 * Узнаем запустил ли юзер ActionMode
	 */
	public boolean isActionModeRunning()
	{
		return getActionMode() != null;
	}
	/////////////////////////////////////////////////////////////////////////////////////////////////
	// DrawerLayout related methods
	/////////////////////////////////////////////////////////////////////////////////////////////////

	public NavigationView getNavigationView()
	{
		return mNavigationView;
	}

	public void disableDrawer(int lockMode)
	{
		if (mDrawerLayout == null || getSupportActionBar() == null)
			return;

		mDrawerLayout.setDrawerLockMode(lockMode, mNavigationView);
		getSupportActionBar().setDisplayHomeAsUpEnabled(false);
	}

	public void enableDrawer()
	{
		if (mDrawerLayout == null || getSupportActionBar() == null)
			return;

		mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mNavigationView);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	public boolean closeDrawer()
	{
		int lockMode = mDrawerLayout.getDrawerLockMode(mNavigationView);
		if (lockMode == DrawerLayout.LOCK_MODE_UNLOCKED && isDrawerOpen())
		{
			mDrawerLayout.closeDrawer(mNavigationView);
			return true;
		}
		return false;
	}

	public void toggleDrawer()
	{
		if (mDrawerLayout == null || mNavigationView == null)
			return;

		int lockMode = mDrawerLayout.getDrawerLockMode(mNavigationView);
		if (lockMode == DrawerLayout.LOCK_MODE_UNLOCKED)
		{
			if (mDrawerLayout.isDrawerOpen(mNavigationView))
				mDrawerLayout.closeDrawer(mNavigationView);
			else
				mDrawerLayout.openDrawer(mNavigationView);
		}
	}

	public void setDrawerClickListener(NavigationView.OnNavigationItemSelectedListener listener)
	{
		if (mNavigationView != null)
			mNavigationView.setNavigationItemSelectedListener(listener);
	}

	public void selectDrawerItemAndSetTitle(@IdRes int resId, int optCounter, String optTitle)
	{
		selectDrawerItemAndSetTitle(resId, optTitle);
		setNavigationViewCounter(resId, optCounter);
	}

	public void selectDrawerItemAndSetTitle(@IdRes int resId, String optTitle)
	{
		if(mDrawerLayout == null || mNavigationView == null)
			return;

		MenuItem menuItem = mNavigationView.getMenu().findItem(resId);

		if(menuItem == null)
			return;

		String title = (String) menuItem.getTitle();

		mDrawerLayout.closeDrawer(mNavigationView);
		setTitle(TextUtils.isEmpty(optTitle) ? title : optTitle);
	}

	public void setNavigationViewCounter(@IdRes int itemId, int count)
	{
		MenuItem item = getNavigationView().getMenu().findItem(itemId);
		if (item == null)
			return;

		TextView view = (TextView) item.getActionView();

		if (view == null)
			return;

		view.setText(count > 0 ? String.valueOf(count) : null);
	}

	/**
	 * узнаем открыто ли боковое меню
	 */
	public boolean isDrawerOpen()
	{
		return mDrawerLayout.isDrawerOpen(mNavigationView);
	}

	/////////////////////////////////////////////////////////////////////////////////////////////////
	// FragmentActivity related methods
	/////////////////////////////////////////////////////////////////////////////////////////////////
	@SuppressWarnings("ResourceType")
	public void setActiveFragment(Fragment dest)
	{
		if (getActionMode() != null)
			getActionMode().finish();

		String tag = dest.getClass().getName();

		try
		{
			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction();
			mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			// mFragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
			mFragmentTransaction.replace(R.id.content_frame, activeFragment = dest, tag).commit();<commit_msg>Fixed some code styling, added some extra<commit_after>package com.starksoft.material_activity;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ActionMode;
import android.support.v7.view.ActionMode.Callback;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class StarksoftActivityNewDrawer extends AppCompatActivity
{
	private DrawerLayout mDrawerLayout;
	private NavigationView mNavigationView;
	private ActionBarDrawerToggle mDrawerToggle;
	private CharSequence mDrawerTitle;
	private CharSequence mTitle;

	//	private StarksoftActivity mActivity;
	private Fragment activeFragment;
	// Одновременно может быть только один ActionMode в пределах одной Activity
	private ActionMode mActionMode;

	/////////////////////////////////////////////////////////////////////////////////////////////////
	// ActionMode related methods
	/////////////////////////////////////////////////////////////////////////////////////////////////
	public ActionMode startMyActionMode(Callback mActionModeCallback)
	{
		if (mActionModeCallback == null)
			return mActionMode = null;

		return mActionMode = startSupportActionMode(mActionModeCallback);
	}

	/**
	 * returns true if mode was closed
	 */
	public boolean closeActionMode()
	{
		boolean isModeEnabled = mActionMode != null;
		if (isModeEnabled)
			mActionMode.finish();

		return isModeEnabled;
	}

	public ActionMode getActionMode()
	{
		return mActionMode;
	}

	/**
	 * Узнаем запустил ли юзер ActionMode
	 */
	public boolean isActionModeRunning()
	{
		return getActionMode() != null;
	}
	/////////////////////////////////////////////////////////////////////////////////////////////////
	// DrawerLayout related methods
	/////////////////////////////////////////////////////////////////////////////////////////////////

	public NavigationView getNavigationView()
	{
		return mNavigationView;
	}

	public void disableDrawer(int lockMode)
	{
		if (mDrawerLayout == null || getSupportActionBar() == null)
			return;

		mDrawerLayout.setDrawerLockMode(lockMode, mNavigationView);
		getSupportActionBar().setDisplayHomeAsUpEnabled(false);
	}

	public void enableDrawer()
	{
		if (mDrawerLayout == null || getSupportActionBar() == null)
			return;

		mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mNavigationView);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	public boolean closeDrawer()
	{
		int lockMode = mDrawerLayout.getDrawerLockMode(mNavigationView);
		if (lockMode == DrawerLayout.LOCK_MODE_UNLOCKED && isDrawerOpen())
		{
			mDrawerLayout.closeDrawer(mNavigationView);
			return true;
		}
		return false;
	}

	public void toggleDrawer()
	{
		if (mDrawerLayout == null || mNavigationView == null)
			return;

		int lockMode = mDrawerLayout.getDrawerLockMode(mNavigationView);
		if (lockMode == DrawerLayout.LOCK_MODE_UNLOCKED)
		{
			if (mDrawerLayout.isDrawerOpen(mNavigationView))
				mDrawerLayout.closeDrawer(mNavigationView);
			else
				mDrawerLayout.openDrawer(mNavigationView);
		}
	}

	public void setDrawerClickListener(NavigationView.OnNavigationItemSelectedListener listener)
	{
		if (mNavigationView != null)
			mNavigationView.setNavigationItemSelectedListener(listener);
	}

	public void selectDrawerItemAndSetTitle(@IdRes int resId, int optCounter, String optTitle)
	{
		selectDrawerItemAndSetTitle(resId, optTitle);
		setNavigationViewCounter(resId, optCounter);
	}

	public void selectDrawerItemAndSetTitle(@IdRes int resId, String optTitle)
	{
		if (mDrawerLayout == null || mNavigationView == null)
			return;

		MenuItem menuItem = mNavigationView.getMenu().findItem(resId);

		if (menuItem == null)
			return;

		String title = (String) menuItem.getTitle();

		mDrawerLayout.closeDrawer(mNavigationView);
		setTitle(TextUtils.isEmpty(optTitle) ? title : optTitle);
	}

	public void setNavigationViewCounter(@IdRes int itemId, int count)
	{
		MenuItem item = getNavigationView().getMenu().findItem(itemId);
		if (item == null)
			return;

		TextView view = (TextView) item.getActionView();

		if (view == null)
			return;

		view.setText(count > 0 ? String.valueOf(count) : null);
	}

	/**
	 * узнаем открыто ли боковое меню
	 */
	public boolean isDrawerOpen()
	{
		return mDrawerLayout.isDrawerOpen(mNavigationView);
	}

	/////////////////////////////////////////////////////////////////////////////////////////////////
	// FragmentActivity related methods
	/////////////////////////////////////////////////////////////////////////////////////////////////
	public void setActiveFragment(Fragment dest)
	{
		setActiveFragment(dest, false, false);
	}

	@SuppressWarnings("ResourceType")
	public void setActiveFragment(@NonNull Fragment dest, boolean addToBackStack, boolean checkIfEqual)
	{
		if (getActionMode() != null)
			getActionMode().finish();

		String tag = dest.getClass().getName();
		// FIXME Не очень хорошо матчить по тэгу, в тестовой либе как раз используетсяо один фрагмент с разными аргументами для одного элемента Drawer
		if (checkIfEqual && getActiveFragment() != null && TextUtils.equals(tag, getActiveFragment().getClass().getName()))
			return;

		try
		{
			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction();
			mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			// mFragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
//			if (addToBackStack)
//				mFragmentTransaction.addToBackStack(null);

			mFragmentTransaction.replace(R.id.content_frame, activeFragment = dest, tag).commit();
{'fraction_non_alphanumeric': 0.18139993427538614, 'fraction_numerical': 0.001807426881367072, 'mean_word_length': 3.2252690038181187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5533580', 'n_tokens_mistral': 3621, 'n_tokens_neox': 3392, 'n_words': 685}