Skip to content

Instantly share code, notes, and snippets.

@SeanZoR
SeanZoR / getVersionCode
Created February 22, 2014 11:02
Android - Get application version code
public static int getVersionCode(Context ctx) {
int versionCode = 0;
try {
PackageInfo pInfo = ctx.getPackageManager().getPackageInfo(
ctx.getPackageName(), 0);
versionCode = pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
@SeanZoR
SeanZoR / getDeviceUniqueId
Created February 22, 2014 11:03
Android - Get device unique Id ("UDID")
public static String getDeviceUniqueId(Context ctx) {
return Settings.Secure.getString(ctx.getContentResolver(),
Settings.Secure.ANDROID_ID);
}
@SeanZoR
SeanZoR / HideShowKeyboard
Created February 22, 2014 11:04
Android - Keyboard visibility actions
public static void hideKeyboards(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService
(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
public static void showKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context
.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, 0);
@SeanZoR
SeanZoR / getLanguageCode
Last active July 15, 2019 18:13
Android - Fix some language code to fit ISO-639-1
/**
* This method helps getting the right langauage ISO code, which suppose to be
* according to ISO-639-1, BUT, on some devices it still returns the deprecated ISO-639.
* <BR>
* Languages codes that are translated in this method:
* <ul>
* <li>Hebrew: IW -> HE
* <li>Indonesian: IN -> ID
* <li>Yiddish: JI -> YI
* </ul>
@SeanZoR
SeanZoR / UnbindingHelper
Created February 22, 2014 11:18
Android - Remove Binding helper - When you want to finish Activity w/o leaks
public static class UnbindingHelper {
/**
* Removes the reference to the activity from every view in a view hierarchy (listeners,
* images etc.).
* This method should be called in the onDestroy() method of each activity
*
* @param viewID normally the id of the root layout
* <p/>
* see http://code.google.com/p/android/issues/detail?id=8488
@SeanZoR
SeanZoR / activateStrictModeFull
Created February 22, 2014 11:26
Android - Monitor using strict mode
public static void activateStrictModeFull(){
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build());
}
@SeanZoR
SeanZoR / BasicStringSecurity
Last active August 29, 2015 13:56
Android - Manage your shared preference with a strings resource file (and secure strings)
import android.content.Context;
import android.provider.Settings;
import android.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
@SeanZoR
SeanZoR / Adding a dependency to library
Last active August 29, 2015 13:57
Gradle - download a library
dependencies {
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.0'
}
@SeanZoR
SeanZoR / ProGuard_ignore_libraries
Last active January 7, 2021 17:15
ProGuard - simple ignore for 3rd party libraries
# Ignoring all of the external "org" libraries
# (for example org.apache & org.jackson)
-keep class org.** { *; }
-dontwarn org.**
# Have more? Simply add them like the one above,
# and change to the desired package name.
@SeanZoR
SeanZoR / build.gradle
Last active November 9, 2020 01:27
Automatic versioning and increment using Git tags and Gradle
android {
defaultConfig {
...
// Fetch the version according to git latest tag and "how far are we from last tag"
def longVersionName = "git -C ${rootDir} describe --tags --long".execute().text.trim()
def (fullVersionTag, versionBuild, gitSha) = longVersionName.tokenize('-')
def(versionMajor, versionMinor, versionPatch) = fullVersionTag.tokenize('.')
// Set the version name
versionName "$versionMajor.$versionMinor.$versionPatch($versionBuild)"