<commit_before>package com.chess.genesis;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;

public class GameList extends Activity implements OnClickListener, OnLongClickListener, OnItemClickListener, OnTouchListener
{
	public static GameList self;

	public GameListAdapter gamelist_adapter;

	private Bundle settings;
	private NetworkClient net;
	private int type;
	private boolean yourmove;

	private final Handler handle = new Handler()
	{
		public void handleMessage(final Message msg)
		{
			switch (msg.what) {
			case NewOnlineGameDialog.MSG:
				Bundle data = (Bundle) msg.obj;

				if (data.getInt("opponent") == Enums.INVITE) {
					(new InviteOptionsDialog(self, handle, data)).show();
					return;
				}
				String username = settings.getString("username");
				String gametype = Enums.GameType(data.getInt("gametype"));

				net.join_game(username, gametype);
				(new Thread(net)).start();
				Toast.makeText(getApplication(), "Connecting to server...", Toast.LENGTH_LONG).show();
				break;
			case InviteOptionsDialog.MSG:
				data = (Bundle) msg.obj;

				username = settings.getString("username");
				gametype = Enums.GameType(data.getInt("gametype"));
				final String color = Enums.ColorType(data.getInt("color"));

				net.new_game(username, data.getString("opp_name"), gametype, color);
				(new Thread(net)).start();
				Toast.makeText(getApplication(), "Connecting to server...", Toast.LENGTH_LONG).show();
				break;
			case NewLocalGameDialog.MSG:
				final GameDataDB db = new GameDataDB(self);
				final Bundle bundle = (Bundle) msg.obj;

				final int gametype2 = bundle.getInt("gametype");
				final int gameopp = bundle.getInt("opponent");
				String gamename = bundle.getString("name");
				if (gamename.length() < 1)
					gamename = "untitled";

				final Intent intent = new Intent(self, Game.class);
				intent.putExtras(db.newLocalGame(gamename, gametype2, gameopp));
				intent.putExtras(settings);
				db.close();

				startActivity(intent);
				break;
			case SyncGameList.MSG:
			case NetworkClient.JOIN_GAME:
			case NetworkClient.NEW_GAME:
				final JSONObject json = (JSONObject) msg.obj;
				try {
					if (json.getString("result").equals("error")) {
						Toast.makeText(self, "ERROR:\n" + json.getString("reason"), Toast.LENGTH_LONG).show();
						return;
					}
					Toast.makeText(getApplication(), json.getString("reason"), Toast.LENGTH_LONG).show();

					if (msg.what == SyncGameList.MSG)
						gamelist_adapter.update();
				} catch (JSONException e) {
					e.printStackTrace();
					throw new RuntimeException();
				}
				break;
			}
		}
	};

	@Override
	public void onCreate(final Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		self = this;

		// Set only portrait
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		// store settings from main menu
		final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

		settings = getIntent().getExtras();
		settings.putString("username", prefs.getString("username", "!error!"));
		type = settings.getInt("type", Enums.ONLINE_GAME);
		yourmove = true;

		net = new NetworkClient(this, handle);

		// set content view
		switch (type) {
		case Enums.ONLINE_GAME:
			setContentView(R.layout.gamelist_online);

			ImageView button = (ImageView) findViewById(R.id.your_move);
			button.setOnTouchListener(this);
			button.setOnClickListener(this);

			button = (ImageView) findViewById(R.id.their_move);
			button.setOnTouchListener(this);
			button.setOnClickListener(this);
			break;
		case Enums.LOCAL_GAME:
		default:
			setContentView(R.layout.gamelist_local);
			break;
		case Enums.ARCHIVE_GAME:
			setContentView(R.layout.gamelist_archive);
			break;
		}

		// set click listeners
		switch (type) {
		case Enums.ONLINE_GAME:
		case Enums.LOCAL_GAME:
		default:
			ImageView button = (ImageView) findViewById(R.id.topbar_genesis);
			button.setOnTouchListener(this);
			button.setOnLongClickListener(this);

			button = (ImageView) findViewById(R.id.topbar_plus);
			button.setOnTouchListener(this);
			button.setOnClickListener(this);
			break;
		case Enums.ARCHIVE_GAME:
			button = (ImageView) findViewById(R.id.topbar);
			button.setOnTouchListener(this);
			button.setOnLongClickListener(this);
			break;
		}

		// set list adapters
		gamelist_adapter = new GameListAdapter(this, settings);

		final ListView gamelist_view = (ListView) findViewById(R.id.game_list);
		gamelist_view.setAdapter(gamelist_adapter);
		gamelist_view.setOnItemClickListener(this);

		registerForContextMenu(gamelist_view);
	}

