08 September, 2012

Stress Testing Android Application Activity Start/Stop

One of the things I find before delivering Android applications is the impact of starting and stopping the application in a rapid manner. It's common that key stroke and touch handlers get invoked before all the necessary resources have been created, or after they have been released. Starting an application, followed by hitting the back button or home button can often result in calls to your application handlers or scheduled behavior after onStop() has been invoked. I find the following script assists in stress testing this form of behavior:

#!/bin/bash


for t in {0..5}; do
  for i in {0..10}; do
    adb -s emulator-5554 shell am start -a android.intent.action.MYAPPNAME -n com.abc.myappname/com.abc.myappname.MyAppName
    sleep $t
    adb shell input keyevent 3
    sleep 5
  done
done

The above script will repeatedly start the application, wait X seconds, then post the 'home' keyevent. If my app passes this test, and a series of monkey tests I'm pretty confident the users won't experience unexpected crashes. Cheers.

06 September, 2012

Customizing Views with Android

My wife, a software engineer as well, pointed out today that I've been making customizing views more difficult than they have to be.  While I've been customizing views, buttons, and the like for some time I've felt that I needed to dynamically create the objects because I didn't think you could specify them in the XML layout.  Fortunately, I was incorrect.

Consider extending the ImageView class, demonstrated in the following example;


package com.abc.customview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.content.Context;
import android.util.AttributeSet;

class MyCanvasView extends ImageView {
        public MyCanvasView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
        }

        public MyCanvasView(Context context, AttributeSet attrs) {
                super(context, attrs);
        }

        public MyCanvasView(Context context) {
                super(context);
        }
};

public class CustomView extends Activity
{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
Normal XML layout would take the form;

<?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:text="Hello World, CustomView"
    />
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tint="#00000000"
    android:layout_gravity="center"
    android:src="@drawable/background"
    android:layout_weight="85"/>
</LinearLayout>
Using the extended class requires replacing the "ImageView" class specifier to our extended sub-class using full package namespace;

<?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:text="Hello World, CustomView"
    />
  <com.abc.customview.MyCanvasView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tint="#00000000"
    android:layout_gravity="center"
    android:src="@drawable/background"
    android:layout_weight="85"/>
</LinearLayout>
Cheers.

28 July, 2012

Android Version Distributions

Came across this site, gives statistics of Android versions currently used:


30 April, 2012

Android Library Reuse

Well, it took me some time, but I finally uncover my original findings concerning creating an Android reuse library. Examples seem tough to come-by, so I'll post what I discovered. The overall objective is to define a reuse package that contains common source and resources. For the sake of this discussion, our reuse package will be defined as com.abc.lib, our sample project utilizing the library packaged as com.abc.sample01; First, let's create our reuse library project:

$ android create lib-project --name Lib --target 4 --path Lib --package com.abc.lib

Next, populate the source with library package contents:

$ mkdir -p Lib/src/com/abc/lib
$ cat LogUtils.java 
package com.abc.lib;

public class LogUtils {
  static public void Log (String msg) {
  }
}
$ cp ./LogUtils.java Lib/src/com/abc/lib

Building the library results in a JAR file located in bin.

/var/tmp/temp/Lib$ ant debug
Buildfile: /var/tmp/temp/Lib/build.xml

-set-mode-check:

-set-debug-files:

-set-debug-mode:

-debug-obfuscation-check:

-setup:
     [echo] Gathering info for Lib...
    [setup] Android SDK Tools Revision 16
    [setup] Project Target: Android 3.0
    [setup] API level: 11
    [setup] Project Type: Android Library
    [setup] 
    [setup] ------------------
    [setup] Resolving library dependencies:
    [setup] No library dependencies.
    [setup] 
    [setup] ------------------
    [setup] 
    [setup] WARNING: No minSdkVersion value set. Application will install on all Android versions.

-build-setup:
     [echo] Creating output directories if needed...
    [mkdir] Created dir: /var/tmp/temp/Lib/bin
    [mkdir] Created dir: /var/tmp/temp/Lib/bin/res
    [mkdir] Created dir: /var/tmp/temp/Lib/gen
    [mkdir] Created dir: /var/tmp/temp/Lib/bin/classes

-pre-build:

-code-gen:
     [echo] ----------
     [echo] Handling aidl files...
     [aidl] No AIDL files to compile.
     [echo] ----------
     [echo] Handling RenderScript files...
[renderscript] No RenderScript files to compile.
     [echo] ----------
     [echo] Handling Resources...
     [aapt] Generating resource IDs...

-pre-compile:

-compile:
    [javac] Compiling 2 source files to /var/tmp/temp/Lib/bin/classes
     [echo] Creating library output jar file...
      [jar] Building jar: /var/tmp/temp/Lib/bin/classes.jar

-post-compile:

-obfuscate:

-dex:
     [echo] Library project: do not convert bytecode...

-crunch:
   [crunch] Crunching PNG Files in source dir: /var/tmp/temp/Lib/res
   [crunch] To destination dir: /var/tmp/temp/Lib/bin/res
   [crunch] Crunched 0 PNG files to u
   [crunch] pdate cache

-package-resources:
     [echo] Library project: do not package resources...

-package:
     [echo] Library project: do not package apk...

-do-debug:
     [echo] Library project: do not create apk...

debug:
[propertyfile] Creating new property file: /var/tmp/temp/Lib/bin/build.prop
[propertyfile] Updating property file: /var/tmp/temp/Lib/bin/build.prop
[propertyfile] Updating property file: /var/tmp/temp/Lib/bin/build.prop
[propertyfile] Updating property file: /var/tmp/temp/Lib/bin/build.prop

BUILD SUCCESSFUL
Total time: 2 seconds
/var/tmp/temp/Lib$ 

The contents of the JAR file can confirm the existence of the LogUtils class.
/var/tmp/temp/Lib$ jar -tf ./bin/classes.jar 
META-INF/
META-INF/MANIFEST.MF
com/
com/abc/
com/abc/lib/
com/abc/lib/LogUtils.class
Next, let's create a project that will utilize the common reuse library.

$ android create project --package com.abc.sample01 --activity Sample01 --target 2 --path Sample01
We add the library dependency by specifying the relative path to the library.
$ android update project --target 4 --path Sample01 --library ../Lib
Finally, update the project to ensure it all takes affect.
$ android update project --path Sample01
Our sample activity can reference our common library components as follows;

/var/tmp/temp/Sample01$ cat src/com/abc/sample01/Sample01.java 
package com.abc.sample01;

import android.app.Activity;
import android.os.Bundle;
import com.abc.lib.LogUtils;

public class Sample01 extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LogUtils.Log("hello");
    }
}

