/
CameraFilesListener.java
114 lines (93 loc) · 3.13 KB
/
CameraFilesListener.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package rhomobile.camera;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import com.rho.RhoEmptyLogger;
import com.rho.RhoLogger;
import net.rim.device.api.io.file.FileSystemJournal;
import net.rim.device.api.io.file.FileSystemJournalEntry;
import net.rim.device.api.io.file.FileSystemJournalListener;
public class CameraFilesListener implements FileSystemJournalListener {
private static final RhoLogger LOG = RhoLogger.RHO_STRIP_LOG ? new RhoEmptyLogger() :
new RhoLogger("CameraFilesListener");
private long _lastUSN = 0;
private CameraScreen _screen;
private Hashtable _exists;
private final String[] _folders = {
"file:///store/home/user/pictures/",
"file:///sdcard/blackberry/pictures/",
"file:///store/home/user/camera/",
"file:///sdcard/blackberry/camera/"
};
public CameraFilesListener(CameraScreen screen) {
_screen = screen;
_exists = new Hashtable();
for(int i = 0; i < _folders.length; i++) {
try {
FileConnection fconn = (FileConnection)Connector.open(_folders[i], Connector.READ);
if (!fconn.isDirectory())
continue;
Enumeration e = fconn.list();
while(e.hasMoreElements()) {
_exists.put(e.nextElement(), new Boolean(true));
}
} catch (IOException e) {
LOG.ERROR("CameraFilesListener: " + _folders[i], e);
continue;
}
}
}
public void fileJournalChanged() {
long nextUSN = FileSystemJournal.getNextUSN();
String msg = null;
for (long lookUSN = nextUSN - 1; lookUSN >= _lastUSN && msg == null; --lookUSN) {
FileSystemJournalEntry entry = FileSystemJournal.getEntry(lookUSN);
if (entry == null) { // we didn't find an entry.
break;
}
//check if this entry was added or deleted
String path = entry.getPath();
if (path == null)
continue;
if (!path.startsWith("file://"))
path = "file://" + path;
int event = entry.getEvent();
LOG.TRACE("event: " + Integer.toString(event));
if (event != FileSystemJournalEntry.FILE_ADDED)
continue;
String lpath = path.toLowerCase();
boolean bKnownFolder = false;
for( int i = 0; i < _folders.length; i++)
{
if ( lpath.startsWith(_folders[i]))
{
bKnownFolder = true;
break;
}
}
if (!bKnownFolder)
continue;
if (lpath.endsWith(".dat"))
continue;
if (_exists.get(path) != null)
continue;
if (_screen != null) {
try {
FileConnection fconn = (FileConnection)Connector.open(path, Connector.READ);
boolean exists = fconn.exists();
fconn.close();
if (!exists)
continue;
} catch (IOException e) {
continue;
}
_exists.put(path, new Boolean(true));
_screen.invokeCloseScreen(true, path);
_screen = null;
}
}
_lastUSN = nextUSN;
}
}