package com.example.sudoku; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; public class Sudoku extends Activity implements OnClickListener { private static final String TAG = "Sudoku"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sudoku); // Set up click listeners for all the buttons View continueButton = findViewById(R.id.continue_button); continueButton.setOnClickListener(this); View newButton = findViewById(R.id.new_button); newButton.setOnClickListener(this); View aboutButton = findViewById(R.id.about_button); aboutButton.setOnClickListener(this); View exitButton = findViewById(R.id.exit_button); exitButton.setOnClickListener(this); // The following segment is to test files on Android String fname = "test.dat"; String data = "hello world!"; try { InputStream is = openFileInput(fname); DataInputStream dis = new DataInputStream(is); Log.d(TAG, "test.dat exists, read contents"); int length = dis.available(); byte d[] = new byte[length]; dis.readFully(d); Log.d(TAG, "File Content: " + new String(d)); is.close(); } catch (IOException ex) { Log.d(TAG, "test.dat doesn't exits, create one"); try { FileOutputStream ps = openFileOutput("test.dat", MODE_PRIVATE); byte[] d = data.getBytes(); ps.write(d); ps.close(); } catch (IOException e) { Log.d(TAG, "error in creating test.dat"); } } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.about_button: Intent i = new Intent(this, About.class); startActivity(i); break; case R.id.new_button: openNewGameDialog(); break; case R.id.continue_button: startGame(Game.DIFFICULTY_CONTINUE); break; case R.id.exit_button: finish(); break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.sudoku, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.settings: startActivity(new Intent(this, Prefs.class)); return true; } return false; } /** Ask the user what difficulty level they want */ private void openNewGameDialog() { new AlertDialog.Builder(this) .setTitle(R.string.new_game_title) .setItems(R.array.difficulty, new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialoginterface, int i) { startGame(i); } }).show(); } /** Start a new game with the given difficulty level */ private void startGame(int i) { Log.d(TAG, "clicked on " + i); Intent intent = new Intent(Sudoku.this, Game.class); intent.putExtra(Game.KEY_DIFFICULTY, i); startActivity(intent); } /** Action on resume */ @Override protected void onResume() { super.onResume(); Music.play(this, R.raw.main); } /** Action on pause */ @Override protected void onPause() { super.onPause(); Music.stop(this); } }