childList = fileInfoBox.query().equal(FileInfo_.parentPath, parentPath).build().find();
576 | for (FileInfo childInfo : childList) {
577 | File file = new File(childInfo.getFilePath());
578 | if (!file.exists()) {
579 | delList.add(childInfo.getId());
580 | } else {
581 | if (childInfo.getLastModifyTime() != file.lastModified()) {//当前文件夹,时间不同,需要更新
582 | updateList.add(childInfo);
583 |
584 | if (!childInfo.getIsFile()) {
585 | traverseChild(childInfo.getFilePath(), delList, updateList);
586 | }
587 | }
588 | }
589 | }
590 | }
591 |
592 | private boolean isNeedToListener(File f) {
593 | if (f == null) return false;
594 | String path = f.getAbsolutePath();
595 | //以下返回必须为false
596 | String fileName = FileUtils.getFolderName(path);
597 | boolean isHidden = fileName.startsWith("_") || fileName.startsWith(".");
598 | boolean isSystem = path.startsWith(START_PATH + "Android") || path.startsWith(START_PATH + "backup") || path.startsWith(START_PATH + "backups") || path.startsWith(START_PATH + "CloudDrive")
599 | || path.startsWith(START_PATH + "huawei") || path.startsWith(START_PATH + "HuaweiBackup") || path.startsWith(START_PATH + "HWThemes") || path.startsWith(START_PATH + "msc") || path.startsWith(START_PATH + "Musiclrc");
600 | // boolean isRxCache = path.startsWith(START_PATH + "com.eblog" + File.separator + "cache" + File.separator + "rxCache");
601 | // boolean isSmiley = path.startsWith(START_PATH + "com.eblog" + File.separator + "cache" + File.separator + "smiley");
602 | // boolean isGlide = path.startsWith(START_PATH + "com.eblog" + File.separator + "cache" + File.separator + "glide");
603 | // boolean isOSS = path.startsWith(START_PATH + "com.eblog" + File.separator + "cache" + File.separator + "oss_record");
604 |
605 | //以下返回必须为true
606 | boolean isQQ = path.startsWith(QQ_PIC_PATH) || path.startsWith(QQ_FILE_PATH);
607 | boolean isWX = path.startsWith(WX_PIC_PATH) || path.startsWith(WX_FILE_PATH);
608 | boolean isSysPic = path.startsWith(SYS_CAMERA_PATH) || path.startsWith(SCREENSHOTS_PATH) || path.startsWith(HW_SCREEN_SAVER_PATH);
609 |
610 | //以下部分返回为true
611 | boolean isTencentPath = path.startsWith(START_PATH + "tencent" + File.separator);
612 |
613 | boolean isNeedFile;
614 | if (isHidden || isSystem)//|| isRxCache || isSmiley || isGlide || isOSS)
615 | isNeedFile = false;
616 | else if (isTencentPath)
617 | isNeedFile = isQQ || isWX;
618 | else if (isSysPic)
619 | isNeedFile = true;
620 | else
621 | isNeedFile = true;
622 |
623 | if (isNeedFile && f.isFile()) {
624 | String exc = FileUtils.getExtensionName(f.getName());
625 | isNeedFile = isNeedFile(exc);
626 | }
627 |
628 | return isNeedFile;
629 | }
630 | }
631 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/db/bean/FileInfo.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.db.bean;
2 |
3 | import io.objectbox.annotation.Entity;
4 | import io.objectbox.annotation.Id;
5 | import io.objectbox.annotation.Index;
6 | import io.objectbox.annotation.Generated;
7 | import io.objectbox.annotation.apihint.Internal;
8 |
9 | /**
10 | * 文件元数据
11 | *
12 | * Created by Z7Dream on 2017/7/4 11:34.
13 | * Email:zhangxyfs@126.com
14 | */
15 |
16 | @Entity
17 | public class FileInfo {
18 | @Id
19 | private long id;
20 |
21 | private String fileName;//文件名
22 |
23 | @Index
24 | private String filePath;//文件路径
25 |
26 | @Index
27 | private String parentPath;//父文件夹路径
28 |
29 | private String extension;//扩展名
30 |
31 | private long createTime;//创建时间
32 |
33 | @Index
34 | private long lastModifyTime;//最后修改时间
35 |
36 | private long fileSize;//文件大小
37 |
38 | private boolean isFile;//是否文件
39 |
40 | @Index
41 | private String fileType;//文件类型
42 |
43 | @Generated(1546775479)
44 | @Internal
45 | /** This constructor was generated by ObjectBox and may change any time. */
46 | public FileInfo(long id, String fileName, String filePath, String parentPath,
47 | String extension, long createTime, long lastModifyTime, long fileSize,
48 | boolean isFile, String fileType) {
49 | this.id = id;
50 | this.fileName = fileName;
51 | this.filePath = filePath;
52 | this.parentPath = parentPath;
53 | this.extension = extension;
54 | this.createTime = createTime;
55 | this.lastModifyTime = lastModifyTime;
56 | this.fileSize = fileSize;
57 | this.isFile = isFile;
58 | this.fileType = fileType;
59 | }
60 |
61 | @Generated(1367591352)
62 | public FileInfo() {
63 | }
64 |
65 | public long getId() {
66 | return id;
67 | }
68 |
69 | public void setId(long id) {
70 | this.id = id;
71 | }
72 |
73 | public String getFileName() {
74 | return fileName;
75 | }
76 |
77 | public void setFileName(String fileName) {
78 | this.fileName = fileName;
79 | }
80 |
81 | public String getFilePath() {
82 | return filePath;
83 | }
84 |
85 | public void setFilePath(String filePath) {
86 | this.filePath = filePath;
87 | }
88 |
89 | public String getParentPath() {
90 | return parentPath;
91 | }
92 |
93 | public void setParentPath(String parentPath) {
94 | this.parentPath = parentPath;
95 | }
96 |
97 | public String getExtension() {
98 | return extension;
99 | }
100 |
101 | public void setExtension(String extension) {
102 | this.extension = extension;
103 | }
104 |
105 | public long getCreateTime() {
106 | return createTime;
107 | }
108 |
109 | public void setCreateTime(long createTime) {
110 | this.createTime = createTime;
111 | }
112 |
113 | public long getLastModifyTime() {
114 | return lastModifyTime;
115 | }
116 |
117 | public void setLastModifyTime(long lastModifyTime) {
118 | this.lastModifyTime = lastModifyTime;
119 | }
120 |
121 | public long getFileSize() {
122 | return fileSize;
123 | }
124 |
125 | public void setFileSize(long fileSize) {
126 | this.fileSize = fileSize;
127 | }
128 |
129 | public boolean getIsFile() {
130 | return isFile;
131 | }
132 |
133 | public void setIsFile(boolean isFile) {
134 | this.isFile = isFile;
135 | }
136 |
137 | public String getFileType() {
138 | return fileType;
139 | }
140 |
141 | public void setFileType(String fileType) {
142 | this.fileType = fileType;
143 | }
144 | }
145 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/db/bean/FileStarInfo.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.db.bean;
2 |
3 | import io.objectbox.annotation.Entity;
4 | import io.objectbox.annotation.Id;
5 | import io.objectbox.annotation.Generated;
6 | import io.objectbox.annotation.apihint.Internal;
7 |
8 | /**
9 | * 文件星标
10 | *
11 | * Created by Z7Dream on 2017/7/4 13:38.
12 | * Email:zhangxyfs@126.com
13 | */
14 | @Entity
15 | public class FileStarInfo {
16 | @Id
17 | private long id;
18 |
19 | private long userId;//用户id
20 |
21 | private long fileInfoId;//文件id
22 |
23 | @Generated(52653744)
24 | @Internal
25 | /** This constructor was generated by ObjectBox and may change any time. */
26 | public FileStarInfo(long id, long userId, long fileInfoId) {
27 | this.id = id;
28 | this.userId = userId;
29 | this.fileInfoId = fileInfoId;
30 | }
31 |
32 | @Generated(298402998)
33 | public FileStarInfo() {
34 | }
35 |
36 | public long getId() {
37 | return id;
38 | }
39 |
40 | public void setId(long id) {
41 | this.id = id;
42 | }
43 |
44 | public long getUserId() {
45 | return userId;
46 | }
47 |
48 | public void setUserId(long userId) {
49 | this.userId = userId;
50 | }
51 |
52 | public long getFileInfoId() {
53 | return fileInfoId;
54 | }
55 |
56 | public void setFileInfoId(long fileInfoId) {
57 | this.fileInfoId = fileInfoId;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/db/bean/FileTypeInfo.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.db.bean;
2 |
3 | import io.objectbox.annotation.Entity;
4 | import io.objectbox.annotation.Id;
5 | import io.objectbox.annotation.Generated;
6 | import io.objectbox.annotation.apihint.Internal;
7 |
8 | /**
9 | * 文件类型:数据固定
10 | *
11 | * Created by Z7Dream on 2017/7/4 11:44.
12 | * Email:zhangxyfs@126.com
13 | */
14 | @Entity
15 | public class FileTypeInfo {
16 | @Id(assignable = true)
17 | private long id;
18 |
19 | private String typeName;//文件类型名称
20 |
21 | private String format;//格式(扩展名)
22 |
23 | public FileTypeInfo(String typeName, String format) {
24 | this.typeName = typeName;
25 | this.format = format;
26 | }
27 |
28 | @Generated(642009613)
29 | @Internal
30 | /** This constructor was generated by ObjectBox and may change any time. */
31 | public FileTypeInfo(long id, String typeName, String format) {
32 | this.id = id;
33 | this.typeName = typeName;
34 | this.format = format;
35 | }
36 |
37 | @Generated(1312591050)
38 | public FileTypeInfo() {
39 | }
40 |
41 | public long getId() {
42 | return id;
43 | }
44 |
45 | public void setId(long id) {
46 | this.id = id;
47 | }
48 |
49 | public String getTypeName() {
50 | return typeName;
51 | }
52 |
53 | public void setTypeName(String typeName) {
54 | this.typeName = typeName;
55 | }
56 |
57 | public String getFormat() {
58 | return format;
59 | }
60 |
61 | public void setFormat(String format) {
62 | this.format = format;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/listener/RecursiveFileObserver.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.listener;
2 |
3 | import android.os.FileObserver;
4 | import android.support.v4.util.ArrayMap;
5 | import android.text.TextUtils;
6 | import android.util.Log;
7 |
8 | import com.z7dream.lib.callback.Callback;
9 | import com.z7dream.lib.db.FileDaoImpl;
10 | import com.z7dream.lib.tool.CacheManager;
11 | import com.z7dream.lib.tool.FileUtils;
12 | import com.z7dream.lib.tool.rx.RxSchedulersHelper;
13 |
14 | import java.io.File;
15 | import java.util.Map;
16 | import java.util.Stack;
17 |
18 | import io.reactivex.Observable;
19 | import io.reactivex.ObservableOnSubscribe;
20 | import io.reactivex.disposables.Disposable;
21 |
22 | import static com.z7dream.lib.tool.MagicExplorer.HW_SCREEN_SAVER_PATH;
23 | import static com.z7dream.lib.tool.MagicExplorer.QQ_FILE_PATH;
24 | import static com.z7dream.lib.tool.MagicExplorer.QQ_PIC_PATH;
25 | import static com.z7dream.lib.tool.MagicExplorer.SCREENSHOTS_PATH;
26 | import static com.z7dream.lib.tool.MagicExplorer.SYS_CAMERA_PATH;
27 | import static com.z7dream.lib.tool.MagicExplorer.WX_FILE_PATH;
28 | import static com.z7dream.lib.tool.MagicExplorer.WX_PIC_PATH;
29 |
30 |
31 | /**
32 | * 文件增删改查监听
33 | *
34 | * Created by Z7Dream on 2017/7/3 16:39.
35 | * Email:zhangxyfs@126.com
36 | */
37 |
38 | public class RecursiveFileObserver extends FileObserver {
39 | private Map mObservers;
40 | private String mPath;
41 | private int mMask;
42 | private static final String START_PATH = CacheManager.getSaveFilePath() + File.separator;
43 | private Callback callback;
44 | private FileDaoImpl fileDaoUtils;
45 |
46 | private Disposable startDisposable, stopDisposable, createDisposable, deleteDisposable;
47 |
48 | public RecursiveFileObserver(String path, Callback callback, FileDaoImpl fileDaoUtils) {
49 | this(path, ALL_EVENTS, callback, fileDaoUtils);
50 | }
51 |
52 | public RecursiveFileObserver(String path, int mask, Callback callback, FileDaoImpl fileDaoUtils) {
53 | super(path, mask);
54 | mPath = path;
55 | mMask = mask;
56 | this.callback = callback;
57 | this.fileDaoUtils = fileDaoUtils;
58 | }
59 |
60 | @Override
61 | public void startWatching() {
62 | if (mObservers == null) {
63 | startDisposable = Observable.create((ObservableOnSubscribe) e -> {
64 | mObservers = new ArrayMap<>();
65 | Stack stack = new Stack<>();
66 | stack.push(mPath);
67 | while (!stack.isEmpty()) {
68 | String temp = stack.pop();
69 | mObservers.put(temp, new SingleFileObserver(temp, mMask));
70 | File path = new File(temp);
71 | File[] files = path.listFiles();
72 | if (null == files)
73 | continue;
74 | for (File f : files) {
75 | // 递归监听目录
76 | if (f.isDirectory() && isNeedToListener(f.getPath())) {
77 | stack.push(f.getAbsolutePath());
78 | }
79 | }
80 | }
81 | for (String key : mObservers.keySet()) {
82 | e.onNext(key);
83 | }
84 | e.onComplete();
85 |
86 | }).compose(RxSchedulersHelper.io()).doOnComplete(() -> {
87 | startDisposable.dispose();
88 | startDisposable = null;
89 | callback.callListener(mObservers.size());
90 | }).subscribe(key -> {
91 | mObservers.get(key).startWatching();
92 | }, error -> {
93 | });
94 | }
95 | }
96 |
97 | @Override
98 | public void stopWatching() {
99 | if (mObservers != null) {
100 | stopDisposable = Observable.create((ObservableOnSubscribe) e -> {
101 | for (String key : mObservers.keySet()) {
102 | e.onNext(key);
103 | }
104 | e.onComplete();
105 | }).compose(RxSchedulersHelper.io())
106 | .doOnComplete(() -> {
107 | mObservers.clear();
108 | mObservers = null;
109 | stopDisposable.dispose();
110 | stopDisposable = null;
111 |
112 | fileDaoUtils = null;
113 | })
114 | .subscribe(key -> {
115 | mObservers.get(key).stopWatching();
116 | }, error -> {
117 | });
118 | }
119 | }
120 |
121 | @Override
122 | public void onEvent(int event, String path) {
123 | File file = new File(path);
124 | int el = event & FileObserver.ALL_EVENTS;
125 | switch (el) {
126 | case FileObserver.ATTRIB:
127 | Log.i("RecursiveFileObserver", "ATTRIB: " + path);
128 | break;
129 | case FileObserver.CREATE:
130 | createDisposable = Observable.create((ObservableOnSubscribe) e -> {
131 | if (file.isDirectory()) {
132 | Stack stack = new Stack<>();
133 | stack.push(path);
134 | while (!stack.isEmpty()) {
135 | String temp = stack.pop();
136 | if (mObservers.containsKey(temp)) {
137 | continue;
138 | } else {
139 | SingleFileObserver sfo = new SingleFileObserver(temp, mMask);
140 | e.onNext(sfo);
141 | mObservers.put(temp, sfo);
142 | }
143 | File tempPath = new File(temp);
144 | File[] files = tempPath.listFiles();
145 | if (null == files)
146 | continue;
147 | for (File f : files) {
148 | // 递归监听目录
149 | if (f.isDirectory() && isNeedToListener(f.getPath())) {
150 | stack.push(f.getAbsolutePath());
151 | }
152 | }
153 | }
154 | }
155 | e.onComplete();
156 | }).compose(RxSchedulersHelper.io())
157 | .doOnComplete(() -> {
158 | createDisposable.dispose();
159 | createDisposable = null;
160 | }).subscribe(FileObserver::startWatching, error -> {
161 | });
162 |
163 | fileDaoUtils.addFileInfo(file);
164 | Log.i("RecursiveFileObserver", "CREATE: " + path);
165 | break;
166 | case FileObserver.DELETE:
167 | if (file.isDirectory()) {
168 | mObservers.get(path).stopWatching();
169 | mObservers.remove(path);
170 | }
171 | fileDaoUtils.removeFileInfo(file.getAbsolutePath());
172 |
173 | Log.i("RecursiveFileObserver", "DELETE: " + path);
174 | break;
175 | case FileObserver.DELETE_SELF:
176 | if (file.isDirectory()) {
177 | mObservers.get(path).stopWatching();
178 | mObservers.remove(path);
179 | }
180 | fileDaoUtils.removeFileInfo(file.getAbsolutePath());
181 |
182 | Log.i("RecursiveFileObserver", "DELETE_SELF: " + path);
183 | break;
184 | case FileObserver.MODIFY:
185 | fileDaoUtils.updateFileInfo(file);
186 | Log.i("RecursiveFileObserver", "MODIFY: " + path);
187 | break;
188 | case FileObserver.MOVE_SELF:
189 | Log.i("RecursiveFileObserver", "MOVE_SELF: " + path);
190 | break;
191 | case FileObserver.MOVED_FROM:
192 | Log.i("RecursiveFileObserver", "MOVED_FROM: " + path);
193 | break;
194 | case FileObserver.MOVED_TO:
195 | Log.i("RecursiveFileObserver", "MOVED_TO: " + path);
196 | break;
197 | }
198 | }
199 |
200 | private class SingleFileObserver extends FileObserver {
201 | String mPath;
202 |
203 | public SingleFileObserver(String path) {
204 | this(path, ALL_EVENTS);
205 | mPath = path;
206 | }
207 |
208 | SingleFileObserver(String path, int mask) {
209 | super(path, mask);
210 | mPath = path;
211 | }
212 |
213 | @Override
214 | public void onEvent(int event, String path) {
215 | if (path != null) {
216 | String newPath = mPath + "/" + path;
217 | RecursiveFileObserver.this.onEvent(event, newPath);
218 | }
219 | }
220 | }
221 |
222 | private boolean isNeedToListener(String path) {
223 | if (TextUtils.isEmpty(path)) return false;
224 | //以下返回必须为false
225 | String fileName = FileUtils.getFolderName(path);
226 | boolean isHidden = fileName.startsWith("_") || fileName.startsWith(".");
227 | boolean isSystem = path.equals(START_PATH + "Android") || path.equals(START_PATH + "backup") || path.equals(START_PATH + "backups") || path.equals(START_PATH + "CloudDrive")
228 | || path.equals(START_PATH + "huawei") || path.equals(START_PATH + "HuaweiBackup") || path.equals(START_PATH + "HWThemes") || path.equals(START_PATH + "msc") || path.endsWith(START_PATH + "Musiclrc");
229 | boolean isRxCache = path.equals(START_PATH + "com.eblog" + File.separator + "cache" + File.separator + "rxCache");
230 | boolean isSmiley = path.equals(START_PATH + "com.eblog" + File.separator + "cache" + File.separator + "smiley");
231 | boolean isGlide = path.equals(START_PATH + "com.eblog" + File.separator + "cache" + File.separator + "glide");
232 | boolean isOSS = path.equals(START_PATH + "com.eblog" + File.separator + "cache" + File.separator + "oss_record");
233 |
234 | //以下返回必须为true
235 | boolean isQQ = path.startsWith(QQ_PIC_PATH) || path.startsWith(QQ_FILE_PATH);
236 | boolean isWX = path.startsWith(WX_PIC_PATH) || path.startsWith(WX_FILE_PATH);
237 | boolean isSysPic = path.equals(SYS_CAMERA_PATH) || path.equals(SCREENSHOTS_PATH) || path.equals(HW_SCREEN_SAVER_PATH);
238 |
239 | //以下部分返回为true
240 | boolean isTencentPath = path.startsWith(START_PATH + "tencent" + File.separator);
241 |
242 | if (isHidden || isSystem || isRxCache || isSmiley || isGlide || isOSS)
243 | return false;
244 | else if (isTencentPath)
245 | if (isQQ || isWX)
246 | return true;
247 | else
248 | return false;
249 |
250 | else if (isSysPic)
251 | return true;
252 | else
253 | return true;
254 | }
255 | }
256 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/model/Extension.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.model;
2 |
3 | /**
4 | * Created by Z7Dream on 2017/2/13 17:23.
5 | * Email:zhangxyfs@126.com
6 | */
7 |
8 | public class Extension {
9 | public static final String[] PIC = {"jpg", "jpeg", "jpe", "bmp", "png"};
10 | public static final String[] TXT = {"txt"};
11 | public static final String[] EXCEL = {"xls", "xlt", "xlm", "xlsx"};
12 | public static final String[] PPT = {"dps", "dpt", "ppt", "pot", "pps", "pptx"};
13 | public static final String[] WORD = {"wps", "wpt", "doc", "dot", "rtf", "docx", "dotx"};
14 | public static final String[] PDF = {"pdf"};
15 |
16 | public static final String[] AUDIO = {"aac", "mp3", "mid", "wav", "flac", "amr", "m4a", "xmf", "ogg"};
17 | public static final String[] VIDEO = {"3gp", "mp4", "mkv", "ts", "rmvb"};
18 |
19 | public static final String[] ZIP = {"rar", "zip", "7z", "z", "iso", "gz", "tar", "cab", "ace", "apk"};
20 |
21 |
22 | public static void addLike(String which, StringBuilder sb, String... strs) {
23 | for (int i = 0; i < strs.length; i++) {
24 | sb.append(which);
25 | sb.append(" LIKE ");
26 | sb.append("'%.").append(strs[i]).append("'");
27 | sb.append(" OR ");
28 | }
29 | sb.delete(sb.length() - 4, sb.length());
30 | }
31 |
32 | public static void addNotLike(String which, StringBuilder sb, String... strs) {
33 | for (int i = 0; i < strs.length; i++) {
34 | sb.append(which);
35 | sb.append(" NOT LIKE ");
36 | sb.append("'%.").append(strs[i]).append("'");
37 | sb.append(" AND ");
38 | }
39 | sb.delete(sb.length() - 4, sb.length());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/model/MagicFileEntity.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.model;
2 |
3 | /**
4 | * Created by Z7Dream on 2017/2/3 17:45.
5 | * Email:zhangxyfs@126.com
6 | */
7 |
8 | public class MagicFileEntity {
9 | public String name;
10 | public String mimieType;
11 | public long size;
12 | public long time;
13 | public String path;
14 | public String icon;
15 |
16 | public String company;
17 | public Long companyId;
18 | public String userName;
19 | public Long userId;
20 |
21 | public String chatId;
22 | public String chatName;
23 |
24 | public boolean isFile;//是否文件
25 | }
26 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/model/MagicPicEntity1.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.model;
2 |
3 | import java.util.ArrayList;
4 |
5 | /**
6 | * Created by Z7Dream on 2017/5/22 11:19.
7 | * Email:zhangxyfs@126.com
8 | */
9 |
10 | public class MagicPicEntity1 {
11 | public String name;
12 | public String path;
13 |
14 | public String icon;
15 | public int childNum;
16 |
17 | public boolean isNull(){
18 | return name == null || path == null;
19 | }
20 |
21 | public ArrayList childList;
22 | }
23 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/service/FileUpdatingService.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.service;
2 |
3 | import android.app.Service;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.os.IBinder;
7 |
8 | import com.z7dream.lib.db.FileDaoImpl;
9 | import com.z7dream.lib.db.FileDaoManager;
10 | import com.z7dream.lib.db.bean.MyObjectBox;
11 | import com.z7dream.lib.listener.RecursiveFileObserver;
12 | import com.z7dream.lib.tool.CacheManager;
13 | import com.z7dream.lib.tool.Utils;
14 |
15 | import io.objectbox.BoxStore;
16 |
17 | public class FileUpdatingService extends Service {
18 | private RecursiveFileObserver recursiveFileObserver;
19 | private FileDaoImpl fileDaoImpl;
20 | private static BoxStore mBoxStore;
21 |
22 | public FileUpdatingService() {
23 |
24 | }
25 |
26 | @Override
27 | public IBinder onBind(Intent intent) {
28 | return null;
29 | }
30 |
31 | @Override
32 | public void onCreate() {
33 | super.onCreate();
34 | if (mBoxStore != null)
35 | fileDaoImpl = new FileDaoManager(mBoxStore);
36 | else
37 | fileDaoImpl = new FileDaoManager(MyObjectBox.builder().androidContext(getApplicationContext()).build());
38 | //全盘文件夹监听
39 | recursiveFileObserver = new RecursiveFileObserver(CacheManager.getSaveFilePath(), param -> {
40 | fileDaoImpl.toPutFileInStorage(param);
41 | }, fileDaoImpl);
42 | recursiveFileObserver.startWatching();
43 | }
44 |
45 | @Override
46 | public void onDestroy() {
47 | super.onDestroy();
48 | if (recursiveFileObserver != null)
49 | recursiveFileObserver.stopWatching();
50 | if (fileDaoImpl != null)
51 | fileDaoImpl.destory();
52 | }
53 |
54 | public void toUpdateFileInfos() {
55 | if (fileDaoImpl == null) return;
56 | if (fileDaoImpl.isPutFileInStorageSucc()) {
57 |
58 | }
59 | }
60 |
61 | public static void startService(BoxStore boxStore, Context context) {
62 | if (mBoxStore == null && boxStore != null) {
63 | mBoxStore = boxStore;
64 | }
65 |
66 | if (!Utils.isServiceRunning(context, FileUpdatingService.class.getName())) {
67 | context.startService(new Intent(context, FileUpdatingService.class));
68 | }
69 | }
70 |
71 | public static void startService(Context context) {
72 | startService(null, context);
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/tool/CacheManager.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.tool;
2 |
3 | import android.content.Context;
4 | import android.os.Environment;
5 | import android.support.annotation.NonNull;
6 |
7 | import java.io.File;
8 |
9 | /**
10 | * 缓存目录控制
11 | * Created by xiaoyu.zhang on 2016/11/10 14:57
12 | *
13 | */
14 | public class CacheManager {
15 | public static final int DEFAULT = 0x0000;
16 | public static final int APK = 0x00100;
17 | public static final int VOICE = 0x00200;
18 | public static final int VIDEO = 0x00300;
19 | public static final int PIC = 0x00400;
20 | public static final int DB = 0x00500;
21 | public static final int CONFIG = 0x00600;
22 | public static final int CACHE = 0x00700;
23 | public static final int USED = 0x00800;
24 | public static final int GIFT = 0x00900;
25 | public static final int GLIDE = 0x01000;
26 | public static final int CRASH = 0x01100;
27 | public static final int SMILEY = 0x01200;
28 | public static final int FILE = 0x01300;
29 | public static final int RES = 0x01400;
30 | public static final int OSS = 0x01500;
31 | public static final int IM = 0x01600;
32 | public static final int ES = 0x01700;//下属文件夹命名-公司id-文件类型-
33 | public static final int EB_PHOTO = 0x01800;
34 | public static final int COLLECT = 0x01900;
35 | public static final int PIC_TEMP = 0x02000;
36 | public static final int OFF_LINE_CACHE = 0x02001;
37 |
38 | public static final int TXT = 0x01710;
39 | public static final int EXCEL = 0x01720;
40 | public static final int PPT = 0x01730;
41 | public static final int WORD = 0x01740;
42 | public static final int PDF = 0x01750;
43 | public static final int OTHER = 0x01760;
44 | public static final int ES_ALL = 0x01799;
45 |
46 |
47 | private static final String STR_APK = "apk";
48 | private static final String STR_VOICE = "voice";
49 | private static final String STR_VIDEO = "video";
50 | private static final String STR_PIC = "picture";
51 | private static final String STR_DB = "database";
52 | private static final String STR_CFG = "config";
53 | private static final String STR_RX = "rxCache";
54 | private static final String STR_USED = "used";
55 | private static final String STR_GIFT = "gift";
56 | private static final String STR_GLIDE = "glide";
57 | private static final String STR_CRASH = "crash";
58 | private static final String STR_SMILEY = "smiley";
59 | private static final String STR_FILE = "file";
60 | private static final String STR_RES = "res";
61 | private static final String STR_OSS = "oss_record";
62 | private static final String STR_IM = "im";
63 | private static final String STR_ES = "es";
64 | private static final String STR_TXT = "txt";
65 | private static final String STR_EXCEL = "excel";
66 | private static final String STR_PPT = "ppt";
67 | private static final String STR_WORD = "word";
68 | private static final String STR_PDF = "pdf";
69 | private static final String STR_OTHER = "other";
70 | private static final String STR_EB_PHOTO = "EB_photo";
71 | private static final String STR_COLLECT = "collect";
72 | private static final String STR_PIC_TEMP = "picTemp";
73 | private static final String STR_OFF_LINE_CACHE = "offlinecache";
74 |
75 | private static final String NOMEDIA = ".nomedia";
76 |
77 |
78 | /**
79 | * 获取缓存路径
80 | */
81 | public static String getCachePath(Context context) {
82 | String savePath = getSaveFilePath() + File.separator + context.getPackageName() + File.separator + "cache";
83 | File fDir = new File(savePath);
84 | if (!fDir.exists()) {
85 | fDir.mkdirs();
86 | }
87 | return savePath;
88 | }
89 |
90 | public static String getRelativePath(Context context) {
91 | String savePath = getSaveFilePath() + File.separator + context.getPackageName() + File.separator + "cache";
92 | String relatviePath = File.separator + context.getPackageName() + File.separator + "cache";
93 | File fDir = new File(savePath);
94 | if (!fDir.exists()) {
95 | fDir.mkdirs();
96 | }
97 |
98 | return relatviePath;
99 | }
100 |
101 | public static String getCachePath(int which) {
102 | return getCachePath(null, which);
103 | }
104 |
105 | public static String getCachePathNoSepa(int which) {
106 | String path = getCachePath(null, which);
107 | if (path.endsWith(File.separator)) {
108 | return path.substring(0, path.length() - 1);
109 | }
110 | return path;
111 | }
112 |
113 | public static String getCachePath(@NonNull Context context, int which) {
114 | String savePath = "";
115 | savePath = getCachePath(context);
116 | savePath = getSavePath(savePath, which);
117 | String nomediaPath = savePath + NOMEDIA;
118 |
119 | File fDir = new File(savePath);
120 | if (!fDir.exists()) {
121 | fDir.mkdirs();
122 | }
123 | File npDir = new File(nomediaPath);
124 | if (!savePath.contains(STR_EB_PHOTO)) {
125 | if (!npDir.exists()) {
126 | npDir.mkdir();
127 | }
128 | } else {
129 | if (npDir.exists())
130 | npDir.delete();
131 | }
132 | return savePath;
133 | }
134 |
135 | public static String getRelativePath(@NonNull Context context, int which) {
136 | String savePath = "";
137 | savePath = getRelativePath(context);
138 | savePath = getSavePath(savePath, which);
139 | File fDir = new File(savePath);
140 | if (!fDir.exists()) {
141 | fDir.mkdirs();
142 | }
143 | return savePath;
144 | }
145 |
146 | private static String getSavePath(String savePath, int which) {
147 | savePath += File.separator;
148 | if (which == APK) {
149 | savePath += STR_APK;
150 | } else if (which == VOICE) {
151 | savePath += STR_VOICE;
152 | } else if (which == VIDEO) {
153 | savePath += STR_VIDEO;
154 | } else if (which == PIC) {
155 | savePath += STR_PIC;
156 | } else if (which == DB) {
157 | savePath += STR_DB;
158 | } else if (which == CONFIG) {
159 | savePath += STR_CFG;
160 | } else if (which == CACHE) {
161 | savePath += STR_RX;
162 | } else if (which == USED) {
163 | savePath += STR_USED;
164 | } else if (which == GIFT) {
165 | savePath += STR_GIFT;
166 | } else if (which == GLIDE) {
167 | savePath += STR_GLIDE;
168 | } else if (which == CRASH) {
169 | savePath += STR_CRASH;
170 | } else if (which == SMILEY) {
171 | savePath += STR_SMILEY;
172 | } else if (which == FILE) {
173 | savePath += STR_FILE;
174 | } else if (which == RES) {
175 | savePath += STR_RES;
176 | } else if (which == OSS) {
177 | savePath += STR_OSS;
178 | } else if (which == IM) {
179 | savePath += STR_IM;
180 | } else if (which == ES) {
181 | savePath += STR_ES;
182 | } else if (which == TXT) {
183 | savePath += STR_TXT;
184 | } else if (which == EXCEL) {
185 | savePath += STR_EXCEL;
186 | } else if (which == PPT) {
187 | savePath += STR_PPT;
188 | } else if (which == WORD) {
189 | savePath += STR_WORD;
190 | } else if (which == PDF) {
191 | savePath += STR_PDF;
192 | } else if (which == OTHER) {
193 | savePath += STR_OTHER;
194 | } else if (which == ES_ALL) {
195 | savePath = savePath.substring(0, savePath.length() - 1);
196 | } else if (which == EB_PHOTO) {
197 | savePath += STR_EB_PHOTO;
198 | } else if (which == COLLECT) {
199 | savePath += STR_COLLECT;
200 | } else if (which == PIC_TEMP) {
201 | savePath += STR_PIC_TEMP;
202 | } else if (which == OFF_LINE_CACHE) {
203 | savePath += STR_OFF_LINE_CACHE;
204 | }
205 | savePath += File.separator;
206 | return savePath;
207 | }
208 |
209 | /**
210 | * 生成下载文件保存路径
211 | *
212 | * @return
213 | */
214 | public static String getSaveFilePath() {
215 | File file = null;
216 | String rootPath = "";
217 | String status = Environment.getExternalStorageState();
218 | if (status.equals(Environment.MEDIA_MOUNTED)) {
219 | file = Environment.getExternalStorageDirectory();//获取跟目录
220 | rootPath = file.getPath();
221 | }
222 | return rootPath;
223 | }
224 | }
225 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/tool/EnumFileType.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.tool;
2 |
3 | import android.text.TextUtils;
4 |
5 | import com.z7dream.lib.R;
6 |
7 |
8 | /**
9 | * Created by Z7Dream on 2017/7/5 13:15.
10 | * Email:zhangxyfs@126.com
11 | */
12 |
13 | public enum EnumFileType {
14 | PIC,
15 | TXT,
16 | EXCEL,
17 | PPT,
18 | WORD,
19 | PDF,
20 | AUDIO,
21 | VIDEO,
22 | ZIP,
23 | OTHER,
24 | ALL;
25 |
26 | // public static EnumFileType getType(int oldFileType) {
27 | // EnumFileType enumFileType;
28 | // switch (oldFileType) {
29 | // case com.eblog.lib.utils.explorer.FileType.PIC:
30 | // enumFileType = PIC;
31 | // break;
32 | // case com.eblog.lib.utils.explorer.FileType.AUDIO:
33 | // enumFileType = AUDIO;
34 | // break;
35 | // case com.eblog.lib.utils.explorer.FileType.VIDEO:
36 | // enumFileType = VIDEO;
37 | // break;
38 | // case com.eblog.lib.utils.explorer.FileType.TXT:
39 | // enumFileType = TXT;
40 | // break;
41 | // case com.eblog.lib.utils.explorer.FileType.EXCEL:
42 | // enumFileType = EXCEL;
43 | // break;
44 | // case com.eblog.lib.utils.explorer.FileType.PPT:
45 | // enumFileType = PPT;
46 | // break;
47 | // case com.eblog.lib.utils.explorer.FileType.WORD:
48 | // enumFileType = WORD;
49 | // break;
50 | // case com.eblog.lib.utils.explorer.FileType.PDF:
51 | // enumFileType = PDF;
52 | // break;
53 | // default:
54 | // enumFileType = OTHER;
55 | // break;
56 | // }
57 | // return enumFileType;
58 | // }
59 |
60 | public static int getCacheWhich(EnumFileType type) {
61 | int fileType = CacheManager.OTHER;
62 | if (type == EnumFileType.PIC) {
63 | fileType = CacheManager.PIC;
64 | } else if (type == EnumFileType.AUDIO) {
65 | fileType = CacheManager.VOICE;
66 | } else if (type == EnumFileType.VIDEO) {
67 | fileType = CacheManager.VIDEO;
68 | } else if (type == EnumFileType.TXT) {
69 | fileType = CacheManager.TXT;
70 | } else if (type == EnumFileType.EXCEL) {
71 | fileType = CacheManager.EXCEL;
72 | } else if (type == EnumFileType.PPT) {
73 | fileType = CacheManager.PPT;
74 | } else if (type == EnumFileType.WORD) {
75 | fileType = CacheManager.WORD;
76 | } else if (type == EnumFileType.PDF) {
77 | fileType = CacheManager.PDF;
78 | }
79 | return fileType;
80 | }
81 |
82 | public static EnumFileType getEnum(String string) {
83 | if (string != null) {
84 | try {
85 | return Enum.valueOf(EnumFileType.class, string.trim());
86 | } catch (IllegalArgumentException ex) {
87 | }
88 | }
89 | return EnumFileType.OTHER;
90 | }
91 |
92 |
93 | public static int createIconResId(String type, boolean isFile) {
94 | int resId = R.drawable.ic_file_other;
95 | if (isFile) {
96 | if (TextUtils.equals(type, EnumFileType.PIC.name())) {
97 | resId = R.drawable.ic_file_pic;
98 | } else if (TextUtils.equals(type, EnumFileType.AUDIO.name())) {
99 | resId = R.drawable.ic_file_audio;
100 | } else if (TextUtils.equals(type, EnumFileType.VIDEO.name())) {
101 | resId = R.drawable.ic_file_video;
102 | } else if (TextUtils.equals(type, EnumFileType.TXT.name())) {
103 | resId = R.drawable.ic_file_txt;
104 | } else if (TextUtils.equals(type, EnumFileType.EXCEL.name())) {
105 | resId = R.drawable.ic_file_excel;
106 | } else if (TextUtils.equals(type, EnumFileType.PPT.name())) {
107 | resId = R.drawable.ic_file_ppt;
108 | } else if (TextUtils.equals(type, EnumFileType.WORD.name())) {
109 | resId = R.drawable.ic_file_word;
110 | } else if (TextUtils.equals(type, EnumFileType.PDF.name())) {
111 | resId = R.drawable.ic_file_pdf;
112 | }
113 | } else {
114 | resId = R.drawable.ic_file_folder;
115 | }
116 | return resId;
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/tool/FileUtils.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.tool;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.content.ContentUris;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.database.Cursor;
8 | import android.graphics.Bitmap;
9 | import android.graphics.BitmapFactory;
10 | import android.media.MediaScannerConnection;
11 | import android.net.Uri;
12 | import android.os.Build;
13 | import android.os.Environment;
14 | import android.provider.DocumentsContract;
15 | import android.provider.MediaStore;
16 | import android.text.TextUtils;
17 | import android.util.Log;
18 |
19 | import java.io.BufferedOutputStream;
20 | import java.io.BufferedReader;
21 | import java.io.File;
22 | import java.io.FileOutputStream;
23 | import java.io.FileReader;
24 | import java.io.FileWriter;
25 | import java.io.IOException;
26 | import java.io.PrintWriter;
27 | import java.util.Map;
28 |
29 | /**
30 | * Created by Z7Dream on 2017/3/7 9:57.
31 | * Email:zhangxyfs@126.com
32 | */
33 |
34 | public class FileUtils {
35 | /**
36 | * Java文件操作 获取文件扩展名
37 | */
38 | public static String getExtensionName(String filename) {
39 | if ((filename != null) && (filename.length() > 0)) {
40 | int dot = filename.lastIndexOf('.');
41 | if ((dot > -1) && (dot < (filename.length() - 1))) {
42 | return filename.substring(dot + 1).trim();
43 | }
44 | }
45 | return "";
46 | }
47 |
48 | /**
49 | * 获取文件夹名
50 | *
51 | * @param path
52 | * @return
53 | */
54 | public static String getFolderName(String path) {
55 | if (!TextUtils.isEmpty(path)) {
56 | String[] paths = path.split("\\/");
57 | if (paths.length > 0)
58 | return paths[paths.length - 1];
59 | }
60 | return path;
61 | }
62 |
63 | /**
64 | * Java文件操作 获取不带扩展名的文件名
65 | */
66 | public static String getFileNameNoEx(String filename) {
67 | if ((filename != null) && (filename.length() > 0)) {
68 | int dot = filename.lastIndexOf('.');
69 | if ((dot > -1) && (dot < (filename.length()))) {
70 | return filename.substring(0, dot);
71 | }
72 | }
73 | return filename;
74 | }
75 |
76 | //删除指定文件夹下所有文件
77 | //param path 文件夹完整绝对路径
78 | public static boolean delAllFile(String path, Map notDeleteMap) {
79 | boolean flag = false;
80 | File file = new File(path);
81 | if (!file.exists()) {
82 | return flag;
83 | }
84 | if (!file.isDirectory()) {
85 | return flag;
86 | }
87 | String[] tempList = file.list();
88 | File temp;
89 | for (int i = 0; i < tempList.length; i++) {
90 | if (path.endsWith(File.separator)) {
91 | temp = new File(path + tempList[i]);
92 | } else {
93 | temp = new File(path + File.separator + tempList[i]);
94 | }
95 | if (temp.isFile()) {
96 | temp.delete();
97 | }
98 | if (temp.isDirectory() && !temp.getPath().contains(".nomedia") && notDeleteMap.get(temp.getPath()) == null) {
99 | delAllFile(path + "/" + tempList[i], notDeleteMap);//先删除文件夹里面的文件
100 | flag = true;
101 | }
102 | }
103 | return flag;
104 | }
105 |
106 | public static int[] getImageSize(String imagePath) {
107 | int[] res = new int[2];
108 |
109 | BitmapFactory.Options options = new BitmapFactory.Options();
110 | options.inJustDecodeBounds = true;
111 | options.inSampleSize = 1;
112 | BitmapFactory.decodeFile(imagePath, options);
113 |
114 | res[0] = options.outWidth;
115 | res[1] = options.outHeight;
116 | return res;
117 | }
118 |
119 | public static String getPathByUri(Context context, Uri data) {
120 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
121 | return getPathByUri4BeforeKitkat(context, data);
122 | } else {
123 | return getPathByUri4AfterKitkat(context, data);
124 | }
125 | }
126 |
127 | //4.4以前通过Uri获取路径:data是Uri,filename是一个String的字符串,用来保存路径
128 | public static String getPathByUri4BeforeKitkat(Context context, Uri data) {
129 | String filename = null;
130 | if (data.getScheme().toString().compareTo("content") == 0) {
131 | Cursor cursor = context.getContentResolver().query(data, new String[]{"_data"}, null, null, null);
132 | if (cursor.moveToFirst()) {
133 | filename = cursor.getString(0);
134 | }
135 | } else if (data.getScheme().toString().compareTo("file") == 0) {// file:///开头的uri
136 | filename = data.toString().replace("file://", "");// 替换file://
137 | if (!filename.startsWith("/mnt")) {// 加上"/mnt"头
138 | filename += "/mnt";
139 | }
140 | }
141 | return filename;
142 | }
143 |
144 | //4.4以后根据Uri获取路径:
145 | @SuppressLint("NewApi")
146 | public static String getPathByUri4AfterKitkat(final Context context, final Uri uri) {
147 | final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
148 | // DocumentProvider
149 | if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
150 | if (isExternalStorageDocument(uri)) {// ExternalStorageProvider
151 | final String docId = DocumentsContract.getDocumentId(uri);
152 | final String[] split = docId.split(":");
153 | final String type = split[0];
154 | if ("primary".equalsIgnoreCase(type)) {
155 | return Environment.getExternalStorageDirectory() + "/" + split[1];
156 | }
157 | } else if (isDownloadsDocument(uri)) {// DownloadsProvider
158 | final String id = DocumentsContract.getDocumentId(uri);
159 | final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
160 | Long.valueOf(id));
161 | return getDataColumn(context, contentUri, null, null);
162 | } else if (isMediaDocument(uri)) {// MediaProvider
163 | final String docId = DocumentsContract.getDocumentId(uri);
164 | final String[] split = docId.split(":");
165 | final String type = split[0];
166 | Uri contentUri = null;
167 | if ("image".equals(type)) {
168 | contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
169 | } else if ("video".equals(type)) {
170 | contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
171 | } else if ("audio".equals(type)) {
172 | contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
173 | }
174 | final String selection = "_id=?";
175 | final String[] selectionArgs = new String[]{split[1]};
176 | return getDataColumn(context, contentUri, selection, selectionArgs);
177 | }
178 | } else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore
179 | // (and
180 | // general)
181 | return getDataColumn(context, uri, null, null);
182 | } else if ("file".equalsIgnoreCase(uri.getScheme())) {// File
183 | return uri.getPath();
184 | }
185 | return null;
186 | }
187 |
188 | /**
189 | * Get the value of the data column for this Uri. This is useful for
190 | * MediaStore Uris, and other file-based ContentProviders.
191 | *
192 | * @param context The context.
193 | * @param uri The Uri to query.
194 | * @param selection (Optional) Filter used in the query.
195 | * @param selectionArgs (Optional) Selection arguments used in the query.
196 | * @return The value of the _data column, which is typically a file path.
197 | */
198 | public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
199 | Cursor cursor = null;
200 | final String column = "_data";
201 | final String[] projection = {column};
202 | try {
203 | cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
204 | if (cursor != null && cursor.moveToFirst()) {
205 | final int column_index = cursor.getColumnIndexOrThrow(column);
206 | return cursor.getString(column_index);
207 | }
208 | } finally {
209 | if (cursor != null)
210 | cursor.close();
211 | }
212 | return null;
213 | }
214 |
215 | /**
216 | * @param uri The Uri to check.
217 | * @return Whether the Uri authority is ExternalStorageProvider.
218 | */
219 | public static boolean isExternalStorageDocument(Uri uri) {
220 | return "com.android.externalstorage.documents".equals(uri.getAuthority());
221 | }
222 |
223 | /**
224 | * @param uri The Uri to check.
225 | * @return Whether the Uri authority is DownloadsProvider.
226 | */
227 | public static boolean isDownloadsDocument(Uri uri) {
228 | return "com.android.providers.downloads.documents".equals(uri.getAuthority());
229 | }
230 |
231 | /**
232 | * @param uri The Uri to check.
233 | * @return Whether the Uri authority is MediaProvider.
234 | */
235 | public static boolean isMediaDocument(Uri uri) {
236 | return "com.android.providers.media.documents".equals(uri.getAuthority());
237 | }
238 |
239 | public static void saveBitmap(Bitmap bitmap, String savePath) {
240 | File file = new File(savePath);
241 | try {
242 | BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
243 | bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
244 | bos.flush();
245 | bos.close();
246 | } catch (IOException e) {
247 | e.printStackTrace();
248 | }
249 | }
250 |
251 | /**
252 | * 扫描指定文件
253 | *
254 | * @param filePath
255 | */
256 | public static void scanFileAsync(Context context, String filePath) {
257 | MediaScannerConnection.scanFile(context, new String[]{filePath}, null, new MediaScannerConnection.MediaScannerConnectionClient() {
258 | @Override
259 | public void onMediaScannerConnected() {
260 | Log.e("tag", "onMediaScannerConnected");
261 | }
262 |
263 | @Override
264 | public void onScanCompleted(String path, Uri uri) {
265 | Log.e("tag", path);
266 | }
267 | });
268 | }
269 |
270 |
271 | /**
272 | * 扫描指定目录
273 | *
274 | * @param dir
275 | */
276 | public static void scanDirAsync(Context context, String dir) {
277 | Intent scanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_DIR");
278 | scanIntent.setData(Uri.fromFile(new File(dir)));
279 | context.sendBroadcast(scanIntent);
280 | }
281 |
282 | /**
283 | * 重命名文件
284 | */
285 | public static boolean renameFile(String filePath, String newPath) {
286 | if (TextUtils.isEmpty(filePath) || TextUtils.isEmpty(newPath))
287 | return false;
288 |
289 | File file = new File(filePath);
290 | if (file != null) {
291 | return file.renameTo(new File(newPath));
292 | }
293 |
294 | return false;
295 | }
296 |
297 | /**
298 | * 写文件
299 | *
300 | * @param filePath
301 | * @param str
302 | */
303 | public static void writeFile(String filePath, String str) {
304 | FileWriter fileWriter = null;
305 | PrintWriter printWriter = null;
306 |
307 | try {
308 | fileWriter = new FileWriter(filePath);
309 | printWriter = new PrintWriter(fileWriter);
310 | printWriter.write(str);
311 | printWriter.println();
312 | } catch (IOException e) {
313 | e.printStackTrace();
314 | } finally {
315 | try {
316 | if (fileWriter != null)
317 | fileWriter.close();
318 | if (printWriter != null)
319 | printWriter.close();
320 | } catch (IOException e) {
321 | e.printStackTrace();
322 | }
323 | }
324 | }
325 |
326 | public static String readFile(File file) {
327 | BufferedReader reader = null;
328 | StringBuffer stringBuffer = new StringBuffer();
329 | try {
330 | reader = new BufferedReader(new FileReader(file));
331 | String tempString;
332 | while ((tempString = reader.readLine()) != null) {
333 | stringBuffer.append(tempString);
334 | }
335 | reader.close();
336 | } catch (IOException e1) {
337 | e1.printStackTrace();
338 | } finally {
339 | if (reader != null) {
340 | try {
341 | reader.close();
342 | } catch (IOException e1) {
343 | }
344 | }
345 | }
346 | return stringBuffer.toString();
347 | }
348 | }
349 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/tool/MagicExplorer.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.tool;
2 |
3 | import java.io.File;
4 |
5 | /**
6 | * Created by Z7Dream on 2017/7/21 16:59.
7 | * Email:zhangxyfs@126.com
8 | */
9 |
10 | public class MagicExplorer {
11 | public static final String WPS_PATH = CacheManager.getSaveFilePath() + File.separator + "/Android/Data/cn.wps.moffice_eng/.cache/KingsoftOffice/.history/attach_mapping_v1.json";
12 | public static final String QQ_PIC_PATH = CacheManager.getSaveFilePath() + File.separator + "tencent" + File.separator + "QQ_Images";
13 | public static final String QQ_FILE_PATH = CacheManager.getSaveFilePath() + File.separator + "tencent" + File.separator + "QQfile_recv";
14 | public static final String WX_PIC_PATH = CacheManager.getSaveFilePath() + File.separator + "tencent" + File.separator + "MicroMsg" + File.separator + "WeiXin";
15 | public static final String WX_FILE_PATH = CacheManager.getSaveFilePath() + File.separator + "tencent" + File.separator + "MicroMsg" + File.separator + "Download";
16 | public static final String SYS_CAMERA_PATH = CacheManager.getSaveFilePath() + File.separator + "DCIM" + File.separator + "Camera";
17 | public static final String SCREENSHOTS_PATH = CacheManager.getSaveFilePath() + File.separator + "Pictures";
18 | public static final String HW_SCREEN_SAVER_PATH = CacheManager.getSaveFilePath() + File.separator + "MagazineUnlock";
19 | // public static final String ES_PATH = CacheManager.getCachePath(Appli.getContext(), CacheManager.ES);
20 | // public static final String PIC_EBPHOTO_PATH = CacheManager.getCachePath(Appli.getContext(), CacheManager.EB_PHOTO);
21 | }
22 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/tool/Utils.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.tool;
2 |
3 | import android.app.ActivityManager;
4 | import android.content.Context;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * Created by Z7Dream on 2017/7/25 11:43.
10 | * Email:zhangxyfs@126.com
11 | */
12 |
13 | public class Utils {
14 | public static boolean isServiceRunning(Context context, String serviceName) {
15 | boolean isRunning = false;
16 | ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
17 | List serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);
18 | if (serviceList == null || serviceList.size() == 0) return false;
19 | for (int i = 0; i < serviceList.size(); i++) {
20 | if (serviceList.get(i).service.getClassName().equals(serviceName)) {
21 | isRunning = true;
22 | break;
23 | }
24 | }
25 | return isRunning;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/z7dream/lib/tool/rx/RxBus.java:
--------------------------------------------------------------------------------
1 | package com.z7dream.lib.tool.rx;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.util.Log;
5 |
6 | import java.util.ArrayList;
7 | import java.util.Collection;
8 | import java.util.Collections;
9 | import java.util.List;
10 | import java.util.Map;
11 | import java.util.concurrent.ConcurrentHashMap;
12 |
13 | import io.reactivex.Observable;
14 | import io.reactivex.subjects.PublishSubject;
15 | import io.reactivex.subjects.Subject;
16 |
17 | /**
18 | * 无法跨进程
19 | * Created by xiaoyu.zhang on 2015/8/13.
20 | */
21 | public class RxBus {
22 | private static final String TAG = RxBus.class.getSimpleName();
23 | private static volatile RxBus instance;
24 | public static boolean DEBUG = false;
25 |
26 |
27 | public static synchronized RxBus get() {
28 | if (null == instance) {
29 | synchronized (RxBus.class) {
30 | if (null == instance) {
31 | instance = new RxBus();
32 | }
33 | }
34 | }
35 | return instance;
36 | }
37 |
38 | private RxBus() {
39 | }
40 |
41 | private ConcurrentHashMap