Wednesday, September 9, 2015

Android Code Coverage (Instrumentation)

1. Add Instrumentation File

2. Add Entry to AndroidManifest.xml

<instrumentation android:name=".EmmaInstrumentation" android:targetPackage=""></instrumentation>

This tag should be added above to the activity Tag.


3. Compile

  1. ant clean
  2. ant instrument

4. Install

  1. ant installi

5. Launch Instrumented Application

adb shell am instrument -e coverage true  -w <application_package>/.EmmaInstrumentation
Now execute the test scenarios and press back until you exit the app.

6. Generate HTML

  1. Make a new folder for test report generation say report.
  2. Open command prompt in this directory report.
  3. Copy coverage.em file from /bin to the current folder
  4. Pull the coverage.ec file from the device using command adb pull /mnt/sdcard/coverage.ec .
  5. Copy emma.jar from the location /tools/lib to the current location.
  6. To genarate the coverage report type the following commandjava -cp emma.jar emma report -r html -in coverage.ec -sp /src -in coverage.em
Note:
  1. Every time a new version of code is available, all the steps needs to be executed again, except for copying emma.jar from the sdk folder.
  2. for consecutive executions, first the old coverage.ec needs to be removed from /mnt/sdcard.

Monday, September 7, 2015

Pretty Simple Encryption/Decryption Tool

AES Tool(256 Bits) - Encrypt / Decrypt Text
    
Text to encrypt/decrypt:
Password:
Encrypted text:
Decrypted text:

Tuesday, August 20, 2013

Launch Facebook from your application

First of all you are required to check if Facebook exists in your mobile device or not. To do so create a method like below


@SuppressWarnings("unused")
private boolean isFacebookExists() {
    try{
        ApplicationInfo info = getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
        return true;
    } catch( PackageManager.NameNotFoundException e ){
        return false;
    }
}

== Once you know that Facebook exists in your device then launch it by following

String uri = "facebook://facebook.com/inbox";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);

In the other way you an directly take the intent if you must call the Facebook even if the app doesn't exists.

@SuppressWarnings("unused")
private Intent isFacebookExists() {
    Intent intent;
    String uri;
    try{
        ApplicationInfo info = getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
        uri = "facebook://facebook.com/inbox";
    } catch( PackageManager.NameNotFoundException e ){
        uri = "http://www.facebook.com";
    }
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    return intent;
}


Cheers !!!

Wednesday, July 31, 2013

Weather & Currency Services

Why don't you try these owesome services.

Weather

http://openweathermap.org/api

This service is amzazing for any of your wheather related application. You can get details of weather for the exact location (latitude & longitude) or city or by city code (if you know. its not zip)

Don't forget to get your app id for services for better service.

Currency

 http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv

Just put your first and second currency's code to the parametere and you have the currency exchange rates free from Yahoo.
I tried to change the format but unfortunately no documentation is available and you will always receive in csv format.

Friday, January 18, 2013

Get result from popup window + Javascript

Well, rather than I write. Easy with the example.


a.html

<html>
<a href="#"  onclick="window.open('b.html', '','width=220,height=265');">Get Value</a>
<script type="text/javascript">
function HandlePopupResult(result) {
alert('Result of popup is: ' + result);
}
</script>
</html>


b.html

<html>

Make an action
<a href="#" result="Allow" onclick="return CloseMySelf(this);">Allow</a>
<a href="#" result="Disallow" onclick="return CloseMySelf(this);">Don't Allow</a>
<script type="text/javascript">
function CloseMySelf(sender) {
    try {
        window.opener.HandlePopupResult(sender.getAttribute("result"));
    }
    catch (err) {}
    window.close();
    return true;
}
</script>
</html>



Cheers!!!

Thursday, January 17, 2013

WebView + Video Tag (HTML5) + Play click doesn't show video

New day with new challange

I am having device Samsung Galexy Y Android version 2.3. While running my program suddenly I was bit surprised. I had a video tag in my web page which was being shown in the WebView in my application but when I click on the play button, video play didn't start. I said whatttttttttttt is that :-o :-o
When I run this in the phone's browser it opens up a video player application with the URL whichever was mentioned in the video tag. I started investigating and soon found that the WebView of android 2.3 doesn't support all HTML5 tags and only few HTML5 tags are supported with limitations.
Since I had to make video stuff working so I started looking into. I found that while using the native browser when I click on play button on the video shown on web page some "play" log is coming in the LogCat (By native web browser). I thought if some how I become able to trap the play button click event with the video URL then I'll launch the player by myself instead of being dependent on the WebView. I did a trick to solve this problem.

First of all I injected a script interface in the web page like below.
protected void onCreate(Bundle savedInstanceState) {
   ...
   webView.addJavascriptInterface(new VideoURLRetriever(), "FORM_VIDEO_GETTER");
   ...
}
private class VideoURLRetriever {
        
    public void play(String path) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(path), "video/*");
        startActivity(intent);
    }
}

This will be called by javascript engine whenever I want. And now time to inject a specific javascript in the page when page loading is done like below.
private class MyWebViewClient extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {
    final String JS = "javascript: " + 
                      "var video = document.getElementsByTagName(\"video\")[0];"+
                      "if (video == undefined);" + 
                      "else video.addEventListener('play', playHandle,false);" + 
                      " function playHandle() {javascript:window.FORM_VIDEO_GETTER.play(video.currentSrc);video.pause();}" +
        Log.v("Injecting script for video play");
        view.loadUrl(JS);
    }
}

As above javascrpt describs, it first find's the first video element in the page, registers the play click handler as javascript function and which is captured by the java code in your java class like VideoURLRetriever.play(url). You can add more events and can handle it in your code like above.

Well, I know this is not the ethical way to achieve what I wanted but found this solution as the only way to have my video playing :-; . Also a demerit of using above code is that, each time any page is loaded, a javascript is injected in your page. But that's not important as you want your video to be playing :-)

Cheers!!!

Thursday, January 3, 2013

"Debug Certificate Expired"- Android



Today when I am working suddenly I found in eclipse that error is there with details showing as "Debug certificate expired". Well this is a normal scenario and need to to worry much.

In the case you are a windows based user then go the folder
For WindowsXP
   (OS Drive):\Documents and Settings\\.android
   Example C:\Documents and settings\vkumar\.android
For Windows 7
   (OS Drive):\Users\\.android
   Example - C:\Users\vkumar\.android

and delete the debug certificate.

Clean and build the project you were getting the error for and you are done.

Happy Coding Android !!!