Blob Blame Raw
package com.icystar.findnumber;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;
import android.widget.TextView;

public class SingleActivity extends Activity implements Game.Listener {
	private static Object staticHandle;
	private static String previousName;
	
	public static void save(FileOutputStream fos) throws IOException {
		synchronized (staticHandle) {
			Serializer.writeString(fos, previousName);
		}
	}
	
	public static void load(FileInputStream fis) throws IOException
		{ synchronized (staticHandle) { previousName = Serializer.readString(fis); } }
	
	public static String getPreviousName()
		{ synchronized (staticHandle) { return previousName; } }
	
	

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SoundPlayer.initSounds(getApplication());
        setContentView(R.layout.activity_single);
    }
    
	@Override
	protected void onResume() {
		super.onResume();
        Game.games[0].setListener(this);
        Game.games[0].setNumberTextView( (TextView)findViewById(R.id.TextView1) );
        Game.games[0].setTimeTextView( (TextView)findViewById(R.id.TextView2) );
        Game.games[0].setNumbersView( (NumbersView)findViewById(R.id.NumbersView1) );
        Game.games[0].setLockView( findViewById(R.id.frameLock) );
        ((NumbersView)findViewById(R.id.NumbersView1)).setGameIndex(0);
        Game.games[0].setPaused(false);
	}
    
	@Override
	protected void onPause() {
        Game.games[0].setPaused(true);
		Game.saveState(getApplication());
        ((NumbersView)findViewById(R.id.NumbersView1)).setGameIndex(-1);
        Game.games[0].setListener(null);
        Game.games[0].setNumberTextView(null);
        Game.games[0].setTimeTextView(null);
        Game.games[0].setNumbersView(null);
        Game.games[0].setLockView(null);
		super.onPause();
	}
	
	@Override
	public void onWin(Game game, float time) {
		SoundPlayer.playSound(R.raw.sound_win);

		final int count = game.getCount();
		final int secs = Math.round(time);
		final int rank = ScoreboardActivity.isRecord(count, secs); 
		final Activity activity = this;
		final String defaultPlayerName = getString(R.string.defaultPlayerName);
				
		if (rank > 0) {
			String name = Game.getPlayerName(0);
			if (name.length() == 0) name = defaultPlayerName;
			ScoreboardActivity.addNewRecord(count, secs, name);
			Game.saveState(getApplication());
			
			final EditText edit = new EditText(this);
			edit.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
			edit.setText(name);
			
			final Intent intent = new Intent(activity, ScoreboardActivity.class);
			intent.putExtra("count", count);
			intent.putExtra("rank", rank);			
			
			new AlertDialog.Builder(this)
				.setTitle(R.string.dialogRecordTitle)
				.setIcon(R.drawable.ic_dialog_win)
				.setMessage(getString(R.string.dialogRecordMessage, defaultPlayerName, rank, count, secs))
				.setView(edit)
				.setPositiveButton(R.string.dialogSubmit, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						String name = edit.getText().toString();
						ScoreboardActivity.updateRecord(count, secs, name);
						Game.setPlayerName(0, name);
						Game.saveState(getApplication());
						startActivity(intent);
						activity.finish();
					}
				})
				.setOnCancelListener(new OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialog) {
						String name = edit.getText().toString();
						ScoreboardActivity.updateRecord(count, secs, name);
						Game.setPlayerName(0, name);
						Game.saveState(getApplication());
						startActivity(intent);
						activity.finish();
					}
				})
				.show();
		} else {
			new AlertDialog.Builder(this)
				.setTitle(R.string.dialogWinTitle)
				.setIcon(R.drawable.ic_dialog_win)
				.setMessage(getString(R.string.dialogWinMessage, defaultPlayerName))
				.setPositiveButton(R.string.dialogOk, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) { activity.finish(); }
				})
				.setOnCancelListener(new OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialog) { activity.finish(); }
				})
				.show();
		}
	}

	@Override
	public void onTouchNumber(Game game, int touchedNumber, int maxNumber, boolean win) {
		if (!win) SoundPlayer.playSound(R.raw.sound_player1_clicked);
	}

	@Override
	public void onLock(Game game) {
		SoundPlayer.playSound(R.raw.sound_lock);
	}
}