Translate

Archive for the 'Code' Category

[SOLVED] Eclipse can’t see my Android Device on Ubuntu

Friday, May 28th, 2010

Are you seeing this on eclipse when you plug your Android device to your Ubuntu box?

Serial Number: ??????????
AVD Name: N/A
Target: unknown
State: ??

Here’s the solution:

1. Create a script to fix this next time it happens, let’s call it “android_device_reset” and save it on a folder contained on your $PATH environment variable.

#!/bin/bash
# android_device_reset script
sudo adb kill-server
sudo service udev stop
sudo adb start-server
sudo adb devices

Save it and make it executable

chmod +x android_device_reset

2. Open this file /etc/udev/rules.d/51-android.rules

Make sure it looks something like this

SUBSYSTEMS=="usb", SYSFS{idVendor}=="0bb4", MODE=="0666"
SUBSYSTEMS=="usb", SYSFS{idVendor}=="22b8", MODE=="0666"

Each line represents a different android device. If you have just one, the file should be one line long.

On that example I’ve configured the rules for a Motorola Droid and a Nexus One.
If you need to know the idVendor numbers for your Android device go here
developer.android.com/guide/developing/device.html#VendorIds

3. Whenever the problem happens, just open a terminal and type

android_device_reset

It’ll ask you for your password, only administrative users will be able to execute the script.

Hope this helps.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

How many lines of code does it take to create the Android OS?

Sunday, May 23rd, 2010

This is a report done on all the projects that make up for the android project, my copy of it is synced as of May 23rd 2010, 6pm
(more…)

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Droid vs Nexus 1: Who can calculate MD5 faster?

Friday, May 21st, 2010

Nexus 1 indeed.


17 files get their MD5 calculated on the Droid


and 17 files get their MD5 hash calculated on the Nexus 1

Nexus 1 pwns.

Here’s the code in case you’re curious.

	public void onClick(View v) {
                                _logTextView.setText("MD5 Benchmark on " + Build.DEVICE + "\n\n");
				if (GlobalVariables.APP_CONTEXT == null)
					GlobalVariables.APP_CONTEXT = getApplicationContext();

				List<FileDescriptor> sharedAudioFiles = Engine.INSTANCE.LIBRARIAN.getSharedAudioFiles(0, 17);
				for (FileDescriptor fs: sharedAudioFiles) {
					long start = 0;
					String md5 = null;
					long length = 0;
					try {
						start = System.currentTimeMillis();
						File f = new File(fs.path);
						length = f.length();
						md5 = FrostWireUtils.getMD5(f);

					} catch (Exception e) { }
					long time = System.currentTimeMillis() - start;
					_logTextView.append(FrostWireUtils.getBytesInHuman(length) + " in " + time + " ms @ " + (length/(double) time)*1000 + " b/s\n");
				}
			}

And here’s how we do the MD5

	public final static String getMD5(File f) throws Exception {
		MessageDigest m = MessageDigest.getInstance("MD5");

		byte[] buf = new byte[65536];
		int num_read;

		InputStream in = new BufferedInputStream(new FileInputStream(f));

		while ((num_read = in.read(buf)) != -1) {
			m.update(buf, 0, num_read);
		}

		String result = new BigInteger(1, m.digest()).toString(16);

		// pad with zeros if until it's 32 chars long.
		if (result.length() < 32) {
			StringBuffer padding = new StringBuffer();
			int paddingSize = 32 - result.length();
			for (int i = 0; i < paddingSize; i++)
				padding.append("0");

			result = padding.toString() + result;
		}

		return result;
	}
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Think you’re in a big project?

Tuesday, May 18th, 2010

Recently I became curious on how many lines of code a huge open source project I contribute to has on what languages. I found a tool called “cloc” on sourceforge, check out the results and I dare you to think again if you think you’re in a big project.
(more…)

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

[SCREENCAST] How to do Unit Testing on Android with Eclipse

Sunday, May 2nd, 2010

I was going to make a tutorial, but then I figured that making a video would be a much better way to show this.
As for the code that you could grab from a tutorial, there’s a link at the end of the post with all the code shown in the video demo.

The video demo covers how to create and run Unit Test classes for regular Java classes on Android, and also how to create and run Unit Test classes that test classes that depend on Android “Context” or “Activity” objects.

If your Android unit tests are not running because of frustrating error messages, the time spent watching this video will save you a lot of reading and headaches.

Check the screencast after the break

(more…)

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Programming Languages Popularity by the number of Tagged Questions at StackOverflow.com

Monday, April 12th, 2010

Caught a Bug - by Gubatron

1. C# 73,833
2. Java 43,006
3. PHP 35,371
4. Javascript 31,244
5. C++ 27,340
6. Python 22,070
7. Objective-C 10,350
8. Ruby 8,773
9. VB.net 7,778
10. ActionScript (Flash) 5,230
11. Perl 4,496

What do these numbers mean to you? Languages on the rise of popularity among programmers, or lack of good documentation?

