db4o at Honeycomb and Ice Cream Sandwich

18/11/2011 4 comments

Did you receive a NetworkOnMainThreadException while opening a new db4o database?

Since my last post I just worked with Andorid 2.x on which I could get db4o easily to run. When I wanted to run my db4o app at Android Honeycomb I received the NetworkOnMainThreadException exception.

Google introduced with Honeycomb the Android.os.NetworkOnMainThreadException which occurs when an application attempts to perform a networking operation on it’s main thread (see here). This doesn’t sound like it matters for a database environment. But that’s exactly what happens when I call


Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);

for the first time. Just for the first time because it seems that when db4o creates a new db file it preforms a kind of network look up. At least the exception java.net.InetAddress.lookupHostByName(InetAddress.java:477) lets me guess so. When I take the db file which has been created at Android Gingerbread without any problems, copy it with the file explorer into the directory and start the app, the exception isn’t thrown.

For that reason it’s necassary to preform the first call for opening a new db not into the UI Thread. A possibility could be to use the AsyncTask:


class OpendDBTask extends AsyncTask<Void, Void, ObjectContainer>{

   @Override
   protected ObjectContainer doInBackground(Void... params) {
      String dbPath = "/data/data/" + DBManager.getPackageName() + "/database";
      ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);
      return db;
   }
}

Maybe I’ll provide a android database manager framework for db4o later. If you want to try db4o check out this post.

Once again I would like to point out that this issue only occurs when a new db is created via “Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);” at Android Honeycomb or later!

Cheers, mavi

Categories: General Tags: , , ,

Using db4o as Database for Android

04/11/2011 3 comments

Sick of object rational mapping with SQLite?

I read a long time ago about the object database db4o, which is very handy for storing objects instead of mapping the variables of objects to tables of a database. After some work with SQLite at Android it crossed my mind and I finally had enough time to give it a try. Normally you have to do the mapping by hand, means writing kind of DB manager which handles requests. These requests need to be coded in SQL which makes it very time-consuming. So you need to take care of all CREATE, INSERT, SELECT, UPDATE and DELETE operations by yourself.

The approach of db40 is to persist an entire object. To make clear how it works in practice I’m going to give some code snippets:

The class for the objects which should be persisted:

public class Person{
   //vars
   public String name;
   public int number;
   public String email;

   //empty constructor
   public Person(){}

   //constructor for a retrieve operation
   public Person(String name, int number, String email){
   this.name = name;
   this.number = number;
   this.email = email;
}

Create the DB:

   String dbPath = "/data/data/" + getPackageName() + "/database";
   ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);

Insert an object:

   //creates object
   Person person = new Person("Tom", "100", "Tom@db4o.com");
   //saves object
      try {
         db.store(person);
      } finally {
         db.close();
      }

Retrieve all objects:

   try {
      result = db.queryByExample(Person.class);

      while(result.hasNext()){
         Person person = (Person)result.next();
         Log.v("TAG", "Name: " + person.name + " Number: " + person.number + " eMail: " + person.email);
      }
   } finally {
      db.close();
   }

Retrieve all objects with the persom.name “Tom”:

   //Specifies the search pattern (null and 0 is wildcard)
   Person protoPerson = new Person("Tom", 0, null);

   try {
      result = db.queryByExample(protoPerson);

      while(result.hasNext()){
         Person person = (Person)result.next();
         Log.v("TAG", "Name: " + person.name + " Number: " + person.number + " eMail: " + person.email);
      }
   } finally {
      db.close();
   }

Do you see what my point is? Very straight forward to use db framework which has in accordance with this document even high performance. Further documentation on how to use db4o is available here. Source of a prototype test app maybe later here.

There is a small snag: It is under GPL or a commercial license.

Cheers mavi

Categories: General Tags: , ,

Blur the Background of Android Standard Search Dialog

13/08/2011 3 comments

How to blur the background of a standard search dialog?

