ScanUtils.java
10.3 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package com.gimi.common.cinema.utils;
import android.content.Context;
import com.gimi.common.cinema.model.FolderItem;
import com.xgimi.gimicinema.R;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by pc on 2014/12/9
*/
public class ScanUtils {
private Context context;
String[] picExtensions;
String[] mediaExtensions;
public ScanUtils(Context context) {
this.context = context;
picExtensions = context.getResources().getStringArray(R.array.photo_filter);
mediaExtensions = context.getResources().getStringArray(R.array.video_filter);
}
public ArrayList<FolderItem> getTypes(String rootPath) {
ArrayList<FolderItem> types = new ArrayList<FolderItem>();
File file = new File(rootPath);
String[] fileDirs = file.list();
for (String fileDir : fileDirs) {
File curFile = new File(rootPath + fileDir);
if (curFile.isDirectory()) {
FolderItem folderItem = new FolderItem();
folderItem.setPosterPath(getPoster(rootPath + fileDir + "/"));
folderItem.setFolderName(fileDir);
folderItem.setFolderPath(rootPath + fileDir + "/");
types.add(folderItem);
}
}
return types;
}
//get one picture from path folder
public String getPoster(String path) {
File file = new File(path);
String[] files = file.list();
String posterPath = null;
for (int i = 0; i < files.length; i++) {
if (checkType(path + files[i], picExtensions)) {
posterPath = path + files[i];
break;
}
}
return posterPath;
}
private boolean checkType(String name, String[] extensions) {
for (String end : extensions) {
// Name never to null, without exception handling
if (name.toLowerCase().endsWith(end)) {
return true;
}
}
return false;
}
// public ArrayList<MoviesItem> getMovies1(String rootPath) {
// ArrayList<MoviesItem> movies = new ArrayList<MoviesItem>();
// File file = new File(rootPath);
// String[] fileDirs = file.list();
// //遍历
// for (String fileDir : fileDirs) {
// HashMap<String, String> nameId = getNameId(rootPath + fileDir + "/");
// if (!checkType(rootPath + fileDir, picExtensions)) {
// File curFiles = new File(rootPath + fileDir);
// if (curFiles.isDirectory()) {
// String curPath = curFiles.getAbsoluteFile().toString();
// MoviesItem moviesItem = new MoviesItem();
// //poster add douban id
// moviesItem.setMoviePoster(nameId.get(fileDir));
// //path as hash key
// String media = getMedia(curPath + "/", mediaExtensions);
// if (!TextUtils.isEmpty(media)) {
// File mFile = new File(media);
// long length = mFile.length();
// moviesItem.setMoviePath(length + "");
// }
// moviesItem.setMovieName(fileDir);
//// File mmPathFile = new File(curPath + "/" + "mm.txt");
//// if (mmPathFile.exists()) {
//// moviesItem.setSummary(mmPathFile.getAbsolutePath());
//// }
// movies.add(moviesItem);
// }
// }
// }
// return movies;
// }
//get the name Id
private HashMap<String, String> getNameId(String rootPath) {
HashMap<String, String> hashMap = new HashMap<String, String>();
File file = new File(rootPath + "name-id.txt");
if (file.exists()) {
ArrayList<String> a = FileReadUtils.readLines(file.getAbsolutePath());
for (String s : a) {
if (s.contains("#")) {
String[] split = s.split("#");
if (split.length >= 2) {
hashMap.put(split[0], split[1]);
}
}
}
}
return hashMap;
}
private String getMedia(String path, String[] extensions) {
File file = new File(path);
String[] files = file.list();
String posterPath = null;
for (int i = 0; i < files.length; i++) {
if (checkType(path + files[i], extensions)) {
posterPath = path + files[i];
break;
}
}
return posterPath;
}
//
// public ArrayList<MoviesItem> searchAll(String rootPath, String keyWord) {
// ArrayList<MoviesItem> moviesItems = new ArrayList<MoviesItem>();
// ArrayList<FolderItem> types = getTypes(rootPath);
// for (FolderItem type : types) {
// moviesItems.addAll(getRightMovies(type.getFolderPath(), keyWord));
// }
// return moviesItems;
// }
//
// public ArrayList<MoviesItem> getRightMovies(String rootPath, String keyWord) {
// ArrayList<MoviesItem> movies = new ArrayList<MoviesItem>();
// File file = new File(rootPath);
// String[] fileDirs = file.list();
// //遍历
// for (String fileDir : fileDirs) {
// if (!checkType(rootPath + fileDir, picExtensions)) {
// File curFiles = new File(rootPath + fileDir);
// if (curFiles.isDirectory()) {
// String curPath = curFiles.getAbsoluteFile().toString();
// MoviesItem moviesItem = new MoviesItem();
// moviesItem.setMoviePoster(getPoster(curPath + "/"));
// moviesItem.setMoviePath(getMedia(curPath + "/", mediaExtensions));
// moviesItem.setMovieName(fileDir);
//
// //to pinyin and charge
// if (isRight(moviesItem.getMovieName(), keyWord)) {
// movies.add(moviesItem);
// }
// }
// }
// }
// return movies;
// }
// public ArrayList<MoviesItem> getAllMovies(String rootPath) {
// ArrayList<MoviesItem> moviesItems = new ArrayList<MoviesItem>();
// ArrayList<FolderItem> types = getTypes(rootPath);
// for (FolderItem type : types) {
// moviesItems.addAll(getTypeMovies(type.getFolderPath()));
// }
// return moviesItems;
// }
// public ArrayList<MoviesItem> getAllMovies1(String rootPath) {
// ArrayList<MoviesItem> moviesItems = new ArrayList<MoviesItem>();
// ArrayList<FolderItem> types = getTypes(rootPath);
// for (FolderItem type : types) {
// moviesItems.addAll(getTypeMovies1(type.getFolderPath()));
// }
// return moviesItems;
// }
// public ArrayList<MoviesItem> getTypeMovies(String rootPath) {
// ArrayList<MoviesItem> movies = new ArrayList<MoviesItem>();
// File file = new File(rootPath);
// String[] fileDirs = file.list();
// //遍历
// for (String fileDir : fileDirs) {
// if (!checkType(rootPath + fileDir, picExtensions)) {
// File curFiles = new File(rootPath + fileDir);
// if (curFiles.isDirectory()) {
// String curPath = curFiles.getAbsoluteFile().toString();
// MoviesItem moviesItem = new MoviesItem();
// moviesItem.setMoviePoster(getPoster(curPath + "/"));
// moviesItem.setMoviePath(getMedia(curPath + "/", mediaExtensions));
// moviesItem.setMovieName(fileDir);
//
// movies.add(moviesItem);
// }
// }
// }
// return movies;
// }
// public ArrayList<MoviesItem> getTypeMovies1(String rootPath) {
// ArrayList<MoviesItem> movies = new ArrayList<MoviesItem>();
// File file = new File(rootPath);
// String[] fileDirs = file.list();
// HashMap<String, String> nameId = getNameId(rootPath + "/");
// //遍历
// for (String fileDir : fileDirs) {
// if (!checkType(rootPath + fileDir, picExtensions)) {
// File curFiles = new File(rootPath + fileDir);
// if (curFiles.isDirectory()) {
// String curPath = curFiles.getAbsoluteFile().toString();
// MoviesItem moviesItem = new MoviesItem();
// moviesItem.setMovieName(fileDir);
// //poster as douban id
// moviesItem.setMoviePoster(nameId.get(fileDir));
// //path as md5
// String media = getMedia(curPath + "/", mediaExtensions);
// if (!TextUtils.isEmpty(media)) {
// File mFile = new File(media);
// long length = mFile.length();
// moviesItem.setMoviePath(MD5Utils.stringMD5(length + ""));
// movies.add(moviesItem);
// }
// }
// }
// }
// return movies;
// }
private boolean isRight(String movieName, String keyWord) {
String firstSpell = PinyinUtil.getFirstSpell(movieName);
if (firstSpell.toLowerCase().contains(keyWord.toLowerCase())) {
return true;
}
return false;
}
public ArrayList<String> getVideo(String rootPath) {
ArrayList<String> movies = new ArrayList<String>();
File file = new File(rootPath);
String[] fileDirs = file.list();
if (fileDirs == null || fileDirs.length == 0) {
return movies;
}
//遍历
for (String fileDir : fileDirs) {
String curPath = rootPath + "/" + fileDir;
if (checkType(curPath, mediaExtensions)) {
movies.add(curPath);
}
}
return movies;
}
public ArrayList<String> getPicture(String rootPath) {
ArrayList<String> movies = new ArrayList<String>();
File file = new File(rootPath);
String[] fileDirs = file.list();
//遍历
for (String fileDir : fileDirs) {
String curPath = rootPath + "/" + fileDir;
if (checkType(curPath, picExtensions)) {
movies.add(curPath);
}
}
return movies;
}
}