Skip to content

Instantly share code, notes, and snippets.

View erfanegtfi's full-sized avatar

Erfan Eghterafi erfanegtfi

  • Iran
View GitHub Profile
@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 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";
}
}
public class MyApp extends Application {
public static boolean isAppInBackground = false;
private Activity mCurrentActivity;
public void onCreate() {
super.onCreate();
setScreenOrientation();
this.mMyApp = (MyApp) getApplicationContext();
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) {
@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;
@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:28
base64 Encode
public String encode(String s, String key) {
if (s != null)
return base64Encode(xorWithKey(s.getBytes(), key.getBytes()));
else
return null;
}
public String decode(String s, String key) {
if (s != null)
return new String(xorWithKey(base64Decode(s), key.getBytes()));
public static final String md5(String s) {
String MD5 = CommonUtils.MD5_INSTANCE;
try {
MessageDigest digest = MessageDigest.getInstance(io.fabric.sdk.android.services.common.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) {
@erfanegtfi
erfanegtfi / timeConvert.java
Created April 13, 2018 12:35
convert Minute To Hour
public static void convertMinuteToHour(int totalMinute, TextView textView) {
if (totalMinute >= 60) {
int hours = (int) Math.floor((double) (totalMinute / 60));
totalMinute %= 60;
if (totalMinute == 0) {
textView.setText("طول بازدید: " + persianNumbers(String.valueOf(hours)) + " ساعت ");
return;
} else {
textView.setText("طول بازدید: " + persianNumbers(String.valueOf(hours)) + " ساعت و " + persianNumbers(String.valueOf(totalMinute)) + " دقیقه ");
return;