<commit_before>/*
 * Copyright (C) 2006 The Android Open Source Project
 *               2011 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.actionbarsherlock.internal.view;

import java.lang.ref.WeakReference;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.view.MenuItem;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;

/**
 * An implementation of the {@link android.view.MenuItem} interface for use in
 * inflating menu XML resources to be added to a third-party action bar. 
 * 
 * @author <NAME> <<EMAIL>>
 * @see <a href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/com/android/internal/view/menu/MenuItemImpl.java">com.android.internal.view.menu.MenuItemImpl</a>
 */
public final class MenuItemImpl implements MenuItem {
	private static final String TAG = "MenuItemImpl";
	
	private final MenuBuilder mMenu;
	
	private final int mItemId;
	private final int mGroupId;
	private final int mOrder;
	
	private Intent mIntent;
	private CharSequence mTitle;
	private CharSequence mTitleCondensed;
	private char mNumericalShortcut;
	private char mAlphabeticalShortcut;
	private int mShowAsAction;
	private SubMenuBuilder mSubMenu;
	private Runnable mItemCallback;
	private OnMenuItemClickListener mClickListener;
	private Drawable mIcon;
	private int mIconRes = View.NO_ID;
	private View mActionView;
	private int mActionViewRes = View.NO_ID;

	int mFlags = ENABLED;
	private static final int CHECKABLE = 0x01;
	private static final int CHECKED   = 0x02;
	private static final int EXCLUSIVE = 0x04;
	private static final int HIDDEN    = 0x08;
	private static final int ENABLED   = 0x10;
	private static final int IS_ACTION = 0x20;
	
	private final WeakReference<MenuView.ItemView>[] mItemViews;
	
	
	/**
	 * Create a new action bar menu item.
	 * 
	 * @param context Context used if resource resolution is required.
	 * @param itemId A unique ID. Used in the activity callback.
	 * @param groupId Group ID. Currently unused.
	 * @param order Item order. Currently unused.
	 * @param title Title of the item.
	 */
	@SuppressWarnings("unchecked")
	MenuItemImpl(MenuBuilder menu, int itemId, int groupId, int order, CharSequence title) {
		mMenu = menu;
		
		mItemId = itemId;
		mGroupId = groupId;
		mOrder = order;
		mTitle = title;
		
		mItemViews = new WeakReference[MenuBuilder.NUM_TYPES];
	}
	
	
	
	public boolean invoke() {
		if (mClickListener != null &&
		    mClickListener.onMenuItemClick(this)) {
			return true;
		}
		
		MenuBuilder.Callback callback = mMenu.getCallback();
		if (callback != null &&
			callback.onMenuItemSelected(mMenu.getRootMenu(), this)) {
			return true;
		}
		
		if (mItemCallback != null) {
			mItemCallback.run();
			return true;
		}
		
		if (mIntent != null) {
			try {
				mMenu.getContext().startActivity(mIntent);
				return true;
			} catch (ActivityNotFoundException e) {
				Log.e(TAG, "Can't find activity to handle intent; ignoring", e);
			}
		}
		
		return false;
	}
	
	private boolean hasItemView(int menuType) {
		return mItemViews[menuType] != null && mItemViews[menuType].get() != null;
	}
	
	public void setItemView(int type, MenuView.ItemView itemView) {
		mItemViews[type] = new WeakReference<MenuView.ItemView>(itemView);
	}
	
	<commit_msg>Add support for submenus on action items. Closes #25.<commit_after>/*
 * Copyright (C) 2006 The Android Open Source Project
 *               2011 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.actionbarsherlock.internal.view;

import java.lang.ref.WeakReference;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.view.MenuItem;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;

/**
 * An implementation of the {@link android.view.MenuItem} interface for use in
 * inflating menu XML resources to be added to a third-party action bar. 
 * 
 * @author <NAME> <<EMAIL>>
 * @see <a href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/com/android/internal/view/menu/MenuItemImpl.java">com.android.internal.view.menu.MenuItemImpl</a>
 */
public final class MenuItemImpl implements MenuItem {
	private static final String TAG = "MenuItemImpl";
	
	private final MenuBuilder mMenu;
	
	private final int mItemId;
	private final int mGroupId;
	private final int mOrder;
	
	private Intent mIntent;
	private CharSequence mTitle;
	private CharSequence mTitleCondensed;
	private char mNumericalShortcut;
	private char mAlphabeticalShortcut;
	private int mShowAsAction;
	private SubMenuBuilder mSubMenu;
	private Runnable mItemCallback;
	private OnMenuItemClickListener mClickListener;
	private Drawable mIcon;
	private int mIconRes = View.NO_ID;
	private View mActionView;
	private int mActionViewRes = View.NO_ID;

