package com.mikepenz.unsplash.other;
import android.animation.ArgbEvaluator;
import android.animation.FloatEvaluator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.animation.PathInterpolator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
@SuppressWarnings("UnnecessaryLocalVariable")
public class Utils {
public final static int COLOR_ANIMATION_DURATION = 1000;
public final static int DEFAULT_DELAY = 0;
public static void animateViewColor(View v, int startColor, int endColor) {
ObjectAnimator animator = ObjectAnimator.ofObject(v, "backgroundColor",
new ArgbEvaluator(), startColor, endColor);
if (Build.VERSION.SDK_INT >= 21) {
animator.setInterpolator(new PathInterpolator(0.4f, 0f, 1f, 1f));
}
animator.setDuration(COLOR_ANIMATION_DURATION);
animator.start();
}
public static void animateViewElevation(View v, float start, float end) {
if (Build.VERSION.SDK_INT >= 21) {
ObjectAnimator animator = ObjectAnimator.ofObject(v, "elevation", new FloatEvaluator(), start, end);
animator.setDuration(500);
animator.start();
}
}
public static void configuredHideYView(View v) {
v.setScaleY(0);
v.setPivotY(0);
}
public static ViewPropertyAnimator hideViewByScaleXY(View v) {
return hideViewByScale(v, DEFAULT_DELAY, 0, 0);
}
public static ViewPropertyAnimator hideViewByScaleY(View v) {
return hideViewByScale(v, DEFAULT_DELAY, 1, 0);
}
public static ViewPropertyAnimator hideViewByScalyInX(View v) {
return hideViewByScale(v, DEFAULT_DELAY, 0, 1);
}
private static ViewPropertyAnimator hideViewByScale(View v, int delay, int x, int y) {
ViewPropertyAnimator propertyAnimator = v.animate().setStartDelay(delay)
.scaleX(x).scaleY(y);
return propertyAnimator;
}
public static ViewPropertyAnimator showViewByScale(View v) {
ViewPropertyAnimator propertyAnimator = v.animate().setStartDelay(DEFAULT_DELAY)
.scaleX(1).scaleY(1);
return propertyAnimator;
}
/**
* calculate the optimal width for the image
*
* @param screenWidth
* @return
*/
public static int optimalImageWidth(int screenWidth) {
int preOptimalWidth = screenWidth / 2;
if (preOptimalWidth >= 720) {
return 720;
} else if (preOptimalWidth >= 540) {
return 540;
} else if (preOptimalWidth >= 360) {
return 360;
} else {
return 360;
}
}
/**
* @param context
* @param px
* @return
*/
public static float dpFromPx(Context context, float px) {
return px / context.getResources().getDisplayMetrics().density;
}
/**
* @param context
* @param dp
* @return
*/
public static float pxFromDp(Context context, float dp) {
return dp * context.getResources().getDisplayMetrics().density;
}
/**
* http://stackoverflow.com/questions/10854211/android-store-inputstream-in-file
*
* @param in
* @param file
*/
public static void copyInputStreamToFile(InputStream in, File file) {
try {
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* http://stackoverflow.com/questions/3934331/android-how-to-encrypt-a-string
*
* @param s
* @return
*/
public static String md5(String s) {
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++)
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
/**
* http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file
*
* @param file
* @return
* @throws IOException
*/
public static String readFile(File file)
throws IOException {
return new Scanner(file, "UTF-8").useDelimiter("\\A").next();
}
/**
* http://developer.android.com/training/basics/data-storage/files.html
*
* @param albumName
* @return
*/
public static File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.i("wall-splash", "Directory not created");
}
return file;
}
/**
* http://developer.android.com/training/basics/data-storage/files.html
*
* @return
*/
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
}