128 lines
4.0 KiB
Java
128 lines
4.0 KiB
Java
package cn.com.magnity.magnitycx.sdk;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.net.Uri;
|
|
import android.webkit.MimeTypeMap;
|
|
import cn.com.magnity.magnitycx.MagApplication;
|
|
import java.io.File;
|
|
import java.io.FileFilter;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Comparator;
|
|
import java.util.Iterator;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class GlobalFunc {
|
|
public static final int MEDIA_IMAGE = 0;
|
|
public static final int MEDIA_OTHER = -1;
|
|
public static final int MEDIA_VIDEO = 1;
|
|
|
|
public static int guessMediaTypeBySuffix(String name) {
|
|
String mimeType = getMimeType(getExtension(name));
|
|
if (mimeType == null) {
|
|
return -1;
|
|
}
|
|
if (mimeType.contains("video/")) {
|
|
return 1;
|
|
}
|
|
return mimeType.contains("image/") ? 0 : -1;
|
|
}
|
|
|
|
public static void updateFileList(ArrayList<String> fileNames) {
|
|
File file = new File(MagApplication.magParameter.mediaDir);
|
|
File[] files = file.listFiles(new FileFilter() { // from class: cn.com.magnity.magnitycx.sdk.GlobalFunc.1
|
|
@Override // java.io.FileFilter
|
|
public boolean accept(File pathname) {
|
|
String name = pathname.getName().toLowerCase();
|
|
return !name.endsWith(".ddt");
|
|
}
|
|
});
|
|
if (file == null || files == null) {
|
|
fileNames.clear();
|
|
return;
|
|
}
|
|
ArrayList<FileInfo> fileInfos = new ArrayList<>();
|
|
for (File f : files) {
|
|
FileInfo fileInfo = new FileInfo();
|
|
fileInfo.name = f.getName();
|
|
fileInfo.lastModified = f.lastModified();
|
|
fileInfos.add(fileInfo);
|
|
}
|
|
Collections.sort(fileInfos, new FileInfoComparator());
|
|
fileNames.clear();
|
|
Iterator<FileInfo> it = fileInfos.iterator();
|
|
while (it.hasNext()) {
|
|
fileNames.add(it.next().name);
|
|
}
|
|
}
|
|
|
|
public static void notifyMediaSync(Context context, File file) {
|
|
Intent intent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
|
|
Uri uri = Uri.fromFile(file);
|
|
intent.setData(uri);
|
|
context.sendBroadcast(intent);
|
|
}
|
|
|
|
public static int byteArrayToInt(byte[] b) {
|
|
return (b[0] & 255) | ((b[1] & 255) << 8) | ((b[2] & 255) << 16) | ((b[3] & 255) << 24);
|
|
}
|
|
|
|
public static byte[] intToByteArray(int a) {
|
|
return new byte[]{(byte) (a & 255), (byte) ((a >> 8) & 255), (byte) ((a >> 16) & 255), (byte) ((a >> 24) & 255)};
|
|
}
|
|
|
|
public static void intToByteArray(int a, byte[] out, int offset) {
|
|
if (out.length >= offset + 4) {
|
|
int offset2 = offset + 1;
|
|
out[offset] = (byte) a;
|
|
int offset3 = offset2 + 1;
|
|
out[offset2] = (byte) (a >> 8);
|
|
int offset4 = offset3 + 1;
|
|
out[offset3] = (byte) (a >> 16);
|
|
int i = offset4 + 1;
|
|
out[offset4] = (byte) (a >> 24);
|
|
}
|
|
}
|
|
|
|
private static String getExtension(String name) {
|
|
int idx = name.lastIndexOf(".");
|
|
if (idx <= 0) {
|
|
return "";
|
|
}
|
|
String suffix = name.substring(idx + 1);
|
|
return suffix;
|
|
}
|
|
|
|
private static String getMimeType(String suffix) {
|
|
if (suffix.isEmpty()) {
|
|
return null;
|
|
}
|
|
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(suffix);
|
|
}
|
|
|
|
private static class FileInfoComparator implements Comparator<FileInfo> {
|
|
private FileInfoComparator() {
|
|
}
|
|
|
|
@Override // java.util.Comparator
|
|
public int compare(FileInfo lhs, FileInfo rhs) {
|
|
if (lhs.lastModified > rhs.lastModified) {
|
|
return -1;
|
|
}
|
|
if (lhs.lastModified == rhs.lastModified) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
private static class FileInfo {
|
|
public long lastModified;
|
|
public String name;
|
|
|
|
private FileInfo() {
|
|
}
|
|
}
|
|
}
|