Saturday, February 16, 2013

Setting Firefox location bar search engine

Recently I installed a freeware which came with ask.com plugin for firefox. This plugin after installing became default for lot of features in the browser which was really irritating me so had to remove the plugin.

Even then any search I do by entering the keyword directly in the address bar (location bar) went through ask.com. If you are also facing similar problem here are the steps to set your favorite search engine.

1. Type in about:config in the location bar and press Enter
2. You might see the This might void your warranty! warning screen. Click I'll be careful, I promise! to continue.
about
3. Enter keyword.URL in search field and press Enter.
4. Double click the keyword.URL to modify the value. Here is the list of URL for different search engines.
about1
Google
https://www.google.com/search?q=

Yahoo
http://search.yahoo.com/search?p=

Bing
http://www.bing.com/search?q=

Note
To disable this feature enter keyword.enabled and double click to set the value to true.

Tuesday, February 12, 2013

Declaring java package and method signature in JRuby

We can add the package declaration for the java classes generated by JRubyc by using java_package. Just add the following line before the JRuby class declaration

java_package 'package name'

And also make sure you have included the statement require 'java'.

[ruby]
require 'java'
java_package 'com.vkslabs.example.ruby'
class MyClass
def initialize()
end
def myfunction(msg)
puts msg;
end
end
MyClass.new.myfunction('Hello JRuby!');
[/ruby]

Now once you convert the code using jrubyc you will find that the package structure is automatically created.

Similarly you can declare signatures for java methods using
java_signature 'put method signature here'

[ruby]
require 'java'
java_package 'com.vkslabs.example.ruby'
class MyClass
def initialize()
end
java_signature 'void myJavaMethod(String msg)'
def myfunction(msg)
puts msg;
end
end
MyClass.new.myfunction('Hello JRuby!');
[/ruby]

Now you can call the new method signature from your java code by creating object for MyClass

[java]
import com.vkslabs.example.ruby;
class Test
{
public static void main(String[] a) {
MyClass obj = new MyClass();
obj.myJavaMethod("Hello Jruby from Java");
}
}
[/java]


Make sure you include jruby.jar while compiling Test.java

Compiling java code

Converting JRuby to Java

Now that we know how to write a basic JRuby code lets see how to convert the JRuby code in to Java code. JRuby comes with JRubyc a cross compiler that does the magic!

Convert to Java code
Compile JRuby in to Java Source file

Convert JRuby Java class files
Compile JRuby in to Java class file

Okay great! now you have Java source class for your JRuby class but what if you want to declare package and method signature for the generated java class?

Introduction to JRuby

JRuby
JRuby is an implementation of Ruby which runs on top of Java Virtual Machine. JRuby code can be embedded in Java code and your JRuby code can interact with Java Objects and has access to every Java API.

Why to use JRuby?
Ruby being a scripting language is dynamic and much faster than Java. There are lot of things that can be easily accomplished in Ruby for example I found that parsing & scraping HTML is much easier and faster in Ruby using HPricot. As a Java programmer and you can write part of your application in JRuby and then either embed this in your Java code or use JRubyC compiler to convert your JRuby code in to Java.

Your first JRuby program

[ruby]
require 'rubygems'
class MyClass
def initialize()
end
def myfunction(msg)
puts msg;
end
end
MyClass.new.myfunction('Hello JRuby!');
[/ruby]

Executing the above code can be done using the following command
jruby

Monday, February 11, 2013

Handling Home Key in Android

I recently came across "Todler lock" application which had lot of interesting features. One of the most intriguing feature was handling  of Home key event.

1.Create a Activity Alias

We use Activity alias in order to programmatically enable or disable home screen launcher. An alias of an activity acts as an independent entity and intent filters that are not in the original activity can be added.

Declare your activity and declare an alias for your activity with the following intent filters


[xml]
<activity
android:name="com.vkslabs.example.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity-alias
android:name=".MainActivityAlias"
android:targetActivity="com.vkslabs.example.MainActivity"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
</intent-filter>
</activity-alias>
[/xml]

2. Enabling and disabling component using setComponentEnabledSetting
[java]
PackageManager manager = getPackageManager();
ComponentName compName = new ComponentName("com.vkslabs.example", "com.vkslabs.example.MainActivityAlias");
//ComponentName compName = new ComponentName("<Package Name>", "<Package Name.Alias Name>");

manager.setComponentEnabledSetting(compName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
[/java]
You can use COMPONENT_ENABLED_STATE_DISABLED for disabling the alias.

Thursday, January 31, 2013

Saving bitmap buffer as bitmap image

Eclipse Memory Analyzer doesn’t provide option to view a bitmap buffer directly. Try the following steps that will allow you to save the bitmap buffer as a bitmap image that you can view using any image viewer.

1. Select the bitmap buffer that you would like to Save


domtree



2. Save the buffer to your computer


Select the Location in your computer where you would like to save the buffer file. You can give any name and extension
savebuffer



3. Enable inspector view in Eclipse


Once you have saved the bitmap buffer you will have to convert the bitmap buffer to bitmap for which you will need to know height and width of the bitmap.

You can use the inspector window to view the height and width of the bitmap. If inspector window is not available you can enable it using the following option.

inspectorview


showview



4. Find the dimension of the bitmap


imagedim



5. Convert the bitmap buffer in to bitmap image


Once you have the bitmap buffer, bitmap dimension you can execute the attached java program with the following command line option.

[code]
Java MATBitmap.java &lt;input.bmp&gt; &lt;output.bmp&gt; &lt;bitmap width&gt; &lt;bitmap height&gt;
[/code]

Example

[code]
java MATBitmap debug.bmp converted.bmp 800 800
[/code]

Bingo! now you can open the converted.bmp in any image editor.

Download MATBitmap.java

Fixing Bitmap memory leaks in Android

The biggest hurdle for Android developers is memory leaks. Especially if you are using a lot of images you tend to see memory leaks more often. Android internally handles the images as Bitmaps so even if the images used are optimized or of low quality it takes the same amount of memory. For example an image of size 800X600 takes 4 bytes per pixel which means the size will be 1.8 MB (4*800*600 = 1920000 bytes) whereas the same image will be 200 KB – 600 KB based on the quality of the image.
If you are facing frequent Out Of Memory errors then the first handy tool will be DDMS and Memory Analyzer tool (MAT).

Whenever you take memory dump you could see that there are bitmaps held in heap but most of the time you will not be able to find which image exactly the bitmap instance holds. MAT doesn’t provide an easy way to view the bitmap image held in a bitmap objects.
You can do the following steps to view the image in the bitmap buffer.