Resource Files

Resource files for the boxing clock. Includes images and sounds. bca-res-boxing-clock-android.tar.gz.

ch06

bca01helloGui.txt

// bca01helloGui - Create a simple graphical user interface
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com

package fi.sulautetut.android.chessboxing; 

import android.app.Activity; 
import android.os.Bundle;
import android.widget.TextView;

public class ChessBoxing extends Activity { 
   @Override
   public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this); 
       tv.append("Welcome to BotBook.com Chess Boxing!"); 
       setContentView(tv); 
   }
}

bca08boxingClockReady.txt

// bca08boxingClockReady - A usable boxing clock for Android. 
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
 
package fi.sulautetut.android.animatedboxing;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
 
public class AnimatedBoxing extends Activity {
    private Handler handler;
    private PieView tPie;
    private long pieEnds;
    private long fightLen=3*60*1000; // ms
    private long breakLen=60*1000;
    private long pieStarted; // round or break, ms
    private boolean fight=true; // will be changed immediately
    private MediaPlayer fightSound;
    private MediaPlayer breakSound;
    private static final int MENU105=105;
    private static final int MENU21=21;
    private static final int MENU31=31;
    private int boxingRedColor = 0xffff2704;
    private TextView tv;
    private long programStarted;
    private long roundsStarted=0;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        fightSound = MediaPlayer.create(this, R.raw.fightsound);
        breakSound = MediaPlayer.create(this, R.raw.breaksound);
 
        programStarted=SystemClock.uptimeMillis();
 
        LinearLayout container=new LinearLayout(this); 
        container.setOrientation(android.widget.LinearLayout.VERTICAL); 
 
        tv = new TextView(this);
        tv.setText("Welcome to BotBook.com Boxing Clock!");
        tv.setGravity(Gravity.CENTER); 
        tv.setTextColor(boxingRedColor); 
        tv.setBackgroundColor(Color.BLACK);
        tv.setPadding(5, 20, 5, 5); 
        container.addView(tv); 
 
        tPie = new PieView(this);
        container.addView(tPie);
 
        keepBackLightOn();
        fullscreen();
 
