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.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TabHost;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class ScoreboardActivity extends Activity {
	public static final int maxScoresCount = 10;
	public static final int[] counts = { 25, 50, 100 };
	
	public static class Score {
		boolean exists = false;
		String name = "";
		int count = 0;
		int time = 0;
		float secondsPerNumber = 0;

		public void save(FileOutputStream fos) throws IOException {
			Serializer.writeInt(fos, exists ? 1 : 0);
			Serializer.writeString(fos, name);
			Serializer.writeInt(fos, count);
			Serializer.writeInt(fos, time);
		}
		
		public void load(FileInputStream fis) throws IOException {
			exists = Serializer.readInt(fis) != 0;
			name   = Serializer.readString(fis);
			count  = Serializer.readInt(fis);
			time   = Serializer.readInt(fis);
			secondsPerNumber = count > 0 ? (float)time/(float)count : time;
		}
	}
	
	private static Object staticHandle = new Object();
	private static Score[][] scores;
	
	private static void fixScores() {
		scores = Serializer.resizeArrayIfNeed(Score[].class, scores, counts.length);
		for(int i = 0; i < scores.length; i++) {
			scores[i] = Serializer.resizeArrayIfNeed(Score.class, scores[i], maxScoresCount);
			for(int j = 0; j < scores[i].length; j++)
				if (scores[i][j] == null) scores[i][j] = new Score();
		}
	}
	
	public static void save(FileOutputStream fos) throws IOException {
		synchronized (staticHandle) {
			fixScores();
			Serializer.writeInt(fos, scores.length);
			for(Score[] ss : scores) {
				Serializer.writeInt(fos, ss.length);
				for(Score score : ss) score.save(fos);
			}
		}
	}
	
	public static void load(FileInputStream fis) throws IOException {
		synchronized (staticHandle) {
			Score[][] ss = null;
			try {
				ss = new Score[Serializer.readInt(fis)][0];
				for(int i = 0; i < ss.length; i++) {
					ss[i] = new Score[Serializer.readInt(fis)];
					for(int j = 0; j < ss[i].length; j++) {
						ss[i][j] = new Score();
						ss[i][j].load(fis);
					}
				}
			} finally {
				scores = ss;
				fixScores();
			}
		}
	}
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scoreboard);

		TabHost tabs = (TabHost)findViewById(android.R.id.tabhost);

		tabs.setup();

		TabHost.TabSpec spec;
		boolean xlarge = getString(R.string.screenSize).compareTo("xlarge") == 0;

		spec = tabs.newTabSpec("tap25");
		spec.setContent(R.id.tab25);
		spec.setIndicator(getString(R.string.radio25), xlarge ? null : getResources().getDrawable(R.drawable.ic_tab_n25));
		tabs.addTab(spec);

		spec = tabs.newTabSpec("tag50");
		spec.setContent(R.id.tab50);
		spec.setIndicator(getString(R.string.radio50), xlarge ? null : getResources().getDrawable(R.drawable.ic_tab_n50));
		tabs.addTab(spec);

		spec = tabs.newTabSpec("tab100");
		spec.setContent(R.id.tab100);
		spec.setIndicator(getString(R.string.radio100), xlarge ? null : getResources().getDrawable(R.drawable.ic_tab_n100));
		tabs.addTab(spec);

		tabs.setCurrentTab( getCountMode(Game.games[0].getCount(), 0) );
    }

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

    	synchronized (staticHandle) {
    		int count = getIntent().getIntExtra("count", 0);
    		int rank = getIntent().getIntExtra("rank", 0);
    		
    		fixScores();
        	final int[] ids = new int[] { R.id.tab25, R.id.tab50, R.id.tab100 };
        	for(int i = 0; i < scores.length; i++) {
        		TableLayout table = (TableLayout)findViewById(ids[i]);
        		table.removeAllViews();

        		// first row - column titles
    			TableRow row = new TableRow(this);
        		TextView text;

        		int margin = getResources().getDimensionPixelSize(R.dimen.margin);
        		TableRow.LayoutParams layoutRank = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
        		layoutRank.setMargins(margin, 0, margin, 0);
        		TableRow.LayoutParams layoutName = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
        		layoutName.setMargins(margin, 0, margin, 0);
        		TableRow.LayoutParams layoutTimeCount = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
        		layoutTimeCount.setMargins(margin, 0, margin, 0);
        		TableRow.LayoutParams layoutSecondsPerNumber = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
        		layoutSecondsPerNumber.setMargins(margin, 0, margin, 0);

    			text = new TextView(this);
    			text.setText(R.string.scoreRank);
    			text.setTypeface(Typeface.DEFAULT_BOLD);
    			text.setTextSize(text.getTextSize()/2f);
    			text.setSingleLine();
    			text.setGravity(Gravity.RIGHT);
    			row.addView(text);
    			text.setLayoutParams(layoutRank);
    			
    			text = new TextView(this);
    			text.setText(R.string.scoreName);
    			text.setTypeface(Typeface.DEFAULT_BOLD);
    			text.setTextSize(text.getTextSize()/2f);
    			text.setSingleLine();
    			text.setGravity(Gravity.LEFT);
    			row.addView(text);
    			text.setLayoutParams(layoutName);
    			
    			text = new TextView(this);
    			text.setText(R.string.scoreTimeCount);
    			text.setTypeface(Typeface.DEFAULT_BOLD);
    			text.setTextSize(text.getTextSize()/2f);
    			text.setSingleLine();
    			text.setGravity(Gravity.RIGHT);
    			row.addView(text);
    			text.setLayoutParams(layoutTimeCount);
    			
    			text = new TextView(this);
    			text.setText(R.string.scoreSecondsPerNumber);
    			text.setTypeface(Typeface.DEFAULT_BOLD);
    			text.setTextSize(text.getTextSize()/2f);
    			text.setSingleLine();
    			text.setGravity(Gravity.RIGHT);
    			row.addView(text);
    			text.setLayoutParams(layoutSecondsPerNumber);

    			table.addView(row);

    			// rows
    			for(int j = 0; j < scores[i].length; j++) {
        			row = new TableRow(this);
        			boolean blank = !scores[i][j].exists;
        			String dummy = "  --  ";
        			
        			if (counts[i] == count && j+1 == rank)
        				row.setBackgroundColor(getResources().getColor(R.color.statusbar_color));
        			
        			text = new TextView(this);
        			text.setText(String.format("%d. ", j+1));
        			text.setSingleLine();
        			text.setGravity(Gravity.RIGHT);
        			row.addView(text);
        			text.setLayoutParams(layoutRank);
        			
        			text = new TextView(this);
        			text.setText(blank ? dummy : scores[i][j].name);
        			text.setSingleLine();
        			text.setGravity(Gravity.LEFT);
        			row.addView(text);
        			text.setLayoutParams(layoutName);
        			
        			text = new TextView(this);
        			text.setText(blank ? dummy : String.format("%d/%d", scores[i][j].time, scores[i][j].count));
        			text.setSingleLine();
        			text.setGravity(Gravity.RIGHT);
        			row.addView(text);
        			text.setLayoutParams(layoutTimeCount);
        			
        			text = new TextView(this);
        			text.setText(blank ? dummy : String.format("%.3f", scores[i][j].secondsPerNumber));
        			text.setSingleLine();
        			text.setGravity(Gravity.RIGHT);
        			row.addView(text);
        			text.setLayoutParams(layoutSecondsPerNumber);

        			table.addView(row);
        		}
        	}
		}
    }
    
    private static int getCountMode(int count, int def) {
    	for(int i = 0; i < counts.length; i++)
    		if (counts[i] == count)
    			return i;
    	return def;
    }
    
    public static int isRecord(int count, int time) {
    	synchronized (staticHandle) {
			fixScores();
			int countMode = getCountMode(count, -1);
			if (countMode >= 0)
				for(int i = 0; i < scores[countMode].length; i++)
					if (!scores[countMode][i].exists || time < scores[countMode][i].time)
						return i+1;
    	}
		return 0;
    }
    
    public static void addNewRecord(int count, int time, String name) {
		int countMode = getCountMode(count, -1);
    	if (name != null && countMode >= 0 && isRecord(count, time) > 0) {
			synchronized (staticHandle) {
				fixScores();

				Score score = new Score();
				score.exists = true;
				score.name = name;
				score.count = counts[countMode];
				score.time = time;
				score.secondsPerNumber = score.count > 0 ? (float)score.time/(float)score.count : 0;
				
				for(int i = 0; i < scores[countMode].length; i++) {
					if (!scores[countMode][i].exists || score.time < scores[countMode][i].time) {
						for(int j = scores[countMode].length-1; j > i ; j--)
							scores[countMode][j] = scores[countMode][j-1];
						scores[countMode][i] = score;
						break;
					}
				}
			}
    	}
    }

    public static void updateRecord(int count, int time, String name) {
		int countMode = getCountMode(count, -1);
    	if (name != null && countMode >= 0) {
			synchronized (staticHandle) {
				fixScores();
				for(int i = scores[countMode].length - 1; i >= 0 ; i--)
					if (scores[countMode][i].exists && scores[countMode][i].time == time) 
						{ scores[countMode][i].name = name; break; }
			}
    	}
    }
    
}