PreviewImageFragment.java 26 KB

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