2016-04-21 20:28:01 -03:00
|
|
|
package us.shandian.giga.util;
|
|
|
|
|
|
|
|
import android.content.ClipData;
|
2017-06-28 07:27:32 +02:00
|
|
|
import android.content.ClipboardManager;
|
2016-04-21 20:28:01 -03:00
|
|
|
import android.content.Context;
|
2017-06-28 07:27:32 +02:00
|
|
|
import android.support.annotation.ColorRes;
|
|
|
|
import android.support.annotation.DrawableRes;
|
2018-04-29 01:06:34 +02:00
|
|
|
import android.support.annotation.NonNull;
|
2017-06-28 07:27:32 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2016-04-21 20:28:01 -03:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2017-06-28 07:27:32 +02:00
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
|
2016-04-21 20:28:01 -03:00
|
|
|
import java.io.BufferedInputStream;
|
2018-05-18 18:23:32 +02:00
|
|
|
import java.io.BufferedOutputStream;
|
2016-04-21 20:28:01 -03:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
2017-06-28 07:27:32 +02:00
|
|
|
import java.io.FileOutputStream;
|
2016-04-21 20:28:01 -03:00
|
|
|
import java.io.IOException;
|
2018-04-29 01:06:34 +02:00
|
|
|
import java.io.ObjectInputStream;
|
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
import java.io.Serializable;
|
2016-04-21 20:28:01 -03:00
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
2017-06-28 07:27:32 +02:00
|
|
|
public class Utility {
|
|
|
|
|
|
|
|
public enum FileType {
|
|
|
|
VIDEO,
|
|
|
|
MUSIC,
|
|
|
|
UNKNOWN
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String formatBytes(long bytes) {
|
|
|
|
if (bytes < 1024) {
|
|
|
|
return String.format("%d B", bytes);
|
|
|
|
} else if (bytes < 1024 * 1024) {
|
|
|
|
return String.format("%.2f kB", (float) bytes / 1024);
|
|
|
|
} else if (bytes < 1024 * 1024 * 1024) {
|
|
|
|
return String.format("%.2f MB", (float) bytes / 1024 / 1024);
|
|
|
|
} else {
|
|
|
|
return String.format("%.2f GB", (float) bytes / 1024 / 1024 / 1024);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String formatSpeed(float speed) {
|
|
|
|
if (speed < 1024) {
|
|
|
|
return String.format("%.2f B/s", speed);
|
|
|
|
} else if (speed < 1024 * 1024) {
|
|
|
|
return String.format("%.2f kB/s", speed / 1024);
|
|
|
|
} else if (speed < 1024 * 1024 * 1024) {
|
|
|
|
return String.format("%.2f MB/s", speed / 1024 / 1024);
|
|
|
|
} else {
|
|
|
|
return String.format("%.2f GB/s", speed / 1024 / 1024 / 1024);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-29 01:06:34 +02:00
|
|
|
public static void writeToFile(@NonNull String fileName, @NonNull Serializable serializable) {
|
|
|
|
ObjectOutputStream objectOutputStream = null;
|
|
|
|
|
2017-06-28 07:27:32 +02:00
|
|
|
try {
|
2018-05-18 18:23:32 +02:00
|
|
|
objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
|
2018-04-29 01:06:34 +02:00
|
|
|
objectOutputStream.writeObject(serializable);
|
2017-06-28 07:27:32 +02:00
|
|
|
} catch (Exception e) {
|
2018-04-29 01:06:34 +02:00
|
|
|
//nothing to do
|
2018-05-18 18:23:32 +02:00
|
|
|
} finally {
|
|
|
|
if(objectOutputStream != null) {
|
|
|
|
try {
|
|
|
|
objectOutputStream.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
//nothing to do
|
|
|
|
}
|
2017-06-28 07:27:32 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-29 01:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <T> T readFromFile(String file) {
|
|
|
|
T object = null;
|
|
|
|
ObjectInputStream objectInputStream = null;
|
2017-06-28 07:27:32 +02:00
|
|
|
|
|
|
|
try {
|
2018-04-29 01:06:34 +02:00
|
|
|
objectInputStream = new ObjectInputStream(new FileInputStream(file));
|
|
|
|
object = (T) objectInputStream.readObject();
|
2017-06-28 07:27:32 +02:00
|
|
|
} catch (Exception e) {
|
2018-04-29 01:06:34 +02:00
|
|
|
//nothing to do
|
2017-06-28 07:27:32 +02:00
|
|
|
}
|
|
|
|
|
2018-04-29 01:06:34 +02:00
|
|
|
if(objectInputStream != null){
|
|
|
|
try {
|
|
|
|
objectInputStream .close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
//nothing to do
|
2017-06-28 07:27:32 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-29 01:06:34 +02:00
|
|
|
|
|
|
|
return object;
|
2017-06-28 07:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public static String getFileExt(String url) {
|
|
|
|
int index;
|
|
|
|
if ((index = url.indexOf("?")) > -1) {
|
|
|
|
url = url.substring(0, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
index = url.lastIndexOf(".");
|
|
|
|
if (index == -1) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
String ext = url.substring(index);
|
|
|
|
if ((index = ext.indexOf("%")) > -1) {
|
|
|
|
ext = ext.substring(0, index);
|
|
|
|
}
|
|
|
|
if ((index = ext.indexOf("/")) > -1) {
|
|
|
|
ext = ext.substring(0, index);
|
|
|
|
}
|
|
|
|
return ext.toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static FileType getFileType(String file) {
|
|
|
|
if (file.endsWith(".mp3") || file.endsWith(".wav") || file.endsWith(".flac") || file.endsWith(".m4a")) {
|
|
|
|
return FileType.MUSIC;
|
|
|
|
} else if (file.endsWith(".mp4") || file.endsWith(".mpeg") || file.endsWith(".rm") || file.endsWith(".rmvb")
|
|
|
|
|| file.endsWith(".flv") || file.endsWith(".webp") || file.endsWith(".webm")) {
|
|
|
|
return FileType.VIDEO;
|
|
|
|
} else {
|
|
|
|
return FileType.UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ColorRes
|
|
|
|
public static int getBackgroundForFileType(FileType type) {
|
|
|
|
switch (type) {
|
|
|
|
case MUSIC:
|
|
|
|
return R.color.audio_left_to_load_color;
|
|
|
|
case VIDEO:
|
|
|
|
return R.color.video_left_to_load_color;
|
|
|
|
default:
|
|
|
|
return R.color.gray;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ColorRes
|
|
|
|
public static int getForegroundForFileType(FileType type) {
|
|
|
|
switch (type) {
|
|
|
|
case MUSIC:
|
|
|
|
return R.color.audio_already_load_color;
|
|
|
|
case VIDEO:
|
|
|
|
return R.color.video_already_load_color;
|
|
|
|
default:
|
|
|
|
return R.color.gray;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@DrawableRes
|
|
|
|
public static int getIconForFileType(FileType type) {
|
|
|
|
switch (type) {
|
|
|
|
case MUSIC:
|
|
|
|
return R.drawable.music;
|
|
|
|
case VIDEO:
|
|
|
|
return R.drawable.video;
|
|
|
|
default:
|
|
|
|
return R.drawable.video;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void copyToClipboard(Context context, String str) {
|
|
|
|
ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
|
|
|
cm.setPrimaryClip(ClipData.newPlainText("text", str));
|
|
|
|
Toast.makeText(context, R.string.msg_copied, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String checksum(String path, String algorithm) {
|
|
|
|
MessageDigest md = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
md = MessageDigest.getInstance(algorithm);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInputStream i = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
i = new FileInputStream(path);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] buf = new byte[1024];
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
while ((len = i.read(buf)) != -1) {
|
|
|
|
md.update(buf, 0, len);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] digest = md.digest();
|
|
|
|
|
|
|
|
// HEX
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (byte b : digest) {
|
|
|
|
sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.toString();
|
2016-04-21 20:28:01 -03:00
|
|
|
|
2017-06-28 07:27:32 +02:00
|
|
|
}
|
2016-04-21 20:28:01 -03:00
|
|
|
}
|