Monday, June 13, 2011

Undersranding android gestures

Android gestures is another dark horse because it is wrapped into an Android library and it becomes a part of SDK.

From Android SDK it is clear that gesture library should be put in a raw folder in resources or it could be just a separate file in private or public folder, but what is the gesture library format?. You can browse Android SDK here that describes only high level design.

I still cannot find gesture library format but I saw on the market there are many applications that uses gestures.

My first clue was to use Gesture Builder from Android example SDK.
It is located under \samples\android-8\GestureBuilder

Lets take a close look:

GestureLibrary lib;
// create lib instanse from file name pass empty file
lib = GestureLibraries.fromFile(new File("gestures")));
..
lib.addGesture("gesture's name", gestureProcessor); // adding
lib.save();

// We could build gesture object  from overlay
private class GestureProcessor implements GestureOverlayView.OnGestureListener {
    public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
    }

    public void onGesture(GestureOverlayView overlay, MotionEvent event) {
    }

    public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
       mGesture = overlay.getGesture();
       if (mGesture.getLength() < LENGTH_THRESHOLD) {
         overlay.clear(false);
       }
}


Saturday, January 29, 2011

Multilayout ListView

This is a multi Layout view sample. You can  use it when you need to display the same amount of data (in sample text and images but it should be located differently
You need to build a special ListView adaptor 

Source code for MultiLayout Adapter is bellow







Beginning Android 2

Code Highlighting

Highlighted code in my samples

I found excellent javascript project. It was written by Alex Gorbatchev, and it is free (Thanks Alex!). I will use it to highlight code samples. There is no need to host JS and CSS inside your blog system, you can just reference files instead.
The most popular programming languages are supported here

You can download project and use it in your web sites or you can use hosted version of highlighter. Unfortunately, Google blog system doesn't allow you to insert link tag, but I have used loadjscssfile function to load script and css files.
Lets test hosted version: (javascript example) Here is a function that loads scripts and css.
function loadjscssfile(filename, filetype)
{
   if (filetype=="js"){
       var fileref=document.createElement('script')
       fileref.setAttribute("type","text/javascript")
       fileref.setAttribute("src", filename)
   } 
   else if (filetype=="css"){
       var fileref=document.createElement("link")
             fileref.setAttribute("rel", "stylesheet")
             fileref.setAttribute("type", "text/css") 
             fileref.setAttribute("href", filename)
   } 
   if (typeof fileref!="undefined"){
      document.getElementsByTagName("head")[0].appendChild(fileref)
   }
} 

var pathToScript = "http://alexgorbatchev.com/pub/sh/current/"; 
loadjscssfile(pathToScript + "styles/shCore.css", "css"); 
loadjscssfile(pathToScript + "styles/shThemeDefault.css", "css"); 
loadjscssfile(pathToScript + "scripts/shCore.js", "js"); 
loadjscssfile(pathToScript + "scripts/shBrushJava.js", "js"); 
loadjscssfile(pathToScript + "scripts/shBrushJScript.js","js"); 


Android Java example:
import android.widget.TextView;
public class TestClass {
    private static final int[] LAYOUTS = new int[] {
        R.layout.center,
        R.layout.left, 
        R.layout.right 
    private final Context _context;
    private final ArrayList _data = new ArrayList();
    public TestClass(Context context, ArrayList data){ 
        _context = context;
        _data.addAll( data );
  
    }
}
 

Tuesday, January 11, 2011

Java pre-processor

How Eclipse works to compile project?

If we are looking for android project compilation process, it contains from many builders
- before resource generator builder;
- resource builder;
- Java compiler;
- package builder;

Next builder will be started after previous one was finished successfully.


Sometimes you may want to use one source for different incompatible platforms (like JavaME MIPS2.0 or Java SE or some other Java specific platform like Android).

The best approach here to have you project separated from Java SDK by creating wrappers on SDK layer and then refer only wrapped classes in your project. Then when in and script for package builder build your project together with Java specific library wrapper.(recommended)

Another way to dial with this problem is to have a preprocessor like C preprocessor and wrap platform specific code. This approach is less clear and it feels like some comments you cannot remove. (not recommended) 

If you need Java cross platform but it is difficult to separate api in your project - you have to redesign you project.

Custom Java preprocessor (also could be done using ant script as well as using external preprocessor tool (like C preprocessor). To understand how it works I have created my own custom preprocessor using ant

Eclipse defines 4 targets
  • before clean
  • manual build
  • auto build
  • during clean 
I have created a simple xml file with 4 targets that is shown below:
  <project name="new builder" >
  <target name="before_clean"/>
  <target name="manual_build"/>
  <target name="auto_build"/>
  <target name="during_clean">
  </project>

Do you have other solution how to build the same app on different Java platforms? 

Sunday, January 9, 2011

Google Translation

I would like to share a Google Translate JavaScript plug-in.
To add a translation to your web pages you can use JavaScript and Google translate services.


Here is a sample that add a translate combo box to your page:
// define div tag where translation combobox will be placed
<div id="google_translate_element">
</div>


// define "new" translate element function  
<script>
function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'uk',

    includedLanguages: 'et,tl',
  }, 'google_translate_element');
}
</script>


