PreviewImageFragment.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * @author Chris Narkiewicz
  6. *
  7. * Copyright (C) 2015 ownCloud Inc.
  8. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android.ui.preview;
  23. import android.app.Activity;
  24. import android.content.Context;
  25. import android.content.res.Configuration;
  26. import android.content.res.Resources;
  27. import android.graphics.Bitmap;
  28. import android.graphics.Color;
  29. import android.graphics.Point;
  30. import android.graphics.drawable.BitmapDrawable;
  31. import android.graphics.drawable.Drawable;
  32. import android.graphics.drawable.LayerDrawable;
  33. import android.graphics.drawable.PictureDrawable;
  34. import android.os.AsyncTask;
  35. import android.os.Bundle;
  36. import android.os.Process;
  37. import android.text.SpannableString;
  38. import android.text.style.ForegroundColorSpan;
  39. import android.util.DisplayMetrics;
  40. import android.view.LayoutInflater;
  41. import android.view.Menu;
  42. import android.view.MenuInflater;
  43. import android.view.MenuItem;
  44. import android.view.View;
  45. import android.view.ViewGroup;
  46. import android.widget.FrameLayout;
  47. import android.widget.ImageView;
  48. import android.widget.LinearLayout;
  49. import com.caverock.androidsvg.SVG;
  50. import com.caverock.androidsvg.SVGParseException;
  51. import com.github.chrisbanes.photoview.PhotoView;
  52. import com.google.android.material.snackbar.Snackbar;
  53. import com.nextcloud.client.account.User;
  54. import com.nextcloud.client.account.UserAccountManager;
  55. import com.nextcloud.client.di.Injectable;
  56. import com.nextcloud.client.network.ConnectivityService;
  57. import com.owncloud.android.MainApp;
  58. import com.owncloud.android.R;
  59. import com.owncloud.android.databinding.PreviewImageFragmentBinding;
  60. import com.owncloud.android.datamodel.OCFile;
  61. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  62. import com.owncloud.android.files.FileMenuFilter;
  63. import com.owncloud.android.lib.common.utils.Log_OC;
  64. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
  65. import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
  66. import com.owncloud.android.ui.fragment.FileFragment;
  67. import com.owncloud.android.utils.BitmapUtils;
  68. import com.owncloud.android.utils.DisplayUtils;
  69. import com.owncloud.android.utils.MimeType;
  70. import com.owncloud.android.utils.MimeTypeUtil;
  71. import java.io.FileInputStream;
  72. import java.io.FileNotFoundException;
  73. import java.io.IOException;
  74. import java.lang.ref.WeakReference;
  75. import javax.annotation.Nullable;
  76. import javax.inject.Inject;
  77. import androidx.annotation.NonNull;
  78. import androidx.annotation.StringRes;
  79. import androidx.core.content.res.ResourcesCompat;
  80. import androidx.fragment.app.FragmentStatePagerAdapter;
  81. import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
  82. import pl.droidsonroids.gif.GifDrawable;
  83. import static com.owncloud.android.datamodel.ThumbnailsCacheManager.PREFIX_THUMBNAIL;
  84. /**
  85. * This fragment shows a preview of a downloaded image.
  86. *
  87. * Trying to get an instance with a NULL {@link OCFile} will produce an
  88. * {@link IllegalStateException}.
  89. *
  90. * If the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on
  91. * instantiation too.
  92. */
  93. public class PreviewImageFragment extends FileFragment implements Injectable {
  94. private static final String EXTRA_FILE = "FILE";
  95. private static final String EXTRA_ZOOM = "ZOOM";
  96. private static final String ARG_FILE = "FILE";
  97. private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST";
  98. private static final String ARG_SHOW_RESIZED_IMAGE = "SHOW_RESIZED_IMAGE";
  99. private static final String MIME_TYPE_PNG = "image/png";
  100. private static final String MIME_TYPE_GIF = "image/gif";
  101. private static final String MIME_TYPE_SVG = "image/svg+xml";
  102. private Boolean showResizedImage;
  103. private Bitmap bitmap;
  104. private static final String TAG = PreviewImageFragment.class.getSimpleName();
  105. private boolean ignoreFirstSavedState;
  106. private LoadBitmapTask loadBitmapTask;
  107. @Inject ConnectivityService connectivityService;
  108. @Inject UserAccountManager accountManager;
  109. private PreviewImageFragmentBinding binding;
  110. /**
  111. * Public factory method to create a new fragment that previews an image.
  112. *
  113. * Android strongly recommends keep the empty constructor of fragments as the only public
  114. * constructor, and
  115. * use {@link #setArguments(Bundle)} to set the needed arguments.
  116. *
  117. * This method hides to client objects the need of doing the construction in two steps.
  118. *
  119. * @param imageFile An {@link OCFile} to preview as an image in the fragment
  120. * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of
  121. * {@link FragmentStatePagerAdapter}
  122. * ; TODO better solution
  123. */
  124. public static PreviewImageFragment newInstance(@NonNull OCFile imageFile,
  125. boolean ignoreFirstSavedState,
  126. boolean showResizedImage) {
  127. PreviewImageFragment frag = new PreviewImageFragment();
  128. frag.showResizedImage = showResizedImage;
  129. Bundle args = new Bundle();
  130. args.putParcelable(ARG_FILE, imageFile);
  131. args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState);
  132. args.putBoolean(ARG_SHOW_RESIZED_IMAGE, showResizedImage);
  133. frag.setArguments(args);
  134. return frag;
  135. }
  136. /**
  137. * Creates an empty fragment for image previews.
  138. *
  139. * MUST BE KEPT: the system uses it when tries to re-instantiate a fragment automatically
  140. * (for instance, when the device is turned a aside).
  141. *
  142. * DO NOT CALL IT: an {@link OCFile} and {@link User} must be provided for a successful
  143. * construction
  144. */
  145. public PreviewImageFragment() {
  146. ignoreFirstSavedState = false;
  147. }
  148. @Override
  149. public void onCreate(Bundle savedInstanceState) {
  150. super.onCreate(savedInstanceState);
  151. Bundle args = getArguments();
  152. if (args == null) {
  153. throw new IllegalArgumentException("Arguments may not be null!");
  154. }
  155. setFile(args.getParcelable(ARG_FILE));
  156. // TODO better in super, but needs to check ALL the class extending FileFragment;
  157. // not right now
  158. ignoreFirstSavedState = args.getBoolean(ARG_IGNORE_FIRST);
  159. showResizedImage = args.getBoolean(ARG_SHOW_RESIZED_IMAGE);
  160. setHasOptionsMenu(true);
  161. }
  162. @Override
  163. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  164. super.onCreateView(inflater, container, savedInstanceState);
  165. binding = PreviewImageFragmentBinding.inflate(inflater, container, false);
  166. View view = binding.getRoot();
  167. binding.image.setVisibility(View.GONE);
  168. view.setOnClickListener(v -> togglePreviewImageFullScreen());
  169. binding.image.setOnClickListener(v -> togglePreviewImageFullScreen());
  170. setMultiListLoadingMessage();
  171. return view;
  172. }
  173. /**
  174. * {@inheritDoc}
  175. */
  176. @Override
  177. public void onActivityCreated(Bundle savedInstanceState) {
  178. super.onActivityCreated(savedInstanceState);
  179. if (savedInstanceState != null) {
  180. if (!ignoreFirstSavedState) {
  181. OCFile file = savedInstanceState.getParcelable(EXTRA_FILE);
  182. setFile(file);
  183. binding.image.setScale(Math.min(binding.image.getMaximumScale(), savedInstanceState.getFloat(EXTRA_ZOOM)));
  184. } else {
  185. ignoreFirstSavedState = false;
  186. }
  187. }
  188. }
  189. @Override
  190. public void onSaveInstanceState(@NonNull Bundle outState) {
  191. super.onSaveInstanceState(outState);
  192. outState.putFloat(EXTRA_ZOOM, binding.image.getScale());
  193. outState.putParcelable(EXTRA_FILE, getFile());
  194. }
  195. @Override
  196. public void onStart() {
  197. super.onStart();
  198. if (getFile() != null) {
  199. binding.image.setTag(getFile().getFileId());
  200. Point screenSize = DisplayUtils.getScreenSize(getActivity());
  201. int width = screenSize.x;
  202. int height = screenSize.y;
  203. // show thumbnail while loading image
  204. binding.image.setVisibility(View.GONE);
  205. binding.emptyListProgress.setVisibility(View.VISIBLE);
  206. Bitmap thumbnail = getThumbnailBitmap(getFile());
  207. if (thumbnail != null) {
  208. binding.shimmer.setVisibility(View.VISIBLE);
  209. binding.shimmerThumbnail.setImageBitmap(thumbnail);
  210. binding.image.setVisibility(View.GONE);
  211. bitmap = thumbnail;
  212. } else {
  213. thumbnail = ThumbnailsCacheManager.mDefaultImg;
  214. }
  215. if (showResizedImage) {
  216. Bitmap resizedImage = getResizedBitmap(getFile(), width, height);
  217. if (resizedImage != null && !getFile().isUpdateThumbnailNeeded()) {
  218. binding.image.setImageBitmap(resizedImage);
  219. binding.image.setVisibility(View.VISIBLE);
  220. binding.emptyListView.setVisibility(View.GONE);
  221. binding.emptyListProgress.setVisibility(View.GONE);
  222. binding.image.setBackgroundColor(getResources().getColor(R.color.background_color_inverse));
  223. bitmap = resizedImage;
  224. } else {
  225. // generate new resized image
  226. if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(getFile(), binding.image) &&
  227. containerActivity.getStorageManager() != null) {
  228. final ThumbnailsCacheManager.ResizedImageGenerationTask task =
  229. new ThumbnailsCacheManager.ResizedImageGenerationTask(this,
  230. binding.image,
  231. binding.emptyListProgress,
  232. containerActivity.getStorageManager(),
  233. connectivityService,
  234. containerActivity.getStorageManager().getUser(),
  235. getResources().getColor(R.color.background_color_inverse)
  236. );
  237. if (resizedImage == null) {
  238. resizedImage = thumbnail;
  239. }
  240. final ThumbnailsCacheManager.AsyncResizedImageDrawable asyncDrawable =
  241. new ThumbnailsCacheManager.AsyncResizedImageDrawable(
  242. MainApp.getAppContext().getResources(),
  243. resizedImage,
  244. task
  245. );
  246. binding.image.setImageDrawable(asyncDrawable);
  247. task.execute(getFile());
  248. }
  249. }
  250. } else {
  251. loadBitmapTask = new LoadBitmapTask(binding.image, binding.emptyListView, binding.emptyListProgress);
  252. binding.image.setVisibility(View.GONE);
  253. binding.emptyListView.setVisibility(View.GONE);
  254. binding.emptyListProgress.setVisibility(View.VISIBLE);
  255. loadBitmapTask.execute(getFile());
  256. }
  257. } else {
  258. showErrorMessage(R.string.preview_image_error_no_local_file);
  259. }
  260. }
  261. private @Nullable
  262. Bitmap getResizedBitmap(OCFile file, int width, int height) {
  263. Bitmap cachedImage = null;
  264. int scaledWidth = width;
  265. int scaledHeight = height;
  266. for (int i = 0; i < 3 && cachedImage == null; i++) {
  267. try {
  268. cachedImage = ThumbnailsCacheManager.getScaledBitmapFromDiskCache(
  269. ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + file.getRemoteId(),
  270. scaledWidth,
  271. scaledHeight);
  272. } catch (OutOfMemoryError e) {
  273. scaledWidth = scaledWidth / 2;
  274. scaledHeight = scaledHeight / 2;
  275. }
  276. }
  277. return cachedImage;
  278. }
  279. private @Nullable
  280. Bitmap getThumbnailBitmap(OCFile file) {
  281. return ThumbnailsCacheManager.getBitmapFromDiskCache(PREFIX_THUMBNAIL + file.getRemoteId());
  282. }
  283. @Override
  284. public void onStop() {
  285. Log_OC.d(TAG, "onStop starts");
  286. if (loadBitmapTask != null) {
  287. loadBitmapTask.cancel(true);
  288. loadBitmapTask = null;
  289. }
  290. super.onStop();
  291. }
  292. /**
  293. * {@inheritDoc}
  294. */
  295. @Override
  296. public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
  297. super.onCreateOptionsMenu(menu, inflater);
  298. inflater.inflate(R.menu.item_file, menu);
  299. int nightModeFlag = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
  300. if (Configuration.UI_MODE_NIGHT_NO == nightModeFlag) {
  301. for (int i = 0; i < menu.size(); i++) {
  302. MenuItem menuItem = menu.getItem(i);
  303. SpannableString spanString = new SpannableString(menuItem.getTitle().toString());
  304. spanString.setSpan(new ForegroundColorSpan(Color.BLACK), 0, spanString.length(), 0);
  305. menuItem.setTitle(spanString);
  306. }
  307. }
  308. }
  309. /**
  310. * {@inheritDoc}
  311. */
  312. @Override
  313. public void onPrepareOptionsMenu(@NonNull Menu menu) {
  314. super.onPrepareOptionsMenu(menu);
  315. if (containerActivity.getStorageManager() != null && getFile() != null) {
  316. // Update the file
  317. setFile(containerActivity.getStorageManager().getFileById(getFile().getFileId()));
  318. User currentUser = accountManager.getUser();
  319. FileMenuFilter mf = new FileMenuFilter(
  320. getFile(),
  321. containerActivity,
  322. getActivity(),
  323. false,
  324. currentUser
  325. );
  326. mf.filter(menu, true);
  327. }
  328. // additional restriction for this fragment
  329. // TODO allow renaming in PreviewImageFragment
  330. // TODO allow refresh file in PreviewImageFragment
  331. FileMenuFilter.hideMenuItems(
  332. menu.findItem(R.id.action_rename_file),
  333. menu.findItem(R.id.action_sync_file),
  334. menu.findItem(R.id.action_select_all),
  335. menu.findItem(R.id.action_move),
  336. menu.findItem(R.id.action_copy),
  337. menu.findItem(R.id.action_favorite),
  338. menu.findItem(R.id.action_unset_favorite)
  339. );
  340. if (getFile().isSharedWithMe() && !getFile().canReshare()) {
  341. FileMenuFilter.hideMenuItem(menu.findItem(R.id.action_send_share_file));
  342. }
  343. }
  344. /**
  345. * {@inheritDoc}
  346. */
  347. @Override
  348. public boolean onOptionsItemSelected(MenuItem item) {
  349. int itemId = item.getItemId();
  350. if (itemId == R.id.action_send_share_file) {
  351. if (getFile().isSharedWithMe() && !getFile().canReshare()) {
  352. Snackbar.make(requireView(),
  353. R.string.resharing_is_not_allowed,
  354. Snackbar.LENGTH_LONG
  355. )
  356. .show();
  357. } else {
  358. containerActivity.getFileOperationsHelper().sendShareFile(getFile());
  359. }
  360. return true;
  361. } else if (itemId == R.id.action_open_file_with) {
  362. openFile();
  363. return true;
  364. } else if (itemId == R.id.action_remove_file) {
  365. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(getFile());
  366. dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
  367. return true;
  368. } else if (itemId == R.id.action_see_details) {
  369. seeDetails();
  370. return true;
  371. } else if (itemId == R.id.action_download_file || itemId == R.id.action_sync_file) {
  372. containerActivity.getFileOperationsHelper().syncFile(getFile());
  373. return true;
  374. } else if (itemId == R.id.action_set_as_wallpaper) {
  375. containerActivity.getFileOperationsHelper().setPictureAs(getFile(), getImageView());
  376. return true;
  377. }
  378. return super.onOptionsItemSelected(item);
  379. }
  380. private void seeDetails() {
  381. containerActivity.showDetails(getFile());
  382. }
  383. @SuppressFBWarnings("Dm")
  384. @Override
  385. public void onDestroy() {
  386. if (bitmap != null) {
  387. bitmap.recycle();
  388. // putting this in onStop() is just the same; the fragment is always destroyed by
  389. // {@link FragmentStatePagerAdapter} when the fragment in swiped further than the
  390. // valid offscreen distance, and onStop() is never called before than that
  391. }
  392. super.onDestroy();
  393. }
  394. /**
  395. * Opens the previewed image with an external application.
  396. */
  397. private void openFile() {
  398. containerActivity.getFileOperationsHelper().openFile(getFile());
  399. finish();
  400. }
  401. private class LoadBitmapTask extends AsyncTask<OCFile, Void, LoadImage> {
  402. private static final int PARAMS_LENGTH = 1;
  403. /**
  404. * Weak reference to the target {@link ImageView} where the bitmap will be loaded into.
  405. *
  406. * Using a weak reference will avoid memory leaks if the target ImageView is retired from
  407. * memory before the load finishes.
  408. */
  409. private final WeakReference<PhotoView> imageViewRef;
  410. private final WeakReference<LinearLayout> infoViewRef;
  411. private final WeakReference<FrameLayout> progressViewRef;
  412. /**
  413. * Error message to show when a load fails.
  414. */
  415. private int mErrorMessageId;
  416. /**
  417. * Constructor.
  418. *
  419. * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
  420. */
  421. LoadBitmapTask(PhotoView imageView, LinearLayout infoView, FrameLayout progressView) {
  422. imageViewRef = new WeakReference<>(imageView);
  423. infoViewRef = new WeakReference<>(infoView);
  424. progressViewRef = new WeakReference<>(progressView);
  425. }
  426. @Override
  427. protected LoadImage doInBackground(OCFile... params) {
  428. Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE);
  429. if (params.length != PARAMS_LENGTH) {
  430. return null;
  431. }
  432. Bitmap bitmapResult = null;
  433. Drawable drawableResult = null;
  434. OCFile ocFile = params[0];
  435. String storagePath = ocFile.getStoragePath();
  436. try {
  437. int maxDownScale = 3; // could be a parameter passed to doInBackground(...)
  438. Point screenSize = DisplayUtils.getScreenSize(getActivity());
  439. int minWidth = screenSize.x;
  440. int minHeight = screenSize.y;
  441. for (int i = 0; i < maxDownScale && bitmapResult == null && drawableResult == null; i++) {
  442. if (MIME_TYPE_SVG.equalsIgnoreCase(ocFile.getMimeType())) {
  443. if (isCancelled()) {
  444. return null;
  445. }
  446. try {
  447. SVG svg = SVG.getFromInputStream(new FileInputStream(storagePath));
  448. drawableResult = new PictureDrawable(svg.renderToPicture());
  449. if (isCancelled()) {
  450. return new LoadImage(null, drawableResult, ocFile);
  451. }
  452. } catch (FileNotFoundException e) {
  453. mErrorMessageId = R.string.common_error_unknown;
  454. Log_OC.e(TAG, "File not found trying to load " + getFile().getStoragePath(), e);
  455. } catch (SVGParseException e) {
  456. mErrorMessageId = R.string.common_error_unknown;
  457. Log_OC.e(TAG, "Couldn't parse SVG " + getFile().getStoragePath(), e);
  458. }
  459. } else {
  460. if (isCancelled()) {
  461. return null;
  462. }
  463. try {
  464. bitmapResult = BitmapUtils.decodeSampledBitmapFromFile(storagePath, minWidth,
  465. minHeight);
  466. if (isCancelled()) {
  467. return new LoadImage(bitmapResult, null, ocFile);
  468. }
  469. if (bitmapResult == null) {
  470. mErrorMessageId = R.string.preview_image_error_unknown_format;
  471. Log_OC.e(TAG, "File could not be loaded as a bitmap: " + storagePath);
  472. break;
  473. } else {
  474. if (MimeType.JPEG.equalsIgnoreCase(ocFile.getMimeType())) {
  475. // Rotate image, obeying exif tag.
  476. bitmapResult = BitmapUtils.rotateImage(bitmapResult, storagePath);
  477. }
  478. }
  479. } catch (OutOfMemoryError e) {
  480. mErrorMessageId = R.string.common_error_out_memory;
  481. if (i < maxDownScale - 1) {
  482. Log_OC.w(TAG, "Out of memory rendering file " + storagePath + " ; scaling down");
  483. minWidth = minWidth / 2;
  484. minHeight = minHeight / 2;
  485. } else {
  486. Log_OC.w(TAG, "Out of memory rendering file " + storagePath + " ; failing");
  487. }
  488. if (bitmapResult != null) {
  489. bitmapResult.recycle();
  490. }
  491. bitmapResult = null;
  492. }
  493. }
  494. }
  495. } catch (NoSuchFieldError e) {
  496. mErrorMessageId = R.string.common_error_unknown;
  497. Log_OC.e(TAG, "Error from access to non-existing field despite protection; file "
  498. + storagePath, e);
  499. } catch (Throwable t) {
  500. mErrorMessageId = R.string.common_error_unknown;
  501. Log_OC.e(TAG, "Unexpected error loading " + getFile().getStoragePath(), t);
  502. }
  503. return new LoadImage(bitmapResult, drawableResult, ocFile);
  504. }
  505. @Override
  506. protected void onCancelled(LoadImage result) {
  507. if (result != null && result.bitmap != null) {
  508. result.bitmap.recycle();
  509. }
  510. }
  511. @Override
  512. protected void onPostExecute(LoadImage result) {
  513. if (result.bitmap != null || result.drawable != null) {
  514. showLoadedImage(result);
  515. } else {
  516. showErrorMessage(mErrorMessageId);
  517. }
  518. if (result.bitmap != null && bitmap != result.bitmap) {
  519. // unused bitmap, release it! (just in case)
  520. result.bitmap.recycle();
  521. }
  522. }
  523. private void showLoadedImage(LoadImage result) {
  524. final PhotoView imageView = imageViewRef.get();
  525. Bitmap bitmap = result.bitmap;
  526. Drawable drawable = result.drawable;
  527. if (imageView != null) {
  528. if (bitmap != null) {
  529. Log_OC.d(TAG, "Showing image with resolution " + bitmap.getWidth() + "x" +
  530. bitmap.getHeight());
  531. if (MIME_TYPE_PNG.equalsIgnoreCase(result.ocFile.getMimeType()) ||
  532. MIME_TYPE_GIF.equalsIgnoreCase(result.ocFile.getMimeType())) {
  533. getResources();
  534. imageView.setImageDrawable(generateCheckerboardLayeredDrawable(result, bitmap));
  535. } else {
  536. imageView.setImageBitmap(bitmap);
  537. }
  538. PreviewImageFragment.this.bitmap = bitmap; // needs to be kept for recycling when not useful
  539. } else {
  540. if (drawable != null
  541. && MIME_TYPE_SVG.equalsIgnoreCase(result.ocFile.getMimeType())) {
  542. getResources();
  543. imageView.setImageDrawable(generateCheckerboardLayeredDrawable(result, null));
  544. }
  545. }
  546. final LinearLayout infoView = infoViewRef.get();
  547. if (infoView != null) {
  548. infoView.setVisibility(View.GONE);
  549. }
  550. final FrameLayout progressView = progressViewRef.get();
  551. if (progressView != null) {
  552. progressView.setVisibility(View.GONE);
  553. }
  554. imageView.setBackgroundColor(getResources().getColor(R.color.background_color_inverse));
  555. imageView.setVisibility(View.VISIBLE);
  556. }
  557. }
  558. }
  559. private LayerDrawable generateCheckerboardLayeredDrawable(LoadImage result, Bitmap bitmap) {
  560. Resources resources = getResources();
  561. Drawable[] layers = new Drawable[2];
  562. layers[0] = ResourcesCompat.getDrawable(resources, R.color.bg_default, null);
  563. Drawable bitmapDrawable;
  564. if (MIME_TYPE_PNG.equalsIgnoreCase(result.ocFile.getMimeType())) {
  565. bitmapDrawable = new BitmapDrawable(resources, bitmap);
  566. } else if (MIME_TYPE_SVG.equalsIgnoreCase(result.ocFile.getMimeType())) {
  567. bitmapDrawable = result.drawable;
  568. } else if (MIME_TYPE_GIF.equalsIgnoreCase(result.ocFile.getMimeType())) {
  569. try {
  570. bitmapDrawable = new GifDrawable(result.ocFile.getStoragePath());
  571. } catch (IOException exception) {
  572. bitmapDrawable = result.drawable;
  573. }
  574. } else {
  575. bitmapDrawable = new BitmapDrawable(resources, bitmap);
  576. }
  577. layers[1] = bitmapDrawable;
  578. LayerDrawable layerDrawable = new LayerDrawable(layers);
  579. Activity activity = getActivity();
  580. if (activity != null) {
  581. int bitmapWidth;
  582. int bitmapHeight;
  583. if (MIME_TYPE_PNG.equalsIgnoreCase(result.ocFile.getMimeType())) {
  584. bitmapWidth = convertDpToPixel(bitmap.getWidth(), getActivity());
  585. bitmapHeight = convertDpToPixel(bitmap.getHeight(), getActivity());
  586. } else {
  587. bitmapWidth = convertDpToPixel(bitmapDrawable.getIntrinsicWidth(), getActivity());
  588. bitmapHeight = convertDpToPixel(bitmapDrawable.getIntrinsicHeight(), getActivity());
  589. }
  590. layerDrawable.setLayerSize(0, bitmapWidth, bitmapHeight);
  591. layerDrawable.setLayerSize(1, bitmapWidth, bitmapHeight);
  592. }
  593. return layerDrawable;
  594. }
  595. private void showErrorMessage(@StringRes int errorMessageId) {
  596. setSorryMessageForMultiList(errorMessageId);
  597. }
  598. private void setMultiListLoadingMessage() {
  599. binding.image.setVisibility(View.GONE);
  600. binding.emptyListView.setVisibility(View.GONE);
  601. binding.emptyListProgress.setVisibility(View.VISIBLE);
  602. }
  603. private void setSorryMessageForMultiList(@StringRes int message) {
  604. binding.emptyListViewHeadline.setText(R.string.preview_sorry);
  605. binding.emptyListViewText.setText(message);
  606. binding.emptyListIcon.setImageResource(R.drawable.file_image);
  607. binding.emptyListView.setBackgroundColor(getResources().getColor(R.color.bg_default));
  608. binding.emptyListViewHeadline.setTextColor(getResources().getColor(R.color.standard_grey));
  609. binding.emptyListViewText.setTextColor(getResources().getColor(R.color.standard_grey));
  610. binding.image.setVisibility(View.GONE);
  611. binding.emptyListView.setVisibility(View.VISIBLE);
  612. binding.emptyListProgress.setVisibility(View.GONE);
  613. }
  614. public void setErrorPreviewMessage() {
  615. try {
  616. if (getActivity() != null) {
  617. Snackbar.make(binding.emptyListView,
  618. R.string.resized_image_not_possible_download,
  619. Snackbar.LENGTH_INDEFINITE)
  620. .setAction(R.string.common_yes, v -> {
  621. PreviewImageActivity activity = (PreviewImageActivity) getActivity();
  622. if (activity != null) {
  623. activity.requestForDownload(getFile());
  624. } else {
  625. Snackbar.make(binding.emptyListView,
  626. getResources().getString(R.string.could_not_download_image),
  627. Snackbar.LENGTH_INDEFINITE).show();
  628. }
  629. }
  630. ).show();
  631. }
  632. } catch (IllegalArgumentException e) {
  633. Log_OC.d(TAG, e.getMessage());
  634. }
  635. }
  636. public void setNoConnectionErrorMessage() {
  637. try {
  638. Snackbar.make(binding.emptyListView, R.string.auth_no_net_conn_title, Snackbar.LENGTH_LONG).show();
  639. } catch (IllegalArgumentException e) {
  640. Log_OC.d(TAG, e.getMessage());
  641. }
  642. }
  643. /**
  644. * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewImageFragment}
  645. * to be previewed.
  646. *
  647. * @param file File to test if can be previewed.
  648. * @return 'True' if the file can be handled by the fragment.
  649. */
  650. public static boolean canBePreviewed(OCFile file) {
  651. return file != null && MimeTypeUtil.isImage(file);
  652. }
  653. /**
  654. * Finishes the preview
  655. */
  656. private void finish() {
  657. Activity container = getActivity();
  658. if (container != null) {
  659. container.finish();
  660. }
  661. }
  662. private void togglePreviewImageFullScreen() {
  663. Activity activity = getActivity();
  664. if (activity != null) {
  665. ((PreviewImageActivity) activity).toggleFullScreen();
  666. }
  667. toggleImageBackground();
  668. }
  669. private void toggleImageBackground() {
  670. if (getFile() != null && (MIME_TYPE_PNG.equalsIgnoreCase(getFile().getMimeType()) ||
  671. MIME_TYPE_SVG.equalsIgnoreCase(getFile().getMimeType())) && getActivity() != null &&
  672. getActivity() instanceof PreviewImageActivity) {
  673. PreviewImageActivity previewImageActivity = (PreviewImageActivity) getActivity();
  674. if (binding.image.getDrawable() instanceof LayerDrawable) {
  675. LayerDrawable layerDrawable = (LayerDrawable) binding.image.getDrawable();
  676. Drawable layerOne;
  677. if (previewImageActivity.isSystemUIVisible()) {
  678. layerOne = ResourcesCompat.getDrawable(getResources(), R.color.bg_default, null);
  679. } else {
  680. layerOne = ResourcesCompat.getDrawable(getResources(), R.drawable.backrepeat, null);
  681. }
  682. layerDrawable.setDrawableByLayerId(layerDrawable.getId(0), layerOne);
  683. binding.image.setImageDrawable(layerDrawable);
  684. binding.image.invalidate();
  685. }
  686. }
  687. }
  688. private static int convertDpToPixel(float dp, Context context) {
  689. Resources resources = context.getResources();
  690. DisplayMetrics metrics = resources.getDisplayMetrics();
  691. return (int) (dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
  692. }
  693. public PhotoView getImageView() {
  694. return binding.image;
  695. }
  696. private class LoadImage {
  697. private final Bitmap bitmap;
  698. private final Drawable drawable;
  699. private final OCFile ocFile;
  700. LoadImage(Bitmap bitmap, Drawable drawable, OCFile ocFile) {
  701. this.bitmap = bitmap;
  702. this.drawable = drawable;
  703. this.ocFile = ocFile;
  704. }
  705. }
  706. }