Search This Blog

Showing posts with label gesture. Show all posts
Showing posts with label gesture. Show all posts

10 July 2010

Using the Barcode Scanner

Let's start with a basic app:

F:\work> mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml
2: http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml -> galatea-archetype (null)
Choose a number: : 2
groupId: org.eoti.android.barcode
artifactId: BarcodeTest
version: 1.0-SNAPSHOT
package: org.eoti.android.barcode
 
F:\work> cd BarcodeTest
F:\work\BarcodeTest> mvn clean install

Now, let's modify it. 

Modify your main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/content"
    />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/format"
    />
</LinearLayout>

Modify your activity:

package org.eoti.android.barcode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class BarcodeTestActivity extends Activity
{
    private static String TAG = "BarcodeTest";
    private static final String INTENT_SCAN = "com.google.zxing.client.android.SCAN";
    private static final String SCAN_MODE = "SCAN_MODE";
    private static final String QR_CODE_MODE = "QR_CODE_MODE";
    private static final String SCAN_RESULT = "SCAN_RESULT";
    private static final String SCAN_RESULT_FORMAT = "SCAN_RESULT_FORMAT";
    private static final int REQCODE_SCAN = 0;
    private Handler handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        handler = new Handler();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        Intent intent = new Intent(INTENT_SCAN);
        intent.putExtra(SCAN_MODE, QR_CODE_MODE);
        startActivityForResult(intent, REQCODE_SCAN);
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == REQCODE_SCAN)
        {
            set(R.id.content, "<b>Contents: </b>" + data.getStringExtra(SCAN_RESULT));
            set(R.id.format, "<b>Format: </b>" + data.getStringExtra(SCAN_RESULT_FORMAT));
        }else{
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    protected void set(final int id, final String text)
    {
        handler.post(new Runnable(){
            public void run() {
                ((TextView)findViewById(id)).setText(Html.fromHtml("<html><body>" + text + "</body></html>"));
            }
        });
    }
}


Unfortunately, we won't be able to test the rest of it in the emulator.  You will need an actual device for that.

Install it on your phone (I do it by using a webserver, but you could use adb) and launch it.

When the screen comes up, click the screen.  That will launch your Barcode Scanner.

Scan a QR CODE (I scanned this one )

It will go back to your test app. 
"Contents" will be the data content (the market url in the above case) and the "Format" will be QR CODE.

04 May 2010

Moving existing samples to Maven: GestureBuilder

So I was intrigued by the Gesture API, but did not want to install Eclipse.  What to do?

First, let's create a blank project:
mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml

Archetype: galatea
GroupId: com.android.gesture.builder
ArtifactId: GestureBuilder
Version: 1.0-SNAPSHOT

  • Remove the src/main/android/res/layout/main.xml
  • Remove the src/main/java/com/android/gesture/builder/MyActivity.java

Now we will copy some files over from our sample directory.  For me, that is at C:\java\android-sdk-windows\samples\android-7\GestureBuilder. We will call that directory $SRC and our top-level maven directory $DEST.

  • Replace $DEST/src/AndroidManifest.xml with $SRC/AndroidManifest.xml
  • Copy $SRC/src/* into $DEST/src
  • Copy $SRC/src/* into $DEST/src/main/java
  • Copy $SRC/res/* into $DEST/src/android/res
  • Copy $SRC/res/* into $DEST/src/main/android/res

Make sure your emulator is running then, from your command-line (in $DEST) deploy the app (mvn clean install).  The app will run in the emulator and save the gestures onto the emulated SD card*.


* What? You didn't have an emulated SD card? Oh.  This is how I created my emulator image:
android create avd -n Device1 -t 11 -c 1000M