	int mFlags = ENABLED;
	private static final int CHECKABLE = 0x01;
	private static final int CHECKED   = 0x02;
	private static final int EXCLUSIVE = 0x04;
	private static final int HIDDEN    = 0x08;
	private static final int ENABLED   = 0x10;
	private static final int IS_ACTION = 0x20;
	
	private final WeakReference<MenuView.ItemView>[] mItemViews;
	
	private final DialogInterface.OnClickListener subMenuClick = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int index) {
			dialog.dismiss();
			mSubMenu.getItem(index).invoke();
		}
	};
	private final DialogInterface.OnMultiChoiceClickListener subMenuMultiClick = new DialogInterface.OnMultiChoiceClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int index, boolean isChecked) {
			dialog.dismiss();
			mSubMenu.getItem(index).setChecked(isChecked);
		}
	};
	
	
	/**
	 * Create a new action bar menu item.
	 * 
	 * @param context Context used if resource resolution is required.
	 * @param itemId A unique ID. Used in the activity callback.
	 * @param groupId Group ID. Currently unused.
	 * @param order Item order. Currently unused.
	 * @param title Title of the item.
	 */
	@SuppressWarnings("unchecked")
	MenuItemImpl(MenuBuilder menu, int itemId, int groupId, int order, CharSequence title) {
		mMenu = menu;
		
		mItemId = itemId;
		mGroupId = groupId;
		mOrder = order;
		mTitle = title;
		
		mItemViews = new WeakReference[MenuBuilder.NUM_TYPES];
	}
	
	
	
	public boolean invoke() {
		if (hasSubMenu()) {
			AlertDialog.Builder builder = new AlertDialog.Builder(mMenu.getContext());
			builder.setTitle(getTitle());
			
			final boolean isExclusive = (mSubMenu.size() > 0) && mSubMenu.getItem(0).isExclusiveCheckable();
			final boolean isCheckable = (mSubMenu.size() > 0) && mSubMenu.getItem(0).isCheckable();
			if (isExclusive) {
				builder.setSingleChoiceItems(getSubMenuTitles(), getSubMenuSelected(), subMenuClick);
			} else if (isCheckable) {
				builder.setMultiChoiceItems(getSubMenuTitles(), getSubMenuChecked(), subMenuMultiClick);
			} else {
				builder.setItems(getSubMenuTitles(), subMenuClick);
			}
			
			builder.show();
			return true;
		}
		
		if (mClickListener != null &&
		    mClickListener.onMenuItemClick(this)) {
			return true;
		}
		
		MenuBuilder.Callback callback = mMenu.getRootMenu().getCallback();
		if (callback != null &&
			callback.onMenuItemSelected(mMenu.getRootMenu(), this)) {
			return true;
		}
		
		if (mItemCallback != null) {
			mItemCallback.run();
			return true;
		}
		
		if (mIntent != null) {
			try {
				mMenu.getContext().startActivity(mIntent);
				return true;
			} catch (ActivityNotFoundException e) {
				Log.e(TAG, "Can't find activity to handle intent; ignoring", e);
			}
		}
		
		return false;
	}
	
	private CharSequence[] getSubMenuTitles() {
		final int count = mSubMenu.size();
		CharSequence[] list = new CharSequence[count];
		for (int i = 0; i < count; i++) {
			list[i] = mSubMenu.getItem(i).getTitle();
		}
		return list;
	}
	
	private int getSubMenuSelected() {
		final int count = mSubMenu.size();
		for (int i = 0; i < count; i++) {
			if (mSubMenu.getItem(i).isChecked()) {
				return i;
			}
		}
		return -1;
	}
	
	private boolean[] getSubMenuChecked() {
		final int count = mSubMenu.size();
		boolean[] checked = new boolean[count];
		for (int i = 0; i < count; i++) {
			checked[i] = mSubMenu.getItem(i).isChecked();
		}
		return checked;
	}
	
	private boolean hasItemView(int menuType) {
		return mItemViews[menuType] != null && mItemViews[menuType].get() != null;
	}
	
	public void setItemView(int type, MenuView.ItemView itemView) {
		mItemViews[type] = new WeakReference<MenuView.ItemView>(itemView);
	}
	
	
{'fraction_non_alphanumeric': 0.09939516129032258, 'fraction_numerical': 0.007258064516129033, 'mean_word_length': 4.09553158705701, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4153686', 'n_tokens_mistral': 3267, 'n_tokens_neox': 2928, 'n_words': 906}