/
FindRomsTask.java
87 lines (68 loc) · 1.98 KB
/
FindRomsTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package net.supware.tipro;
import java.io.File;
import java.util.LinkedList;
import java.util.Queue;
import android.os.AsyncTask;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import net.supware.tipro.model.TIGutsModel;
abstract public class FindRomsTask extends
AsyncTask<Void, FindRomsTask.ProgressData, Void> {
private static final String TAG = FindRomsTask.class.getSimpleName();
private String[] SEARCH_LOCATIONS = {
Environment.getExternalStorageDirectory().getAbsolutePath(),
"/mnt/sdcard", "/mnt/sdcard-ext" };
protected Queue<String> mFileQueue = new LinkedList<>();
class ProgressData {
public int model;
public String filename;
public ProgressData(int m, String f) {
this.model = m;
this.filename = f;
}
}
public void addToFileQueue(String... pathnames) {
for (String path : pathnames) {
if (TextUtils.isEmpty(path) || mFileQueue.contains(path))
continue;
mFileQueue.add(path);
Log.e(TAG, "adding " + path);
}
}
@Override
public Void doInBackground(Void... params) {
addToFileQueue(SEARCH_LOCATIONS);
String next = mFileQueue.poll();
while (next != null) {
File nextFile = new File(next);
if (nextFile.isDirectory()) {
publishProgress(new ProgressData(-1, next));
File subfiles[] = nextFile.listFiles();
if (subfiles != null) {
for (File f : subfiles) {
mFileQueue.add(f.getAbsolutePath());
}
}
}
else if (nextFile.isFile()) {
int model = TIGutsModel.getRomFileType(nextFile);
if (model != -1)
publishProgress(new ProgressData(model, next));
}
next = mFileQueue.poll();
}
return null;
}
@Override
public void onProgressUpdate(ProgressData... progress) {
for (ProgressData p : progress) {
if (p.model == -1)
onSearchDirectory(p.filename);
else
onFoundModel(p.model, p.filename);
}
}
public abstract void onSearchDirectory(String dirname);
public abstract void onFoundModel(int model, String filename);
}