<commit_before>package com.anysoftkeyboard.keyboards.views.preview;

import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;

import com.anysoftkeyboard.keyboards.Keyboard;
import com.anysoftkeyboard.keyboards.views.AnyKeyboardBaseView;
import com.anysoftkeyboard.utils.CompatUtils;
import com.menny.android.anysoftkeyboard.R;

import junit.framework.Assert;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class PreviewPopupManager {

	private final int mMaxPopupInstances;
	private PreviewPopupTheme mPreviewPopupTheme = new PreviewPopupTheme();

	private final Queue<PreviewPopup> mFreePreviewPopups = new LinkedList<>();
	private final Queue<PreviewPopup> mActivePreviewPopups = new LinkedList<>();
	private final Map<Keyboard.Key, PreviewPopup> mActivePopupByKeyMap = new HashMap<>();
	private final Context mContext;
	private final AnyKeyboardBaseView mKeyboardView;
	private final UIHandler mUIHandler;

	private boolean mEnabled = true;

	public PreviewPopupManager(Context context, AnyKeyboardBaseView keyboardView) {
		mContext = context;
		mKeyboardView = keyboardView;
		mMaxPopupInstances = context.getResources().getInteger(R.integer.maximum_instances_of_preview_popups);
		mUIHandler = new UIHandler(this, context.getResources().getInteger(R.integer.preview_in_animation_duration));
	}

	public void setEnabled(boolean enabled) {
		mEnabled = enabled;
		cancelAllPreviews();
	}

	public void hidePreviewForKey(Keyboard.Key key) {
		mUIHandler.dismissPreview(key);
	}

	public void showPreviewForKey(Keyboard.Key key, Drawable icon) {
		if (!mEnabled) return;
		PreviewPopup popup = getPopupForKey(key, false);
		if (popup != null) {
			Point previewPosition = PreviewPopupPositionCalculator.calculatePositionForPreview(key, mKeyboardView, mPreviewPopupTheme);
			popup.showPreviewForKey(key, icon, previewPosition);
		}
	}

	public void showPreviewForKey(Keyboard.Key key, CharSequence label) {
		if (!mEnabled) return;
		PreviewPopup popup = getPopupForKey(key, false);
		if (popup != null) {
			Point previewPosition = PreviewPopupPositionCalculator.calculatePositionForPreview(key, mKeyboardView, mPreviewPopupTheme);
			popup.showPreviewForKey(key, label, previewPosition);<commit_msg>fix crash when openning extension keyboard<commit_after>package com.anysoftkeyboard.keyboards.views.preview;

import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;

import com.anysoftkeyboard.keyboards.Keyboard;
import com.anysoftkeyboard.keyboards.views.AnyKeyboardBaseView;
import com.anysoftkeyboard.utils.CompatUtils;
import com.menny.android.anysoftkeyboard.R;

import junit.framework.Assert;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class PreviewPopupManager {

	private final int mMaxPopupInstances;
	private PreviewPopupTheme mPreviewPopupTheme = new PreviewPopupTheme();

	private final Queue<PreviewPopup> mFreePreviewPopups = new LinkedList<>();
	private final Queue<PreviewPopup> mActivePreviewPopups = new LinkedList<>();
	private final Map<Keyboard.Key, PreviewPopup> mActivePopupByKeyMap = new HashMap<>();
	private final Context mContext;
	private final AnyKeyboardBaseView mKeyboardView;
	private final UIHandler mUIHandler;

	private boolean mEnabled = true;

	public PreviewPopupManager(Context context, AnyKeyboardBaseView keyboardView) {
		mContext = context;
		mKeyboardView = keyboardView;
		mMaxPopupInstances = context.getResources().getInteger(R.integer.maximum_instances_of_preview_popups);
		mUIHandler = new UIHandler(this, context.getResources().getInteger(R.integer.preview_in_animation_duration));
	}

	public void setEnabled(boolean enabled) {
		mEnabled = enabled;
		cancelAllPreviews();
	}

	public void hidePreviewForKey(Keyboard.Key key) {
		mUIHandler.dismissPreview(key);
	}

	public void showPreviewForKey(Keyboard.Key key, Drawable icon) {
		if (isDisabled()) return;
		PreviewPopup popup = getPopupForKey(key, false);
		if (popup != null) {
			Point previewPosition = PreviewPopupPositionCalculator.calculatePositionForPreview(key, mKeyboardView, mPreviewPopupTheme);
			popup.showPreviewForKey(key, icon, previewPosition);
		}
	}

	private boolean isDisabled() {
		return !mEnabled || mPreviewPopupTheme.getPreviewKeyTextSize() <= 0;
	}

	public void showPreviewForKey(Keyboard.Key key, CharSequence label) {
		if (isDisabled()) return;
		PreviewPopup popup = getPopupForKey(key, false);
		if (popup != null) {
			Point previewPosition = PreviewPopupPositionCalculator.calculatePositionForPreview(key, mKeyboardView, mPreviewPopupTheme);
			popup.showPreviewForKey(key, label, previewPosition);
{'fraction_non_alphanumeric': 0.08944863149543832, 'fraction_numerical': 0.00019833399444664816, 'mean_word_length': 6.675799086757991, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, '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': '6443682', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1499, 'n_words': 323}