2013-04-29

0 Comments

Simple javascript to validate java packageName

Here is a simple javascript that validates a java package name

function isValidJavaPackageName(packageName, warn) {
if(packageName.length < 3) {
	       if(warn) alert('to short!');
	       return false;
	    }

	    if(packageName.indexOf(".") == -1) {
	        if(warn) alert('must contain at least one . (dot)');
	        return false;
	    }

	    if(packageName.indexOf("..") !== -1) {
	        if(warn) alert('must not contain two . in a row');
	        return false;
	    }


	    if(packageName.indexOf("_.") !== -1) {
	        if(warn) alert('must not contain _.');
	        return false;
	    }

	    if(packageName.indexOf("._") !== -1) {
	        if(warn) alert('must not contain ._');
	        return false;
	    }


	    if(packageName.indexOf(".0") !== -1) {
	        if(warn) alert('must not contain .0');
	        return false;
	    }
	    if(packageName.indexOf(".1") !== -1) {
	        if(warn) alert('must not contain .1');
	        return false;
	    }
	    if(packageName.indexOf(".2") !== -1) {
	        if(warn) alert('must not contain .2');
	        return false;
	    }
	    if(packageName.indexOf(".3") !== -1) {
	        if(warn) alert('must not contain .3');
	        return false;
	    }
	    if(packageName.indexOf(".4") !== -1) {
	        if(warn) alert('must not contain .4');
	        return false;
	    }
	    if(packageName.indexOf(".5") !== -1) {
	        if(warn) alert('must not contain .5');
	        return false;
	    }
	    if(packageName.indexOf(".6") !== -1) {
	        if(warn) alert('must not contain .6');
	        return false;
	    }
	    if(packageName.indexOf(".7") !== -1) {
	        if(warn) alert('must not contain .7');
	        return false;
	    }
	    if(packageName.indexOf(".8") !== -1) {
	        if(warn) alert('must not contain .8');
	        return false;
	    }
	    if(packageName.indexOf(".9") !== -1) {
	        if(warn) alert('must not contain .9');
	        return false;
	    }

	    if(/^[0-9a-z._]*$/.test(packageName) == false) {
	        if(warn) alert('packagename contains invalid character, valid characters is: 0-9a-z . _');
	        return false;
	    }

	    var firstChar = packageName.charAt(0);
	    var lastChar = packageName.charAt(packageName.length-1);

	    if($scope.isNumber(firstChar)) {
	        if(warn) alert('first character in packageName cannot be numeric');
	    	return false;
	    }
	    if($scope.isNumber(lastChar)) {
	        if(warn) alert('last character in packageName cannot be numeric');
	    	return false;
	    }

	    if(firstChar == '.') {
	        if(warn) alert('packageName cannot start with a . (dot)');
	        return false;
	    }
	    if(firstChar == '_') {
	        if(warn) alert('packageName cannot start with a _ (underscore)');
	        return false;
	    }
	    if(lastChar == '.') {
	        if(warn) alert('packageName cannot end with a . (dot)');
	        return false;
	    }
	    if(lastChar == '_') {
	        if(warn) alert('packageName cannot end with a _ (underscore)');
	        return false;
	    }

	    return true; // valid
}
Continue reading...

2013-04-12

0 Comments

Keep aspect ratio in android ImageView

I just wanted a ImageView in an android layout to fill the parents width and scale X while keeping aspect ratio, my solution was to create a custom image view and since i knew the image always have a ratio 544*144 i could easily  create it like this:

package se.warting;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

public class KeepAspectRatioImageView extends ImageView {

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

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

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = (int) (width * (144f / 544f));

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		this.setMeasuredDimension((int) width, (int) height);
	}

}

And now simply add the image view to the xml layout:

<se.warting.KeepAspectRatioImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/menu_header" />
Continue reading...

2013-02-08

1 Comment

Splunk is spamming by pretending to send out personalized emails

Splunk is spamming by pretending to send out personalized emails without unsubscribe link which apparently is automated and does not understand that the same message is sent to the same person multiple times.

splunk-is-crap

2013-01-31

1 Comment

My set of android devices used for testing