// add all necessary sources to do the translation 
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

pros:
- easy to use 
- using it you can create a copy of your blog/site to other languages 

cons:
- not all languages supported 
- engine itself is quite new and translation is not perfect


if you need translation to any language remove includedLanguages parameter

There are some useful links that may help you with building your copy:

Tuesday, January 4, 2011

Android and Ant builder. Part 2

About ant commands :
You can create your own command processor with ant (you are able to create elements and ant is able to process commands in ants way (using xml to join parts )

For example for search and replace there is a command that is mapped to java or to implemented in other language application


 <replaceregexp file="file"   match="pattern"    replace="pattern"  

 flags="options"
 byline="true|false" /> ;

 
it is actually a prepared a java class(s) that do the job

Is Ant depends on Java JRE or it is a separate native application? I think it is...
Probably that's why Java and Ant is so close...

I also would like to understand how preprocessor works in Eclipse. It may helps me with my tasks

Wednesday, December 22, 2010

Android Ant builder

Android build process is quite complicated
First of all it is based on ant script together with sdk tools and java apps aligned to ant.

Lets take a closer look to it
to create ant script manually just type:

cmd <enter>
if you don't have Android SDK folder in system PATH environmental variable - add it
set path=%path%;X:\android\android-sdk-windows-1.5_r3\tools

now you can create new or update existent eclipse project
(I assume that you like me create project in eclipse first and then, for some reason, you need to build your fancy application from command line

set current dir to your android project dir

cd c:\apps\myapp <enter>
android update project  <enter>

now you have build.xml in your projects root folder

for now you can open them in eclipse but you cannot build (execute) ant script from eclipse
The problem is in java component that connect you build.xml ant script with platform dependent android ant script that located in SDK

for example for API level 8 it will be android_rules_r3.xml.

File contains a lot of comments about every section
but I'll explain some of them just for those who  who know nothing about ant engine :)

Ant stuff
I will explain only things that I need to build android project manually

every project contains
<project name="myapps" default="<default target>" basedir=<path to your android project> 
...
project>
nice to see basedir here because you are able to move you Ant script

main concept of ant is targets (like "release", "debug", "release-signed", ets.)
every target could have dependency on other targets
and targets could be public and private (hidden)

for example simple target:
<target name="help">
<echo>This project has extended helpecho>
target> 

complex target
<target name="release" depends="resources, java_compiler, zip, sign, renameing">

target> 

you can specify properties ( they are playing the same function as variables in other languages )
<property name="app.file.name" value="${build.prop.name}" />
let me explain this
you can read/ write app.file.name using ${app.file.name} sintax
in sample you define property and set value for it equal to value of  build.prop.name property

you could override this prop value in build.properties file
lets call it first level of customization

you also  able to define properties in build.properties and then load to project
<property file="build.properties"/>

Combination of both <property name.../> and <property file=.../> creates a list of properties that could be redefined in a separate settings file 
Example:

<property name="one" value="ha-ha"/>
<property file="build.properties"/>

now we can move to targets and other xml elements

<taskdef name="setup" classname="com.android.ant.SetupTask"
classpathref="android.antlibs"/>


Defines a java class with a starting "main" point in com.android.ant.SetupTask.
reference android.antlibs - a link to path(s) where SetupTask could be found.
All android elements (tags) could use (execute) this application
using <setup>setup> tag 

few more samples
<taskdef name="apkbuilder"
classname="com.android.ant.ApkBuilderTask"
classpathref="android.antlibs" />


<taskdef name="xpath"
classname="com.android.ant.XPathTask"
classpathref="android.antlibs" />
usage:

<xpath input="AndroidManifest.xml" expression="/manifest/@package"
output="manifest.package" />


<aaptexec executable="${aapt}"
    command="package"
    verbose="${verbose}"
    manifest="AndroidManifest.xml"
    androidjar="${android.jar}"
    rfolder="${gen.absolute.dir}">

aaptexec>   
if you command sequence that you have to use in different targets you could write a macro
definition:

<macrodef name="dex-helper">
   <element name="external-libs" optional="yes" />
   <element name="extra-parameters" optional="yes" />
   <sequential> 

      <echo>Converting compiled files and external libraries into ${intermediate.dex.file}...echo>
   sequential>
 macrodef>
usage: 
<dex-helper /> 
or 
<dex-helper>
   <extra-parameters>
     <arg value="--no-locals" />
   extra-parameters>
   <external-libs>
     <fileset file="${emma.dir}/emma_device.jar" />
   external-libs>
 dex-helper/>


I hope you've got the idea.

Now about problem I met

for android platform 2.2 (API level 8) 
if you open build script that contains external targets (targets that were defined in android_rules_r3.xml or other external files)
in eclipse. you've get ant compilation errors (unknown target)

if you run the same script (build.xml) from command promt - it works fine

for example in case with auto-generated build.xml

this one return error unknown help target if you open it in eclipse
after that you are not able to compile you app!!! 
As a not very good solution is moving  build.xml file outside project root dir in this case eclipce will not see it and you can add to get it working from command line