Processing – Validate a date

How to validate a date in Processing?

boolean isValidDate(String _date) {
  try {
    DateFormat df = new SimpleDateFormat("dd-M-yyyy");
    df.setLenient(false);
    df.parse(_date);
    return true;
  } 
  catch (Exception e) {
    return false;
  } // try
} // isValidDate()

Processing Android – Signing an Android app

How to sign an Android app, generated with Processing Android (or generated with another tool)?

Create a (Windows-) batch file (.bat) with the following code:

@echo off
ECHO.
set /p varSketchName="Please enter the Name of your sketch: "
ECHO.
set /p varSketchAlias="Please enter an Alias for your sketch: "
ECHO.
ECHO [KEYTOOL]
keytool -genkey -v -keystore %~dp0%varSketchName%-release-key.keystore -alias %varSketchAlias% -keyalg RSA -keysize 2048 -validity 10000
pause
ECHO [ANT RELEASE]
call ant release
pause
ECHO [JARSIGNER]
call jarsigner -verbose -keystore %~dp0%varSketchName%-release-key.keystore %~dp0bin\%varSketchName%-release-unsigned.apk %varSketchAlias%
pause
ECHO [JARSIGNER VERIFY]
call jarsigner -verify %~dp0bin\%varSketchName%-release-unsigned.apk
pause
ECHO [ZIPALIGN]
set /p varSignedAppName="Please enter name for final signed apk (w/o .apk extension): "
call zipalign -v 4 %~dp0bin\%varSketchName%-release-unsigned.apk %~dp0%varSignedAppName%.apk

Programs you need:

  • keytool.exe (part of the Java JDK)
  • ant.bat (part of Apache ant)
  • jarsigner.exe (part of the Java JDK)
  • zipalign.exe (part of the Android SDK)

Steps:

  1. In Processing: create your sketch in Android Mode
  2. Export the sketch as an Android Project (Ctrl-Shift-E)
  3. Copy the .bat-file above to the newly created android directory and run it
  4. Name of your sketch = name of the .pde-file (without .pde!)
  5. Alias = same as the name (or something else)
  6. Enter a password for your keystore
  7. Enter your personal data
  8. Enter the same keystore password again
  9. Enter the name of the final .apk-file (without the .apk extension!)

N.B. Make sure your folder names DON’T include any SPACES! That will break the batch file.

And there you go! You’ve got a signed .apk file!


Processing – Java keyEvents

How to use native Java keyEvents in Processing?

import java.awt.event.KeyEvent;
void keyPressed()
{
 if (key == CODED)
 { if (keyCode == KeyEvent.VK_PAGE_UP) println("Page Up pressed");
 }
}

How to detect if the SHIFT-key (or ALT-key or CTRL-key) is down?

import java.awt.event.KeyEvent;
void keyPressed(KeyEvent e)
{
  if (key == CODED)
  {  
    if (keyCode == UP)
    {
      if (e.isShiftDown())
        println("SHIFTED ARROW UP");
      else
        println("ARROW UP");
    }
  }
}

Resources:
http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html
http://docs.oracle.com/javase/7/docs/api/java/awt/event/InputEvent.html