Skip to content

Commit a71ce91

Browse files
committed
draft
1 parent 1b5d616 commit a71ce91

File tree

1 file changed

+13
-56
lines changed

1 file changed

+13
-56
lines changed

app/src/main/java/at/tomtasche/reader/background/MetadataLoader.java

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import android.provider.OpenableColumns;
77
import android.webkit.MimeTypeMap;
88

9-
import com.hzy.libmagic.MagicApi;
10-
119
import java.io.File;
1210
import java.io.FileInputStream;
1311
import java.io.FileNotFoundException;
@@ -21,30 +19,6 @@ public MetadataLoader(Context context) {
2119
super(context, LoaderType.METADATA);
2220
}
2321

24-
private boolean initMagicFromAssets() {
25-
InputStream inputStream = null;
26-
try {
27-
inputStream = context.getAssets().open("magic.mgc");
28-
int length = inputStream.available();
29-
byte[] buffer = new byte[length];
30-
if (inputStream.read(buffer) > 0) {
31-
return MagicApi.loadFromBytes(buffer, MagicApi.MAGIC_MIME_TYPE | MagicApi.MAGIC_COMPRESS_TRANSP) == 0;
32-
}
33-
} catch (Throwable e) {
34-
crashManager.log(e);
35-
} finally {
36-
if (inputStream != null) {
37-
try {
38-
inputStream.close();
39-
} catch (IOException e) {
40-
crashManager.log(e);
41-
}
42-
}
43-
}
44-
45-
return false;
46-
}
47-
4822
@Override
4923
public boolean isSupported(Options options) {
5024
return true;
@@ -113,22 +87,21 @@ public void loadSync(Options options) {
11387
String[] fileSplit = options.filename.split("\\.");
11488
String extension = fileSplit.length > 0 ? fileSplit[fileSplit.length - 1] : "N/A";
11589

116-
String type = null;
90+
String mimetype = null;
11791
try {
118-
if (initMagicFromAssets()) {
119-
type = MagicApi.magicFile(cachedFile.getAbsolutePath());
120-
}
92+
// todo core get mimetype
93+
//type = MagicApi.magicFile(cachedFile.getAbsolutePath());
12194
} catch (Throwable e) {
12295
crashManager.log(e);
12396
}
12497

125-
if (type == null) {
126-
type = context.getContentResolver().getType(uri);
98+
if (mimetype == null) {
99+
mimetype = context.getContentResolver().getType(uri);
127100
}
128101

129-
if (type == null) {
102+
if (mimetype == null) {
130103
try {
131-
type = URLConnection.guessContentTypeFromName(filename);
104+
mimetype = URLConnection.guessContentTypeFromName(filename);
132105
} catch (Exception e) {
133106
// Samsung S7 Edge crashes with java.lang.StringIndexOutOfBoundsException
134107
crashManager.log(e);
@@ -138,27 +111,27 @@ public void loadSync(Options options) {
138111
if (type == null) {
139112
try {
140113
try (InputStream tempStream = new FileInputStream(cachedFile)) {
141-
type = URLConnection.guessContentTypeFromStream(tempStream);
114+
mimetype = URLConnection.guessContentTypeFromStream(tempStream);
142115
}
143116
} catch (Exception e) {
144117
crashManager.log(e);
145118
}
146119
}
147120

148121
if (type != null) {
149-
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(type);
122+
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimetype);
150123
} else {
151-
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(options.fileExtension);
124+
mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(options.fileExtension);
152125
}
153126

154127
if (extension != null) {
155128
options.fileExtension = extension;
156129
}
157-
if (type != null) {
158-
options.fileType = type;
130+
if (mimetype != null) {
131+
options.fileType = mimetype;
159132
}
160133

161-
if ("inode/x-empty".equals(type)) {
134+
if ("inode/x-empty".equals(mimetype)) {
162135
throw new FileNotFoundException();
163136
}
164137

@@ -183,20 +156,4 @@ public void loadSync(Options options) {
183156
callOnError(result, e);
184157
}
185158
}
186-
187-
@Override
188-
public void close() {
189-
super.close();
190-
191-
backgroundHandler.post(new Runnable() {
192-
@Override
193-
public void run() {
194-
try {
195-
MagicApi.close();
196-
} catch (Throwable e) {
197-
crashManager.log(e);
198-
}
199-
}
200-
});
201-
}
202159
}

0 commit comments

Comments
 (0)