package com.owncloud.android.ui.preview;
import android.accounts.Account;
-import android.content.Context;
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.BaseAdapter;
-import android.widget.ListView;
-import android.widget.ProgressBar;
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;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringWriter;
-import java.util.ArrayList;
+import java.lang.ref.WeakReference;
+import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
private static final String TAG = PreviewTextFragment.class.getSimpleName();
private Account mAccount;
- private ListView mTextPreviewList;
- private ProgressBar mProgressBar;
+ private TextView mTextPreview;
+ private TextLoadAsyncTask mTextLoadTask;
/**
* Creates an empty fragment for previews.
View ret = inflater.inflate(R.layout.text_file_preview, container, false);
- mTextPreviewList = (ListView) ret.findViewById(R.id.text_preview_list);
- mTextPreviewList.setAdapter(new TextLineAdapter());
- mProgressBar = (ProgressBar) ret.findViewById(R.id.progress_bar);
+ mTextPreview = (TextView) ret.findViewById(R.id.text_preview);
return ret;
}
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) {
@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());
+ 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, StringWriter, Void> {
+ private class TextLoadAsyncTask extends AsyncTask<Object, Void, StringWriter> {
+ private final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
+ private final WeakReference<TextView> mTextViewReference;
+
+ private TextLoadAsyncTask(WeakReference<TextView> textView) {
+ mTextViewReference = textView;
+ }
+
@Override
protected void onPreExecute() {
- ((TextLineAdapter) mTextPreviewList.getAdapter()).clear();
+ showLoadingDialog();
}
@Override
- protected Void doInBackground(java.lang.Object... params) {
- if (params.length != 1)
- throw new IllegalArgumentException("The parameter to " + TextLoadAsyncTask.class.getName() + " must be the file location only");
+ protected StringWriter doInBackground(java.lang.Object... params) {
+ 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];
FileInputStream inputStream = null;
Scanner sc = null;
+ StringWriter source = new StringWriter();
+ BufferedWriter bufferedWriter = new BufferedWriter(source);
try {
inputStream = new FileInputStream(location);
sc = new Scanner(inputStream);
while (sc.hasNextLine()) {
- StringWriter target = new StringWriter();
- BufferedWriter bufferedWriter = new BufferedWriter(target);
- if (sc.hasNextLine())
- bufferedWriter.write(sc.nextLine());
- bufferedWriter.close();
- publishProgress(target);
+ bufferedWriter.append(sc.nextLine());
+ if (sc.hasNextLine()) bufferedWriter.append("\n");
}
+ bufferedWriter.close();
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();
}
}
sc.close();
}
}
- return null;
- }
-
- @Override
- protected void onProgressUpdate(StringWriter... values) {
- super.onProgressUpdate(values);
- //Using a ListView allows to show large text without the UI freeze that happens
- //when calling TextView#setText() with a large CharSequence
- ((TextLineAdapter) mTextPreviewList.getAdapter()).add(values[0].toString());
+ return source;
}
@Override
- protected void onPostExecute(Void aVoid) {
- super.onPostExecute(aVoid);
- mProgressBar.setVisibility(View.GONE);
- mTextPreviewList.setVisibility(View.VISIBLE);
- }
- }
-
- private class TextLineAdapter extends BaseAdapter {
- private static final int LIST_ITEM_LAYOUT = R.layout.text_file_preview_list_item;
- private final List<String> items = new ArrayList<String>();
-
- private void add(String line) {
- items.add(line);
- notifyDataSetChanged();
- }
-
- private void clear() {
- items.clear();
- notifyDataSetChanged();
- }
+ protected void onPostExecute(final StringWriter stringWriter) {
+ final TextView textView = mTextViewReference.get();
- @Override
- public int getCount() {
- return items.size();
- }
+ if (textView != null) {
+ textView.setText(new String(stringWriter.getBuffer()));
+ textView.setVisibility(View.VISIBLE);
+ }
- @Override
- public String getItem(int position) {
- if (position >= items.size())
- throw new IllegalArgumentException();
- return items.get(position);
+ dismissLoadingDialog();
}
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder viewHolder;
- if (convertView == null) {
- convertView =
- ((LayoutInflater) getActivity().getApplicationContext()
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE))
- .inflate(
- LIST_ITEM_LAYOUT, null);
- viewHolder = new ViewHolder();
- viewHolder.setLineView((TextView) convertView.findViewById(R.id.text_preview));
- convertView.setTag(viewHolder);
+ /**
+ * 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 {
- viewHolder = (ViewHolder) convertView.getTag();
+ loading = (LoadingDialog) frag;
+ loading.setShowsDialog(true);
}
- viewHolder.getLineView().setText(items.get(position));
-
- return convertView;
- }
- }
-
- private static class ViewHolder {
- private TextView lineView;
-
- private ViewHolder() {
}
- public TextView getLineView() {
- return lineView;
- }
-
- public void setLineView(TextView lineView) {
- this.lineView = lineView;
+ /**
+ * Dismiss loading dialog
+ */
+ public void dismissLoadingDialog() {
+ final Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
+ if (frag != null) {
+ LoadingDialog loading = (LoadingDialog) frag;
+ loading.dismiss();
+ }
}
}
getFile(),
mContainerActivity.getStorageManager().getAccount(),
mContainerActivity,
- getSherlockActivity()
+ getActivity()
);
mf.filter(menu);
}
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);
+ }
}
/**
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share_file: {
- mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
- return true;
- }
- case R.id.action_unshare_file: {
- mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
+ mContainerActivity.getFileOperationsHelper().showShareFile(getFile());
return true;
}
case R.id.action_open_file_with: {
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())
+ );
}
/**
* Finishes the preview
*/
private void finish() {
- getSherlockActivity().onBackPressed();
+ getActivity().runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ getActivity().onBackPressed();
+ }
+ });
}
}