Earlier this morning, I asked which android devices are goot to use as test units for different ansdroid versions when developing. I got no clear answer and I found no updated guide online. I have tried to summarize the different devices I think should be used when testing. Some have probably misinformation regarding screen resolution and dpi, so I would appreciate a comment on what is right and wrong.

what should be added?
What should be removed?
What should be moved arround?

Phones listed into groups

Group 1 – Newest and coolest

  • Nexus 4
  • Nexus 7
  • Nexus 10
  • HTC one x
  • Droid Razr Maxx HD
  • Samsung Galaxy S3
  • Galaxy nexus
  • GT-P3100 Samsung Galaxy Tab 2
  • GT-N7000 Samsung Galaxy Note

Group 2 – Avarge

  • LG Optimus 2X
  • Samsung Galaxy S
  • Sony Xperia™ active
  • Motoroola XOOM
  • Nexus S
  • Samsung Galaxy S II
  • GT-P7500 Samsung Galaxy Tab 10.1

Group 3 – Worst case

  • HTC Wildfire S
  • HTC Wildfire
  • Sony Xperia™ mini
  • Samsung Galaxy Mini

Testing android versions

Donut – 1.6

  • Sony Xperia™ mini

Eclair – 2.1

  • HTC Wildfire

Froyo – 2.2:

  • HTC Wildfire S

Gingerbread – 2.3.x:

  • Sony Xperia™ active

Honeycomb:

  • Motoroola XOOM

Ice Cream Sandwich:

  • Galaxy nexus

Jelly Bean:

  • Nexus 4
  • Nexus 7
  • Nexus 10

Testing screen sizes and densities

Small – ldpi

  • Sony Ericsson Xperia X10 Mini
  • Samsung Galaxy Mini

Normal – hdpi

  • Sony Xperia™ active
  • Optimus 2X

Normal – ldpi

  • which devices?

Normal – mdpi

  • HTC Wildfire S
  • Sony Xperia™ mini
  • Sony Xperia™ active

Normal – hdpi

  • Samsung Galaxy S II
  • Nexus S

Normal – xhdpi

  • Samsung Galaxy Nexus
  • Nexus 4

large – ldpi

  • which devices?

large – mdpi

  • GT-P3100 Samsung Galaxy Tab 2

large – xhdpi

  • GT-N7000 Samsung Galaxy Note

xlarge – mdpi

  • Motorola XOOM
  • GT-P7500 Samsung Galaxy Tab 10.1

Testing different Open GL Version

1.1 only

  • Android version < 2.2

2.0 & 1.1

  • Android versions >= 2.2

 

References:

2012-06-04

13 Comments

Chat bubbles in Android

Here is a simple project that shows how to make chat bubbles in android.
http://warting.github.com/AndroidChatBubbles/

It’s a good project to inspect if you want to learn some basic stuff from the following parts:

  • How to design Nine patch images
  • How to use a listview
  • How to connect a array adapter to a listview
  • How to populate a array adapter on the fly
  • How to generate random lorem ipsum text
Screen shoot of android chat bubbles

Learn how to create android chat bubbles

2011-12-19

0 Comments

Convert time greater than 24H in MsSQL

If you have a time like 24:00 or even 28:12 in SQL and want to convert it to a normal time this is the way to do it:

SELECT [time] = RIGHT(’0′+ RTRIM(CAST(substring(’28:12′,1,2)%24 as NCHAR(2))),2)+’:'+substring(’28:12′,4,5)

Sample results:
08:00=08:00
23:45=23:45
24:00=00:00
28:12=04:12

If you know a better way, please let me know!

2011-12-05

0 Comments

Logout from all android activities

If you have some activities in an android application and what to close all activities without having to worry about the back button, you can register a broadcast receiver that get triggered when you push the logout button.

Lets create a new class that we can inherit on all activities that requires login:

public class LogedInActivity extends Activity {

	public class LogoutReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals("com.package.ACTION_LOGOUT")) {
				finish();
			}
		}
	}

	private LogoutReceiver logoutReceiver;

	@Override
	protected void onDestroy() {
		// Unregister the logout receiver
		unregisterReceiver(logoutReceiver);
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		logoutReceiver = new LogoutReceiver();

		// Register the logout receiver
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction("com.package.ACTION_LOGOUT");
		registerReceiver(logoutReceiver, intentFilter);

	}

}

