Wednesday 10 September 2014

Fetch all the accounts with the account name in Android Device

   
 1.Get an instance of AccountManager using get(activity).

public void GetGmailUsername(Activity activity) {

        AccountManager accmanager = AccountManager.get(activity);
        Account[] accounts = accmanager .getAccountsByType("com.google");
        List<String> Usernamelists = new LinkedList<String>();

        for (Account account : accounts) {
           
            Usernamelists .add(account.name);

        }

        System.out.println("Usernamelists --"+Usernamelists );
             
    }

Check external storage available in device,readable and writeable in android

1.It returns true if the external storage is available else false otherwise.


/**
     * Check if external storage available
     * @return
     */
   
    public static boolean isExternalStorageAvailable() {
          String extStorageState = Environment.getExternalStorageState();
          if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
           return true;
          }
          return false;
         }   


2.It returns true if the external storage is read only else false otherwise

/**
     * Check if external storage is read only
     * @return
     */
    public static boolean isExternalStorageReadOnly() {
          String extStorageState = Environment.getExternalStorageState();
          if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
           return true;
          }
          return false;
         }  

3.It returns true if the external storage is writable else false otherwise

/**
       * @return True if the external storage is writable.
       * False otherwise.
       */
      public static boolean isExternalStoragewriteonly() {
        String state = Environment.getExternalStorageState();
        // Check if writable
        if (Environment.MEDIA_MOUNTED.equals(state)) {
          return true;
        }
        return false;
       
      }

Encryption and decryption in android example code

1.Create a Class AESHelper
Use these method with following parameters

public class AESHelper {

public static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
public static final String UTF8 = "UTF-8";
public final static String AES = "AES";

public static final String PKCS5 = "AES/CBC/PKCS5Padding";


    /**
     * --Function for AES Encryption with 32 bytes key---
     * @param key
     * @param str
     * @return
     */
   
    public static String AES_Encode(String key, String str){
        byte[] textBytes;
        String encodeStr = null;
        Cipher cipher = null;
        try {
            textBytes = str.getBytes(UTF8);
            AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
            SecretKeySpec newKey = new SecretKeySpec(key.getBytes(UTF8),AES);
            cipher = Cipher.getInstance(PKCS5);
            cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
            encodeStr = Base64.encodeToString(cipher.doFinal(textBytes), 0);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }

   
    /**
     * --Function for AES Decryption with 32 bytes key---
     * @param key
     * @param str
     * @return
     */
    public static String AES_Decode(String key, String str){
        byte[] textBytes = Base64.decode(str, 0);
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey;
        String decodeStr = null;
        try {
            newKey = new SecretKeySpec(key.getBytes(UTF8), AES);
            Cipher cipher = Cipher.getInstance(PKCS5);
            cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
            decodeStr = new String(cipher.doFinal(textBytes), UTF8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return decodeStr;
    }
   
}


2.Now use these in your MainActivity

public class MainActivity extends Activity {

               public static final char[] KEY_ENCRYPTION = { '1', '2', '3', '4', '5','6','d','b','h','s','h','e','q','l','e','1','8', 's', 'o', 'e', 'n','p','d','s','p','d','s','n','n','p','e','1' };

String strtextsample = "Sample";
String strencrypted;
String strdecrypted;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
            String strKeyEncryption = String.valueOf(KEY_ENCRYPTION);
            strencrypted= AESHelper.AES_Encode(strKeyEncryption, strtextsample );
            System.out.println("Encrypted text--"+strencrypted);

             strdecrypted= AESHelper.AES_Decode(strKeyEncryption, strencrypted);
            System.out.println("Decrypted text--"+strdecrypted);





           }

}

Now, are able to encrypt and decrypt a string in a android application.