Sunday, March 31, 2013

Android Common Utilities

Hi guys,

In android projects there are many programs/commands we use frequently. Most of the time we implement these programs from scratch or take these program files from existing projects and refactor to adapt into new projects, This process take time. So I decide to put these programs together as much I can. So that developers can use them and reduce their development time. These days I am looking at every possibility to decrease the development time. This is one of my steps.

Pre-req: You should know how to create android project. Then download AndroidCommonUtils.jar jar and put into your project.

In this package we have these classes

    1. ActivityDTO
            Use Map (data structure) in this class, To pass data from activities to other components, Because I have seen under the development cycle, Most of the time we need to refactor functions and need to add or remove parameters, and some time functions took large number of parameters. So adding removing function parameters takes time. So this is my suggestion you can do following thing.
                ActivityDTO activityDTO = new ActivityDTO();
       activityDTO.getMap().put("activity", this);
       activityDTO.getMap().put("progressBarThread", progressBarThread);
       activityDTO.getMap().put("appDebuger",appDebuger);
       activityDTO.getMap().put("userConfigDb",userConfigDb);
     
LoginTask loginTask = new LoginTask(activityDTO);
loginTask.execute(userDTO);
Above example demonstrates how I am  sending different objects into  LoginTask constructor.And inside constructor I can get back these objects.

public LoginTask(ActivityDTO activityDTO) {
activity = (LoginActivity)activityDTO.getMap().get("activity");
progressBarThread =(ProgressThreadUtils)activityDTO.getMap().get("progressBarThread");
appDebuger = (AppDebuger)activityDTO.getMap().get("appDebuger");
userConfigDb = (UserConfigDb)activityDTO.getMap().get("userConfigDb");
}

     2.  AppDebuger
         This utility has methods to print logs on screen for purpose of debugung. I am using Singleton Pattern, Developer can on/off debuging from any where.
          AppDebuger appDebuger = AppDebuger.init();
          appDebuger.setOn(true);
          appDebuger.printInfo("Printhing from blog" , "1234");

    3. CommonMessagesUtils 
        This class has methods to show Toast messages and Dialog Messages.

    4. CommonValidationsUtils
        This class has methods to validate email, strings, password, convert stream into string, check strings are alpha numeric, decimal adjustment. ect

     5. DAOTemplate
           This class has method to insert, delete, remove, update, open close db connections almost every method that will help you to play with you database.
To use do like this
    
    6.  PrivateSharedPrefeqencesForStringsUtils
        This class has methods to save,remove and get presistant strings from android SharedPrefeqences.

    7.  ProgressThreadUtils
         This is a thread utility to manage progress dialog on you activities, State of thread is mentioned in ProgressThreadState enum. Inside your activity use this thread like this


    8.   SDCardUtils

        This class has all methods to save, retrieve files from sd card, you can download resources from internet into sd card and retrieve these as file or bitmap


Download source.