        setContentView(container); 
    }
 
    public void onPause() 
    {
        super.onPause();
        Toast.makeText(this, "Bye bye! "+statusMessage(), 
Toast.LENGTH_LONG).show();
        handler.removeCallbacks(update);
    }
 
    public void onResume() 
    {
        super.onResume();
        handler = new Handler();
        handler.removeCallbacks(update);
        handler.postDelayed(update, 50);        
    }
 
    public void keepBackLightOn() { 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                             WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
 
    public void fullscreen() 
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
 
    public boolean onCreateOptionsMenu(Menu menu) { 
        menu.add(Menu.NONE, MENU105, 0, "10 s / 5 s");
        menu.add(Menu.NONE, MENU21, 0, "2 min / 1 min");
        menu.add(Menu.NONE, MENU31, 0, "3 min / 1 min");
        return true;
    }
 
    public boolean onOptionsItemSelected(MenuItem item) { 
        long now=SystemClock.uptimeMillis();
        pieEnds=now;
        programStarted=now;
        fight=true; // will be changed immediately because pieEnds=now
        roundsStarted=0;
 
        switch (item.getItemId()) {
        case MENU105:
            fightLen = 10*1000;
            breakLen = 5*1000;
            Toast.makeText(this, "10 second round, 5 second break. ",
                           Toast.LENGTH_LONG).show();
            return true; // will exit, so a "break;" would be 
                         // unreachable
        case MENU21:
            fightLen = 2*60*1000;
            breakLen = 60*1000;
            Toast.makeText(this, "2 minute round, 1 minute break. ",
                           Toast.LENGTH_LONG).show();
            return true;
        case MENU31:
            fightLen = 3*60*1000;
            breakLen = 60*1000;
            Toast.makeText(this, "3 minute round, 1 minute break. ",
                           Toast.LENGTH_LONG).show();
            return true;
        }
        return false;
    }
 
    private String ms2mins(long ms)
    { // convert millisecond time to minutes and seconds
        int seconds = (int) (ms / 1000);
        int min = seconds / 60; 
        int sec = seconds % 60; 
        String pad;
        if (sec<10)
            pad="0"; 
        else
            pad="";
        return ""+ min +":"+pad+sec;
    }
 
    private String statusMessage()
    {
        String s=""; 
        long totalElapsed=SystemClock.uptimeMillis()-programStarted;
        s+=ms2mins(totalElapsed); 
        s+=" elapsed. ";
 
        if (fight) {
            s+="Round ";
        } else {
            s+="Break ";
        }
        s+=roundsStarted+".";
        return s;
    }
 
    private Runnable update = new Runnable() {
        public void run() {
            long now=SystemClock.uptimeMillis();
            if (pieEnds<now) {
                fight=!fight;
                pieStarted=now;
                if (fight) { // round starts
                    roundsStarted++;
                    pieEnds=pieStarted+fightLen;
                    fightSound.start();
                } else { // break starts
                    pieEnds=pieStarted+breakLen;
                    breakSound.start();
                }
            } 
            tv.setText(statusMessage());
            tPie.fightColors=fight;
            tPie.percent=(float)(now-pieStarted)/(float)(pieEnds-pieStarted);
            tPie.invalidate();
 
            handler.removeCallbacks(update);
            handler.postDelayed(this, 80); // ms
        }                                          
    };
 
    public class PieView extends View {
        public float percent;
        public boolean fightColors=false;
        Bitmap fightBg;
        Bitmap breakBg;
 
        public PieView(Context context) {
            super(context);
            fightBg=BitmapFactory.decodeResource(getResources(),
                                                 R.drawable.clockfight);
            breakBg=BitmapFactory.decodeResource(getResources(), 
                                                 R.drawable.clockbreak);
        }
 
        public void onDraw(Canvas canvas) {
            canvas.drawColor(Color.BLACK);
 
            Paint paint = new Paint();
            paint.setColor(Color.WHITE);
 
            float w=canvas.getWidth(); 
            float h=canvas.getHeight();
            float margin=10; // pixels from pie to nearest wall
            float r=(Math.min(h,w)-margin)/2; // ray, half diameter 
            // center first - because we want it centered 
            float cx=w/2;
            float liftCenter=-50;
            float cy=h/2+liftCenter;
            // top left 
            float tx=cx-r;
            float ty=cy-r;
            // bottom right 
            float bx=tx+2*r;
            float by=ty+2*r;
 
            RectF bgRect = new RectF(tx, ty, bx, by); 
 
            RectF oval=new RectF(bgRect); // copy bgRect measures instead 
                                          // of creating a reference
            oval.inset((int)(r*0.29), (int)(r*0.29)); 
 
            if (fightColors) { 
                canvas.drawBitmap(fightBg, null, bgRect, null);
            } else {
                canvas.drawBitmap(breakBg, null, bgRect, null);
            }
 
            canvas.drawArc(oval, -90, 360*percent, true, paint); 
        }
    };
}

bca05roundAndBreak.txt

// bca05roundAndBreak - Round and break with different length and sound.
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com

package fi.sulautetut.android.loudboxing;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.widget.TextView;

public class LoudBoxing extends Activity {
       private Handler handler;
       private TextView tv;
       private MediaPlayer fightSound;
       private MediaPlayer breakSound; 
       private boolean fight=false; 
       private long pieStarted; // round or break, ms 
       private long pieEnds;
       private long fightLen=10*1000; // ms 
       private long breakLen=5*1000;

       @Override
       public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               tv = new TextView(this);
               tv.append("Welcome to BotBook.com Chess Boxing! ");
               setContentView(tv);

               fightSound= MediaPlayer.create(this, R.raw.fightsound );
               breakSound= MediaPlayer.create(this, R.raw.breaksound );

               handler = new Handler();
               handler.removeCallbacks(update);
               handler.postDelayed(update, 50);
       }

       private Runnable update = new Runnable() {
               public void run() { 
                       if (pieEnds<SystemClock.uptimeMillis()) { 
                               fight=!fight; 
                               pieStarted=SystemClock.uptimeMillis(); 
                               if (fight) { // round starts 
                                       pieEnds=pieStarted+fightLen; bk
                                       fightSound.start(); bl
                                       tv.setText("Fight! ");  
                               } else { bm
                                       pieEnds=pieStarted+breakLen;
                                       breakSound.start();
                                       tv.setText("Break. ");
                               }
                       }
                       handler.removeCallbacks(update);
                       handler.postDelayed(this, 1000);
               }
       };
}