Building the sample project via 'ant debug' somewhat duplicates the resources and source in the referencing project.

Kinda rough post, but it hits most of the generals.

13 January, 2012

Unity -- Mouse Under Keyboard Focus

I've always preferred my focus to follow my mouse in my desktop preferences.

The recent push for the Unity desktop manager for Ubuntu, Debian...has prodded me to try it out.

One of my first challenges was enabling the focus to follow the mouse location. No simple way to do it via preferences, however:


$ gconftool-2 --type string --set /apps/metacity/general/focus_mode mouse
$ gconftool-2 --set /apps/metacity/general/auto_raise -t boolean false


That'll do it.

05 October, 2011

ImageMagick -- Send Directly To Display

'til recently I've always manipulated images, stored them to a temporary file, then displayed them to confirm the desired results.

I've recently found that this is an unnecessary step, as the output of any command can be sent directly to the display using the 'win:' command directive.

E.g.

$ convert main/trunk/Media/Photos/Cats/Dempsey/DSCF0002.JPG -resize "100x100^" -gravity center -crop 100x100 win:


Cheers.

23 August, 2011

Making Use Of Idle Workstation

Have you ever given thought to the amount of computational horsepower your machine packs and how much of it is wasted when not in use? I thought it would be useful to write a script that could interrogate the current state of a Linux workstation to determine if it was available to run some background activities, similar to SETI desktop behavior.

Seems determining if the screensaver is active is a pretty good indicator of whether a user is currently using the system. Bundle that with evaluation of the system load and you can get a pretty good indication of an idle system.

Following is a Tcl-script that determines if the system screen saver is active. One could imaging bundling this will suspending/resuming a process or virtual machine to make use of the idle time.

Let me know what you think and usages you may think of.



#!/usr/bin/tclsh

proc isScreenSaverRunning { } {
set response [exec gnome-screensaver-command -q]
# puts $response
foreach e [split $response \n] {
switch -glob -- $e {
"* inactive"
{
set running 0
}
"* active"
{
set running 1
}
default
{
}
}
}
return $running
}

proc check { } {
set running [isScreenSaverRunning]
if { $running } {
puts "screen saver running"
exit
}
after 1000 [list check]
}

#-----main------
after 1000 [list check]
vwait forever