|
@ -9,89 +9,55 @@ import android.content.ActivityNotFoundException;
|
9
|
9
|
import android.content.Context;
|
10
|
10
|
import android.widget.Toast;
|
11
|
11
|
|
12
|
|
import com.litesuits.android.log.Log;
|
13
|
12
|
import com.wade.mobile.common.MobileException;
|
14
|
13
|
import com.wade.mobile.frame.IWadeMobile;
|
15
|
14
|
import com.wade.mobile.frame.plugin.Plugin;
|
16
|
|
import com.wade.mobile.ui.helper.HintHelper;
|
17
|
|
import com.wade.mobile.util.Constant;
|
18
|
15
|
import com.wade.mobile.util.DirectionUtil;
|
19
|
16
|
import com.wade.mobile.util.EscapeUnescape;
|
20
|
17
|
import com.wade.mobile.util.FileUtil;
|
21
|
|
import com.wade.mobile.util.Messages;
|
22
|
18
|
|
23
|
19
|
public class MobileFile extends Plugin{
|
24
|
20
|
private DirectionUtil util;
|
25
|
21
|
|
26
|
22
|
public MobileFile(IWadeMobile wademobile) {
|
27
|
23
|
super(wademobile);
|
28
|
|
util=DirectionUtil.getInstance(context);
|
|
24
|
util = DirectionUtil.getInstance(context);
|
29
|
25
|
}
|
|
26
|
|
30
|
27
|
/**
|
31
|
28
|
* 清空指定的资源文件
|
32
|
29
|
*/
|
33
|
|
public void cleanResource(JSONArray param)throws Exception{
|
34
|
|
int type=param.getInt(0);
|
35
|
|
util.cleanResource(type);
|
36
|
|
Log.i("Test", "AA"+util.getAllFileNames(type));
|
37
|
|
if(util.getAllFileNames(type).length==0){
|
38
|
|
Toast.makeText(context, "清除成功!", Toast.LENGTH_SHORT).show();
|
39
|
|
}else{
|
40
|
|
Toast.makeText(context, "清除失败!", Toast.LENGTH_SHORT).show();
|
41
|
|
}
|
|
30
|
public void cleanResource(JSONArray param) throws Exception {
|
|
31
|
String relativePath = param.getString(0); //相对路径参数
|
|
32
|
boolean isSdcard = !"false".equals(param.getString(1));
|
|
33
|
cleanResource(relativePath, isSdcard);
|
42
|
34
|
}
|
43
|
35
|
|
44
|
|
/**
|
45
|
|
* 获取files和sdcard/files下面所有的文件
|
46
|
|
*/
|
47
|
|
public void getAllFile(JSONArray param) throws Exception{
|
48
|
|
int type = param.getInt(0);
|
49
|
|
String dir;
|
50
|
|
switch (type) {
|
51
|
|
case Constant.Direction.APP_FILES:
|
52
|
|
dir = util.getDirection(Constant.Direction.APP_FILES);
|
53
|
|
break;
|
54
|
|
case Constant.Direction.SDCARD_APP_FILES:
|
55
|
|
dir = util.getDirection(Constant.Direction.SDCARD_APP_FILES);
|
56
|
|
break;
|
57
|
|
default:
|
58
|
|
return;
|
59
|
|
}
|
60
|
|
String[] fileNames = new File(dir).list();
|
61
|
|
StringBuilder sb = new StringBuilder("[");
|
62
|
|
if (fileNames != null && fileNames.length > 0) {
|
63
|
|
for (String n : fileNames) {
|
64
|
|
sb.append("\"");
|
65
|
|
sb.append(n);
|
66
|
|
sb.append("\",");
|
67
|
|
}
|
68
|
|
sb.deleteCharAt(sb.length() - 1);
|
|
36
|
public void cleanResource(String relativePath, boolean isSdcard) {
|
|
37
|
// TODO Auto-generated method stub
|
|
38
|
String absolutePath = util.getDirection(relativePath, isSdcard);
|
|
39
|
FileUtil.deleteFolder(absolutePath);
|
|
40
|
|
|
41
|
String[] fileList = new File(absolutePath).list();
|
|
42
|
if (fileList.length == 0) {
|
|
43
|
Toast.makeText(context, "清除成功!", Toast.LENGTH_SHORT).show();
|
|
44
|
} else {
|
|
45
|
Toast.makeText(context, "清除失败!", Toast.LENGTH_SHORT).show();
|
69
|
46
|
}
|
70
|
|
sb.append("]");
|
71
|
|
callback(sb.toString());
|
72
|
47
|
}
|
73
|
48
|
|
74
|
|
|
75
|
49
|
/**
|
76
|
|
* 打开文件
|
77
|
|
* @param param
|
|
50
|
* 打开指定文件
|
78
|
51
|
*/
|
79
|
52
|
public void openFile(JSONArray param) throws Exception{
|
80
|
|
String fileName = param.getString(0);
|
81
|
|
int type = param.getInt(1);
|
82
|
|
openFile(fileName, type);
|
|
53
|
String relativePath = param.getString(0); //相对路径参数
|
|
54
|
boolean isSdcard = !"false".equals(param.getString(1));
|
|
55
|
openFile(relativePath, isSdcard);
|
83
|
56
|
}
|
84
|
57
|
|
85
|
|
public void openFile(String fileName,int type) throws Exception {
|
86
|
|
// TODO Auto-generated method stub
|
87
|
|
if (type == Constant.Direction.APP_FILES) {
|
88
|
|
/*data/files目录*/
|
89
|
|
fileName = util.getDirection(Constant.Direction.APP_FILES) + File.separator + fileName;
|
90
|
|
} else if (type == Constant.Direction.SDCARD_APP_FILES) {
|
91
|
|
/*sdcard/appname/files目录*/
|
92
|
|
fileName=util.getDirection(Constant.Direction.SDCARD_APP_FILES) + File.separator + fileName;
|
93
|
|
}
|
94
|
|
File file = new File(fileName);
|
|
58
|
public void openFile(String relativePath, boolean isSdcard) throws Exception {
|
|
59
|
String absolutePath = util.getDirection(relativePath, isSdcard);
|
|
60
|
File file = new File(absolutePath);
|
95
|
61
|
try {
|
96
|
62
|
FileUtil.openFile(context, file);
|
97
|
63
|
} catch (ActivityNotFoundException e) {
|
|
@ -99,12 +65,15 @@ public class MobileFile extends Plugin{
|
99
|
65
|
}
|
100
|
66
|
}
|
101
|
67
|
|
|
68
|
/**
|
|
69
|
* 读取指定文件
|
|
70
|
*/
|
102
|
71
|
public void readFile(JSONArray param) throws JSONException{
|
103
|
|
String fileName = param.getString(0);
|
104
|
|
int type = param.getInt(1);
|
|
72
|
String relativePath = param.getString(0); //相对路径参数
|
|
73
|
boolean isSdcard = !"false".equals(param.getString(1));
|
105
|
74
|
String isEscape = param.getString(2);
|
106
|
75
|
try {
|
107
|
|
String value = readFile(fileName, type);
|
|
76
|
String value = readFile(relativePath, isSdcard);
|
108
|
77
|
if ("true".equals(isEscape)) {
|
109
|
78
|
value = EscapeUnescape.escape(value);
|
110
|
79
|
}
|
|
@ -114,69 +83,64 @@ public class MobileFile extends Plugin{
|
114
|
83
|
}
|
115
|
84
|
}
|
116
|
85
|
|
117
|
|
public String readFile(String fileName,int type) throws Exception {
|
118
|
|
if (type == Constant.Direction.APP_FILES) {
|
119
|
|
/*data/files目录*/
|
120
|
|
return FileUtil.readFile(this.context.openFileInput(fileName));
|
121
|
|
} else if (type == Constant.Direction.SDCARD_APP_FILES) {
|
122
|
|
/*sdcard/appname/files目录*/
|
123
|
|
fileName=util.getDirection(Constant.Direction.SDCARD_APP_FILES) + File.separator + fileName;
|
124
|
|
return FileUtil.readFile(fileName);
|
|
86
|
public String readFile(String relativePath, boolean isSdcard) throws Exception {
|
|
87
|
//对比
|
|
88
|
if (!isSdcard) {
|
|
89
|
return FileUtil.readFile(this.context.openFileInput(relativePath));
|
|
90
|
} else {
|
|
91
|
String absolutePath = util.getDirection(relativePath, isSdcard);
|
|
92
|
return FileUtil.readFile(absolutePath);
|
125
|
93
|
}
|
126
|
|
return null;
|
127
|
94
|
}
|
128
|
|
|
|
95
|
|
|
96
|
/**
|
|
97
|
* 追加指定文件
|
|
98
|
*/
|
129
|
99
|
public void appendFile(JSONArray param) throws Exception{
|
130
|
100
|
String content = param.getString(0);
|
131
|
|
String fileName = param.getString(1);
|
132
|
|
int type = param.getInt(2);
|
133
|
|
writeFile(content, fileName, type, true);
|
|
101
|
String relativePath = param.getString(1); //相对路径参数
|
|
102
|
boolean isSdcard = !"false".equals(param.getString(2));
|
|
103
|
writeFile(content, relativePath, isSdcard, true);
|
134
|
104
|
}
|
135
|
105
|
|
|
106
|
/**
|
|
107
|
* 写入指定文件
|
|
108
|
*/
|
136
|
109
|
public void writeFile(JSONArray param) throws Exception{
|
137
|
110
|
String content = param.getString(0);
|
138
|
|
String fileName = param.getString(1);
|
139
|
|
int type = param.getInt(2);
|
140
|
|
writeFile(content, fileName, type, false);
|
|
111
|
String relativePath = param.getString(1); //相对路径参数
|
|
112
|
boolean isSdcard = !"false".equals(param.getString(2));
|
|
113
|
writeFile(content, relativePath, isSdcard, false);
|
141
|
114
|
}
|
142
|
115
|
|
143
|
|
public String writeFile(String content, String fileName, int type, boolean append) throws Exception {
|
|
116
|
public void writeFile(String content, String relativePath, boolean isSdcard, boolean append) throws Exception {
|
144
|
117
|
// TODO Auto-generated method stub
|
145
|
|
if (type == Constant.Direction.APP_FILES) {
|
146
|
|
if(append){
|
147
|
|
FileUtil.writeFile(content, this.context.openFileOutput(fileName, Context.MODE_PRIVATE|Context.MODE_APPEND));
|
148
|
|
}else{
|
149
|
|
FileUtil.writeFile(content, this.context.openFileOutput(fileName, Context.MODE_PRIVATE));
|
|
118
|
if (!isSdcard) {
|
|
119
|
if (append) {
|
|
120
|
FileUtil.writeFile(content, this.context.openFileOutput(
|
|
121
|
relativePath, Context.MODE_PRIVATE | Context.MODE_APPEND));
|
|
122
|
} else {
|
|
123
|
FileUtil.writeFile(content, this.context.openFileOutput(
|
|
124
|
relativePath, Context.MODE_PRIVATE));
|
150
|
125
|
}
|
151
|
|
} else if (type == Constant.Direction.SDCARD_APP_FILES) {
|
152
|
|
String path = util.getDirection(Constant.Direction.SDCARD_APP_FILES);
|
153
|
|
if (!FileUtil.check(path)) {
|
154
|
|
FileUtil.createDir(path);
|
|
126
|
} else {
|
|
127
|
String absolutePath = util.getDirection(relativePath, isSdcard);
|
|
128
|
if (!FileUtil.check(absolutePath)) {
|
|
129
|
FileUtil.createDir(absolutePath);
|
155
|
130
|
}
|
156
|
|
fileName = path + File.separator + fileName;
|
157
|
|
FileUtil.writeFile(content, fileName, append);
|
|
131
|
FileUtil.writeFile(content, absolutePath, append);
|
158
|
132
|
}
|
159
|
|
|
160
|
|
HintHelper.tip(context,Messages.bind(Messages.FILE_WRITE_SUCCESS, fileName));
|
161
|
|
return fileName;
|
162
|
133
|
}
|
163
|
134
|
|
164
|
135
|
//删除文件
|
165
|
136
|
public void deleteFile(JSONArray param) throws Exception{
|
166
|
|
String fileName=param.getString(0);
|
167
|
|
int type=param.getInt(1);
|
168
|
|
switch (type) {
|
169
|
|
case Constant.Direction.APP_FILES:
|
170
|
|
/*data/files目录*/
|
171
|
|
fileName = util.getDirection(Constant.Direction.APP_FILES) + File.separator + fileName;
|
172
|
|
break;
|
173
|
|
case Constant.Direction.SDCARD_APP_FILES:
|
174
|
|
/*sdcard/appname/files目录*/
|
175
|
|
fileName = util.getDirection(Constant.Direction.SDCARD_APP_FILES) + File.separator + fileName;
|
176
|
|
break;
|
177
|
|
}
|
178
|
|
if(!isNull(fileName)){
|
179
|
|
FileUtil.deleteFile(fileName);
|
180
|
|
}
|
|
137
|
String relativePath = param.getString(0); //相对路径参数
|
|
138
|
boolean isSdcard = !"false".equals(param.getString(1));
|
|
139
|
deleteFile(relativePath, isSdcard);
|
|
140
|
}
|
|
141
|
|
|
142
|
public void deleteFile(String relativePath, boolean isSdcard){
|
|
143
|
String absolutePath = util.getDirection(relativePath, isSdcard);
|
|
144
|
FileUtil.deleteFile(absolutePath);
|
181
|
145
|
}
|
182
|
146
|
}
|