PreviewImageFragment.java 26 KB

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