PreviewImageFragment.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.ui.preview;
  18. import java.lang.ref.WeakReference;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import android.accounts.Account;
  22. import android.annotation.SuppressLint;
  23. import android.app.Activity;
  24. import android.content.ActivityNotFoundException;
  25. import android.content.Intent;
  26. import android.graphics.Bitmap;
  27. import android.graphics.BitmapFactory;
  28. import android.graphics.BitmapFactory.Options;
  29. import android.graphics.Point;
  30. import android.net.Uri;
  31. import android.os.AsyncTask;
  32. import android.os.Bundle;
  33. import android.os.Handler;
  34. import android.support.v4.app.FragmentStatePagerAdapter;
  35. import android.view.Display;
  36. import android.view.LayoutInflater;
  37. import android.view.View;
  38. import android.view.View.OnTouchListener;
  39. import android.view.ViewGroup;
  40. import android.webkit.MimeTypeMap;
  41. import android.widget.ImageView;
  42. import android.widget.ProgressBar;
  43. import android.widget.TextView;
  44. import android.widget.Toast;
  45. import com.actionbarsherlock.view.Menu;
  46. import com.actionbarsherlock.view.MenuInflater;
  47. import com.actionbarsherlock.view.MenuItem;
  48. import com.owncloud.android.R;
  49. import com.owncloud.android.datamodel.FileDataStorageManager;
  50. import com.owncloud.android.datamodel.OCFile;
  51. import com.owncloud.android.lib.common.network.WebdavUtils;
  52. import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
  53. import com.owncloud.android.lib.common.operations.RemoteOperation;
  54. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  55. import com.owncloud.android.operations.RemoveFileOperation;
  56. import com.owncloud.android.ui.activity.FileActivity;
  57. import com.owncloud.android.ui.fragment.ConfirmationDialogFragment;
  58. import com.owncloud.android.ui.fragment.FileFragment;
  59. import com.owncloud.android.utils.Log_OC;
  60. /**
  61. * This fragment shows a preview of a downloaded image.
  62. *
  63. * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will produce an {@link IllegalStateException}.
  64. *
  65. * If the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too.
  66. *
  67. * @author David A. Velasco
  68. */
  69. public class PreviewImageFragment extends FileFragment implements OnRemoteOperationListener,
  70. ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
  71. public static final String EXTRA_FILE = "FILE";
  72. public static final String EXTRA_ACCOUNT = "ACCOUNT";
  73. private View mView;
  74. private Account mAccount;
  75. private FileDataStorageManager mStorageManager;
  76. private ImageView mImageView;
  77. private TextView mMessageView;
  78. private ProgressBar mProgressWheel;
  79. public Bitmap mBitmap = null;
  80. private Handler mHandler;
  81. private RemoteOperation mLastRemoteOperation;
  82. private static final String TAG = PreviewImageFragment.class.getSimpleName();
  83. private boolean mIgnoreFirstSavedState;
  84. /**
  85. * Creates a fragment to preview an image.
  86. *
  87. * When 'imageFile' or 'ocAccount' are null
  88. *
  89. * @param imageFile An {@link OCFile} to preview as an image in the fragment
  90. * @param ocAccount An ownCloud account; needed to start downloads
  91. * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
  92. */
  93. public PreviewImageFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState) {
  94. super(fileToDetail);
  95. mAccount = ocAccount;
  96. mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment
  97. mIgnoreFirstSavedState = ignoreFirstSavedState;
  98. }
  99. /**
  100. * Creates an empty fragment for image previews.
  101. *
  102. * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically (for instance, when the device is turned a aside).
  103. *
  104. * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction
  105. */
  106. public PreviewImageFragment() {
  107. super();
  108. mAccount = null;
  109. mStorageManager = null;
  110. mIgnoreFirstSavedState = false;
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. @Override
  116. public void onCreate(Bundle savedInstanceState) {
  117. super.onCreate(savedInstanceState);
  118. mHandler = new Handler();
  119. setHasOptionsMenu(true);
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. @Override
  125. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  126. Bundle savedInstanceState) {
  127. super.onCreateView(inflater, container, savedInstanceState);
  128. mView = inflater.inflate(R.layout.preview_image_fragment, container, false);
  129. mImageView = (ImageView)mView.findViewById(R.id.image);
  130. mImageView.setVisibility(View.GONE);
  131. mView.setOnTouchListener((OnTouchListener)getActivity()); // WATCH OUT THAT CAST
  132. mMessageView = (TextView)mView.findViewById(R.id.message);
  133. mMessageView.setVisibility(View.GONE);
  134. mProgressWheel = (ProgressBar)mView.findViewById(R.id.progressWheel);
  135. mProgressWheel.setVisibility(View.VISIBLE);
  136. return mView;
  137. }
  138. /**
  139. * {@inheritDoc}
  140. */
  141. @Override
  142. public void onAttach(Activity activity) {
  143. super.onAttach(activity);
  144. if (!(activity instanceof FileFragment.ContainerActivity))
  145. throw new ClassCastException(activity.toString() + " must implement " + FileFragment.ContainerActivity.class.getSimpleName());
  146. }
  147. /**
  148. * {@inheritDoc}
  149. */
  150. @Override
  151. public void onActivityCreated(Bundle savedInstanceState) {
  152. super.onActivityCreated(savedInstanceState);
  153. mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());
  154. if (savedInstanceState != null) {
  155. if (!mIgnoreFirstSavedState) {
  156. OCFile file = (OCFile)savedInstanceState.getParcelable(PreviewImageFragment.EXTRA_FILE);
  157. mAccount = savedInstanceState.getParcelable(PreviewImageFragment.EXTRA_ACCOUNT);
  158. // Update the file
  159. if (mAccount!= null) {
  160. mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());
  161. OCFile updatedFile = mStorageManager.getFileByPath(file.getRemotePath());
  162. if (updatedFile != null) {
  163. setFile(updatedFile);
  164. } else {
  165. setFile(file);
  166. }
  167. } else {
  168. setFile(file);
  169. }
  170. } else {
  171. mIgnoreFirstSavedState = false;
  172. }
  173. }
  174. if (getFile() == null) {
  175. throw new IllegalStateException("Instanced with a NULL OCFile");
  176. }
  177. if (mAccount == null) {
  178. throw new IllegalStateException("Instanced with a NULL ownCloud Account");
  179. }
  180. if (!getFile().isDown()) {
  181. throw new IllegalStateException("There is no local file to preview");
  182. }
  183. }
  184. /**
  185. * {@inheritDoc}
  186. */
  187. @Override
  188. public void onSaveInstanceState(Bundle outState) {
  189. super.onSaveInstanceState(outState);
  190. outState.putParcelable(PreviewImageFragment.EXTRA_FILE, getFile());
  191. outState.putParcelable(PreviewImageFragment.EXTRA_ACCOUNT, mAccount);
  192. }
  193. @Override
  194. public void onStart() {
  195. super.onStart();
  196. if (getFile() != null) {
  197. BitmapLoader bl = new BitmapLoader(mImageView, mMessageView, mProgressWheel);
  198. bl.execute(new String[]{getFile().getStoragePath()});
  199. }
  200. }
  201. /**
  202. * {@inheritDoc}
  203. */
  204. @Override
  205. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  206. super.onCreateOptionsMenu(menu, inflater);
  207. inflater.inflate(R.menu.file_actions_menu, menu);
  208. List<Integer> toHide = new ArrayList<Integer>();
  209. MenuItem item = null;
  210. toHide.add(R.id.action_cancel_download);
  211. toHide.add(R.id.action_cancel_upload);
  212. toHide.add(R.id.action_download_file);
  213. toHide.add(R.id.action_rename_file); // by now
  214. // Options shareLink
  215. if (!getFile().isShareByLink()) {
  216. toHide.add(R.id.action_unshare_file);
  217. }
  218. // Send file
  219. boolean sendEnabled = getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on");
  220. if (!sendEnabled) {
  221. toHide.add(R.id.action_send_file);
  222. }
  223. for (int i : toHide) {
  224. item = menu.findItem(i);
  225. if (item != null) {
  226. item.setVisible(false);
  227. item.setEnabled(false);
  228. }
  229. }
  230. }
  231. /**
  232. * {@inheritDoc}
  233. */
  234. @Override
  235. public void onPrepareOptionsMenu(Menu menu) {
  236. super.onPrepareOptionsMenu(menu);
  237. MenuItem item = menu.findItem(R.id.action_unshare_file);
  238. // Options shareLink
  239. OCFile file = ((FileActivity) getSherlockActivity()).getFile();
  240. if (!file.isShareByLink()) {
  241. item.setVisible(false);
  242. item.setEnabled(false);
  243. } else {
  244. item.setVisible(true);
  245. item.setEnabled(true);
  246. }
  247. }
  248. /**
  249. * {@inheritDoc}
  250. */
  251. @Override
  252. public boolean onOptionsItemSelected(MenuItem item) {
  253. switch (item.getItemId()) {
  254. case R.id.action_share_file: {
  255. FileActivity act = (FileActivity)getSherlockActivity();
  256. act.getFileOperationsHelper().shareFileWithLink(getFile(), act);
  257. return true;
  258. }
  259. case R.id.action_unshare_file: {
  260. FileActivity act = (FileActivity)getSherlockActivity();
  261. act.getFileOperationsHelper().unshareFileWithLink(getFile(), act);
  262. return true;
  263. }
  264. case R.id.action_open_file_with: {
  265. openFile();
  266. return true;
  267. }
  268. case R.id.action_remove_file: {
  269. removeFile();
  270. return true;
  271. }
  272. case R.id.action_see_details: {
  273. seeDetails();
  274. return true;
  275. }
  276. case R.id.action_send_file: {
  277. FileActivity act = (FileActivity)getSherlockActivity();
  278. act.getFileOperationsHelper().sendDownloadedFile(getFile(), act);
  279. return true;
  280. }
  281. default:
  282. return false;
  283. }
  284. }
  285. private void seeDetails() {
  286. ((FileFragment.ContainerActivity)getActivity()).showDetails(getFile());
  287. }
  288. @Override
  289. public void onResume() {
  290. super.onResume();
  291. }
  292. @Override
  293. public void onPause() {
  294. super.onPause();
  295. }
  296. @Override
  297. public void onDestroy() {
  298. super.onDestroy();
  299. if (mBitmap != null) {
  300. mBitmap.recycle();
  301. }
  302. }
  303. /**
  304. * Opens the previewed image with an external application.
  305. *
  306. * TODO - improve this; instead of prioritize the actions available for the MIME type in the server,
  307. * we should get a list of available apps for MIME tpye in the server and join it with the list of
  308. * available apps for the MIME type known from the file extension, to let the user choose
  309. */
  310. private void openFile() {
  311. OCFile file = getFile();
  312. String storagePath = file.getStoragePath();
  313. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  314. try {
  315. Intent i = new Intent(Intent.ACTION_VIEW);
  316. i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
  317. i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  318. startActivity(i);
  319. } catch (Throwable t) {
  320. Log_OC.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + file.getMimetype());
  321. boolean toastIt = true;
  322. String mimeType = "";
  323. try {
  324. Intent i = new Intent(Intent.ACTION_VIEW);
  325. mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
  326. if (mimeType == null || !mimeType.equals(file.getMimetype())) {
  327. if (mimeType != null) {
  328. i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);
  329. } else {
  330. // desperate try
  331. i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*-/*");
  332. }
  333. i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  334. startActivity(i);
  335. toastIt = false;
  336. }
  337. } catch (IndexOutOfBoundsException e) {
  338. Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);
  339. } catch (ActivityNotFoundException e) {
  340. Log_OC.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");
  341. } catch (Throwable th) {
  342. Log_OC.e(TAG, "Unexpected problem when opening: " + storagePath, th);
  343. } finally {
  344. if (toastIt) {
  345. Toast.makeText(getActivity(), "There is no application to handle file " + file.getFileName(), Toast.LENGTH_SHORT).show();
  346. }
  347. }
  348. }
  349. finish();
  350. }
  351. /**
  352. * Starts a the removal of the previewed file.
  353. *
  354. * Shows a confirmation dialog. The action continues in {@link #onConfirmation(String)} , {@link #onNeutral(String)} or {@link #onCancel(String)},
  355. * depending upon the user selection in the dialog.
  356. */
  357. private void removeFile() {
  358. ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
  359. R.string.confirmation_remove_alert,
  360. new String[]{getFile().getFileName()},
  361. R.string.confirmation_remove_remote_and_local,
  362. R.string.confirmation_remove_local,
  363. R.string.common_cancel);
  364. confDialog.setOnConfirmationListener(this);
  365. confDialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
  366. }
  367. /**
  368. * Performs the removal of the previewed file, both locally and in the server.
  369. */
  370. @Override
  371. public void onConfirmation(String callerTag) {
  372. if (mStorageManager.getFileById(getFile().getFileId()) != null) { // check that the file is still there;
  373. mLastRemoteOperation = new RemoveFileOperation( getFile(), // TODO we need to review the interface with RemoteOperations, and use OCFile IDs instead of OCFile objects as parameters
  374. true,
  375. mStorageManager);
  376. mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity());
  377. ((PreviewImageActivity) getActivity()).showLoadingDialog();
  378. }
  379. }
  380. /**
  381. * Removes the file from local storage
  382. */
  383. @Override
  384. public void onNeutral(String callerTag) {
  385. OCFile file = getFile();
  386. mStorageManager.removeFile(file, false, true); // TODO perform in background task / new thread
  387. finish();
  388. }
  389. /**
  390. * User cancelled the removal action.
  391. */
  392. @Override
  393. public void onCancel(String callerTag) {
  394. // nothing to do here
  395. }
  396. private class BitmapLoader extends AsyncTask<String, Void, Bitmap> {
  397. /**
  398. * Weak reference to the target {@link ImageView} where the bitmap will be loaded into.
  399. *
  400. * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
  401. */
  402. private final WeakReference<ImageView> mImageViewRef;
  403. /**
  404. * Weak reference to the target {@link TextView} where error messages will be written.
  405. *
  406. * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
  407. */
  408. private final WeakReference<TextView> mMessageViewRef;
  409. /**
  410. * Weak reference to the target {@link Progressbar} shown while the load is in progress.
  411. *
  412. * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
  413. */
  414. private final WeakReference<ProgressBar> mProgressWheelRef;
  415. /**
  416. * Error message to show when a load fails
  417. */
  418. private int mErrorMessageId;
  419. /**
  420. * Constructor.
  421. *
  422. * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
  423. */
  424. public BitmapLoader(ImageView imageView, TextView messageView, ProgressBar progressWheel) {
  425. mImageViewRef = new WeakReference<ImageView>(imageView);
  426. mMessageViewRef = new WeakReference<TextView>(messageView);
  427. mProgressWheelRef = new WeakReference<ProgressBar>(progressWheel);
  428. }
  429. @SuppressWarnings("deprecation")
  430. @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20
  431. @Override
  432. protected Bitmap doInBackground(String... params) {
  433. Bitmap result = null;
  434. if (params.length != 1) return result;
  435. String storagePath = params[0];
  436. try {
  437. // set desired options that will affect the size of the bitmap
  438. BitmapFactory.Options options = new Options();
  439. options.inScaled = true;
  440. options.inPurgeable = true;
  441. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
  442. options.inPreferQualityOverSpeed = false;
  443. }
  444. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
  445. options.inMutable = false;
  446. }
  447. // make a false load of the bitmap - just to be able to read outWidth, outHeight and outMimeType
  448. options.inJustDecodeBounds = true;
  449. BitmapFactory.decodeFile(storagePath, options);
  450. int width = options.outWidth;
  451. int height = options.outHeight;
  452. int scale = 1;
  453. Display display = getActivity().getWindowManager().getDefaultDisplay();
  454. Point size = new Point();
  455. int screenWidth;
  456. int screenHeight;
  457. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
  458. display.getSize(size);
  459. screenWidth = size.x;
  460. screenHeight = size.y;
  461. } else {
  462. screenWidth = display.getWidth();
  463. screenHeight = display.getHeight();
  464. }
  465. if (width > screenWidth) {
  466. // second try to scale down the image , this time depending upon the screen size
  467. scale = (int) Math.floor((float)width / screenWidth);
  468. }
  469. if (height > screenHeight) {
  470. scale = Math.max(scale, (int) Math.floor((float)height / screenHeight));
  471. }
  472. options.inSampleSize = scale;
  473. // really load the bitmap
  474. options.inJustDecodeBounds = false; // the next decodeFile call will be real
  475. result = BitmapFactory.decodeFile(storagePath, options);
  476. //Log_OC.d(TAG, "Image loaded - width: " + options.outWidth + ", loaded height: " + options.outHeight);
  477. if (result == null) {
  478. mErrorMessageId = R.string.preview_image_error_unknown_format;
  479. Log_OC.e(TAG, "File could not be loaded as a bitmap: " + storagePath);
  480. }
  481. } catch (OutOfMemoryError e) {
  482. mErrorMessageId = R.string.preview_image_error_unknown_format;
  483. Log_OC.e(TAG, "Out of memory occured for file " + storagePath, e);
  484. } catch (NoSuchFieldError e) {
  485. mErrorMessageId = R.string.common_error_unknown;
  486. Log_OC.e(TAG, "Error from access to unexisting field despite protection; file " + storagePath, e);
  487. } catch (Throwable t) {
  488. mErrorMessageId = R.string.common_error_unknown;
  489. Log_OC.e(TAG, "Unexpected error loading " + getFile().getStoragePath(), t);
  490. }
  491. return result;
  492. }
  493. @Override
  494. protected void onPostExecute(Bitmap result) {
  495. hideProgressWheel();
  496. if (result != null) {
  497. showLoadedImage(result);
  498. } else {
  499. showErrorMessage();
  500. }
  501. }
  502. private void showLoadedImage(Bitmap result) {
  503. if (mImageViewRef != null) {
  504. final ImageView imageView = mImageViewRef.get();
  505. if (imageView != null) {
  506. imageView.setImageBitmap(result);
  507. imageView.setVisibility(View.VISIBLE);
  508. mBitmap = result;
  509. } // else , silently finish, the fragment was destroyed
  510. }
  511. if (mMessageViewRef != null) {
  512. final TextView messageView = mMessageViewRef.get();
  513. if (messageView != null) {
  514. messageView.setVisibility(View.GONE);
  515. } // else , silently finish, the fragment was destroyed
  516. }
  517. }
  518. private void showErrorMessage() {
  519. if (mImageViewRef != null) {
  520. final ImageView imageView = mImageViewRef.get();
  521. if (imageView != null) {
  522. // shows the default error icon
  523. imageView.setVisibility(View.VISIBLE);
  524. } // else , silently finish, the fragment was destroyed
  525. }
  526. if (mMessageViewRef != null) {
  527. final TextView messageView = mMessageViewRef.get();
  528. if (messageView != null) {
  529. messageView.setText(mErrorMessageId);
  530. messageView.setVisibility(View.VISIBLE);
  531. } // else , silently finish, the fragment was destroyed
  532. }
  533. }
  534. private void hideProgressWheel() {
  535. if (mProgressWheelRef != null) {
  536. final ProgressBar progressWheel = mProgressWheelRef.get();
  537. if (progressWheel != null) {
  538. progressWheel.setVisibility(View.GONE);
  539. }
  540. }
  541. }
  542. }
  543. /**
  544. * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewImageFragment} to be previewed.
  545. *
  546. * @param file File to test if can be previewed.
  547. * @return 'True' if the file can be handled by the fragment.
  548. */
  549. public static boolean canBePreviewed(OCFile file) {
  550. return (file != null && file.isImage());
  551. }
  552. /**
  553. * {@inheritDoc}
  554. */
  555. @Override
  556. public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
  557. if (operation.equals(mLastRemoteOperation) && operation instanceof RemoveFileOperation) {
  558. onRemoveFileOperationFinish((RemoveFileOperation)operation, result);
  559. }
  560. }
  561. private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {
  562. ((PreviewImageActivity) getActivity()).dismissLoadingDialog();
  563. if (result.isSuccess()) {
  564. Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
  565. msg.show();
  566. finish();
  567. } else {
  568. Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG);
  569. msg.show();
  570. if (result.isSslRecoverableException()) {
  571. // TODO show the SSL warning dialog
  572. }
  573. }
  574. }
  575. /**
  576. * Finishes the preview
  577. */
  578. private void finish() {
  579. Activity container = getActivity();
  580. container.finish();
  581. }
  582. }