Babel Fish for Twitter – New Twim Release

Google Translator is a great translator tool which has a very simple AJAX API that can be easily used from mobile apps. Compared to other translator tools it has one feature which other services don’t have, the auto detection of language.

The API is really easy to use. All you need to do is to pass three parameters to the API call:

  1. Version, v=1.0
  2. Translated text, q=Hei maailma!
  3. Language pair (if source is empty then language is automatically identified), langpair=|en

In the end you end up with the following URL:

http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=|en&q=Hei+maailma!

And if you click the link you’ll see that it returns the translated text in JSON format which is pretty easy to parse.

{ "responseData": {
"translatedText":"Hello world!",
"detectedSourceLanguage":"fi" },
"responseDetails": null,
"responseStatus": 200}

This made it pretty easy to integrate the API call to Twim. So now in Twim 1.6 you can select a tweet from status list and select “Translate to English” from status menu and Twim will display the translation to you in seconds. Pretty handy if you are following people from other countries in Twitter. Go ahead and download the latest Twim to your mobile phone from here.

…and by the way, new feature also has the “paging” feature for home screen and search results as a bonus! Happy Twittering!

Posted using Mobypicture.com

Twim 1.15 with Auto-Refresh and TwitrPix Support

Just finished testing the latest version of Twim, v1.15. Now you can also send your photos to new TwitrPix service.

twitrpix_logo_v2

I also added few options that you can configure via settings view. You can set Twim to automatically load the home screen tweets when application is started and you can also turn on the auto-refresh option that will automatically load new tweets in every 5 minutes. If new tweets are found then phone vibrates and plays a small beep sound. With this feature you won’t miss a single tweet again :)

New settings view

New settings view

Full release notes:

  • TwitrPix support
  • Cancel option to media menu
  • Initial auto-refresh option with alarming of new tweets
  • Option to automatically load tweets on startup
  • Tab names changed: “Recent”>”Home”, “Friends”>”Following”
  • Fix for non-latin characters when posting photos (thanks Lexa!)

Have fun with the new Twim and download it from here. If you like the source code you can see that in here. Twim is still free as a free beer.

In case you didn’t know: Twim is a mobile Twitter client. I’m twittering with username @tlaukkanen.

Coding Touchscreen Scrolling in Java ME

Here’s the trick how you can add touchscreen scrolling to your J2ME applications. The key to touchscreen support is the Canvas class and its’ methods pointerPressed, pointerDragged and pointerReleased. These events are called when user puts finger on the screen (pressed) and drags the finger (dragged) and finally when removes the finger (released). Each method has the x and y coordinates for the pointer action.

