Revving,
eDreamZ
PizzaView.java
Using java Syntax Highlighting
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.RectF;
- import android.graphics.Paint.Style;
- import android.view.View;
- public class PizzaView extends View{
- // ===========================================================
- // Fields
- // ===========================================================
- protected final int ARCSTROKEWIDTH = 20;
- // Set startup-values
- protected int mySecondsPassed = 0;
- protected int mySecondsTotal = 0;
- // Our Painting-Device (Pen/Pencil/Brush/Whatever...)
- protected final Paint myArcSecondPaint = new Paint();
- protected final Paint myArcMinutePaint = new Paint();
- protected final Paint myCountDownTextPaint = new Paint();
- protected final Paint myPizzaTimeTextPaint = new Paint();
- // ===========================================================
- // Constructors
- // ===========================================================
- public PizzaView(Context context) {
- super(context);
- this.setBackgroundDrawable(getResources().getDrawable(R.drawable.pizza));
- // Black text for the countdown
- this.myCountDownTextPaint.setARGB(150, 255, 0, 0);
- this.myCountDownTextPaint.setTextSize(150);
- this.myCountDownTextPaint.setFakeBoldText(true);
- // Orange text for the IT PIZZA TIME
- this.myPizzaTimeTextPaint.setARGB(255, 255, 60, 10);
- this.myPizzaTimeTextPaint.setTextSize(110);
- this.myPizzaTimeTextPaint.setFakeBoldText(true);
- // Our minute-arc-paint fill be a lookthrough-red.
- this.myArcMinutePaint.setARGB(150, 170, 0, 0);
- this.myArcMinutePaint.setAntiAlias(true);
- this.myArcMinutePaint.setStyle(Style.STROKE);
- this.myArcMinutePaint.setStrokeWidth(ARCSTROKEWIDTH);
- // Our minute-arc-paint fill be a less lookthrough-orange.
- this.myArcSecondPaint.setARGB(200, 255, 130, 20);
- this.myArcSecondPaint.setAntiAlias(true);
- this.myArcSecondPaint.setStyle(Style.STROKE);
- this.myArcSecondPaint.setStrokeWidth(ARCSTROKEWIDTH / 3);
- }
- // ===========================================================
- // onXYZ(...) - Methods
- // ===========================================================
- @Override
- protected void onDraw(Canvas canvas) {
- /* Calculate the time left,
- * until our pizza is finished. */
- int secondsLeft = this.mySecondsTotal - this.mySecondsPassed;
- // Check if pizza is already done
- if(secondsLeft <= 0){
- /* Draw the "! PIZZA !"-String
- * to the middle of the screen */
- String itIsPizzaTime = getResources().getString(
- R.string.pizza_countdown_end);
- this.myPizzaTimeTextPaint.setTextSize(50);
- canvas.drawText(itIsPizzaTime,
- 10, (this.getHeight() / 2) + 30,
- this.myPizzaTimeTextPaint);
- }else{
- // At least one second left
- float angleAmountMinutes = ((this.mySecondsPassed * 1.0f)
- / this.mySecondsTotal)
- * 360;
- float angleAmountSeconds = ((60 -secondsLeft % 60) * 1.0f)
- / 60
- * 360;
- /* Calculate an Rectangle,
- * with some spacing to the edges */
- RectF arcRect = new RectF(ARCSTROKEWIDTH / 2,
- ARCSTROKEWIDTH / 2,
- this.getWidth() - ARCSTROKEWIDTH / 2,
- this.getHeight() - ARCSTROKEWIDTH / 2);
- // Draw the Minutes-Arc into that rectangle
- canvas.drawArc(arcRect, -90, angleAmountMinutes, true,
- this.myArcMinutePaint);
- // Draw the Seconds-Arc into that rectangle
- canvas.drawArc(arcRect, -90, angleAmountSeconds, true,
- this.myArcSecondPaint);
- String timeDisplayString;
- if(secondsLeft > 60) // Show minutes
- timeDisplayString = "" + (secondsLeft / 60);
- else // Show seconds when less than a minute
- timeDisplayString = "" + secondsLeft;
- // Draw the remaining time.
- canvas.drawText(timeDisplayString,
- this.getWidth() / 2 - (30 * timeDisplayString.length()),
- this.getHeight()/ 2 + 30,
- this.myCountDownTextPaint);
- }
- }
- // ===========================================================
- // Getter & Setter
- // ===========================================================
- public void updateSecondsPassed(int someSeconds){
- this.mySecondsPassed = someSeconds;
- }
- public void updateSecondsTotal(int totalSeconds){
- this.mySecondsTotal = totalSeconds;
- }
- }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4
PizzaTimer.java
Using java Syntax Highlighting
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuItem;
- public class PizzaTimer extends Activity {
- protected static final int DEFAULTSECONDS = 60 * 12; // 12 MInutes
- /* The value of these IDs is random!
- * they are just needed to be recognized */
- protected static final int SECONDPASSEDIDENTIFIER = 0x1337;
- protected static final int GUIUPDATEIDENTIFIER = 0x101;
- protected static final int PIZZA_NOTIFICATION_ID = 0x1991;
- protected static final int NOTIFY_PIZZA = 0x101;
- /** is the countdown running at the moment ?*/
- protected boolean running = false;
- /** Seconds passed so far */
- protected int mySecondsPassed = 0;
- /** Seconds to be passed totally */
- protected int mySecondsTotal = DEFAULTSECONDS;
- /* Thread that sends a message
- * to the handler every second */
- Thread myRefreshThread = null;
- // One View is all that we see.
- PizzaView myPizzaView = null;
- /* The Handler that receives the messages
- * sent out by myRefreshThread every second */
- Handler myPizzaViewUpdateHandler = new Handler(){
- /** Gets called on every message that is received */
- // @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case PizzaTimer.SECONDPASSEDIDENTIFIER:
- // We identified the Message by its What-ID
- if (running) {
- // One second has passed
- mySecondsPassed++;
- if(mySecondsPassed == mySecondsTotal){
- // Time is finished, lets display a notification!
- // Get the notification manager service.
- NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- /* The id we use here happens to be the
- * id of the text we display. You can use
- * any int here that is unique within
- * your application. */
- int iconNot = R.drawable.icon;
- CharSequence ticker = getResources().getString(R.string.pizza_notification_text);
- Notification notifyme = new Notification(iconNot, ticker, System.currentTimeMillis());
- notifyme.defaults |= Notification.DEFAULT_SOUND;
- Context context = getApplicationContext();
- CharSequence contentTitle = "Pizza Timer";
- CharSequence contentText = "Your Pizza is ready";
- Intent notIntent = new Intent(context,PizzaView.class);
- PendingIntent contentIntent = PendingIntent.getActivity(context,0, notIntent, 0);
- notifyme.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
- nm.notify(NOTIFY_PIZZA, notifyme);
- }
- }
- // No break here --> runs into the next case
- case PizzaTimer.GUIUPDATEIDENTIFIER:
- // Redraw our Pizza !!
- myPizzaView.updateSecondsPassed(mySecondsPassed);
- myPizzaView.updateSecondsTotal(mySecondsTotal);
- myPizzaView.invalidate();
- break;
- }
- super.handleMessage(msg);
- }
- };
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- this.myPizzaView = new PizzaView(this);
- this.myPizzaView.updateSecondsTotal(PizzaTimer.DEFAULTSECONDS);
- setContentView(this.myPizzaView);
- this.myRefreshThread = new Thread(new secondCountDownRunner());
- this.myRefreshThread.start();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add(0,0,android.view.Menu.NONE,getResources().getString(R.string.menu_reset));
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onMenuItemSelected(int featureId, MenuItem item) {
- switch(item.getItemId()){
- case 0:
- // Reset the counter and stop it
- this.mySecondsTotal = PizzaTimer.DEFAULTSECONDS;
- this.mySecondsPassed = 0;
- this.running = false;
- return true;
- }
- return super.onMenuItemSelected(featureId, item);
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- Message m = new Message();
- m.what = PizzaTimer.GUIUPDATEIDENTIFIER;
- switch(keyCode){
- case KeyEvent.KEYCODE_DPAD_UP:
- this.mySecondsTotal += 60; // One minute later
- break;
- case KeyEvent.KEYCODE_DPAD_DOWN:
- this.mySecondsTotal -= 60; // One minute earlier
- break;
- case KeyEvent.KEYCODE_DPAD_CENTER:
- this.running = !this.running; // START / PAUSE
- break;
- case KeyEvent.KEYCODE_DPAD_LEFT:
- this.mySecondsTotal += 1; // One second later
- break;
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- this.mySecondsTotal -= 1; // One second earlier
- break;
- default:
- return super.onKeyDown(keyCode, event);
- }
- this.myPizzaViewUpdateHandler.sendMessage(m);
- return true;
- }
- class secondCountDownRunner implements Runnable{
- // @Override
- public void run() {
- while(!Thread.currentThread().isInterrupted()){
- Message m = new Message();
- m.what = PizzaTimer.SECONDPASSEDIDENTIFIER;
- PizzaTimer.this.myPizzaViewUpdateHandler.sendMessage(m);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
- }
- }
- }
- }
Parsed in 0.051 seconds, using GeSHi 1.0.8.4
strings.xml
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">PizzaTimer!</string>
- <string name="app_name">Pizza Timer</string>
- <string name="pizza_countdown_end">Pizza Ready </string>
- <string name="pizza_notification_text">Your Pizza is Ready</string>
- <string name="menu_reset">Reset</string>
- </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


