PreviewTextFragment.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. package com.owncloud.android.ui.preview;
  2. import android.accounts.Account;
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.support.v4.app.Fragment;
  6. import android.support.v4.app.FragmentManager;
  7. import android.support.v4.app.FragmentTransaction;
  8. import android.view.LayoutInflater;
  9. import android.view.Menu;
  10. import android.view.MenuInflater;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.TextView;
  15. import com.owncloud.android.R;
  16. import com.owncloud.android.datamodel.OCFile;
  17. import com.owncloud.android.files.FileMenuFilter;
  18. import com.owncloud.android.lib.common.utils.Log_OC;
  19. import com.owncloud.android.ui.activity.FileDisplayActivity;
  20. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
  21. import com.owncloud.android.ui.dialog.LoadingDialog;
  22. import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
  23. import com.owncloud.android.ui.fragment.FileFragment;
  24. import java.io.BufferedWriter;
  25. import java.io.FileInputStream;
  26. import java.io.IOException;
  27. import java.io.StringWriter;
  28. import java.lang.ref.WeakReference;
  29. import java.util.LinkedList;
  30. import java.util.List;
  31. import java.util.Scanner;
  32. public class PreviewTextFragment extends FileFragment {
  33. private static final String EXTRA_FILE = "FILE";
  34. private static final String EXTRA_ACCOUNT = "ACCOUNT";
  35. private static final String TAG = PreviewTextFragment.class.getSimpleName();
  36. private Account mAccount;
  37. private TextView mTextPreview;
  38. private TextLoadAsyncTask mTextLoadTask;
  39. /**
  40. * Creates an empty fragment for previews.
  41. * <p/>
  42. * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
  43. * (for instance, when the device is turned a aside).
  44. * <p/>
  45. * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
  46. * construction
  47. */
  48. public PreviewTextFragment() {
  49. super();
  50. mAccount = null;
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. @Override
  56. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  57. Bundle savedInstanceState) {
  58. super.onCreateView(inflater, container, savedInstanceState);
  59. Log_OC.e(TAG, "onCreateView");
  60. View ret = inflater.inflate(R.layout.text_file_preview, container, false);
  61. mTextPreview = (TextView) ret.findViewById(R.id.text_preview);
  62. return ret;
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. @Override
  68. public void onCreate(Bundle savedInstanceState) {
  69. super.onCreate(savedInstanceState);
  70. OCFile file = getFile();
  71. Bundle args = getArguments();
  72. if (file == null) {
  73. file = args.getParcelable(FileDisplayActivity.EXTRA_FILE);
  74. }
  75. if (mAccount == null) {
  76. mAccount = args.getParcelable(FileDisplayActivity.EXTRA_ACCOUNT);
  77. }
  78. if (savedInstanceState == null) {
  79. if (file == null) {
  80. throw new IllegalStateException("Instanced with a NULL OCFile");
  81. }
  82. if (mAccount == null) {
  83. throw new IllegalStateException("Instanced with a NULL ownCloud Account");
  84. }
  85. } else {
  86. file = savedInstanceState.getParcelable(EXTRA_FILE);
  87. mAccount = savedInstanceState.getParcelable(EXTRA_ACCOUNT);
  88. }
  89. setFile(file);
  90. setHasOptionsMenu(true);
  91. }
  92. /**
  93. * {@inheritDoc}
  94. */
  95. @Override
  96. public void onSaveInstanceState(Bundle outState) {
  97. super.onSaveInstanceState(outState);
  98. outState.putParcelable(PreviewTextFragment.EXTRA_FILE, getFile());
  99. outState.putParcelable(PreviewTextFragment.EXTRA_ACCOUNT, mAccount);
  100. }
  101. @Override
  102. public void onStart() {
  103. super.onStart();
  104. Log_OC.e(TAG, "onStart");
  105. loadAndShowTextPreview();
  106. }
  107. private void loadAndShowTextPreview() {
  108. mTextLoadTask = new TextLoadAsyncTask(new WeakReference<TextView>(mTextPreview));
  109. mTextLoadTask.execute(getFile().getStoragePath());
  110. }
  111. /**
  112. * Reads the file to preview and shows its contents. Too critical to be anonymous.
  113. */
  114. private class TextLoadAsyncTask extends AsyncTask<Object, Void, StringWriter> {
  115. private final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
  116. private final WeakReference<TextView> mTextViewReference;
  117. private TextLoadAsyncTask(WeakReference<TextView> textView) {
  118. mTextViewReference = textView;
  119. }
  120. @Override
  121. protected void onPreExecute() {
  122. showLoadingDialog();
  123. }
  124. @Override
  125. protected StringWriter doInBackground(java.lang.Object... params) {
  126. if (params.length != 1) {
  127. throw new IllegalArgumentException("The parameter to " + TextLoadAsyncTask.class.getName() + " must be (1) the file location");
  128. }
  129. final String location = (String) params[0];
  130. FileInputStream inputStream = null;
  131. Scanner sc = null;
  132. StringWriter source = new StringWriter();
  133. BufferedWriter bufferedWriter = new BufferedWriter(source);
  134. try {
  135. inputStream = new FileInputStream(location);
  136. sc = new Scanner(inputStream);
  137. while (sc.hasNextLine()) {
  138. bufferedWriter.append(sc.nextLine());
  139. if (sc.hasNextLine()) bufferedWriter.append("\n");
  140. }
  141. bufferedWriter.close();
  142. IOException exc = sc.ioException();
  143. if (exc != null) throw exc;
  144. } catch (IOException e) {
  145. Log_OC.e(TAG, e.getMessage(), e);
  146. finish();
  147. } finally {
  148. if (inputStream != null) {
  149. try {
  150. inputStream.close();
  151. } catch (IOException e) {
  152. Log_OC.e(TAG, e.getMessage(), e);
  153. finish();
  154. }
  155. }
  156. if (sc != null) {
  157. sc.close();
  158. }
  159. }
  160. return source;
  161. }
  162. @Override
  163. protected void onPostExecute(final StringWriter stringWriter) {
  164. final TextView textView = mTextViewReference.get();
  165. if (textView != null) {
  166. textView.setText(new String(stringWriter.getBuffer()));
  167. textView.setVisibility(View.VISIBLE);
  168. }
  169. dismissLoadingDialog();
  170. }
  171. /**
  172. * Show loading dialog
  173. */
  174. public void showLoadingDialog() {
  175. // only once
  176. Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
  177. LoadingDialog loading = null;
  178. if (frag == null) {
  179. // Construct dialog
  180. loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment));
  181. FragmentManager fm = getActivity().getSupportFragmentManager();
  182. FragmentTransaction ft = fm.beginTransaction();
  183. loading.show(ft, DIALOG_WAIT_TAG);
  184. } else {
  185. loading = (LoadingDialog) frag;
  186. loading.setShowsDialog(true);
  187. }
  188. }
  189. /**
  190. * Dismiss loading dialog
  191. */
  192. public void dismissLoadingDialog() {
  193. final Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
  194. if (frag != null) {
  195. LoadingDialog loading = (LoadingDialog) frag;
  196. loading.dismiss();
  197. }
  198. }
  199. }
  200. /**
  201. * {@inheritDoc}
  202. */
  203. @Override
  204. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  205. super.onCreateOptionsMenu(menu, inflater);
  206. inflater.inflate(R.menu.file_actions_menu, menu);
  207. }
  208. /**
  209. * {@inheritDoc}
  210. */
  211. @Override
  212. public void onPrepareOptionsMenu(Menu menu) {
  213. super.onPrepareOptionsMenu(menu);
  214. if (mContainerActivity.getStorageManager() != null) {
  215. FileMenuFilter mf = new FileMenuFilter(
  216. getFile(),
  217. mContainerActivity.getStorageManager().getAccount(),
  218. mContainerActivity,
  219. getActivity()
  220. );
  221. mf.filter(menu);
  222. }
  223. // additional restriction for this fragment
  224. MenuItem item = menu.findItem(R.id.action_rename_file);
  225. if (item != null) {
  226. item.setVisible(false);
  227. item.setEnabled(false);
  228. }
  229. // additional restriction for this fragment
  230. item = menu.findItem(R.id.action_move);
  231. if (item != null) {
  232. item.setVisible(false);
  233. item.setEnabled(false);
  234. }
  235. // this one doesn't make sense since the file has to be down in order to be previewed
  236. item = menu.findItem(R.id.action_download_file);
  237. if (item != null) {
  238. item.setVisible(false);
  239. item.setEnabled(false);
  240. }
  241. item = menu.findItem(R.id.action_sync_file);
  242. if (item != null) {
  243. item.setVisible(false);
  244. item.setEnabled(false);
  245. }
  246. item = menu.findItem(R.id.action_sync_account);
  247. if (item != null) {
  248. item.setVisible(false);
  249. item.setEnabled(false);
  250. }
  251. }
  252. /**
  253. * {@inheritDoc}
  254. */
  255. @Override
  256. public boolean onOptionsItemSelected(MenuItem item) {
  257. switch (item.getItemId()) {
  258. case R.id.action_share_file: {
  259. mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
  260. return true;
  261. }
  262. case R.id.action_share_with_users: {
  263. seeShareFile();
  264. return true;
  265. }
  266. case R.id.action_unshare_file: {
  267. mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
  268. return true;
  269. }
  270. case R.id.action_open_file_with: {
  271. openFile();
  272. return true;
  273. }
  274. case R.id.action_remove_file: {
  275. RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
  276. dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
  277. return true;
  278. }
  279. case R.id.action_see_details: {
  280. seeDetails();
  281. return true;
  282. }
  283. case R.id.action_send_file: {
  284. sendFile();
  285. return true;
  286. }
  287. case R.id.action_sync_file: {
  288. mContainerActivity.getFileOperationsHelper().syncFile(getFile());
  289. return true;
  290. }
  291. default:
  292. return false;
  293. }
  294. }
  295. /**
  296. * Update the file of the fragment with file value
  297. *
  298. * @param file The new file to set
  299. */
  300. public void updateFile(OCFile file) {
  301. setFile(file);
  302. }
  303. private void sendFile() {
  304. mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
  305. }
  306. private void seeDetails() {
  307. mContainerActivity.showDetails(getFile());
  308. }
  309. private void seeShareFile(){
  310. mContainerActivity.showShareFile(getFile());
  311. }
  312. @Override
  313. public void onPause() {
  314. Log_OC.e(TAG, "onPause");
  315. super.onPause();
  316. }
  317. @Override
  318. public void onResume() {
  319. super.onResume();
  320. Log_OC.e(TAG, "onResume");
  321. }
  322. @Override
  323. public void onDestroy() {
  324. Log_OC.e(TAG, "onDestroy");
  325. super.onDestroy();
  326. }
  327. @Override
  328. public void onStop() {
  329. super.onStop();
  330. Log_OC.e(TAG, "onStop");
  331. if (mTextLoadTask != null)
  332. mTextLoadTask.cancel(Boolean.TRUE);
  333. }
  334. /**
  335. * Opens the previewed file with an external application.
  336. */
  337. private void openFile() {
  338. mContainerActivity.getFileOperationsHelper().openFile(getFile());
  339. finish();
  340. }
  341. /**
  342. * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewTextFragment} to be previewed.
  343. *
  344. * @param file File to test if can be previewed.
  345. * @return 'True' if the file can be handled by the fragment.
  346. */
  347. public static boolean canBePreviewed(OCFile file) {
  348. final List<String> unsupportedTypes = new LinkedList<String>();
  349. unsupportedTypes.add("text/richtext");
  350. unsupportedTypes.add("text/rtf");
  351. unsupportedTypes.add("text/vnd.abc");
  352. unsupportedTypes.add("text/vnd.fmi.flexstor");
  353. unsupportedTypes.add("text/vnd.rn-realtext");
  354. unsupportedTypes.add("text/vnd.wap.wml");
  355. unsupportedTypes.add("text/vnd.wap.wmlscript");
  356. return (file != null && file.isDown() && file.isText() &&
  357. !unsupportedTypes.contains(file.getMimetype()) &&
  358. !unsupportedTypes.contains(file.getMimeTypeFromName())
  359. );
  360. }
  361. /**
  362. * Finishes the preview
  363. */
  364. private void finish() {
  365. getActivity().runOnUiThread(new Runnable() {
  366. @Override
  367. public void run() {
  368. getActivity().onBackPressed();
  369. }
  370. });
  371. }
  372. }