ImageDeepDetector.java 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. package com.datarecovery.master.utils;
  2. import static android.content.Context.POWER_SERVICE;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.net.Uri;
  7. import android.os.CancellationSignal;
  8. import android.os.Environment;
  9. import android.os.PowerManager;
  10. import android.text.TextUtils;
  11. import androidx.databinding.BaseObservable;
  12. import androidx.databinding.Bindable;
  13. import com.atmob.common.crypto.CryptoUtils;
  14. import com.atmob.common.runtime.ContextUtil;
  15. import com.datarecovery.master.BR;
  16. import com.datarecovery.master.utils.xfile.XFile;
  17. import com.datarecovery.master.utils.xfile.XFileSearch;
  18. import com.datarecovery.master.utils.xfile.XPathFile;
  19. import org.reactivestreams.Publisher;
  20. import org.reactivestreams.Subscriber;
  21. import java.io.Closeable;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;
  27. import java.io.RandomAccessFile;
  28. import java.nio.ByteOrder;
  29. import java.nio.MappedByteBuffer;
  30. import java.nio.channels.FileChannel;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.Objects;
  34. import java.util.UUID;
  35. import java.util.concurrent.TimeUnit;
  36. import java.util.zip.Adler32;
  37. import atmob.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
  38. import atmob.reactivex.rxjava3.annotations.NonNull;
  39. import atmob.reactivex.rxjava3.core.BackpressureStrategy;
  40. import atmob.reactivex.rxjava3.core.Flowable;
  41. import atmob.reactivex.rxjava3.core.FlowableOnSubscribe;
  42. import atmob.reactivex.rxjava3.functions.Function;
  43. import atmob.reactivex.rxjava3.schedulers.Schedulers;
  44. public class ImageDeepDetector {
  45. private static final int IMAGE_SUFFIX = 1;
  46. private static final int WECHAT_CACHE = 2;
  47. private static final int GALLERY_CACHE = 3;
  48. private static final int IMG_MAGIC = 4;
  49. private static final int OPPO_GALLERY_CACHE = 5;
  50. private static final int VIVO_GALLERY_CACHE = 6;
  51. private static final int XIAOMI_GALLERY_CACHE = 7;
  52. private static final int MEIZU_GALLERY_CACHE = 8;
  53. private static final int HUAWEI_GALLERY_CACHE = 9;
  54. public static Flowable<List<ImageFile>> detect(Context context) {
  55. return Flowable.create((FlowableOnSubscribe<XFile>) emitter -> {
  56. try {
  57. CancellationSignal cancellationSignal = XFileSearch.searchExternalStorageAsync(context,
  58. new XFileSearch.FileFilter() {
  59. @Override
  60. public boolean acceptFile(XFile file) {
  61. return isAcceptFile(file);
  62. }
  63. @Override
  64. public boolean acceptDirectory(XFile file) {
  65. return isAcceptDirectory(file);
  66. }
  67. @Override
  68. public boolean filterDirectory(XFile file) {
  69. return isFilterDirectory(file);
  70. }
  71. },
  72. new XFileSearch.FileSearchCallback() {
  73. @Override
  74. public void onStart() {
  75. }
  76. @Override
  77. public void onEachFile(XFile file) {
  78. emitter.onNext(file);
  79. }
  80. @Override
  81. public void onFinish() {
  82. emitter.onComplete();
  83. }
  84. });
  85. emitter.setCancellable(cancellationSignal::cancel);
  86. } catch (Exception e) {
  87. emitter.onError(e);
  88. }
  89. }, BackpressureStrategy.BUFFER)
  90. .filter(xFile -> xFile.getTag() != null)
  91. .flatMap((Function<XFile, Publisher<ImageFile>>) xFile -> {
  92. int tag = (int) xFile.getTag();
  93. switch (tag) {
  94. case IMG_MAGIC:
  95. case IMAGE_SUFFIX:
  96. return Flowable.just(new ImageFile(xFile));
  97. case XIAOMI_GALLERY_CACHE:
  98. return Flowable.just(new ImageFile(xFile, ImageFile.CATEGORY_GALLERY));
  99. case WECHAT_CACHE:
  100. return detectWechatCache(context, xFile);
  101. case GALLERY_CACHE:
  102. return detectGalleryCache(context, xFile);
  103. case OPPO_GALLERY_CACHE:
  104. return detectOppoGalleryCache(context, xFile);
  105. case VIVO_GALLERY_CACHE:
  106. return detectVivoGalleryCache(context, xFile);
  107. case MEIZU_GALLERY_CACHE:
  108. return detectMeizuGalleryCache(context, xFile);
  109. case HUAWEI_GALLERY_CACHE:
  110. return detectHuaweiGalleryCache(context, xFile);
  111. default:
  112. return Flowable.empty();
  113. }
  114. })
  115. .buffer(200, TimeUnit.MILLISECONDS)
  116. .filter(imageFiles -> imageFiles != null && imageFiles.size() > 0)
  117. .observeOn(AndroidSchedulers.mainThread())
  118. .doOnSubscribe(subscription -> {
  119. subscription.request(Long.MAX_VALUE);
  120. acquireWakeLock(context);
  121. })
  122. .doOnCancel(() -> releaseWakeLock(context))
  123. .doOnTerminate(() -> releaseWakeLock(context));
  124. }
  125. private static void releaseWakeLock(Context context) {
  126. PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
  127. PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
  128. "ImageDeepDetector::detect");
  129. if (wakeLock.isHeld()) {
  130. wakeLock.release();
  131. }
  132. }
  133. private static void acquireWakeLock(Context context) {
  134. PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
  135. PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
  136. "ImageDeepDetector::detect");
  137. wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/);
  138. }
  139. private static boolean isFilterDirectory(XFile file) {
  140. try {
  141. String path = file.getPath();
  142. if (TextUtils.isEmpty(path)) {
  143. return false;
  144. }
  145. if (path.endsWith(ContextUtil.getContext().getPackageName())) {
  146. return true;
  147. }
  148. if (path.contains("com.kuaishou.nebula") && (path.endsWith("live_gift_store_icon_directory") ||
  149. path.endsWith("magic_finger_resource") || path.endsWith("theme_resource") ||
  150. path.endsWith("magic_emoji_resource") || path.endsWith(".material_library_resource") ||
  151. path.endsWith("sticker_resource") || path.endsWith("preload%2Ficon%2Fcommon") ||
  152. path.endsWith("preload/icon/common") || path.endsWith(".emoji")
  153. )) {
  154. return true;
  155. }
  156. if ((path.contains("com.tencent.mobileqq") || path.endsWith("com.tencent.tim")) && (path.endsWith("qvideo_newvideo_tips") ||
  157. path.endsWith("Gameicon") || path.endsWith("html5") || path.endsWith(".preloaduni") ||
  158. path.endsWith(".apollo")) || path.endsWith("editor%2Fresources") || path.endsWith("editor/resources") ||
  159. path.endsWith("lottie") || path.endsWith(".vaspoke") || path.endsWith("newpoke") ||
  160. path.endsWith("qcircle%2Ffile%2Fdownload") || path.endsWith("qcircle/file/download") ||
  161. path.endsWith("qzone%2Fzip_cache") || path.endsWith("qzone/zip_cache") || path.endsWith("poke") ||
  162. path.endsWith("DoutuRes")) {
  163. return true;
  164. }
  165. if ((path.contains("com.ss.android.article.video") || path.contains("com.ss.android.ugc.aweme")) && (path.endsWith("liveroom") ||
  166. path.endsWith("effect") || path.endsWith("card_3d_res") || path.endsWith("weboffline") || path.endsWith("fantasy_lottie_res")
  167. )) {
  168. return true;
  169. }
  170. if ((path.contains("com.taobao.taobao") || path.contains("com.tmall.wireless")) && (path.endsWith("AVFSCache") ||
  171. path.endsWith("gs_fs"))) {
  172. return true;
  173. }
  174. if ((path.contains("com.baidu.BaiduMap")) && (path.endsWith("sticker"))) {
  175. return true;
  176. }
  177. if ((path.contains("com.eg.android.AlipayGphone")) && (path.endsWith("Sandbox") ||
  178. path.endsWith("emojiFiles"))) {
  179. return true;
  180. }
  181. if ((path.contains("air.tv.douyu.android")) && (path.endsWith("skin_download_dir"))) {
  182. return true;
  183. }
  184. if ((path.contains("com.kugou.android")) && (path.endsWith("kugou/lyric") ||
  185. path.endsWith("kugou%2Flyric"))) {
  186. return true;
  187. }
  188. if ((path.contains("com.ss.android.article.news") || path.contains("com.ss.android.ugc.aweme"))
  189. && (path.endsWith("resources%2Fvariety") || path.endsWith("resources/variety"))) {
  190. return true;
  191. }
  192. if ((path.contains("com.autonavi.minimap") || path.contains("com.amap.android.ams")) && (path.endsWith("sharetrip.taxi") ||
  193. path.endsWith("sharetrip%2Ftaxi") || path.endsWith("httpcache"))) {
  194. return true;
  195. }
  196. if (path.contains("tencent/MobileQQ/doodle_template") || path.endsWith("tencent%2FMobileQQ%2Fdoodle_template")) {
  197. return true;
  198. }
  199. if (path.contains(".mob_ad/.material") || path.endsWith("mob_ad%2F.material")) {
  200. return true;
  201. }
  202. if (path.contains("bddownload/common") || path.endsWith("bddownload%2Fcommon")) {
  203. return true;
  204. }
  205. if (path.contains("Pictures/.gs_fs") || path.endsWith("Pictures%2F.gs_fs")) {
  206. return true;
  207. }
  208. if (path.endsWith("files/amap") || path.endsWith("files%2Famap")) {
  209. return true;
  210. }
  211. if (path.endsWith("ksadsdk")) {
  212. return true;
  213. }
  214. if (path.endsWith("files%2Fbddownload%2Fimg_download") || path.endsWith("files/bddownload/img_download")) {
  215. return true;
  216. }
  217. if (path.endsWith("__MACOSX")) {
  218. return true;
  219. }
  220. } catch (Exception ignore) {
  221. }
  222. return false;
  223. }
  224. private static Flowable<ImageFile> detectHuaweiGalleryCache(Context context, XFile xFile) {
  225. return new HuaweiGalleryCacheDetector(context, xFile)
  226. .subscribeOn(Schedulers.io())
  227. .onErrorComplete();
  228. }
  229. private static Flowable<ImageFile> detectMeizuGalleryCache(Context context, XFile xFile) {
  230. return new GenericImgCollectionDetector(context, xFile, ImageFile.CATEGORY_GALLERY)
  231. .subscribeOn(Schedulers.io())
  232. .onErrorComplete();
  233. }
  234. private static Flowable<ImageFile> detectVivoGalleryCache(Context context, XFile xFile) {
  235. return new GenericImgCollectionDetector(context, xFile, ImageFile.CATEGORY_GALLERY)
  236. .subscribeOn(Schedulers.io())
  237. .onErrorComplete();
  238. }
  239. private static Flowable<ImageFile> detectOppoGalleryCache(Context context, XFile xFile) {
  240. return new GenericImgCollectionDetector(context, xFile, ImageFile.CATEGORY_GALLERY)
  241. .subscribeOn(Schedulers.io())
  242. .onErrorComplete();
  243. }
  244. private static Flowable<ImageFile> detectGalleryCache(Context context, XFile xFile) {
  245. return new GalleryCacheDetector(context, xFile)
  246. .subscribeOn(Schedulers.io())
  247. .onErrorComplete();
  248. }
  249. private static Flowable<ImageFile> detectWechatCache(Context context, XFile xFile) {
  250. return new WechatCacheDetector(context, xFile)
  251. .subscribeOn(Schedulers.io())
  252. .onErrorComplete();
  253. }
  254. private static boolean isAcceptDirectory(XFile file) {
  255. try {
  256. String path = file.getPath();
  257. if (isGalleryCacheDirectory(path)) {
  258. file.setTag(GALLERY_CACHE);
  259. return true;
  260. }
  261. } catch (Exception ignore) {
  262. }
  263. return false;
  264. }
  265. private static boolean isAcceptFile(XFile file) {
  266. try {
  267. if (file.length() == 0) {
  268. return false;
  269. }
  270. } catch (Exception ignore) {
  271. }
  272. try {
  273. String name = file.getName();
  274. if (isImageSuffix(name)) {
  275. file.setTag(IMAGE_SUFFIX);
  276. return true;
  277. }
  278. } catch (Exception ignore) {
  279. }
  280. try {
  281. String path = file.getPath();
  282. if (isWechatCacheFile(path)) {
  283. file.setTag(WECHAT_CACHE);
  284. return true;
  285. }
  286. if (isOppoGalleryCacheFile(path)) {
  287. file.setTag(OPPO_GALLERY_CACHE);
  288. return true;
  289. }
  290. if (isVivoGalleryCacheFile(path)) {
  291. file.setTag(VIVO_GALLERY_CACHE);
  292. return true;
  293. }
  294. if (isXiaomiGalleryCacheFile(path)) {
  295. file.setTag(XIAOMI_GALLERY_CACHE);
  296. return true;
  297. }
  298. if (isMeizuGalleryCacheFile(path)) {
  299. file.setTag(MEIZU_GALLERY_CACHE);
  300. return true;
  301. }
  302. if (isHuaweiGalleryCacheFile(path)) {
  303. file.setTag(HUAWEI_GALLERY_CACHE);
  304. return true;
  305. }
  306. } catch (Exception ignore) {
  307. }
  308. if (hasImgMagic(file)) {
  309. file.setTag(IMG_MAGIC);
  310. return true;
  311. }
  312. return false;
  313. }
  314. private static boolean hasImgMagic(XFile file) {
  315. try (InputStream inputStream = file.newInputStream()) {
  316. byte[] bytes = new byte[8];
  317. if (inputStream.read(bytes) != 8) {
  318. return false;
  319. }
  320. if (bytes[0] == (byte) 0x89 && bytes[1] == (byte) 0x50 && bytes[2] == (byte) 0x4E && bytes[3] == (byte) 0x47
  321. && bytes[4] == (byte) 0x0D && bytes[5] == (byte) 0x0A && bytes[6] == (byte) 0x1A && bytes[7] == (byte) 0x0A) {
  322. // png
  323. return true;
  324. }
  325. boolean hasHeaderMagic = bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xD8 && bytes[2] == (byte) 0xFF; // jpg header
  326. if (!hasHeaderMagic) {
  327. return false;
  328. }
  329. long skip = inputStream.available() - 2;
  330. if (inputStream.skip(skip) != skip) {
  331. return false;
  332. }
  333. if (inputStream.read(bytes) != 2) {
  334. return false;
  335. }
  336. return bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xD9;
  337. } catch (Exception ignore) {
  338. }
  339. return false;
  340. }
  341. private static boolean isGalleryCacheDirectory(String path) {
  342. if (TextUtils.isEmpty(path)) {
  343. return false;
  344. }
  345. return path.contains("com.android.gallery3d%2Fcache") ||
  346. path.contains("com.android.gallery3d/cache");
  347. }
  348. private static boolean isWechatCacheFile(String name) {
  349. if (TextUtils.isEmpty(name)) {
  350. return false;
  351. }
  352. return name.contains("com.tencent.mm%2Fcache%2Fimgcache%2Fcache.data") ||
  353. name.contains("com.tencent.mm/cache/imgcache/cache.data");
  354. }
  355. private static boolean isOppoGalleryCacheFile(String path) {
  356. if (TextUtils.isEmpty(path)) {
  357. return false;
  358. }
  359. if (!path.contains("com.coloros.gallery3d%2Fcache") &&
  360. !path.contains("com.coloros.gallery3d/cache")) {
  361. return false;
  362. }
  363. return path.contains("imgcache") || path.contains("screennailcache")
  364. || path.contains("tilecache");
  365. }
  366. private static boolean isVivoGalleryCacheFile(String path) {
  367. if (TextUtils.isEmpty(path)) {
  368. return false;
  369. }
  370. if (!path.contains("com.vivo.gallery%2Fcache") &&
  371. !path.contains("com.vivo.gallery/cache")) {
  372. return false;
  373. }
  374. return path.contains("imgcache") || path.contains("trackthumbnail_cache");
  375. }
  376. private static boolean isXiaomiGalleryCacheFile(String path) {
  377. if (TextUtils.isEmpty(path)) {
  378. return false;
  379. }
  380. if (!path.contains("com.miui.gallery%2Ffiles%2Fgallery_disk_cache") &&
  381. !path.contains("com.miui.gallery/files/gallery_disk_cache")) {
  382. return false;
  383. }
  384. return path.contains("full_size") || path.contains("small_size");
  385. }
  386. private static boolean isMeizuGalleryCacheFile(String path) {
  387. if (TextUtils.isEmpty(path)) {
  388. return false;
  389. }
  390. if (!path.contains("com.meizu.media.gallery%2Fcache") &&
  391. !path.contains("com.meizu.media.gallery/cache")) {
  392. return false;
  393. }
  394. return path.contains("bestPhotoCache") || path.contains("face_thumbnails")
  395. || path.contains("uri_thumbnail_cache");
  396. }
  397. private static boolean isHuaweiGalleryCacheFile(String path) {
  398. if (TextUtils.isEmpty(path)) {
  399. return false;
  400. }
  401. if (!path.contains("com.huawei.photos%2Ffiles%2Fthumbdb") &&
  402. !path.contains("com.huawei.photos/files/thumbdb")) {
  403. return false;
  404. }
  405. return path.endsWith("photoshare.db");
  406. }
  407. private static boolean isImageSuffix(String name) {
  408. if (TextUtils.isEmpty(name)) {
  409. return false;
  410. }
  411. return name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".png")
  412. || name.endsWith(".gif") || name.endsWith(".bmp") || name.endsWith(".webp")
  413. || name.endsWith(".tiff") || name.endsWith(".psd") || name.endsWith(".svg")
  414. || name.endsWith(".raw") || name.endsWith(".heif") || name.endsWith(".indd");
  415. }
  416. private static boolean bytes2File(byte[] bytes, File file) {
  417. try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
  418. fileOutputStream.write(bytes);
  419. fileOutputStream.flush();
  420. return true;
  421. } catch (Exception e) {
  422. e.printStackTrace();
  423. }
  424. return false;
  425. }
  426. private static boolean bytes2File(List<Byte> bytes, File file) {
  427. try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
  428. for (Byte aByte : bytes) {
  429. fileOutputStream.write(aByte);
  430. }
  431. fileOutputStream.flush();
  432. return true;
  433. } catch (Exception e) {
  434. e.printStackTrace();
  435. }
  436. return false;
  437. }
  438. private static File getDetectedCacheDir(Context context, String domain) {
  439. File cacheDir;
  440. if (Objects.equals(Environment.getExternalStorageState(), Environment.MEDIA_MOUNTED)
  441. && Environment.getExternalStorageDirectory().canWrite()) {
  442. cacheDir = context.getExternalCacheDir();
  443. } else {
  444. cacheDir = context.getCacheDir();
  445. }
  446. File detectedCacheDir = new File(cacheDir, CryptoUtils.HASH.md5(domain));
  447. if (!detectedCacheDir.exists()) {
  448. detectedCacheDir.mkdirs();
  449. }
  450. return detectedCacheDir;
  451. }
  452. private static void clearDetectedCache(Context context, String domain) {
  453. File detectedCacheDir = getDetectedCacheDir(context, domain);
  454. try {
  455. clearDir(detectedCacheDir);
  456. } catch (Exception e) {
  457. e.printStackTrace();
  458. }
  459. }
  460. private static void clearDir(File dir) {
  461. if (dir == null || !dir.exists()) {
  462. return;
  463. }
  464. File[] files = dir.listFiles();
  465. if (files == null || files.length == 0) {
  466. dir.delete();
  467. return;
  468. }
  469. for (File file : files) {
  470. if (file.isDirectory()) {
  471. clearDir(file);
  472. } else {
  473. file.delete();
  474. }
  475. }
  476. dir.delete();
  477. }
  478. public static class ImageFile extends BaseObservable {
  479. public static int CATEGORY_UNKNOWN = -1;
  480. public static int CATEGORY_OTHER = 0;
  481. public static int CATEGORY_QQ = 1;
  482. public static int CATEGORY_WECHAT = 2;
  483. public static int CATEGORY_GALLERY = 3;
  484. private final XFile xFile;
  485. private String name;
  486. private long size;
  487. private String sizeDescribe;
  488. private Uri uri;
  489. private String path;
  490. private int category;
  491. private String fileType;
  492. private long createTime;
  493. private boolean isCheck;
  494. public ImageFile(XFile xFile) {
  495. this(xFile, CATEGORY_UNKNOWN);
  496. }
  497. public ImageFile(XFile xFile, int category) {
  498. this.category = category;
  499. this.xFile = xFile;
  500. try {
  501. this.name = xFile.getName();
  502. } catch (Exception ignore) {
  503. }
  504. try {
  505. this.size = xFile.length();
  506. } catch (Exception ignore) {
  507. }
  508. try {
  509. this.uri = xFile.getUri();
  510. } catch (Exception ignore) {
  511. }
  512. try {
  513. this.path = xFile.getPath();
  514. } catch (Exception ignore) {
  515. }
  516. this.fileType = FileUtil.getImageFileType(name);
  517. this.sizeDescribe = FileUtil.formatShortBytes(this.size);
  518. }
  519. public long getCreateTime() {
  520. if (createTime == 0 && uri != null) {
  521. this.createTime = FileUtil.getFileCreationDateFromUri(ContextUtil.getContext(), uri, path);
  522. }
  523. return createTime;
  524. }
  525. public String getSizeDescribe() {
  526. return sizeDescribe;
  527. }
  528. public String getFileType() {
  529. return fileType;
  530. }
  531. public String getName() {
  532. return name;
  533. }
  534. public long getSize() {
  535. return size;
  536. }
  537. public Uri getUri() {
  538. return uri;
  539. }
  540. @Bindable
  541. public boolean isCheck() {
  542. return isCheck;
  543. }
  544. public void setCheck(boolean check) {
  545. isCheck = check;
  546. notifyPropertyChanged(BR.check);
  547. }
  548. public InputStream newInputStream() throws Exception {
  549. return xFile.newInputStream();
  550. }
  551. public boolean delete() throws Exception {
  552. return xFile.delete();
  553. }
  554. public int getCategory() {
  555. if (category != CATEGORY_UNKNOWN) {
  556. return category;
  557. }
  558. if (isGallery()) {
  559. return CATEGORY_GALLERY;
  560. } else if (isWechat()) {
  561. return CATEGORY_WECHAT;
  562. } else if (isQQ()) {
  563. return CATEGORY_QQ;
  564. } else {
  565. return CATEGORY_OTHER;
  566. }
  567. }
  568. private boolean isQQ() {
  569. if (TextUtils.isEmpty(path)) {
  570. return false;
  571. }
  572. return path.contains("com.tencent.mobileqq") || path.contains("com.tencent.tim");
  573. }
  574. private boolean isWechat() {
  575. if (!TextUtils.isEmpty(path) && path.contains("com.tencent.mm")) {
  576. return true;
  577. }
  578. return false;
  579. }
  580. private boolean isGallery() {
  581. return !TextUtils.isEmpty(path) && (
  582. path.contains("com.android.gallery3d") ||
  583. path.contains("com.coloros.gallery3d") ||
  584. path.contains("com.vivo.gallery") ||
  585. path.contains("com.miui.gallery") ||
  586. path.contains("com.meizu.media.gallery") ||
  587. path.contains("com.oppo.gallery3d") ||
  588. path.contains("com.android.gallery") ||
  589. path.contains("com.huawei.photos") ||
  590. path.contains("DCIM") ||
  591. path.contains("Pictures") ||
  592. path.contains(".RecycleBin")
  593. );
  594. }
  595. }
  596. private static class WechatCacheDetector extends Flowable<ImageFile> {
  597. private static final String CACHE_DOMAIN = "wechat_cache_detector";
  598. private final XFile xFile;
  599. private final Context context;
  600. public WechatCacheDetector(Context context, XFile xFile) {
  601. this.context = context;
  602. this.xFile = xFile;
  603. }
  604. @Override
  605. protected void subscribeActual(@NonNull Subscriber<? super ImageFile> subscriber) {
  606. long lastModified;
  607. try {
  608. lastModified = xFile.lastModified();
  609. } catch (Exception e) {
  610. subscriber.onError(e);
  611. return;
  612. }
  613. if (checkDetectedCache(context, lastModified, subscriber)) {
  614. subscriber.onComplete();
  615. return;
  616. } else {
  617. clearDetectedCache(context, CACHE_DOMAIN);
  618. }
  619. File detectedCacheDir = getDetectedCacheDir(context, CACHE_DOMAIN);
  620. detectedCacheDir = new File(detectedCacheDir, CryptoUtils.HASH.md5(String.valueOf(lastModified)));
  621. if (!detectedCacheDir.exists()) {
  622. detectedCacheDir.mkdirs();
  623. }
  624. try (InputStream inputStream = xFile.newInputStream()) {
  625. ArrayList<Byte> imageBytes = new ArrayList<>();
  626. byte[] buffer = new byte[2048];
  627. int read;
  628. while ((read = inputStream.read(buffer)) != -1) {
  629. for (int i = 0; i < read; i++) {
  630. byte b = buffer[i];
  631. imageBytes.add(b);
  632. if (imageBytes.size() < 2) {
  633. continue;
  634. }
  635. if (imageBytes.size() == 2) {
  636. if (imageBytes.get(0) != (byte) 0xFF || imageBytes.get(1) != (byte) 0xD8) {
  637. imageBytes.remove(0);
  638. }
  639. continue;
  640. }
  641. if (i == read - 1 && inputStream.available() == 0) {
  642. if (imageBytes.get(imageBytes.size() - 2) == (byte) 0xFF && imageBytes.get(imageBytes.size() - 1) == (byte) 0xD9) {
  643. File cache = new File(detectedCacheDir, UUID.randomUUID().toString());
  644. if (cache.createNewFile() && bytes2File(imageBytes, cache)) {
  645. subscriber.onNext(new ImageFile(new XPathFile(context, cache), ImageFile.CATEGORY_WECHAT));
  646. }
  647. }
  648. imageBytes.clear();
  649. } else if (imageBytes.size() >= 6) {
  650. if (imageBytes.get(imageBytes.size() - 1) == (byte) 0xD8
  651. && imageBytes.get(imageBytes.size() - 2) == (byte) 0xFF
  652. && imageBytes.get(imageBytes.size() - 3) == (byte) 0xD9
  653. && imageBytes.get(imageBytes.size() - 4) == (byte) 0xFF
  654. ) {
  655. imageBytes.remove(imageBytes.size() - 1);
  656. imageBytes.remove(imageBytes.size() - 1);
  657. File cache = new File(detectedCacheDir, UUID.randomUUID().toString());
  658. if (cache.createNewFile() && bytes2File(imageBytes, cache)) {
  659. subscriber.onNext(new ImageFile(new XPathFile(context, cache), ImageFile.CATEGORY_WECHAT));
  660. }
  661. imageBytes.clear();
  662. imageBytes.add((byte) 0xFF);
  663. imageBytes.add((byte) 0xD8);
  664. }
  665. }
  666. }
  667. }
  668. subscriber.onComplete();
  669. } catch (Exception e) {
  670. subscriber.onError(e);
  671. }
  672. }
  673. private boolean checkDetectedCache(Context context, long lastModified, Subscriber<? super ImageFile> subscriber) {
  674. File detectedCacheDir = getDetectedCacheDir(context, CACHE_DOMAIN);
  675. File targetCaches = new File(detectedCacheDir, CryptoUtils.HASH.md5(String.valueOf(lastModified)));
  676. File[] files = targetCaches.exists() && targetCaches.isDirectory() ? targetCaches.listFiles() : null;
  677. if (files != null && files.length > 0) {
  678. for (File file : files) {
  679. subscriber.onNext(new ImageFile(new XPathFile(this.context, file), ImageFile.CATEGORY_WECHAT));
  680. }
  681. return true;
  682. }
  683. return false;
  684. }
  685. }
  686. private static class GalleryCacheDetector extends Flowable<ImageFile> {
  687. private static final String CACHE_DOMAIN = "gallery_cache_detector";
  688. private static final int MAGIC_INDEX_FILE = 0xB3273030;
  689. private static final int MAGIC_DATA_FILE = 0xBD248510;
  690. // index header offset
  691. private static final int IH_MAGIC = 0;
  692. private static final int IH_MAX_ENTRIES = 4;
  693. private static final int IH_MAX_BYTES = 8;
  694. private static final int IH_ACTIVE_REGION = 12;
  695. private static final int IH_ACTIVE_ENTRIES = 16;
  696. private static final int IH_ACTIVE_BYTES = 20;
  697. private static final int IH_CHECKSUM = 28;
  698. private static final int INDEX_HEADER_SIZE = 32;
  699. private static final int DATA_HEADER_SIZE = 4;
  700. // blob header offset
  701. private static final int BH_KEY = 0;
  702. private static final int BH_CHECKSUM = 8;
  703. private static final int BH_OFFSET = 12;
  704. private static final int BH_LENGTH = 16;
  705. private static final int BLOB_HEADER_SIZE = 20;
  706. private final XFile galleryCacheDir;
  707. private final byte[] indexHeader;
  708. private final byte[] blobHeader;
  709. private final Adler32 mAdler32 = new Adler32();
  710. private final Context context;
  711. private int mMaxEntries;
  712. private int mMaxBytes;
  713. private int mActiveRegion;
  714. private int mActiveBytes;
  715. private FileChannel mIndexChannel;
  716. private MappedByteBuffer mIndexBuffer;
  717. private RandomAccessFile mIndexFile;
  718. private RandomAccessFile mDataFile0;
  719. private RandomAccessFile mDataFile1;
  720. private RandomAccessFile mActiveDataFile;
  721. private int mActiveHashStart;
  722. private File indexTemp;
  723. private File data0Temp;
  724. private File data1Temp;
  725. public GalleryCacheDetector(Context context, XFile galleryCacheDir) {
  726. this.context = context;
  727. this.galleryCacheDir = galleryCacheDir;
  728. this.indexHeader = new byte[INDEX_HEADER_SIZE];
  729. this.blobHeader = new byte[BLOB_HEADER_SIZE];
  730. }
  731. @Override
  732. protected void subscribeActual(@NonNull Subscriber<? super ImageFile> subscriber) {
  733. XFile[] xFiles;
  734. try {
  735. xFiles = galleryCacheDir.listFiles();
  736. } catch (Exception e) {
  737. subscriber.onError(e);
  738. return;
  739. }
  740. if (xFiles == null || xFiles.length == 0) {
  741. subscriber.onComplete();
  742. return;
  743. }
  744. XFile indexFile = null;
  745. XFile dataFile0 = null;
  746. XFile dataFile1 = null;
  747. for (XFile xFile : xFiles) {
  748. try {
  749. String name = xFile.getName();
  750. if (name.endsWith(".idx")) {
  751. indexFile = xFile;
  752. } else if (name.endsWith(".0")) {
  753. dataFile0 = xFile;
  754. } else if (name.endsWith(".1")) {
  755. dataFile1 = xFile;
  756. }
  757. } catch (Exception e) {
  758. subscriber.onError(e);
  759. }
  760. }
  761. if (indexFile == null || dataFile0 == null || dataFile1 == null) {
  762. subscriber.onComplete();
  763. return;
  764. }
  765. doDetect(indexFile, dataFile0, dataFile1, subscriber);
  766. }
  767. private void doDetect(XFile indexFile, XFile dataFile0, XFile dataFile1, Subscriber<? super ImageFile> subscriber) {
  768. try {
  769. long lastModified;
  770. try {
  771. lastModified = indexFile.lastModified();
  772. } catch (Exception e) {
  773. subscriber.onError(e);
  774. return;
  775. }
  776. if (checkDetectedCache(context, lastModified, subscriber)) {
  777. subscriber.onComplete();
  778. return;
  779. } else {
  780. clearDetectedCache(context, CACHE_DOMAIN);
  781. }
  782. File detectedCacheDir = getDetectedCacheDir(context, CACHE_DOMAIN);
  783. detectedCacheDir = new File(detectedCacheDir, CryptoUtils.HASH.md5(String.valueOf(lastModified)));
  784. if (!detectedCacheDir.exists()) {
  785. detectedCacheDir.mkdirs();
  786. }
  787. loadIndex(indexFile, dataFile0, dataFile1);
  788. for (int i = 0; i < mMaxEntries; i++) {
  789. int offset = mActiveHashStart + i * 12;
  790. long candidateKey = mIndexBuffer.getLong(offset);
  791. try {
  792. LookupRequest lookupRequest = new LookupRequest(candidateKey);
  793. if (!lookup(lookupRequest)) {
  794. continue;
  795. }
  796. byte[] lookup = lookupRequest.buffer;
  797. if (lookup == null) {
  798. continue;
  799. }
  800. byte[] cropData = cropLookup(lookup);
  801. if (cropData == null) {
  802. continue;
  803. }
  804. File cache = new File(detectedCacheDir, UUID.randomUUID().toString());
  805. if (cache.createNewFile() && bytes2File(cropData, cache)) {
  806. subscriber.onNext(new ImageFile(new XPathFile(context, cache), ImageFile.CATEGORY_GALLERY));
  807. }
  808. } catch (Exception ignore) {
  809. }
  810. }
  811. subscriber.onComplete();
  812. } catch (Exception e) {
  813. subscriber.onError(e);
  814. } finally {
  815. closeAll();
  816. deleteTempFiles();
  817. }
  818. }
  819. private boolean checkDetectedCache(Context context, long lastModified, Subscriber<? super ImageFile> subscriber) {
  820. File detectedCacheDir = getDetectedCacheDir(context, CACHE_DOMAIN);
  821. File targetCaches = new File(detectedCacheDir, CryptoUtils.HASH.md5(String.valueOf(lastModified)));
  822. File[] files = targetCaches.exists() && targetCaches.isDirectory() ? targetCaches.listFiles() : null;
  823. if (files != null && files.length > 0) {
  824. for (File file : files) {
  825. subscriber.onNext(new ImageFile(new XPathFile(this.context, file), ImageFile.CATEGORY_GALLERY));
  826. }
  827. return true;
  828. }
  829. return false;
  830. }
  831. private byte[] cropLookup(byte[] lookup) {
  832. for (int i = 0; i < lookup.length; i++) {
  833. if (lookup[i] == (byte) 0xFF && i + 1 < lookup.length && lookup[i + 1] == (byte) 0xD8) {
  834. return crop(lookup, i);
  835. }
  836. }
  837. return null;
  838. }
  839. private byte[] crop(byte[] lookup, int i) {
  840. byte[] crop = new byte[lookup.length - i];
  841. System.arraycopy(lookup, i, crop, 0, crop.length);
  842. return crop;
  843. }
  844. private void loadIndex(XFile indexFile, XFile dataFile0, XFile dataFile1) throws Exception {
  845. checkFileValid(indexFile, dataFile0, dataFile1);
  846. try (InputStream idxIs = indexFile.newInputStream();
  847. InputStream data0Is = dataFile0.newInputStream();
  848. InputStream data1Is = dataFile1.newInputStream()
  849. ) {
  850. indexTemp = createTempFile("index.temp", idxIs);
  851. mIndexFile = new RandomAccessFile(indexTemp, "rw");
  852. data0Temp = createTempFile("data0.temp", data0Is);
  853. mDataFile0 = new RandomAccessFile(data0Temp, "rw");
  854. data1Temp = createTempFile("data1.temp", data1Is);
  855. mDataFile1 = new RandomAccessFile(data1Temp, "rw");
  856. // Map index file to memory
  857. mIndexChannel = mIndexFile.getChannel();
  858. mIndexBuffer = mIndexChannel.map(FileChannel.MapMode.READ_WRITE,
  859. 0, mIndexFile.length());
  860. mIndexBuffer.order(ByteOrder.LITTLE_ENDIAN);
  861. setActiveVariables();
  862. }
  863. }
  864. private void setActiveVariables() throws Exception {
  865. mActiveDataFile = (mActiveRegion == 0) ? mDataFile0 : mDataFile1;
  866. mActiveDataFile.setLength(mActiveBytes);
  867. mActiveDataFile.seek(mActiveBytes);
  868. mActiveHashStart = INDEX_HEADER_SIZE;
  869. if (mActiveRegion != 0) {
  870. mActiveHashStart += mMaxEntries * 12;
  871. }
  872. }
  873. private void checkFileValid(XFile indexFile, XFile dataFile0, XFile dataFile1) throws Exception {
  874. byte[] buf = indexHeader;
  875. try (InputStream inputStream = indexFile.newInputStream()) {
  876. if (inputStream.read(buf) != INDEX_HEADER_SIZE) {
  877. throw new Exception("cannot read header");
  878. }
  879. if (readInt(buf, IH_MAGIC) != MAGIC_INDEX_FILE) {
  880. throw new Exception("cannot read header magic");
  881. }
  882. }
  883. mMaxEntries = readInt(buf, IH_MAX_ENTRIES);
  884. mMaxBytes = readInt(buf, IH_MAX_BYTES);
  885. mActiveRegion = readInt(buf, IH_ACTIVE_REGION);
  886. int mActiveEntries = readInt(buf, IH_ACTIVE_ENTRIES);
  887. mActiveBytes = readInt(buf, IH_ACTIVE_BYTES);
  888. int sum = readInt(buf, IH_CHECKSUM);
  889. if (checkSum(buf, 0, IH_CHECKSUM) != sum) {
  890. throw new Exception("header checksum does not match");
  891. }
  892. // Sanity check
  893. if (mMaxEntries <= 0) {
  894. throw new Exception("invalid max entries");
  895. }
  896. if (mMaxBytes <= 0) {
  897. throw new Exception("invalid max bytes");
  898. }
  899. if (mActiveRegion != 0 && mActiveRegion != 1) {
  900. throw new Exception("invalid active region");
  901. }
  902. if (mActiveEntries < 0 || mActiveEntries > mMaxEntries) {
  903. throw new Exception("invalid active entries");
  904. }
  905. if (mActiveBytes < DATA_HEADER_SIZE || mActiveBytes > mMaxBytes) {
  906. throw new Exception("invalid active bytes");
  907. }
  908. if (indexFile.length() != INDEX_HEADER_SIZE + mMaxEntries * 12 * 2L) {
  909. throw new Exception("invalid index file length");
  910. }
  911. // Make sure data file has magic
  912. byte[] magic = new byte[4];
  913. try (InputStream data0Is = dataFile0.newInputStream()) {
  914. if (data0Is.read(magic) != 4) {
  915. throw new Exception("cannot read data file magic");
  916. }
  917. }
  918. if (readInt(magic, 0) != MAGIC_DATA_FILE) {
  919. throw new Exception("invalid data file magic");
  920. }
  921. try (InputStream data1Is = dataFile1.newInputStream()) {
  922. if (data1Is.read(magic) != 4) {
  923. throw new Exception("cannot read data file magic");
  924. }
  925. }
  926. if (readInt(magic, 0) != MAGIC_DATA_FILE) {
  927. throw new Exception("invalid data file magic");
  928. }
  929. }
  930. private File createTempFile(String fileName, InputStream inputStream) throws Exception {
  931. File tempFile = new File(context.getCacheDir(), fileName);
  932. if (tempFile.exists()) {
  933. tempFile.delete();
  934. }
  935. if (!tempFile.createNewFile()) {
  936. throw new Exception("cannot create temp file");
  937. }
  938. try (OutputStream outputStream = new FileOutputStream(tempFile)) {
  939. copyStream(inputStream, outputStream);
  940. }
  941. return tempFile;
  942. }
  943. public boolean lookup(LookupRequest req) throws IOException {
  944. if (lookupInternal(req.key, mActiveHashStart)) {
  945. return getBlob(mActiveDataFile, mFileOffset, req);
  946. }
  947. return false;
  948. }
  949. private int mFileOffset;
  950. private boolean lookupInternal(long key, int hashStart) {
  951. int slot = (int) (key % mMaxEntries);
  952. if (slot < 0) slot += mMaxEntries;
  953. int slotBegin = slot;
  954. while (true) {
  955. int offset = hashStart + slot * 12;
  956. long candidateKey = mIndexBuffer.getLong(offset);
  957. int candidateOffset = mIndexBuffer.getInt(offset + 8);
  958. if (candidateOffset == 0) {
  959. return false;
  960. } else if (candidateKey == key) {
  961. mFileOffset = candidateOffset;
  962. return true;
  963. } else {
  964. if (++slot >= mMaxEntries) {
  965. slot = 0;
  966. }
  967. if (slot == slotBegin) {
  968. mIndexBuffer.putInt(hashStart + slot * 12 + 8, 0);
  969. }
  970. }
  971. }
  972. }
  973. private boolean getBlob(RandomAccessFile file, int offset,
  974. LookupRequest req) throws IOException {
  975. byte[] header = blobHeader;
  976. long oldPosition = file.getFilePointer();
  977. try {
  978. file.seek(offset);
  979. if (file.read(header) != BLOB_HEADER_SIZE) {
  980. return false;
  981. }
  982. long blobKey = readLong(header, BH_KEY);
  983. if (blobKey == 0) {
  984. return false; // This entry has been cleared.
  985. }
  986. if (blobKey != req.key) {
  987. return false;
  988. }
  989. int sum = readInt(header, BH_CHECKSUM);
  990. int blobOffset = readInt(header, BH_OFFSET);
  991. if (blobOffset != offset) {
  992. return false;
  993. }
  994. int length = readInt(header, BH_LENGTH);
  995. if (length < 0 || length > mMaxBytes - offset - BLOB_HEADER_SIZE) {
  996. return false;
  997. }
  998. if (req.buffer == null || req.buffer.length < length) {
  999. req.buffer = new byte[length];
  1000. }
  1001. byte[] blob = req.buffer;
  1002. req.length = length;
  1003. if (file.read(blob, 0, length) != length) {
  1004. return false;
  1005. }
  1006. return checkSum(blob, 0, length) == sum;
  1007. } catch (Throwable t) {
  1008. return false;
  1009. } finally {
  1010. file.seek(oldPosition);
  1011. }
  1012. }
  1013. static int readInt(byte[] buf, int offset) {
  1014. return (buf[offset] & 0xff)
  1015. | ((buf[offset + 1] & 0xff) << 8)
  1016. | ((buf[offset + 2] & 0xff) << 16)
  1017. | ((buf[offset + 3] & 0xff) << 24);
  1018. }
  1019. static long readLong(byte[] buf, int offset) {
  1020. long result = buf[offset + 7] & 0xff;
  1021. for (int i = 6; i >= 0; i--) {
  1022. result = (result << 8) | (buf[offset + i] & 0xff);
  1023. }
  1024. return result;
  1025. }
  1026. int checkSum(byte[] data, int offset, int nbytes) {
  1027. mAdler32.reset();
  1028. mAdler32.update(data, offset, nbytes);
  1029. return (int) mAdler32.getValue();
  1030. }
  1031. private void copyStream(InputStream is, OutputStream os) throws Exception {
  1032. byte[] buf = new byte[2048];
  1033. int n;
  1034. while ((n = is.read(buf)) > 0) {
  1035. os.write(buf, 0, n);
  1036. }
  1037. os.flush();
  1038. }
  1039. private void closeAll() {
  1040. closeSilently(mIndexChannel);
  1041. closeSilently(mIndexFile);
  1042. closeSilently(mDataFile0);
  1043. closeSilently(mDataFile1);
  1044. }
  1045. private void closeSilently(Closeable c) {
  1046. if (c == null) return;
  1047. try {
  1048. c.close();
  1049. } catch (Throwable t) {
  1050. // do nothing
  1051. }
  1052. }
  1053. private void deleteTempFiles() {
  1054. deleteSilently(indexTemp);
  1055. deleteSilently(data0Temp);
  1056. deleteSilently(data1Temp);
  1057. }
  1058. private void deleteSilently(File tempFile) {
  1059. if (tempFile == null) {
  1060. return;
  1061. }
  1062. try {
  1063. if (tempFile.exists()) {
  1064. tempFile.delete();
  1065. }
  1066. } catch (Throwable t) {
  1067. // do nothing
  1068. }
  1069. }
  1070. public static class LookupRequest {
  1071. public long key; // input: the key to find
  1072. public byte[] buffer; // input/output: the buffer to store the blob
  1073. public int length; // output: the length of the blob
  1074. public LookupRequest(long key) {
  1075. this.key = key;
  1076. }
  1077. }
  1078. }
  1079. private static class GenericImgCollectionDetector extends Flowable<ImageFile> {
  1080. private final int category;
  1081. private String CACHE_DOMAIN = "generic_img_collection_detector";
  1082. private final Context context;
  1083. private final XFile xFile;
  1084. public GenericImgCollectionDetector(Context context, XFile xFile, int category) {
  1085. this.context = context;
  1086. this.xFile = xFile;
  1087. this.category = category;
  1088. }
  1089. @Override
  1090. protected void subscribeActual(@NonNull Subscriber<? super ImageFile> subscriber) {
  1091. long lastModified;
  1092. try {
  1093. lastModified = xFile.lastModified();
  1094. CACHE_DOMAIN += xFile.getName();
  1095. } catch (Exception e) {
  1096. subscriber.onError(e);
  1097. return;
  1098. }
  1099. if (checkDetectedCache(context, lastModified, subscriber)) {
  1100. subscriber.onComplete();
  1101. return;
  1102. } else {
  1103. clearDetectedCache(context, CACHE_DOMAIN);
  1104. }
  1105. File detectedCacheDir = getDetectedCacheDir(context, CACHE_DOMAIN);
  1106. detectedCacheDir = new File(detectedCacheDir, CryptoUtils.HASH.md5(String.valueOf(lastModified)));
  1107. if (!detectedCacheDir.exists()) {
  1108. detectedCacheDir.mkdirs();
  1109. }
  1110. try (InputStream inputStream = xFile.newInputStream()) {
  1111. ArrayList<Byte> imageBytes = new ArrayList<>();
  1112. byte[] buffer = new byte[2048];
  1113. int read;
  1114. while ((read = inputStream.read(buffer)) != -1) {
  1115. for (int i = 0; i < read; i++) {
  1116. byte b = buffer[i];
  1117. imageBytes.add(b);
  1118. if (imageBytes.size() < 3) {
  1119. continue;
  1120. }
  1121. if (imageBytes.size() == 3) {
  1122. if (imageBytes.get(0) != (byte) 0xFF || imageBytes.get(1) != (byte) 0xD8 || imageBytes.get(2) != (byte) 0xFF) {
  1123. imageBytes.remove(0);
  1124. }
  1125. continue;
  1126. }
  1127. if (i == read - 1 && inputStream.available() == 0) {
  1128. if (imageBytes.get(imageBytes.size() - 2) == (byte) 0xFF && imageBytes.get(imageBytes.size() - 1) == (byte) 0xD9) {
  1129. File cache = new File(detectedCacheDir, UUID.randomUUID().toString());
  1130. if (cache.createNewFile() && bytes2File(imageBytes, cache)) {
  1131. subscriber.onNext(new ImageFile(new XPathFile(context, cache), category));
  1132. }
  1133. }
  1134. imageBytes.clear();
  1135. } else if (imageBytes.size() >= 5) {
  1136. if (imageBytes.get(imageBytes.size() - 1) == (byte) 0xD9
  1137. && imageBytes.get(imageBytes.size() - 2) == (byte) 0xFF
  1138. ) {
  1139. File cache = new File(detectedCacheDir, UUID.randomUUID().toString());
  1140. if (cache.createNewFile() && bytes2File(imageBytes, cache)) {
  1141. subscriber.onNext(new ImageFile(new XPathFile(context, cache), category));
  1142. }
  1143. imageBytes.clear();
  1144. }
  1145. }
  1146. }
  1147. }
  1148. subscriber.onComplete();
  1149. } catch (Exception e) {
  1150. subscriber.onError(e);
  1151. }
  1152. }
  1153. private boolean checkDetectedCache(Context context, long lastModified, Subscriber<? super ImageFile> subscriber) {
  1154. File detectedCacheDir = getDetectedCacheDir(context, CACHE_DOMAIN);
  1155. File targetCaches = new File(detectedCacheDir, CryptoUtils.HASH.md5(String.valueOf(lastModified)));
  1156. File[] files = targetCaches.exists() && targetCaches.isDirectory() ? targetCaches.listFiles() : null;
  1157. if (files != null && files.length > 0) {
  1158. for (File file : files) {
  1159. subscriber.onNext(new ImageFile(new XPathFile(this.context, file), category));
  1160. }
  1161. return true;
  1162. }
  1163. return false;
  1164. }
  1165. }
  1166. private static class HuaweiGalleryCacheDetector extends Flowable<ImageFile> {
  1167. private static final String CACHE_DOMAIN = "huawei_gallery_cache_detector";
  1168. private final Context context;
  1169. private final XFile dbFile;
  1170. public HuaweiGalleryCacheDetector(Context context, XFile dbFile) {
  1171. this.context = context;
  1172. this.dbFile = dbFile;
  1173. }
  1174. @Override
  1175. protected void subscribeActual(@NonNull Subscriber<? super ImageFile> subscriber) {
  1176. long lastModified;
  1177. try {
  1178. lastModified = dbFile.lastModified();
  1179. } catch (Exception e) {
  1180. subscriber.onError(e);
  1181. return;
  1182. }
  1183. if (checkDetectedCache(context, lastModified, subscriber)) {
  1184. subscriber.onComplete();
  1185. return;
  1186. } else {
  1187. clearDetectedCache(context, CACHE_DOMAIN);
  1188. }
  1189. File detectedCacheDir = getDetectedCacheDir(context, CACHE_DOMAIN);
  1190. detectedCacheDir = new File(detectedCacheDir, CryptoUtils.HASH.md5(String.valueOf(lastModified)));
  1191. if (!detectedCacheDir.exists()) {
  1192. detectedCacheDir.mkdirs();
  1193. }
  1194. File dbTempFile;
  1195. try {
  1196. dbTempFile = createDbTempFile(detectedCacheDir);
  1197. } catch (Exception e) {
  1198. subscriber.onError(e);
  1199. return;
  1200. }
  1201. try (SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(dbTempFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
  1202. Cursor cursor = sqLiteDatabase.rawQuery("select * from general_kv", null)
  1203. ) {
  1204. int vIndex = cursor.getColumnIndex("v");
  1205. if (vIndex == -1) {
  1206. subscriber.onComplete();
  1207. return;
  1208. }
  1209. while (cursor.moveToNext()) {
  1210. byte[] data = cursor.getBlob(vIndex);
  1211. if (data == null || data.length == 0) {
  1212. continue;
  1213. }
  1214. File cache = new File(detectedCacheDir, UUID.randomUUID().toString());
  1215. if (cache.createNewFile() && bytes2File(data, cache)) {
  1216. subscriber.onNext(new ImageFile(new XPathFile(context, cache), ImageFile.CATEGORY_GALLERY));
  1217. }
  1218. }
  1219. subscriber.onComplete();
  1220. } catch (Exception e) {
  1221. subscriber.onError(e);
  1222. } finally {
  1223. if (dbTempFile != null && dbTempFile.exists()) {
  1224. dbTempFile.delete();
  1225. }
  1226. }
  1227. }
  1228. private File createDbTempFile(File detectedCacheDir) throws Exception {
  1229. File dbTempFile = new File(detectedCacheDir, "huawei_gallery_cache_detector.db");
  1230. if (dbTempFile.exists()) {
  1231. dbTempFile.delete();
  1232. }
  1233. try (InputStream inputStream = dbFile.newInputStream();
  1234. OutputStream outputStream = new FileOutputStream(dbTempFile)
  1235. ) {
  1236. byte[] buffer = new byte[2048];
  1237. int read;
  1238. while ((read = inputStream.read(buffer)) != -1) {
  1239. outputStream.write(buffer, 0, read);
  1240. }
  1241. outputStream.flush();
  1242. return dbTempFile;
  1243. }
  1244. }
  1245. private boolean checkDetectedCache(Context context, long lastModified, Subscriber<? super ImageFile> subscriber) {
  1246. File detectedCacheDir = getDetectedCacheDir(context, CACHE_DOMAIN);
  1247. File targetCaches = new File(detectedCacheDir, CryptoUtils.HASH.md5(String.valueOf(lastModified)));
  1248. File[] files = targetCaches.exists() && targetCaches.isDirectory() ? targetCaches.listFiles() : null;
  1249. if (files != null && files.length > 0) {
  1250. for (File file : files) {
  1251. if (file.getName().endsWith(".db")) {
  1252. continue;
  1253. }
  1254. subscriber.onNext(new ImageFile(new XPathFile(this.context, file), ImageFile.CATEGORY_GALLERY));
  1255. }
  1256. return true;
  1257. }
  1258. return false;
  1259. }
  1260. }
  1261. }