<commit_before>package retrobox.keyboard;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import retrobox.utils.R;
import retrobox.utils.RetroBoxUtils;
import xtvapps.core.AndroidFonts;
import xtvapps.core.SimpleCallback;
import xtvapps.core.Utils;

public class KeyboardView extends FrameLayout {
	
	public static final String SWITCH_LAYOUT = "SWITCH_LAYOUT_";
	public static final String TOGGLE_POSITION = "TOGGLE_POSITION";

	private List<KeyboardLayout> keylayouts = new ArrayList<KeyboardLayout>();
	private VirtualKeyListener onKeyListener;
	int activeLayout = 0;
	private SimpleCallback onTogglePositionCallback;

	public KeyboardView(Context context) {
		super(context);
	}

	public KeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public KeyboardView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public void init(Context ctx, KeyboardLayout keylayout) {
		Log.d("KEYB", "init");
		keylayouts.add(keylayout);
		if (keylayouts.size() == 1) switchLayout(ctx, 0);
		
	}
	
	private void switchLayout(final Context ctx, int index) {
		activeLayout = index;
		
		removeAllViews();
		
		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View view) {
				String keyCode = (String)view.getTag();
				if (keyCode.startsWith(SWITCH_LAYOUT)) {
					int layout = Utils.str2i(keyCode.substring(SWITCH_LAYOUT.length()));
					switchLayout(ctx, layout);
					getChildAt(0).requestFocus();
					return;
				}
				if (keyCode.equals(TOGGLE_POSITION) && onTogglePositionCallback!=null) {
					onTogglePositionCallback.onResult();
				}
				if (onKeyListener!=null) {
					onKeyListener.onKeyPressed(keyCode);
				}
			}
		};
		
		KeyboardLayout keylayout = keylayouts.get(index);
		for(List<KeyDef> row : keylayout.getKeys()) {
			for(KeyDef keydef : row) {
				final Button view = new Button(new ContextThemeWrapper(ctx, R.style.KeyButton), null, 0);
				view.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));

				/*
				view.setBackgroundDrawable(null);
				view.setMinHeight(0);
				view.setMinWidth(0);s
				view.setPadding(0, 0, 0, 0);
				// view.setBackgroundResource(R.drawable.button_bar);
				view.setTextSize(TypedValue.COMPLEX_UNIT_PX, ctx.getResources().getDimensionPixelSize(R.dimen.text_normal));
				*/
				view.setGravity(Gravity.CENTER);
				view.setText(keydef.getLabel());
				view.setTag(keydef.getValue());
				view.setFocusable(true);
				view.setFocusableInTouchMode(true);
				AndroidFonts.setViewFont(view, RetroBoxUtils.FONT_DEFAULT_R);<commit_msg>When focusable in touch mode, key must be pressed twice to work<commit_after>package retrobox.keyboard;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import retrobox.utils.R;
import retrobox.utils.RetroBoxUtils;
import xtvapps.core.AndroidFonts;
import xtvapps.core.SimpleCallback;
import xtvapps.core.Utils;

public class KeyboardView extends FrameLayout {
	
	public static final String SWITCH_LAYOUT = "SWITCH_LAYOUT_";
	public static final String TOGGLE_POSITION = "TOGGLE_POSITION";

	private List<KeyboardLayout> keylayouts = new ArrayList<KeyboardLayout>();
	private VirtualKeyListener onKeyListener;
	int activeLayout = 0;
	private SimpleCallback onTogglePositionCallback;

	public KeyboardView(Context context) {
		super(context);
	}

	public KeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public KeyboardView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public void init(Context ctx, KeyboardLayout keylayout) {
		Log.d("KEYB", "init");
		keylayouts.add(keylayout);
		if (keylayouts.size() == 1) switchLayout(ctx, 0);
		
	}
	
	private void switchLayout(final Context ctx, int index) {
		activeLayout = index;
		
		removeAllViews();
		
		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View view) {
				String keyCode = (String)view.getTag();
				Log.d("KEYB", "keyCode " + keyCode);
				if (keyCode.startsWith(SWITCH_LAYOUT)) {
					int layout = Utils.str2i(keyCode.substring(SWITCH_LAYOUT.length()));
					switchLayout(ctx, layout);
					getChildAt(0).requestFocus();
					return;
				}
				if (keyCode.equals(TOGGLE_POSITION) && onTogglePositionCallback!=null) {
					onTogglePositionCallback.onResult();
				}
				if (onKeyListener!=null) {
					onKeyListener.onKeyPressed(keyCode);
				}
			}
		};
		
		KeyboardLayout keylayout = keylayouts.get(index);
		for(List<KeyDef> row : keylayout.getKeys()) {
			for(KeyDef keydef : row) {
				final Button view = new Button(new ContextThemeWrapper(ctx, R.style.KeyButton), null, 0);
				view.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));

				/*
				view.setBackgroundDrawable(null);
				view.setMinHeight(0);
				view.setMinWidth(0);s
				view.setPadding(0, 0, 0, 0);
				// view.setBackgroundResource(R.drawable.button_bar);
				view.setTextSize(TypedValue.COMPLEX_UNIT_PX, ctx.getResources().getDimensionPixelSize(R.dimen.text_normal));
				*/
				view.setGravity(Gravity.CENTER);
				view.setText(keydef.getLabel());
				view.setTag(keydef.getValue());
				view.setFocusable(true);
				// view.setFocusableInTouchMode(true);
				AndroidFonts.setViewFont(view, RetroBoxUtils.FONT_DEFAULT_R);
{'fraction_non_alphanumeric': 0.10947401774397972, 'fraction_numerical': 0.0038022813688212928, 'mean_word_length': 4.552330694810906, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3102048', 'n_tokens_mistral': 2241, 'n_tokens_neox': 1955, 'n_words': 379}