Monday, November 28, 2011

How to encrypt a serializable class in Android

public boolean save(YourClass pref){
boolean isSuccess = false;
try {
final File persistedFile = new File("/sdcard/sr.dat");
if(persistedFile.exists())
persistedFile.delete();
OutputStream file = new FileOutputStream(persistedFile);
BufferedOutputStream buffer = new BufferedOutputStream(file);
Cipher desCipher = getCipher(Cipher.ENCRYPT_MODE);
CipherOutputStream cos = new CipherOutputStream(buffer, desCipher);
ObjectOutputStream oos = new ObjectOutputStream(cos);
oos.writeObject(pref);
oos.flush();
oos.close();
} catch (IOException ex) {
Log.d(TAG, ex.getMessage());
isSuccess = false;
} catch (InvalidKeyException e) {
Log.d(TAG, e.getMessage());
isSuccess = false;
} catch (InvalidKeySpecException e) {
Log.d(TAG, e.getMessage());
isSuccess = false;
} catch (NoSuchAlgorithmException e) {
Log.d(TAG, e.getMessage());
isSuccess = false;
} catch (NoSuchPaddingException e) {
Log.d(TAG, e.getMessage());
isSuccess = false;
}
return isSuccess;
}
private Cipher getCipher(int opmode) throws InvalidKeyException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException {
byte key[] = "Aruna Tennakoon".getBytes();
DESKeySpec desKeySpec = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

// Create Cipher
Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(opmode, secretKey);
return desCipher;
}
public Preference load(){
Preference repo = null;
try {

final File persistedFile = File("/sdcard/sr.dat");
if(!persistedFile.exists()) {
// sr is not created yet. Create one and return
return;
}
Cipher desCipher = getCipher(Cipher.DECRYPT_MODE);
FileInputStream fis = new FileInputStream(persistedFile.getAbsolutePath());
BufferedInputStream bis = new BufferedInputStream(fis);
CipherInputStream cis = new CipherInputStream(bis, desCipher);
ObjectInputStream ois = new ObjectInputStream(cis);
repo = (YourClass) ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
Log.d(TAG, e.getMessage());
} catch (StreamCorruptedException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
} catch (InvalidKeyException e) {
Log.d(TAG, e.getMessage());
} catch (InvalidKeySpecException e) {
Log.d(TAG, e.getMessage());
} catch (NoSuchAlgorithmException e) {
Log.d(TAG, e.getMessage());
} catch (NoSuchPaddingException e) {
Log.d(TAG, e.getMessage());
} catch (ClassNotFoundException e) {
Log.d(TAG, e.printStackTrace());
}

return repo;
}

Wednesday, November 23, 2011

how to get the ip address of android phone?

private String getLocalIpAddress() {
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
}
}
} catch (SocketException ex) {
Log.e("ServerActivity", ex.toString());
}
return null;
}

Wednesday, November 16, 2011

'Conversion to Dalvik format failed with error 1' and 'java.lang.IllegalArgumentException: already added: '

Recently we moved to Android SDK tools Rev 15 , In my opinion it has may problems comparing to previous versions of the SDK and should stick with the previous version if you are developing a huge applications and have library projects referenced.

Today, I got this error and few hours of Googling found the solution,

Remove the source folder link in Project properties->Java Build Path->Source

Clean and add the projects back, You will see that it will add the projects as jar files under "Library Projects" not as project_src anymore.

Monday, November 7, 2011

How to do a simple visible animation in Android

public void showConfigurationBar() {
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(500);
set.addAnimation(animation);
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(500);
set.addAnimation(animation);
RelativeLayout configBar = findViewById(R.id.baridhere);
configBar.setVisibility(View.VISIBLE);
configBar.startAnimation(set);
}

How to generate a MD5 Hash in Android

public static String getMd5Hash(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger number = new BigInteger(1, messageDigest);
String md5 = number.toString(16);
while (md5.length() < 32)
md5 = "0" + md5;
return md5;
} catch (NoSuchAlgorithmException e) {
Log.e("MD5", e.getLocalizedMessage());
return null;
}
}

Tuesday, November 1, 2011

Eclipse Layout Editor Graphical Layout is not Showing or missing ?


Specify the Android Layout Editor as the default for Android XML files under Preferences -> General- >Editors->File Associations.


1. If Android layout editor is missing Add Android Layout Editor and make it default.
2. Close eclipse and restart. Otherwise this will not work.