	@Override
	public void onResume()
	{
		super.onResume();

		if (settings.getInt("type", Enums.ONLINE_GAME) == Enums.ONLINE_GAME) {
			NetActive.inc();

			final SyncGameList sync = new SyncGameList(this, handle, settings.getString("username"));
			(new Thread(sync)).start();
			Toast.makeText(getApplication(), "Updating game list...", Toast.LENGTH_LONG).show();
		} else {
			gamelist_adapter.update();
		}
	}

	@Override
	public void onPause()
	{
		if (settings.getInt("type") == Enums.ONLINE_GAME)
			NetActive.dec();

		super.onPause();
	}

	@Override
	public void onDestroy()
	{
		gamelist_adapter.close();
		super.onDestroy();
	}

	public boolean onTouch(final View v, final MotionEvent event)
	{
		switch (v.getId()) {
		case R.id.topbar:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
				((ImageView) v).setImageResource(R.drawable.topbar_pressed);
			else if (event.getAction() == MotionEvent.ACTION_UP)
				((ImageView) v).setImageResource(R.drawable.topbar);<commit_msg>fix crash when SyncGameList is final<commit_after>package com.chess.genesis;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;

public class GameList extends Activity implements OnClickListener, OnLongClickListener, OnItemClickListener, OnTouchListener
{
	public static GameList self;

	public GameListAdapter gamelist_adapter;

	private Bundle settings;
	private NetworkClient net;
	private int type;
	private boolean yourmove;

	private final Handler handle = new Handler()
	{
		public void handleMessage(final Message msg)
		{
			switch (msg.what) {
			case NewOnlineGameDialog.MSG:
				Bundle data = (Bundle) msg.obj;

				if (data.getInt("opponent") == Enums.INVITE) {
					(new InviteOptionsDialog(self, handle, data)).show();
					return;
				}
				String username = settings.getString("username");
				String gametype = Enums.GameType(data.getInt("gametype"));

				net.join_game(username, gametype);
				(new Thread(net)).start();
				Toast.makeText(getApplication(), "Connecting to server...", Toast.LENGTH_LONG).show();
				break;
			case InviteOptionsDialog.MSG:
				data = (Bundle) msg.obj;

				username = settings.getString("username");
				gametype = Enums.GameType(data.getInt("gametype"));
				final String color = Enums.ColorType(data.getInt("color"));

				net.new_game(username, data.getString("opp_name"), gametype, color);
				(new Thread(net)).start();
				Toast.makeText(getApplication(), "Connecting to server...", Toast.LENGTH_LONG).show();
				break;
			case NewLocalGameDialog.MSG:
				final GameDataDB db = new GameDataDB(self);
				final Bundle bundle = (Bundle) msg.obj;

				final int gametype2 = bundle.getInt("gametype");
				final int gameopp = bundle.getInt("opponent");
				String gamename = bundle.getString("name");
				if (gamename.length() < 1)
					gamename = "untitled";

				final Intent intent = new Intent(self, Game.class);
				intent.putExtras(db.newLocalGame(gamename, gametype2, gameopp));
				intent.putExtras(settings);
				db.close();

				startActivity(intent);
				break;
			case SyncGameList.MSG:
			case NetworkClient.JOIN_GAME:
			case NetworkClient.NEW_GAME:
				final JSONObject json = (JSONObject) msg.obj;
				try {
					if (json.getString("result").equals("error")) {
						Toast.makeText(self, "ERROR:\n" + json.getString("reason"), Toast.LENGTH_LONG).show();
						return;
					}
					Toast.makeText(getApplication(), json.getString("reason"), Toast.LENGTH_LONG).show();

					if (msg.what == SyncGameList.MSG)
						gamelist_adapter.update();
				} catch (JSONException e) {
					e.printStackTrace();
					throw new RuntimeException();
				}
				break;
			}
		}
	};

	@Override
	public void onCreate(final Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		self = this;

		// Set only portrait
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		// store settings from main menu
		final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

		settings = getIntent().getExtras();
		settings.putString("username", prefs.getString("username", "!error!"));
		type = settings.getInt("type", Enums.ONLINE_GAME);
		yourmove = true;

		net = new NetworkClient(this, handle);

		// set content view
		switch (type) {
		case Enums.ONLINE_GAME:
			setContentView(R.layout.gamelist_online);

			ImageView button = (ImageView) findViewById(R.id.your_move);
			button.setOnTouchListener(this);
			button.setOnClickListener(this);

			button = (ImageView) findViewById(R.id.their_move);
			button.setOnTouchListener(this);
			button.setOnClickListener(this);
			break;
		case Enums.LOCAL_GAME:
		default:
			setContentView(R.layout.gamelist_local);
			break;
		case Enums.ARCHIVE_GAME:
			setContentView(R.layout.gamelist_archive);
			break;
		}

		// set click listeners
		switch (type) {
		case Enums.ONLINE_GAME:
		case Enums.LOCAL_GAME:
		default:
			ImageView button = (ImageView) findViewById(R.id.topbar_genesis);
			button.setOnTouchListener(this);
			button.setOnLongClickListener(this);

			button = (ImageView) findViewById(R.id.topbar_plus);
			button.setOnTouchListener(this);
			button.setOnClickListener(this);
			break;
		case Enums.ARCHIVE_GAME:
			button = (ImageView) findViewById(R.id.topbar);
			button.setOnTouchListener(this);
			button.setOnLongClickListener(this);
			break;
		}

		// set list adapters
		gamelist_adapter = new GameListAdapter(this, settings);

		final ListView gamelist_view = (ListView) findViewById(R.id.game_list);
		gamelist_view.setAdapter(gamelist_adapter);
		gamelist_view.setOnItemClickListener(this);

		registerForContextMenu(gamelist_view);
	}

	@Override
	public void onResume()
	{
		super.onResume();

		if (settings.getInt("type", Enums.ONLINE_GAME) == Enums.ONLINE_GAME) {
			NetActive.inc();

			// Must not be final
			SyncGameList sync = new SyncGameList(this, handle, settings.getString("username"));
			(new Thread(sync)).start();
			Toast.makeText(getApplication(), "Updating game list...", Toast.LENGTH_LONG).show();
		} else {
			gamelist_adapter.update();
		}
	}

	@Override
	public void onPause()
	{
		if (settings.getInt("type") == Enums.ONLINE_GAME)
			NetActive.dec();

		super.onPause();
	}

	@Override
	public void onDestroy()
	{
		gamelist_adapter.close();
		super.onDestroy();
	}

	public boolean onTouch(final View v, final MotionEvent event)
	{
		switch (v.getId()) {
		case R.id.topbar:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
				((ImageView) v).setImageResource(R.drawable.topbar_pressed);
			else if (event.getAction() == MotionEvent.ACTION_UP)
				((ImageView) v).setImageResource(R.drawable.topbar);
{'fraction_non_alphanumeric': 0.12271844660194174, 'fraction_numerical': 0.00046601941747572817, 'mean_word_length': 4.542832544123978, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2959305', 'n_tokens_mistral': 4722, 'n_tokens_neox': 3975, 'n_words': 803}