-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo
113 lines (99 loc) · 3.23 KB
/
demo
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
package com.example.test.alltest;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.os.FileObserver;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import static java.lang.Thread.sleep;
public class FileMonitor extends FileObserver {
static String TAG = "filemonitor";
String path = "";
String assetfile = "";
Context context = null;
boolean done = false;
public FileMonitor(String path, Context context, String assetfile) {
super(path);
this.path = path;
this.context = context;
this.assetfile = assetfile;
}
@Override
public void onEvent(int event, String file) {
switch (event){
case FileObserver.CREATE:
Log.i(TAG+" create", file);
//copyFile(file);
break;
case FileObserver.OPEN:
Log.i(TAG+" OPEN", file);
//copyFile(file);
break;
case FileObserver.DELETE:
Log.i(TAG+" DELETE", file);
//copyFile(file);
break;
case FileObserver.ACCESS:
Log.i(TAG+" ACCESS", file);
copyFile(file);
break;
case FileObserver.CLOSE_WRITE:
Log.i(TAG+" close_write", file);
//copyFile(file);
break;
}
}
private void copyFile(String file){
if(!(file.equals(this.assetfile) || file.equals(this.assetfile+".download")))return;
if(done)return;
try{
sleep(500);
}catch (Exception e){
Log.i(TAG, e.toString());
}
Log.i(TAG, "monitoring...");
AssetManager assetManager = this.context.getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(this.assetfile);
String newFileName = this.path+"/"+this.assetfile;
File origin = new File(newFileName);
if(origin.exists())origin.delete();
out = new FileOutputStream(newFileName);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.flush();
out.close();
done = true;
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
done = true;
}
}
=================
void monitorFile(){
new Thread(new Runnable() {
@Override
public void run() {
try {
String path = "/storage/emulated/0/Android/data/com.wuba/cache/wuba/apkfile";
fm = new FileMonitor(path, getApplicationContext(), "client_v8.21.2_496.apk");
fm.startWatching();
Log.i(FileMonitor.TAG, "started monitor "+path);
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
}