The scrolling is implemented with the pointerDragged event. We store the current location of the canvas to variables verticalLoc and horizontalLoc. When finger (pointer) is dragged then location is changed according to the dragged amount. Variables are used on paint method to draw the area so that it is scrolled that correctly. You can also see from the sample code how you can draw background with repeatable pattern image.

  1. public class ScrollingCanvas extends Canvas {
  2.  
  3.  // Current location
  4.  private int verticalLoc, horizontalLoc;
  5.  // Last pointer (finger) location
  6.  private int lastX, lastY;
  7.  // Background image size
  8.  private static final int IMAGE_WIDTH = 183;
  9.  private static final int IMAGE_HEIGHT = 183;
  10.  
  11.  public ScrollingCanvas() {
  12.    verticalLoc = 0;
  13.    horizontalLoc = 0;
  14.    setFullScreenMode(true);
  15.  }
  16.  
  17.  protected void paint(Graphics g) {
  18.  
  19.    // Draw background with pattern image
  20.    int x = horizontalLoc % IMAGE_WIDTH;
  21.    if(x>0) {
  22.      x -= IMAGE_WIDTH;
  23.    }
  24.    int origX = x;
  25.  
  26.    int y = verticalLoc % IMAGE_HEIGHT;
  27.    if(y>0) {
  28.      y -= IMAGE_HEIGHT;
  29.    }
  30.  
  31.    boolean verticalDone = false;
  32.    while(!verticalDone) {
  33.      boolean horizontalDone = false;
  34.      while(!horizontalDone) {
  35.        g.drawImage(
  36.          ImageRepository.getBackground(),
  37.          x, y, Graphics.LEFT|Graphics.TOP);
  38.        x += IMAGE_WIDTH;
  39.        if(x>getWidth()) {
  40.          horizontalDone = true;
  41.        }
  42.      }
  43.      x = origX;
  44.      y += IMAGE_HEIGHT;
  45.      if(y>getHeight()) {
  46.        verticalDone = true;
  47.      }
  48.    }
  49.  }
  50.  
  51.  /**
  52.  * Finger is pressed on screen
  53.  * @param x coordinate
  54.  * @param y coordinate
  55.  */
  56.  protected void pointerPressed(int x, int y) {
  57.    lastX = x;
  58.    lastY = y;
  59.  }
  60.  
  61.  /**
  62.  * Finger is dragged on screen
  63.  * @param x coordinate
  64.  * @param y coordinate
  65.  */
  66.  protected void pointerDragged(int x, int y) {
  67.    // Calculate how much we moved horizontally
  68.    int deltaHorizontal = lastX – x;
  69.    horizontalLoc -= deltaHorizontal;
  70.    lastX = x;
  71.  
  72.    // Calculate how much we moved vertically
  73.    int deltaVertical = lastY – y;
  74.    verticalLoc -= deltaVertical;
  75.    lastY = y;
  76.  
  77.    // Repaint the screen since we have scrolled
  78.    repaint();
  79.  }
  80.  
  81. }

Here is a video of running this demonstration app in Nokia N97 SDK emulator:

Learning the git, Mobidentica now Open Source

I’m learning to use the git, distributed version control system. It seems to be much faster then Subversion that I have used with my Google Code projects. I just created a repository on GitHub for Mobidentica project. Source code is available under the Apache License 2.0. If you like to clone the repository, use this:

git clone git://github.com/tlaukkanen/mobidentica.git

I’ll have to see if I’ll start using git with those projects that are on Google Code as it seems to be possible too: Benjamin Lynn from Google Developer Programs wrote a blog post on how to use git on Google Code projects.

Optimizing Trail Recording

I coded a prototype how we could record fewer GPS positions in Mobile Trail Explorer and therefore save some memory but still have the same level of detail of the trail. Video might explain it best. Unoptimized recording is on the left and optimized trail is on the right.

The algorithm simply checks if the current direction have changed since last recorded position. If direction isn’t changed over the tolerance value then we can replace the last position with current position. Otherwise we only append the new position to the trail.

  1. public boolean canRemovePreviousPosition(
  2.         Vector<GpsPosition> positions,
  3.         GpsPosition pos1) {
  4.  
  5.     if(positions.size()<2) {
  6.         return false;
  7.     }
  8.  
  9.     GpsPosition pos2 = positions.elementAt(
  10.             positions.size()-2 );
  11.     GpsPosition pos3 = positions.elementAt(
  12.             positions.size()-1 );
  13.  
  14.     // Calculate last angle of trail
  15.     double latDelta = pos2.lat – pos3.lat;
  16.     double lonDelta = pos2.lon – pos3.lon;
  17.     double lastAngle = Math.atan(latDelta/lonDelta);
  18.  
  19.     // Calculate current angle
  20.     double latDelta2 = pos3.lat – pos1.lat;
  21.     double lonDelta2 = pos3.lon – pos1.lon;
  22.     double currentAngle =
  23.             Math.atan(latDelta2/lonDelta2);
  24.  
  25.     // Get absolute value of direction change
  26.     double angleDelta =
  27.             Math.abs(lastAngle – currentAngle);
  28.  
  29.     // Check the tolerance (0.105 radians = 6 degrees)
  30.     if(angleDelta>0.105) {
  31.         return false;
  32.     } else {
  33.         return true;
  34.     }
  35. }