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.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 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 {
private Account mAccount;
private TextView mTextPreview;
+ private TextLoadAsyncTask mTextLoadTask;
/**
* Creates an empty fragment for previews.
Bundle args = getArguments();
- if (file == null){
+ if (file == null) {
file = args.getParcelable(FileDisplayActivity.EXTRA_FILE);
- }
+ }
if (mAccount == null) {
mAccount = args.getParcelable(FileDisplayActivity.EXTRA_ACCOUNT);
@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
public void onStart() {
super.onStart();
Log_OC.e(TAG, "onStart");
+
+ loadAndShowTextPreview();
}
private void loadAndShowTextPreview() {
- new TextLoadAsyncTask().execute(getFile().getStoragePath(), mTextPreview);
+ 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> {
private final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
- private TextView mTextView;
+ private final WeakReference<TextView> mTextViewReference;
+
+ private TextLoadAsyncTask(WeakReference<TextView> textView) {
+ mTextViewReference = textView;
+ }
+
@Override
protected void onPreExecute() {
@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;
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();
}
}
@Override
protected void onPostExecute(final StringWriter stringWriter) {
- mTextView.setText(new String(stringWriter.getBuffer()));
- mTextView.setVisibility(View.VISIBLE);
+ 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() {
- // Construct dialog
- LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment));
- FragmentManager fm = getActivity().getSupportFragmentManager();
- FragmentTransaction ft = fm.beginTransaction();
- loading.show(ft, DIALOG_WAIT_TAG);
+ // 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);
+ }
+
}
/**
getFile(),
mContainerActivity.getStorageManager().getAccount(),
mContainerActivity,
- getSherlockActivity()
+ getActivity()
);
mf.filter(menu);
}
item.setEnabled(false);
}
- item = menu.findItem(R.id.action_settings);
- if (item != null) {
- item.setVisible(false);
- item.setEnabled(false);
- }
-
- item = menu.findItem(R.id.action_logger);
- if (item != null) {
- item.setVisible(false);
- item.setEnabled(false);
- }
-
item = menu.findItem(R.id.action_sync_file);
if (item != null) {
item.setVisible(false);
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;
public void onResume() {
super.onResume();
Log_OC.e(TAG, "onResume");
-
- loadAndShowTextPreview();
}
@Override
public void onStop() {
super.onStop();
Log_OC.e(TAG, "onStop");
+ if (mTextLoadTask != null)
+ mTextLoadTask.cancel(Boolean.TRUE);
}
/**
* @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())
+ );
}
/**
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
- getSherlockActivity().onBackPressed();
+ getActivity().onBackPressed();
}
});
}