Last active
November 24, 2016 09:15
-
-
Save Antarix/25595d867f729910102e to your computer and use it in GitHub Desktop.
Commonly used functions are added in this class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigDecimal; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import android.content.Context; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.content.pm.PackageManager.NameNotFoundException; | |
import android.content.pm.Signature; | |
import android.net.ConnectivityManager; | |
import android.util.Base64; | |
import android.widget.EditText; | |
public class Utility { | |
/** | |
* | |
* @param edit | |
* : EditText to validate | |
* @param validateEmail | |
* : if true validate email also | |
* @return true/false | |
*/ | |
public static boolean notBlank(EditText edit, boolean validateEmail) { | |
if (edit.getText().length() > 0) { | |
if (validateEmail) { | |
if (!Utility.isValidEmail(edit.getText().toString())) { | |
edit.setError("Not valid email!"); | |
return false; | |
} | |
} | |
edit.setError(null); | |
return true; | |
} else { | |
edit.setError("Required " + edit.getHint()); | |
return false; | |
} | |
} | |
/** | |
* | |
* @param target | |
* : send email to validate | |
* @return true/false | |
*/ | |
public static boolean isValidEmail(CharSequence target) { | |
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches(); | |
} | |
/** | |
* | |
* @param context | |
* @return true/false Check Internet connection available | |
*/ | |
public static boolean isOnline(Context context) { | |
ConnectivityManager cm = (ConnectivityManager) context | |
.getSystemService(Context.CONNECTIVITY_SERVICE); | |
return (cm == null || cm.getActiveNetworkInfo() == null) ? false : cm | |
.getActiveNetworkInfo().isConnectedOrConnecting(); | |
} | |
public static void getHashKey(Context context) { | |
try { | |
PackageInfo info = context.getPackageManager().getPackageInfo( | |
"com.something.yourpackage", PackageManager.GET_SIGNATURES); | |
for (Signature signature : info.signatures) { | |
MessageDigest md = MessageDigest.getInstance("SHA"); | |
md.update(signature.toByteArray()); | |
Logcat.e("KeyHash:", | |
Base64.encodeToString(md.digest(), Base64.DEFAULT)); | |
} | |
} catch (NameNotFoundException e) { | |
} catch (NoSuchAlgorithmException e) { | |
} | |
} | |
/** | |
* Round to certain number of decimals | |
* | |
* @param d | |
* @param decimalPlace | |
* @return | |
*/ | |
public static float round(float d, int decimalPlace) { | |
BigDecimal bd = new BigDecimal(Float.toString(d)); | |
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP); | |
return bd.floatValue(); | |
} | |
/** | |
* Generate random numbersNeeded from max number | |
* @param numbersNeeded | |
* @param max | |
*/ | |
public static List<Integer> getRandomNumbers(int numbersNeeded,int max){ | |
Random rng = new Random(); // Ideally just create one instance globally | |
// Note: use LinkedHashSet to maintain insertion order | |
Set<Integer> generated = new LinkedHashSet<Integer>(); | |
while (generated.size() < numbersNeeded) | |
{ | |
Integer next = rng.nextInt(max) + 1; | |
// As we're adding to a set, this will automatically do a containment check | |
generated.add(next); | |
} | |
Logcat.e("Random numbers", generated.toString()); | |
List<Integer> randomNumbers = new ArrayList<Integer>(); | |
Iterator<Integer> numberIterator = generated.iterator(); | |
while (numberIterator.hasNext()) { | |
randomNumbers.add(numberIterator.next()); | |
} | |
Collections.sort(randomNumbers); | |
return randomNumbers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment