PreviewImageFragment.java 30 KB

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