Thursday, 29 September 2011

Block Home Key

I searched a lot to block Home key but no success and finally I got it and here it is for you guys.

public void onAttachedToWindow() {



Note: You should never block the Home key. Please see this post before doing so.

Monday, 12 September 2011

Auto App start on Boot Completion

You can start your application when the device starts again thru BroadcastReceiver:

public class PhoneStateReceiver extends BroadcastReceiver{
    public void onReceive(final Context context, Intent intent) {
            Intent launch = new Intent(context, ActivityToLaunch.class);

In your manifest add this:

<receiver android:name=".receiver.PhoneStateReceiver">
        <action android:name="android.intent.action.BOOT_COMPLETED" />

Add permission:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Monday, 5 September 2011

Rating your code from A to F

Assuming that a piece of code is functional and that it complies with its requirements, that piece of code can be rated from A to F based in two parameters, the simplicity and the extensibility.


The simplicity is a characteristic that tells us how easy is to understand and read the code. In order to determine how simple a code is, the following can be taken into consideration.
  • Good identifiers for Class names, variables, methods…
  • Straight forward logic.
  • Crisp and minimal abstractions.
  • Low representational gap. The representational gap is how different is the domain logic of an application from its design, having a low representational gap means that the design has very few differences with the domain logic it represents.
The simplicity is classified in “Very simple”, “Simple” or “Not simple”
Very simple: Code that doesn’t require additional documentation to be understood, any developer can pick up the code and do changes without further explanation.
Simple: Code that requires some additional documentation to be understood, only developers with some background on the design and in the business domain knowledge will be able to do changes.
Not simple: Code that requires lots of additional documentation to be understood, only senior developers with a deep knowledge of the application will be able to do changes.



The extensibility is a characteristic that tells us how easy will be to add new functionality on top of the current code, don’t confuse extensibility with over engineering, which is a bad practice. In order to determine how extensible a code is, the following can be taken into consideration.
  • Atomicity, use of DRY.
  • Testability. Code that includes unit tests.
  • Use of design patterns.
  • Low coupling
The extensibility is classified in “Very extensible”, “Extensible” or “Not extensible”
Very extensible: To add new functionality on the top of the code is really easy, no refactoring would be necessary.
Extensible: To add new functionality on the top of the existing code is relatively easy, minor refactoring would be necessary.
Not extensible: To add new functionality on the top of the existing code is difficult, complex refactoring, even redesign would be necessary.

Code Rates


A- Excellent code.

A is the best rate for code, it can’t be more simple and it can’t be more extensible, the problem with A codes is that is almost impossible to achieve, usually there is a trade off between extensibility and readability, that’s why I always recommend to produce B codes.


B- Good code

B code is good code, B code will ensure that the maintenance of the application will be much easier. This code is the one I recommend to aim for.


C- Average code

C code is what some developers consider good enough, if we are under a tight deadline or high pressure we might have to aim for this rate of code. Maintenance of the application is harder than with B code but still is possible.


D- Dangerous code

With D code still is possible to maintain the application, but it takes a lot of time to perform any change and the risk to enter new bugs doing so is high, some teams don’t want to refactor D codes because they don’t want to change something that is working, that’s an error, D codes easily become E codes after a few changes and then it will become unmaintainable.


E- Bad code

E code is bad code, the application is unmaintainable, urgent changes are necessary, still there is time for reaction to change the code and improve it.


F- Start over again

Not only the application is unmaintainable, the code should be scrapped and created again from scratch

Thursday, 1 September 2011

Android Intents

//show webapp:

Uri uri = Uri.parse("");
Intent intent  = new Intent(Intent.ACTION_VIEW,uri);

//show maps:
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent intent = new Intent(Intent.Action_VIEW,uri);

//show ways
Uri uri = Uri.parse("");
Intent intent = new Intent(Intent.ACTION_VIEW,URI);

//call dial program
Uri uri = Uri.parse("tel:xxxxxx");
Intent intent = new Intent(Intent.ACTION_DIAL, uri); 

Uri uri = Uri.parse("tel.xxxxxx");
Intent intent =new Intent(Intent.ACTION_CALL,uri);
//don't forget add this config:<uses-permission id="android.permission.CALL_PHONE" />

//send sms/mms
//call sender program
Intent intent = new Intent(Intent.ACTION_VIEW);  
intent.putExtra("sms_body", "The SMS text");  

//send sms
Uri uri = Uri.parse("smsto:0800000123");  
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);  
intent.putExtra("sms_body", "The SMS text");  

//send mms
Uri uri = Uri.parse("content://media/external/images/media/23");  
Intent intent = new Intent(Intent.ACTION_SEND);  
intent.putExtra("sms_body", "some text");  
intent.putExtra(Intent.EXTRA_STREAM, uri);  

//send email

Uri uri = Uri.parse("");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);

Intent intent = new Intent(Intent.ACTION_SEND);  
intent.putExtra(Intent.EXTRA_EMAIL, "");  
intent.putExtra(Intent.EXTRA_TEXT, "The email body text");  
startActivity(Intent.createChooser(intent, "Choose Email Client")); 

Intent intent=new Intent(Intent.ACTION_SEND);    
String[] tos={""};    
String[] ccs={""};    
intent.putExtra(Intent.EXTRA_EMAIL, tos);    
intent.putExtra(Intent.EXTRA_CC, ccs);    
intent.putExtra(Intent.EXTRA_TEXT, "The email body text");    
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");    
startActivity(Intent.createChooser(intent, "Choose Email Client"));  

//add extra
Intent intent = new Intent(Intent.ACTION_SEND);  
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");  
intent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");  
startActivity(Intent.createChooser(intent, "Choose Email Client"));

//play media
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
intent.setDataAndType(uri, "audio/mp3");

Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");  
Intent intent = new Intent(Intent.ACTION_VIEW, uri);  

Uri uri = Uri.fromParts("package", strPackageName, null);  
Intent intent = new Intent(Intent.ACTION_DELETE, uri);  

//uninstall apk
Uri uninstallUri = Uri.fromParts("package", "xxx", null);
Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUri);

//install apk
Uri installUri = Uri.fromParts("package", "xxx", null);
Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

//play audio
Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, playUri);

//send extra
Intent intent = new Intent(Intent.ACTION_SEND); 
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); 
intent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/eoe.mp3"); 
startActivity(Intent.createChooser(intent, "Choose Email Client"));

Uri uri = Uri.parse("market://search?q=pname:pkg_name"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
//where pkg_name is the full package path for an application 

//show program detail page
Uri uri = Uri.parse("market://details?id=app_id"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
//where app_id is the application ID, find the ID 
//by clicking on your application on Market home 
//page, and notice the ID from the address bar

//search google
Intent intent = new Intent();