bca06customView.txt

// bca06customView - Build custom view with green background
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com

package fi.sulautetut.android.greencolor;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

public class GreenColor extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState);
       PieView tPie = new PieView(this); 
       setContentView(tPie); 
   }

   public class PieView extends View { 
       
       public PieView(Context context) { 
               super(context);
       }

       public void onDraw(Canvas canvas) {
               canvas.drawColor(Color.GREEN); 
       }
   }
}

bca03helloMp3.txt

// bca03helloMp3 - Play a sound
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com

package fi.sulautetut.android.hellomp3;

import android.app.Activity;
import android.media.MediaPlayer; 
import android.os.Bundle;

public class HelloMp3 extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       MediaPlayer fightSound = MediaPlayer.create(this, R.raw.fightsound); 
       fightSound.start(); 
       setContentView(R.layout.main); 
   }
}

bca02chessBoxing.txt

// bca02chessBoxing - Timed events
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
package fi.sulautetut.android.chessboxing;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class ChessBoxing extends Activity {
   private Handler handler; 
   private TextView tv; 

   @Override
   public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState);
       tv = new TextView(this);
       tv.append("Welcome to BotBook.com Chess Boxing! ");
       setContentView(tv);

       handler = new Handler(); 
       handler.removeCallbacks(update); 
       handler.postDelayed(update, 50); 
   }

   private Runnable update = new Runnable() { 
           public void run() { 
               tv.append("Bling! ");
               handler.removeCallbacks(update);
               handler.postDelayed(this, 10*1000); 
           }
    };
}

bca04loudBoxing.txt

// bca04loudBoxing - Signal rounds with a gong sound.
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com

package fi.sulautetut.android.loudboxing;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class LoudBoxing extends Activity {
       private Handler handler;
       private TextView tv;
       MediaPlayer fightSound;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       tv = new TextView(this);
       tv.append("Welcome to BotBook.com Chess Boxing! ");
       setContentView(tv);

       fightSound= MediaPlayer.create(this, R.raw.fightsound ); 
       
       handler = new Handler();
       handler.removeCallbacks(update);
       handler.postDelayed(update, 50);
   }

       private Runnable update = new Runnable() {
               public void run() {
                      fightSound.start(); 
                      tv.append("Bling! ");
                      handler.removeCallbacks(update);
                      handler.postDelayed(this, 10*1000); 
               }
       };
}

bca07animatedBoxing.txt

// bca07animatedBoxing - Animated pie shows time left.
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com

package fi.sulautetut.android.animatedboxing;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;

public class AnimatedBoxing extends Activity {
       private Handler handler;
       private PieView tPie; 
       private long pieEnds;
       private long fightLen=10*1000; // ms
       private long pieStarted; // round or break, ms

       @Override
       public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);

               handler = new Handler();
               handler.removeCallbacks(update);
               handler.postDelayed(update, 50);

               tPie = new PieView(this);
               setContentView(tPie); 
       }

       private Runnable update = new Runnable() {
               public void run() {
                       long now=SystemClock.uptimeMillis();
                       if (pieEnds<now) {
                                       pieStarted=now;
                                       pieEnds=pieStarted+fightLen;
                       }
                       tPie.percent=
                         (float)(now-pieStarted)/(pieEnds-pieStarted); 
                       tPie.invalidate(); 
                       
                       handler.removeCallbacks(update);
                       handler.postDelayed(this, 50); // ms 
               }                                                                              
       };

       public class PieView extends View { 
               public float percent;
               public PieView(Context context) {
                       super(context);
               }

               public void onDraw(Canvas canvas) {
                       Paint paint = new Paint();
                       paint.setColor(Color.WHITE); 
                       RectF oval=new RectF(0, 0, 
                                            canvas.getWidth(), 
                                            canvas.getHeight());
                       canvas.drawArc(oval, 0, 360*percent, true, paint); 
               }
       };
}