[buzz href="http://www.google.com/buzz/gubatron2/UJr3LGeN53k/Programming-Languages-Popularity-by-the-number-of" liked="14"]

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Java: How to create dynamic PNGs, JPGs, GIFs.

Sunday, February 14th, 2010

Sometimes you need to create graphics, or compose images and have them saved as regular PNGs, JPEGs or GIFs.

Here’s a quick and dirty reference of how to do it with BufferedImages, Graphics2D and javax.imageio.*.
Very straightforward.

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
...

//1. Create a BufferedImage, in this case a simple 1024x768 using only RGB colors (you could use alpha for example)
BufferedImage bufferedImage = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB);

//2. Get a hold of a Graphics2D object to do all the painting and compositing on the BufferedImage
Graphics2D graphics = bufferedImage.createGraphics();

//3. Do the painting... in this case, I just filled with yellow
graphics.setColor(new Color(255,255,0));
graphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());

//4. Write the image
try {
    ImageIO.write(bufferedImage, "png",new File("test.png"));
} catch (IOException e) {
   e.printStackTrace();
}
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Quick N Dirty way to Map Commands to remote servers via ssh

Saturday, October 10th, 2009

You may be running several independent but similar servers at the same time and wasting time by executing commands in all of them one by one.

Wouldn’t it be nice to send a command to all of them at once? or to monitor all of them at once.

The following script can be used as a building block to more complex automation tasks for a small size set of servers. (If you’re managing over 50 servers, I’d probably consider looking a different way to arrange servers (map/reduce cluster), but if you’re doing something below that number this might suffice)

#!/usr/bin/python                                                                                                                                                                                                                                                      

#########################################################
# Author: Angel Leon (gubatron@gmail.com) - October 2009
#
# Invokes a command locally and invokes the same command
# in all machines under the specified username, servers
#
# Requirement: Have a public ssh_key for that user on all
# the other machines so you don't have to authenticate
# on all the other machines.
#########################################################
import sys
import os

# set the username that has access to all the machines here
user='safeuser'

# add all your server names here
servers=['server1.mydomain.com','server2.mydomain.com','server3.mydomain.com']

if __name__ == "__main__":
  if len(sys.argv) < 2:
    print "Usage: ssh_map_command <cmd>"
    sys.exit(0)

  cmd= ' '.join(sys.argv[1:])

  #Execute locally first
  print cmd
  os.system(cmd)

  #Execute for all the servers in the list
  for server in servers:
    remote_cmd="ssh %s@%s %s" % (user,server,cmd)
    print remote_cmd
    os.system(remote_cmd)
    print

Save as ssh_map_command and chmod +x it.

Sample uses
Check the average load of all machines at once (then use output to mitigate high load issues)

$ ssh_map_command uptime

Send HUP signal to all your web servers (put it in an alias or other script… and that’s how you start building more complex scripts)

$ ssh_map_command ps aux | grep [l]ighttpd | kill -HUP `awk {'print $2'}`

Check if processes are alive, check memory usage on processes across different machines, grep remote all logs at once, svn up on all machines, rsync from one to many, hey, you can even tail -f and grep all the logs at once, you can go nuts with this thing. Depends on what you need to do.

Requirements

Security Advisory
Make sure only the desired user has read/write/execute access to it and keep your private ssh keys safe (preferably only read and execute for the owner, and no permissions whatsoever to anybody else chmod 500 ssh_mod_map), if possible change them as often as possible, for it may become a big security whole if an attacker can manage to write code on this script, specially if you have cronjobs invoking it. Your attacker would only need to change code here to mess up all of your machines.

Disclaimer and Call for Knowledge
Please, if someone knows of a standard way to map commands to multiple servers, please let me know in the comment section, in my case I needed a solution and I wrote a quick and dirty python script and tried to secure it as best as I could, by no means I’m saying that this is the best solution to mapping commands, in fact I believe it might be the least efficient way, however it works good enough for my personal needs.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Ubuntu/Debian Quick Reference: How To Change Your Server’s UTC Timezone on the command line

Saturday, October 10th, 2009

Just Type…
sudo dpkg-reconfigure tzdata

…and follow the instructions on screen.

The process should look something like the following:


Select your Region


Select a city on your time zone


You’re done.

Tip
You can always check the status of your configuration using
sudo debconf-show tzdata

You could for example map that command via ssh to several machines and grep for “*”, that way you could easily spot servers with wrong timezones very quickly.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Check the Top 10 Linux Commands you can’t live without

Monday, September 28th, 2009

Type the following on your cmd line (or make into an alias)

cat ~/.bash_history | sort | uniq -c | sort -r | head

In my case they are (for this week)

ls
fg
svnSync (script I created)
stats_fetch; stats_display (other scripts)
cd
crontab -e
ps aux | grep
ssh_map_command (another script)
python
emacs -nw
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)


  • Categories

  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • October 2009
  • September 2009
  • July 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004