Then when you press the logout button you can close all open activities by just sending a broadcast intent:

// on your logout method:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_LOGOUT");
c.sendBroadcast(broadcastIntent);

2011-11-07

0 Comments

Design a splash screen for android

Here is a good summary of what to consider when designing a splash screen for android:

http://stackoverflow.com/questions/5523888/android-how-to-design-image-for-splash-screen

2011-09-22

0 Comments

Simple PHP script to determine if a image is a valid 9 patch png image

This is a PHP script to determine if an image is an valid 9 patch PNG image that can be used in an android application

<?php 

$src = dirname(__FILE__).'/btn.9.png'; //change me
if(function_exists('imagecreatefrompng') && function_exists('imagecolorat')) {
	$isvalid = IsValid9PatchImage($src);
	print $src . ' is a ' . ($isvalid ? 'valid':'invalid') . ' 9patch image';
}
else {
	print "Could not check if ".$src." is a valid 9patch image. the php functions imagecreatefrompng and imagecolorat is required";
}

function IsValid9PatchImage($pngRel) {
	$im = imagecreatefrompng($pngRel);
	$height = imagesy($im);
	$width = imagesx($im);
	
	//Not a valid png image?
	if($height <= 0 || $width <= 0) {
		return false;
	}
	$anyBlack = false;
	for($x = 0; $x < $width; $x++){
		$y = 0;
		$rgba = imagecolorat($im,$x,$y);
		$alpha = ($rgba & 0x7F000000) >> 24;
		$red = ($rgba & 0xFF0000) >> 16;
		$green = ($rgba & 0x00FF00) >> 8;
		$blue = ($rgba & 0x0000FF);
		
		$isAlpha = false;
		$isBlack = false;
		if($alpha == 0 && $red == 0 && $green == 0 && $blue == 0) {
			$isBlack = true;
			$anyBlack = true;
		}
		else if($alpha == 127) {
			$isAlpha = true;
		}
		
		//If the top left pixel is not transparent then false
		if($x == 0 && $isAlpha == false) {
			//die('If the top left pixel is not transparent then false. x:' . $x . ' y:' . $y);
			return false;
		}
		
		//If the top right pixel is not transparent then false
		if($x == $width-1 && $isAlpha == false) {
			//die('If the top right pixel is not transparent then false. x:' . $x . ' y:' . $y);
			return false;
		}
		
		//If it is not completly transparent or black, then false 
		if(!$isAlpha && !$isBlack) {
			//die('If it is not completly transparent or black, then false. x:' . $x . ' y:' . $y);
			return false;
		}
	}
	if($!anyBlack) {
		return false;
	}

	$anyBlack = false;
	for($y = 0; $y < $height; $y++){
		$x = 0;
		$rgba = imagecolorat($im,$x,$y);
		$alpha = ($rgba & 0x7F000000) >> 24;
		$red = ($rgba & 0xFF0000) >> 16;
		$green = ($rgba & 0x00FF00) >> 8;
		$blue = ($rgba & 0x0000FF);
		
		$isAlpha = false;
		$isBlack = false;
		if($alpha == 0 && $red == 0 && $green == 0 && $blue == 0) {
			$isBlack = true;
			$anyBlack = true;
		}
		else if($alpha == 127) {
			$isAlpha = true;
		}
		
		//If the top left pixel is not transparent then false
		if($y == 0 && $isAlpha == false) {
			//die('If the top left pixel is not transparent then false. x:' . $x . ' y:' . $y);
			return false;
		}
		
		//If the top right pixel is not transparent then false
		if($y == $width-1 && $isAlpha == false) {
			//die('If the top right pixel is not transparent then false. x:' . $x . ' y:' . $y);
			return false;
		}
		
		//If it is not completly transparent or black, then false 
		if(!$isAlpha && !$isBlack) {
			//die('If it is not completly transparent or black, then false. x:' . $x . ' y:' . $y);
			return false;
		}
	}
	if($!anyBlack) {
		return false;
	}

	return true;
}



?>

2011-08-28

1 Comment

Easy tool to draw a nine patch image

 I have created a new tool to to draw a nine patch image for android online. Its free and easy to use!

Please try it out: http://draw9patch.com/