Skip to content

Instantly share code, notes, and snippets.

View erfanegtfi's full-sized avatar

Erfan Eghterafi erfanegtfi

  • Iran
View GitHub Profile
package ir.gushtomorghebaradaran.Utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.google.gson.Gson;
public class Prefs<T> {
package com.besat.Utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
@erfanegtfi
erfanegtfi / encode_decode.java
Created April 13, 2018 12:23
convert hexString To Byte Array
public static byte[] convertToByteArray(String hexString) {
int len = hexString.length();
byte[] data = new byte[(len / 2)];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));
}
return data;
}
@erfanegtfi
erfanegtfi / encode_decode.java
Created April 13, 2018 12:27
Secret Key Spec Cipher
import android.util.Base64;
import android.util.Log;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import pl.droidsonroids.gif.BuildConfig;
@erfanegtfi
erfanegtfi / encode_decode.java
Created April 13, 2018 12:23
convert To Hex String
public static String convertToHexString(byte[] data) {
StringBuilder buf = new StringBuilder();
for (byte b : data) {
int halfbyte = (b >>> 4) & 15;
int two_halfs = 0;
while (true) {
char c = (halfbyte < 0 || halfbyte > 9) ? (char) ((halfbyte - 10) + 97) : (char) (halfbyte + 48);
buf.append(c);
halfbyte = b & 15;
int two_halfs2 = two_halfs + 1;
public static final String md5(String s) {
String MD5 = CommonUtils.MD5_INSTANCE;
try {
MessageDigest digest = MessageDigest.getInstance(CommonUtils.MD5_INSTANCE);
digest.update(s.getBytes());
byte[] messageDigest = digest.digest();
StringBuilder hexString = new StringBuilder();
for (byte aMessageDigest : messageDigest) {
String h = Integer.toHexString(aMessageDigest & 255);
while (h.length() < 2) {
public class MyApp extends Application {
public static boolean isAppInBackground = false;
private Activity mCurrentActivity;
public void onCreate() {
super.onCreate();
setScreenOrientation();
this.mMyApp = (MyApp) getApplicationContext();
@erfanegtfi
erfanegtfi / AndroidUtils.java
Created April 13, 2018 11:30
getAppVersionName
public static String getAppVersionName() {
try {
return MyApplication.getContext().getPackageManager().getPackageInfo(MyApplication.getContext().getPackageName(), 0).versionName;
} catch (Exception ex) {
ex.printStackTrace();
return "1";
}
}
@erfanegtfi
erfanegtfi / AndroidUtils.java
Created April 13, 2018 11:18
getAppVersionCode
public static Integer getAppVersionCode() {
try {
return Integer.valueOf(MyApplication.getContext().getPackageManager().getPackageInfo(MyApplication.getContext().getPackageName(), 0).versionCode);
} catch (Exception ex) {
ex.printStackTrace();
return Integer.valueOf(1);
}
}
@erfanegtfi
erfanegtfi / AndroidUtils.java
Created April 13, 2018 12:46
is App In Background
public static boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;