PreviewImageFragment.java 29 KB

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