ImageDeepDetector.java 54 KB

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