I didn’t find any good solution on this topic in the web. The common response was “it’s not possible”. So I began to figure out a solution by myself, which finally works fine! Let’s start over:

Because you don’t get a handle of the standard android search dialog it seems to be impossible to blur the background by calling

Window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

But there is an other approach: Generally you want to perform a search on a list. The ListView can be set on a RelativeLayout. On top of it you can set a View with the android:background=”@drawable/aShape”, which covers the entire ListView. The visibility should be “gone”. The drawable aShape is in my case a gradient with an alpha value.

The RelativeLayout:

<ListView android:id="@+id/list"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"               
          android:layout_weight="1"
          android:drawSelectorOnTop="false"/>
               
<View android:layout_weight="1"
      android:layout_width="wrap_content"
      android:id="@+id/list_search_overlay"
      android:layout_height="wrap_content"
      android:background="@drawable/search_overlay"
      android:visibility="gone"></View>

The shape (search_overlay in my case):

<gradient
    android:startColor="#80000001"
    android:endColor="#80000001"/>

Now onSearchRequested() must be overridden and set the View (in my case R.id.list_search_overlay) to visible. Finally a listener must be registered at SearchManager.setOnDismissListener() to set the View to visible again if the search dialog is closed.

The code (from the activity):

@Override
public boolean onSearchRequested() {
 ((View) findViewById(R.id.list_search_overlay)).setVisibility(View.VISIBLE);
 final SearchManager searchManager = (SearchManager) this.getSystemService
                                                        (this.SEARCH_SERVICE);
 searchManager.setOnDismissListener(new OnDismissListener() {

 @Override
 public void onDismiss() {
  ((View)ServiceListBase.this.
         findViewById(R.id.list_search_overlay)).setVisibility(View.GONE);
 }
});
return super.onSearchRequested();

I hope this will assist you by optimizing the user experience with your app.

Cheers Mavi

Categories: General Tags: , , ,

How to write a paper in a scientific context

17/07/2011 Leave a comment

Have you ever been in the position to write a scientific paper and you didn’t know where to start and what to do?

In the paper “How to Write a Paper” the author Mike Ashby describes the 5 steps to write a successful paper.It was very supportive while I wrote my Bachelor-Thesis, so I would like to provide it here.

First he describes how to figure out the market needs and how this brings you to a concept. Based on the concept get the first draft and improve it recursive. Finally the layout has to be done.

This brief manual gives guidance in writing a paper about your
research. Most of the advice applies equally to your thesis or to
writing a research proposal. The content of the paper reflects the
kind of work you have done: experimental, theoretical,
computational.

Download as *.pdf: How to Write a Paper

Cheers Mavi

Categories: General Tags: , , ,

PowerShell 2.0 – Binary Runtime measuring

24/06/2011 Leave a comment

How to measure runtimes for applications not in the program itself?

Therefore the PowerShell is a nice utility under windows. Sometimes there are reasons why it’s not possible to measure the runtime of a specific algorithm or program in itself. Means not in the native code of the program. Or you just want to measure different programs automated. Get the job done by the Windows PowerShell!

To be concrete:

#!msh
function Pause ($Message="Press any key to continue...")
{
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host ""
}

function Benchmark($path)
{
write-host "Benchmark for "$path
$startTime = New-TimeSpan "01 January 1970 00:00:00" $(Get-Date)
$startTimeLong = [LONG] $startTime.TotalMilliseconds
write-host "Started @" $startTimeLong
Start-Process -wait $path
$endTime = New-TimeSpan "01 January 1970 00:00:00" $(Get-Date)
$endTimeLong = [LONG] $endTime.TotalMilliseconds
write-host "Ended @" $endTimeLong
$result = $endTimeLong - $startTimeLong
write-host "Time(Milliseconds):" $result
}

Benchmark("a.exe")
Benchmark("b.exe")
Benchmark("c.exe")
Benchmark("../pki3/d.exe")
Benchmark("../pki3/e.exe")
Pause

The function “Pause” I’ve taken from Windows Powershell Blog. The function “Benchmark” handles the measurments for the given program (program path).

For measuring the time while the program is running it first gets the date and saves it as a long (milliseconds since 01.01.1970) in $startTimeLong. Afterwards the external program is started, the parameter “-wait” lets the script pause until it terminates. After termination it the gets the date again and saves it as $endTimeLong. The result in milliseconds is the execution time.

Various usage of this function is thinkable.

Cheers Mavi

Categories: General Tags: , ,

The Shell with Power! PowerShell 2.0 [how to activate script execution]

23/06/2011 Leave a comment

Sick of *.bat – shell scripts? – Powershell (2.0)!

It’s not really something new, but I just did need it until now. So I’m surprised by the mighty of it. Additional it comes with a small IDE which makes developing and debugging very handy.

Before you enjoy the power of the shell you have to activate the script execution within your system. Therefore you have to set the execution policies:

Start the PowerShell as administrator

Get-ExecutionPolicy

Displays the current policy

Set-ExecutionPolicy RemoteSigned

This sets a policy. Valid values are:

  • Restricted -> Impossible to run any PS script (standart)
  • AllSigned -> Each script needs to be signed (check PS Blog for details). Possible choice but to unhandy for my taste.
  • RemoteSigned -> Each local script can run unsigned, scripts from the web need to be signed. My choice!
  • Unrestricted -> Each script runs! Way to insecure!!!

After changing the policy it’s possible to run your scripts. You can simply work with a text editor (eg. Notepad++) and save the script as *.ps1. If you’re doing so, you can start the script by double click in your file browser.

Even more comfortable is the PowerShell ISE. It comes with syntax highligthing, part execution and real debugging.

Finally I have an other scope: Using it as a calculator! Nice if you want to see your calculation and not just the final result. More…

Cheers Mavi

Galaxy S Driver for Android debugging with Eclipse

18/05/2011 3 comments

Don’t want to install Samsung Kies? Just want to debug your Android app at the Galaxy S? Here are the drivers!

Since I spent hours with searching for a simple driver to run my in Eclipse developed Android Application directly at the phone, I provide them here:

USB driver Galaxy S for win 32 bit (Has been tested with Win XP 32Bit)

USB driver Galaxy S for win 64 bit (Has been tested with Win 7 64Bit)

Just install the driver by running the corresponding *.exe file. Afterwards switch to debugging mode at your Galaxy: Settings -> Application -> Development; Tick the debugging mode! Finally you have to plug in the USB cable. Now “run” your application from Eclipse and it will automated chose your mobile as target!

Cheers, Mavi

Netbeans 6.8 – Interesting Editor Parsing Bug

29/04/2011 Leave a comment

Do you love the syntax highlighting, code completion and live error reporting too?

That’s why I like NetBeans so much. And in comparison to Eclipse is the work flow a lot better. While I worked at a Java project I got an weird error message (see picture below). I was confused for a while and tried to figure out what exactly the mistake was. Nothing of course, just using a given parameter.

NetBeans editor parsing bug

So I added by copy & paste the line and commented the old one out. And it WORKED! I removed the commented line and could compile the source. I guess it’s a parsing bug by NetBeans, but I wasn’t able to reproduce it…

The same line, but no error...

Very funny story =) I hope you don’t have the same trouble…

Greetz Mavi

Categories: General Tags: , , ,

Evolution of MS Windows

13/03/2011 Leave a comment

Upgrading OS from Win 1.0 to Win 7!

I found a nice piece in the web. A guy upgrading through all versions of Windows since 1.0 in a VM. Games from this early Version (1985) still work on Win 7 (2009) :-)



Enjoy, Mavi

Categories: General Tags: , ,
Follow

Get every new post delivered to your Inbox.