Wednesday, January 11, 2012

Reducing ArrayList add()/remove() memory re-allocation/shifts

This post is Android related, but probably goes to all java systems.

Reason: ArrayList implementation is based on an array, with a pointers to the start and end of the array.
(1) When you remove the first or last items, the pointers are moved. If you remove and items from the middle all the items after the removed location are shifted.
(2) After many removes/adds the start pointer is set so high the ArrayList is forced to shift all the items.

Solution: when removing item - swap the item with the last item, and remove the last item. Drawback - you lose the order of the items in the ArrayList.

import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collection;

public class SwapArrayList extends ArrayList {
private static final long serialVersionUID = 2995040970224168173L;

public SwapArrayList() {
this(0);
}

public SwapArrayList(int capacity) {
super(capacity);
}

public SwapArrayList(Collection collection) {
super(collection);
}

@Override
public boolean remove(Object object) {
int index = indexOf(object);
if (index >= 0) {
int size = this.size();
if (index < size - 1) {
// Swap the last item with the item to be removed
E tmp = this.get(size - 1);
this.set(size - 1, this.get(index));
this.set(index, tmp);
index = size - 1;
}

remove(index);
return true;
}
return false;
}

@Override
public E remove(int location) {
int size = this.size();
if (0 <= location && location < size) {
if (location < size - 1) {
// Swap the last item with the item to be removed
E tmp = this.get(size - 1);
this.set(size - 1, this.get(location));
this.set(location, tmp);
location = size - 1;
}
} else {
throw new IndexOutOfBoundsException();
}

return super.remove(location);
}
}

Sunday, January 8, 2012

onKeyDown/onKeyUp/onTrackballEvent does not fire in a View

When trying to get keypad events in a view, the view does not get those events unless explicitly sent by the Activity, so to fix this add the following code in the Activity:

@Override
public boolean onTrackballEvent(MotionEvent event) {
if (mView.dispatchTrackballEvent(event) == true)
return true;

return super.onTrackballEvent(event);
}

Thursday, December 15, 2011

Finding table by column name in a database

From time to time I need to find a table in a database which I know the column name but I have to search for the table.

Here's how to do it in Oracle:
SELECT TABLE_NAME, COLUMN_NAME
FROM USER_TAB_COLUMNS -- COLS,
WHERE COLUMN_NAME = 'ColumnNameHere'
GROUP BY TABLE_NAME, COLUMN_NAME;

Here's how to do it in SQL Server:
SELECT obj.Name, col.Name
FROM sysobjects obj, syscolumns col
WHERE obj.id = col.id
AND col.Name = 'ColumnNameHere'

Thursday, November 24, 2011

Protected free Android applications

One issue I'm facing from time to time is people copying my apps & change the ad network code.

I found the following methods good fighting them:
  1. Use ProGuard to obfuscate the code. If the code is obfuscated it's harder to remove other protective measures listed below. Some notes on ProGuard:
    (a) Move important functionality from non-obfuscated functions (such as onCreate)
  2. Check application signature, use: getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES).signatures[0]
  3. CRC/Checksum validation of resources
  4. CRC/Checksum validation of the APK.
  5. Check if the application is running in debugger.
  6. Check installed package name.

When you discover an issue DO NOT stop the application. Start a sequence that will cause the application crash (for example, clear a list that should not be empty).

Monday, October 24, 2011

AndEngine BitmapTextureAtlas extension

Last few days I played with AndEngine, an open source, free, games framework for Android. I'm not sure yet if AndEngine will save time building new games.

Anyhow, one weird part in AndEngine is the way you load all the resources into a single canvas. You have to define the location of each resource on the canvas, and if you change the resource size you have to recalculate everything.

Here is a simple extension to BitmapTextureAtlas that calculate everything automatically and saves you the trouble of positioning the resources on the canvas.



And this is how to use it (in the onLoadResources funcation):

Tuesday, October 18, 2011

Free resources for applications

This post is a work in progress.

Here are few places I've found for useful for free resources. You should verify if the specific resource license fit your needs.

Free fonts:
*. http://www.fontsquirrel.com/ (verify the copy rights per font, most are not really free)
*. http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL (read the OFL license first)

Free clip arts:
*. http://www.openclipart.org/.
*. Huge list of sites that contain free images.

Free music/sound affects:
*. http://www.freesound.org - Creative common license.
*. There are many sites that looks OK, just search for 'free sound effects'.

A bit off-topic - royalty free resources (not free):
*. Music loops
*. Tons of resources: The Marketing Guide for Game Developer

Tuesday, October 11, 2011

Monetizing Android applications

In a recent talk I've heard by Itay Rokni of StartApp.com he listed the various monetizing options Android developer has (to date):

  • In app ads - Ads inside the application: banners, full page, leads generators etc. See for example AdMob.

  • Paid apps - The user pay to get the application. Not free options.

  • Freemium - Basic application is free, the user can buy the full application or in-app buy more features.

  • Notification bar ads - Ads that appear not inside the application, but in the notification bar. For example see airpush.com.

  • Search icon - Create a search icon after app installation. The provider pay per installation or revenue share. For example see StartApp.com






Few notes if you're using In app ads:

  • Trust no one - AdMob is the industry benchmark and every says 'we're better than AdMob'. I found this statement to be almost always wrong.

  • Use mediation layer - Use mediation layer such as AdWhirl to control which ad networks you're using. Ad networks go up and down, it's better to control the follow of ads dynamically and not depend on updating the APK in the market.