<commit_before>package com.virtualapplications.play;

import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import java.util.*;

public class EmulatorActivity extends Activity 
{
	private static final String PREFERENCE_UI_SHOWFPS = "ui.showfps";
	private static final String PREFERENCE_UI_SHOWVIRTUALPAD = "ui.showvirtualpad";
	
	private SurfaceView _renderView;
	private TextView _statsTextView;
	private Timer _statsTimer = new Timer();
	private Handler _statsTimerHandler;
	
	public static void RegisterPreferences()
	{
		SettingsManager.registerPreferenceBoolean(PREFERENCE_UI_SHOWFPS, false);
		SettingsManager.registerPreferenceBoolean(PREFERENCE_UI_SHOWVIRTUALPAD, true);
	}
	
	@Override 
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		//Log.w(Constants.TAG, "EmulatorActivity - onCreate");
		
		setContentView(R.layout.emulator);

		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
				View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
				View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
				View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
				View.SYSTEM_UI_FLAG_FULLSCREEN |
				View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
		
		_renderView = (SurfaceView)findViewById(R.id.emulator_view);
		SurfaceHolder holder = _renderView.getHolder();
		holder.addCallback(new SurfaceCallback());
		
		if(!SettingsManager.getPreferenceBoolean(PREFERENCE_UI_SHOWVIRTUALPAD))
		{
			View virtualPadView = (View)findViewById(R.id.emulator_virtualpad);
			virtualPadView.setVisibility(View.GONE);
		}
		
