Merge branch 'material_buttons' of https://github.com/owncloud/android into material_fab
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewTextFragment.java
index d0a678c..a07d3df 100644 (file)
@@ -3,22 +3,24 @@ package com.owncloud.android.ui.preview;
 import android.accounts.Account;
 import android.os.AsyncTask;
 import android.os.Bundle;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentTransaction;
 import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.ProgressBar;
-import android.widget.ScrollView;
 import android.widget.TextView;
 
-import com.actionbarsherlock.view.Menu;
-import com.actionbarsherlock.view.MenuInflater;
-import com.actionbarsherlock.view.MenuItem;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.FileMenuFilter;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
+import com.owncloud.android.ui.dialog.LoadingDialog;
 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
 import com.owncloud.android.ui.fragment.FileFragment;
 
@@ -26,6 +28,9 @@ import java.io.BufferedWriter;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.StringWriter;
+import java.lang.ref.WeakReference;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Scanner;
 
 public class PreviewTextFragment extends FileFragment {
@@ -35,8 +40,7 @@ public class PreviewTextFragment extends FileFragment {
 
     private Account mAccount;
     private TextView mTextPreview;
-    private ProgressBar mProgressBar;
-    private ScrollView mScrollView;
+    private TextLoadAsyncTask mTextLoadTask;
 
     /**
      * Creates an empty fragment for previews.
@@ -64,9 +68,7 @@ public class PreviewTextFragment extends FileFragment {
 
         View ret = inflater.inflate(R.layout.text_file_preview, container, false);
 
-        mScrollView = (ScrollView) ret.findViewById(R.id.text_scrollview);
         mTextPreview = (TextView) ret.findViewById(R.id.text_preview);
-        mProgressBar = (ProgressBar) ret.findViewById(R.id.progress_bar);
 
         return ret;
     }
@@ -82,12 +84,13 @@ public class PreviewTextFragment extends FileFragment {
 
         Bundle args = getArguments();
 
-        if (file == null)
+        if (file == null) {
             file = args.getParcelable(FileDisplayActivity.EXTRA_FILE);
+        }
 
-        if (mAccount == null)
+        if (mAccount == null) {
             mAccount = args.getParcelable(FileDisplayActivity.EXTRA_ACCOUNT);
-
+        }
 
         if (savedInstanceState == null) {
             if (file == null) {
@@ -110,8 +113,8 @@ public class PreviewTextFragment extends FileFragment {
     @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
-        outState.putParcelable(PreviewImageFragment.EXTRA_FILE, getFile());
-        outState.putParcelable(PreviewImageFragment.EXTRA_ACCOUNT, mAccount);
+        outState.putParcelable(PreviewTextFragment.EXTRA_FILE, getFile());
+        outState.putParcelable(PreviewTextFragment.EXTRA_ACCOUNT, mAccount);
     }
 
     @Override
@@ -119,30 +122,38 @@ public class PreviewTextFragment extends FileFragment {
         super.onStart();
         Log_OC.e(TAG, "onStart");
 
-        loadAndShowTextPreview(getFile().getStoragePath(), mTextPreview);
+        loadAndShowTextPreview();
     }
 
-    private void loadAndShowTextPreview(String location, TextView textView) {
-        new TextLoadAsyncTask().execute(location, textView);
+    private void loadAndShowTextPreview() {
+        mTextLoadTask = new TextLoadAsyncTask(new WeakReference<TextView>(mTextPreview));
+        mTextLoadTask.execute(getFile().getStoragePath());
     }
 
+
     /**
      * Reads the file to preview and shows its contents. Too critical to be anonymous.
      */
     private class TextLoadAsyncTask extends AsyncTask<Object, Void, StringWriter> {
-        TextView mTextView;
+        private final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
+        private final WeakReference<TextView> mTextViewReference;
+
+        private TextLoadAsyncTask(WeakReference<TextView> textView) {
+            mTextViewReference = textView;
+        }
+
 
         @Override
         protected void onPreExecute() {
-            mProgressBar.setVisibility(View.VISIBLE);
+            showLoadingDialog();
         }
 
         @Override
         protected StringWriter doInBackground(java.lang.Object... params) {
-            if (params.length != 2)
-                throw new IllegalArgumentException("The parameters to " + TextLoadAsyncTask.class.getName() + " must be (1) the file location and (2) the text view to update");
+            if (params.length != 1) {
+                throw new IllegalArgumentException("The parameter to " + TextLoadAsyncTask.class.getName() + " must be (1) the file location");
+            }
             final String location = (String) params[0];
-            mTextView = (TextView) params[1];
 
             FileInputStream inputStream = null;
             Scanner sc = null;
@@ -159,12 +170,14 @@ public class PreviewTextFragment extends FileFragment {
                 IOException exc = sc.ioException();
                 if (exc != null) throw exc;
             } catch (IOException e) {
+                Log_OC.e(TAG, e.getMessage(), e);
                 finish();
             } finally {
                 if (inputStream != null) {
                     try {
                         inputStream.close();
                     } catch (IOException e) {
+                        Log_OC.e(TAG, e.getMessage(), e);
                         finish();
                     }
                 }
@@ -177,10 +190,45 @@ public class PreviewTextFragment extends FileFragment {
 
         @Override
         protected void onPostExecute(final StringWriter stringWriter) {
-            super.onPostExecute(stringWriter);
-            mProgressBar.setVisibility(View.GONE);
-            mScrollView.setVisibility(View.VISIBLE);
-            mTextView.setText(new String(stringWriter.getBuffer()));
+            final TextView textView = mTextViewReference.get();
+
+            if (textView != null) {
+                textView.setText(new String(stringWriter.getBuffer()));
+                textView.setVisibility(View.VISIBLE);
+            }
+
+            dismissLoadingDialog();
+        }
+
+        /**
+         * Show loading dialog
+         */
+        public void showLoadingDialog() {
+            // only once
+            Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
+            LoadingDialog loading = null;
+            if (frag == null) {
+                // Construct dialog
+                loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment));
+                FragmentManager fm = getActivity().getSupportFragmentManager();
+                FragmentTransaction ft = fm.beginTransaction();
+                loading.show(ft, DIALOG_WAIT_TAG);
+            } else {
+                loading = (LoadingDialog) frag;
+                loading.setShowsDialog(true);
+            }
+
+        }
+
+        /**
+         * Dismiss loading dialog
+         */
+        public void dismissLoadingDialog() {
+            final Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
+            if (frag != null) {
+                LoadingDialog loading = (LoadingDialog) frag;
+                loading.dismiss();
+            }
         }
     }
 
@@ -205,7 +253,7 @@ public class PreviewTextFragment extends FileFragment {
                     getFile(),
                     mContainerActivity.getStorageManager().getAccount(),
                     mContainerActivity,
-                    getSherlockActivity()
+                    getActivity()
             );
             mf.filter(menu);
         }
@@ -230,6 +278,18 @@ public class PreviewTextFragment extends FileFragment {
             item.setVisible(false);
             item.setEnabled(false);
         }
+
+        item = menu.findItem(R.id.action_sync_file);
+        if (item != null) {
+            item.setVisible(false);
+            item.setEnabled(false);
+        }
+
+        item = menu.findItem(R.id.action_sync_account);
+        if (item != null) {
+            item.setVisible(false);
+            item.setEnabled(false);
+        }
     }
 
     /**
@@ -242,6 +302,10 @@ public class PreviewTextFragment extends FileFragment {
                 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
                 return true;
             }
+            case R.id.action_share_with_users: {
+                mContainerActivity.getFileOperationsHelper().showShareFile(getFile());
+                return true;
+            }
             case R.id.action_unshare_file: {
                 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
                 return true;
@@ -276,7 +340,7 @@ public class PreviewTextFragment extends FileFragment {
     /**
      * Update the file of the fragment with file value
      *
-     * @param file
+     * @param file The new file to set
      */
     public void updateFile(OCFile file) {
         setFile(file);
@@ -312,6 +376,8 @@ public class PreviewTextFragment extends FileFragment {
     public void onStop() {
         super.onStop();
         Log_OC.e(TAG, "onStop");
+        if (mTextLoadTask != null)
+            mTextLoadTask.cancel(Boolean.TRUE);
     }
 
     /**
@@ -329,13 +395,29 @@ public class PreviewTextFragment extends FileFragment {
      * @return 'True' if the file can be handled by the fragment.
      */
     public static boolean canBePreviewed(OCFile file) {
-        return (file != null && file.isDown() && file.isText());
+        final List<String> unsupportedTypes = new LinkedList<String>();
+        unsupportedTypes.add("text/richtext");
+        unsupportedTypes.add("text/rtf");
+        unsupportedTypes.add("text/vnd.abc");
+        unsupportedTypes.add("text/vnd.fmi.flexstor");
+        unsupportedTypes.add("text/vnd.rn-realtext");
+        unsupportedTypes.add("text/vnd.wap.wml");
+        unsupportedTypes.add("text/vnd.wap.wmlscript");
+        return (file != null && file.isDown() && file.isText() &&
+                !unsupportedTypes.contains(file.getMimetype()) &&
+                !unsupportedTypes.contains(file.getMimeTypeFromName())
+        );
     }
 
     /**
      * Finishes the preview
      */
     private void finish() {
-        getSherlockActivity().onBackPressed();
+        getActivity().runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                getActivity().onBackPressed();
+            }
+        });
     }
 }