Blame src/com/icystar/findnumber/SingleActivity.java

1b8111
package com.icystar.findnumber;
1b8111
1b8111
import java.io.FileInputStream;
1b8111
import java.io.FileOutputStream;
1b8111
import java.io.IOException;
1b8111
1b8111
import android.app.Activity;
1b8111
import android.app.AlertDialog;
1b8111
import android.content.DialogInterface;
1b8111
import android.content.DialogInterface.OnCancelListener;
1b8111
import android.content.DialogInterface.OnClickListener;
1b8111
import android.content.Intent;
1b8111
import android.os.Bundle;
1b8111
import android.text.InputType;
1b8111
import android.widget.EditText;
1b8111
import android.widget.TextView;
1b8111
1b8111
public class SingleActivity extends Activity implements Game.Listener {
1b8111
	private static Object staticHandle;
1b8111
	private static String previousName;
1b8111
	
1b8111
	public static void save(FileOutputStream fos) throws IOException {
1b8111
		synchronized (staticHandle) {
1b8111
			Serializer.writeString(fos, previousName);
1b8111
		}
1b8111
	}
1b8111
	
1b8111
	public static void load(FileInputStream fis) throws IOException
1b8111
		{ synchronized (staticHandle) { previousName = Serializer.readString(fis); } }
1b8111
	
1b8111
	public static String getPreviousName()
1b8111
		{ synchronized (staticHandle) { return previousName; } }
1b8111
	
1b8111
	
1b8111
1b8111
	@Override
1b8111
    public void onCreate(Bundle savedInstanceState) {
1b8111
        super.onCreate(savedInstanceState);
1b8111
        SoundPlayer.initSounds(getApplication());
1b8111
        setContentView(R.layout.activity_single);
1b8111
    }
1b8111
    
1b8111
	@Override
1b8111
	protected void onResume() {
1b8111
		super.onResume();
1b8111
        Game.games[0].setListener(this);
1b8111
        Game.games[0].setNumberTextView( (TextView)findViewById(R.id.TextView1) );
1b8111
        Game.games[0].setTimeTextView( (TextView)findViewById(R.id.TextView2) );
1b8111
        Game.games[0].setNumbersView( (NumbersView)findViewById(R.id.NumbersView1) );
1b8111
        Game.games[0].setLockView( findViewById(R.id.frameLock) );
1b8111
        ((NumbersView)findViewById(R.id.NumbersView1)).setGameIndex(0);
1b8111
        Game.games[0].setPaused(false);
1b8111
	}
1b8111
    
1b8111
	@Override
1b8111
	protected void onPause() {
1b8111
        Game.games[0].setPaused(true);
1b8111
		Game.saveState(getApplication());
1b8111
        ((NumbersView)findViewById(R.id.NumbersView1)).setGameIndex(-1);
1b8111
        Game.games[0].setListener(null);
1b8111
        Game.games[0].setNumberTextView(null);
1b8111
        Game.games[0].setTimeTextView(null);
1b8111
        Game.games[0].setNumbersView(null);
1b8111
        Game.games[0].setLockView(null);
1b8111
		super.onPause();
1b8111
	}
1b8111
	
1b8111
	@Override
1b8111
	public void onWin(Game game, float time) {
1b8111
		SoundPlayer.playSound(R.raw.sound_win);
1b8111
1b8111
		final int count = game.getCount();
1b8111
		final int secs = Math.round(time);
1b8111
		final int rank = ScoreboardActivity.isRecord(count, secs); 
1b8111
		final Activity activity = this;
1b8111
		final String defaultPlayerName = getString(R.string.defaultPlayerName);
1b8111
				
1b8111
		if (rank > 0) {
1b8111
			String name = Game.getPlayerName(0);
1b8111
			if (name.length() == 0) name = defaultPlayerName;
1b8111
			ScoreboardActivity.addNewRecord(count, secs, name);
1b8111
			Game.saveState(getApplication());
1b8111
			
1b8111
			final EditText edit = new EditText(this);
1b8111
			edit.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
1b8111
			edit.setText(name);
1b8111
			
1b8111
			final Intent intent = new Intent(activity, ScoreboardActivity.class);
1b8111
			intent.putExtra("count", count);
1b8111
			intent.putExtra("rank", rank);			
1b8111
			
1b8111
			new AlertDialog.Builder(this)
1b8111
				.setTitle(R.string.dialogRecordTitle)
1b8111
				.setIcon(R.drawable.ic_dialog_win)
1b8111
				.setMessage(getString(R.string.dialogRecordMessage, defaultPlayerName, rank, count, secs))
1b8111
				.setView(edit)
1b8111
				.setPositiveButton(R.string.dialogSubmit, new OnClickListener() {
1b8111
					@Override
1b8111
					public void onClick(DialogInterface dialog, int which) {
1b8111
						String name = edit.getText().toString();
1b8111
						ScoreboardActivity.updateRecord(count, secs, name);
1b8111
						Game.setPlayerName(0, name);
1b8111
						Game.saveState(getApplication());
1b8111
						startActivity(intent);
1b8111
						activity.finish();
1b8111
					}
1b8111
				})
1b8111
				.setOnCancelListener(new OnCancelListener() {
1b8111
					@Override
1b8111
					public void onCancel(DialogInterface dialog) {
1b8111
						String name = edit.getText().toString();
1b8111
						ScoreboardActivity.updateRecord(count, secs, name);
1b8111
						Game.setPlayerName(0, name);
1b8111
						Game.saveState(getApplication());
1b8111
						startActivity(intent);
1b8111
						activity.finish();
1b8111
					}
1b8111
				})
1b8111
				.show();
1b8111
		} else {
1b8111
			new AlertDialog.Builder(this)
1b8111
				.setTitle(R.string.dialogWinTitle)
1b8111
				.setIcon(R.drawable.ic_dialog_win)
1b8111
				.setMessage(getString(R.string.dialogWinMessage, defaultPlayerName))
1b8111
				.setPositiveButton(R.string.dialogOk, new OnClickListener() {
1b8111
					@Override
1b8111
					public void onClick(DialogInterface dialog, int which) { activity.finish(); }
1b8111
				})
1b8111
				.setOnCancelListener(new OnCancelListener() {
1b8111
					@Override
1b8111
					public void onCancel(DialogInterface dialog) { activity.finish(); }
1b8111
				})
1b8111
				.show();
1b8111
		}
1b8111
	}
1b8111
1b8111
	@Override
1b8111
	public void onTouchNumber(Game game, int touchedNumber, int maxNumber, boolean win) {
1b8111
		if (!win) SoundPlayer.playSound(R.raw.sound_player1_clicked);
1b8111
	}
1b8111
1b8111
	@Override
1b8111
	public void onLock(Game game) {
1b8111
		SoundPlayer.playSound(R.raw.sound_lock);
1b8111
	}
1b8111
}