		if(SettingsManager.getPreferenceBoolean(PREFERENCE_UI_SHOWFPS))
		{
			_statsTextView = (TextView)findViewById(R.id.emulator_stats);
			setupStatsTimer();
		}
	}
	
	@Override
	public void onPause()
	{
		super.onPause();
		NativeInterop.pauseVirtualMachine();
	}
	
	@Override
	public void onDestroy()
	{
		_statsTimer.cancel();
		super.onDestroy();
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event)
	{
		int action = event.getAction();
		if((action == KeyEvent.ACTION_DOWN) || (action == KeyEvent.ACTION_UP))
		{
			boolean pressed = (action == KeyEvent.ACTION_DOWN);
			switch(event.getKeyCode())
			{
			case KeyEvent.KEYCODE_DPAD_UP:
				InputManager.setButtonState(InputManagerConstants.BUTTON_UP, pressed);
				return true;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				InputManager.setButtonState(InputManagerConstants.BUTTON_DOWN, pressed);
				return true;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				InputManager.setButtonState(InputManagerConstants.BUTTON_LEFT, pressed);
				return true;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				InputManager.setButtonState(InputManagerConstants.BUTTON_RIGHT, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_A:
				InputManager.setButtonState(InputManagerConstants.BUTTON_CROSS, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_B:
				InputManager.setButtonState(InputManagerConstants.BUTTON_CIRCLE, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_X:
				InputManager.setButtonState(InputManagerConstants.BUTTON_SQUARE, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_Y:
				InputManager.setButtonState(InputManagerConstants.BUTTON_TRIANGLE, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_L1:
				InputManager.setButtonState(InputManagerConstants.BUTTON_L1, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_L2:
				InputManager.setButtonState(InputManagerConstants.BUTTON_L2, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_R1:
				InputManager.setButtonState(InputManagerConstants.BUTTON_R1, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_R2:
				InputManager.setButtonState(InputManagerConstants.BUTTON_R2, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_START:
				InputManager.setButtonState(InputManagerConstants.BUTTON_START, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_SELECT:
				InputManager.setButtonState(InputManagerConstants.BUTTON_SELECT, pressed);
				return true;
			}
		}
		//Log.w(Constants.TAG, String.format("Key event %d, device id %d.", event.getKeyCode(), event.getDeviceId()));
		return super.dispatchKeyEvent(event);
	}
	
	@Override
	public boolean dispatchGenericMotionEvent(MotionEvent event)
	{
		int action = event.getActionMasked();
		if(action == MotionEvent.ACTION_MOVE)
		{
			float lx = event.getAxisValue(MotionEvent.AXIS_X);
			float ly = event.getAxisValue(MotionEvent.AXIS_Y);
			float rx = event.getAxisValue(MotionEvent.AXIS_Z);
			float ry = event.getAxisValue(MotionEvent.AXIS_RZ);
			InputManager.setAxisState(InputManagerConstants.ANALOG_LEFT_X, lx);
			InputManager.setAxisState(InputManagerConstants.ANALOG_LEFT_Y, ly);
			InputManager.setAxisState(InputManagerConstants.ANALOG_RIGHT_X, rx);
			InputManager.setAxisState(InputManagerConstants.ANALOG_RIGHT_Y, ry);
			//Log.w(Constants.TAG, String.format("lx: %f, ly: %f, rx: %f, ry: %f", lx, ly, rx, ry));
		}
		return super.dispatchGenericMotionEvent(event);
	}

	@Override
	public void onBackPressed()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog));

		builder.setTitle("Exit Game?");
		builder.setMessage("Are You Sure You Want To Exit The Game? Any Unsaved Progress Will Be Lost.");

		builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						finish();
					}
				  })
				.setNegativeButton("No",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						dialog.cancel();
					}
				});
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	private void setupStatsTimer()
	{
		_statsTimerHandler = 
			new Handler()
			{
				@Override
				public void handleMessage(Message message)
				{
					int frames = StatsManager.getFrames();
					int drawCalls = StatsManager.getDrawCalls();<commit_msg>Update EmulatorActivity.java<commit_after>package com.virtualapplications.play;

import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import java.util.*;

public class EmulatorActivity extends Activity 
{
	private static final String PREFERENCE_UI_SHOWFPS = "ui.showfps";
	private static final String PREFERENCE_UI_SHOWVIRTUALPAD = "ui.showvirtualpad";
	
	private SurfaceView _renderView;
	private TextView _statsTextView;
	private Timer _statsTimer = new Timer();
	private Handler _statsTimerHandler;
	
	public static void RegisterPreferences()
	{
		SettingsManager.registerPreferenceBoolean(PREFERENCE_UI_SHOWFPS, false);
		SettingsManager.registerPreferenceBoolean(PREFERENCE_UI_SHOWVIRTUALPAD, true);
	}
	
	@Override 
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		//Log.w(Constants.TAG, "EmulatorActivity - onCreate");
		
		setContentView(R.layout.emulator);

		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
				View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
				View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
				View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
				View.SYSTEM_UI_FLAG_FULLSCREEN |
				View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
		
		_renderView = (SurfaceView)findViewById(R.id.emulator_view);
		SurfaceHolder holder = _renderView.getHolder();
		holder.addCallback(new SurfaceCallback());
		
		if(!SettingsManager.getPreferenceBoolean(PREFERENCE_UI_SHOWVIRTUALPAD))
		{
			View virtualPadView = (View)findViewById(R.id.emulator_virtualpad);
			virtualPadView.setVisibility(View.GONE);
		}
		
		if(SettingsManager.getPreferenceBoolean(PREFERENCE_UI_SHOWFPS))
		{
			_statsTextView = (TextView)findViewById(R.id.emulator_stats);
			setupStatsTimer();
		}
	}
	
	@Override
	public void onPause()
	{
		super.onPause();
		NativeInterop.pauseVirtualMachine();
	}
	
	@Override
	public void onDestroy()
	{
		_statsTimer.cancel();
		super.onDestroy();
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event)
	{
		int action = event.getAction();
		if((action == KeyEvent.ACTION_DOWN) || (action == KeyEvent.ACTION_UP))
		{
			boolean pressed = (action == KeyEvent.ACTION_DOWN);
			switch(event.getKeyCode())
			{
			case KeyEvent.KEYCODE_DPAD_UP:
				InputManager.setButtonState(InputManagerConstants.BUTTON_UP, pressed);
				return true;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				InputManager.setButtonState(InputManagerConstants.BUTTON_DOWN, pressed);
				return true;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				InputManager.setButtonState(InputManagerConstants.BUTTON_LEFT, pressed);
				return true;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				InputManager.setButtonState(InputManagerConstants.BUTTON_RIGHT, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_A:
				InputManager.setButtonState(InputManagerConstants.BUTTON_CROSS, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_B:
				InputManager.setButtonState(InputManagerConstants.BUTTON_CIRCLE, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_X:
				InputManager.setButtonState(InputManagerConstants.BUTTON_SQUARE, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_Y:
				InputManager.setButtonState(InputManagerConstants.BUTTON_TRIANGLE, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_L1:
				InputManager.setButtonState(InputManagerConstants.BUTTON_L1, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_L2:
				InputManager.setButtonState(InputManagerConstants.BUTTON_L2, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_R1:
				InputManager.setButtonState(InputManagerConstants.BUTTON_R1, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_R2:
				InputManager.setButtonState(InputManagerConstants.BUTTON_R2, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_START:
				InputManager.setButtonState(InputManagerConstants.BUTTON_START, pressed);
				return true;
			case KeyEvent.KEYCODE_BUTTON_SELECT:
				InputManager.setButtonState(InputManagerConstants.BUTTON_SELECT, pressed);
				return true;
			}
		}
		//Log.w(Constants.TAG, String.format("Key event %d, device id %d.", event.getKeyCode(), event.getDeviceId()));
		return super.dispatchKeyEvent(event);
	}
	
	@Override
	public boolean dispatchGenericMotionEvent(MotionEvent event)
	{
		int action = event.getActionMasked();
		if(action == MotionEvent.ACTION_MOVE)
		{
			float lx = event.getAxisValue(MotionEvent.AXIS_X);
			float ly = event.getAxisValue(MotionEvent.AXIS_Y);
			float rx = event.getAxisValue(MotionEvent.AXIS_Z);
			float ry = event.getAxisValue(MotionEvent.AXIS_RZ);
			InputManager.setAxisState(InputManagerConstants.ANALOG_LEFT_X, lx);
			InputManager.setAxisState(InputManagerConstants.ANALOG_LEFT_Y, ly);
			InputManager.setAxisState(InputManagerConstants.ANALOG_RIGHT_X, rx);
			InputManager.setAxisState(InputManagerConstants.ANALOG_RIGHT_Y, ry);
			//Log.w(Constants.TAG, String.format("lx: %f, ly: %f, rx: %f, ry: %f", lx, ly, rx, ry));
		}
		return super.dispatchGenericMotionEvent(event);
	}

	@Override
	public void onBackPressed()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog));

		builder.setTitle("Exit Game?");
		builder.setMessage("Are You Sure You Want To Exit The Game?");

		builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						finish();
					}
				  })
				.setNegativeButton("No",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						dialog.cancel();
					}
				});
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	private void setupStatsTimer()
	{
		_statsTimerHandler = 
			new Handler()
			{
				@Override
				public void handleMessage(Message message)
				{
					int frames = StatsManager.getFrames();
					int drawCalls = StatsManager.getDrawCalls();
{'fraction_non_alphanumeric': 0.09827160493827161, 'fraction_numerical': 0.0013168724279835392, 'mean_word_length': 5.155521783181357, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6722302', 'n_tokens_mistral': 4419, 'n_tokens_neox': 3806, 'n_words': 636}