PreviewImageFragment.java 32 KB

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