From: Jorge Antonio Diaz-Benito Soriano Date: Sun, 26 Oct 2014 09:45:06 +0000 (+0100) Subject: Reverted the text preview system to a single TextView X-Git-Tag: oc-android-1.8~17^2~8^2~14^2~1 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/afefef9ae3194024dc82c590d632aed5c76e3fd5?ds=sidebyside;hp=--cc Reverted the text preview system to a single TextView --- afefef9ae3194024dc82c590d632aed5c76e3fd5 diff --git a/res/layout/text_file_preview.xml b/res/layout/text_file_preview.xml index f33b3d80..aa6cf76c 100644 --- a/res/layout/text_file_preview.xml +++ b/res/layout/text_file_preview.xml @@ -1,12 +1,31 @@ - - + \ No newline at end of file + android:fillViewport="true"> + + + \ No newline at end of file diff --git a/res/layout/text_file_preview_list_item.xml b/res/layout/text_file_preview_list_item.xml deleted file mode 100644 index 269765ea..00000000 --- a/res/layout/text_file_preview_list_item.xml +++ /dev/null @@ -1,24 +0,0 @@ - - \ No newline at end of file diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index 07445db5..1be63998 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -1,21 +1,15 @@ package com.owncloud.android.ui.preview; import android.accounts.Account; -import android.annotation.SuppressLint; -import android.content.Context; -import android.graphics.Paint; -import android.graphics.Rect; 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.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.BaseAdapter; -import android.widget.ListView; +import android.widget.ScrollView; import android.widget.TextView; import com.actionbarsherlock.view.Menu; @@ -35,10 +29,6 @@ import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.IOException; import java.io.StringWriter; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; import java.util.Scanner; public class PreviewTextFragment extends FileFragment { @@ -47,7 +37,7 @@ public class PreviewTextFragment extends FileFragment { private static final String TAG = PreviewTextFragment.class.getSimpleName(); private Account mAccount; - private ListView mTextPreviewList; + private TextView mTextPreview; /** * Creates an empty fragment for previews. @@ -75,8 +65,7 @@ public class PreviewTextFragment extends FileFragment { View ret = inflater.inflate(R.layout.text_file_preview, container, false); - mTextPreviewList = (ListView) ret.findViewById(R.id.text_preview_list); - mTextPreviewList.setAdapter(new TextLineAdapter()); + mTextPreview = (TextView) ret.findViewById(R.id.text_preview); return ret; } @@ -131,54 +120,40 @@ public class PreviewTextFragment extends FileFragment { } private void loadAndShowTextPreview() { - new TextLoadAsyncTask().execute(getFile().getStoragePath()); + new TextLoadAsyncTask().execute(getFile().getStoragePath(), mTextPreview); } /** * Reads the file to preview and shows its contents. Too critical to be anonymous. */ - private class TextLoadAsyncTask extends AsyncTask { - private int TEXTVIEW_WIDTH; - private float TEXTVIEW_SIZE; - private final Queue accumulatedText = new LinkedList(); + private class TextLoadAsyncTask extends AsyncTask { private final String DIALOG_WAIT_TAG = "DIALOG_WAIT"; - private final Rect bounds = new Rect(); - private final Paint paint = new Paint(); + private TextView mTextView; - @SuppressLint("InflateParams") @Override protected void onPreExecute() { - ((TextLineAdapter) mTextPreviewList.getAdapter()).clear(); - DisplayMetrics displaymetrics = new DisplayMetrics(); - getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); - TEXTVIEW_WIDTH = displaymetrics.widthPixels; - TEXTVIEW_SIZE = ((TextView) ((LayoutInflater) getActivity().getApplicationContext() - .getSystemService(Context.LAYOUT_INFLATER_SERVICE)) - .inflate( - R.layout.text_file_preview_list_item, null)).getTextSize(); showLoadingDialog(); - paint.setTextSize(TEXTVIEW_SIZE); } @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 != 2) + throw new IllegalArgumentException("The parameters to " + TextLoadAsyncTask.class.getName() + " must be (1) the file location and (2) the text view to update"); final String location = (String) params[0]; + mTextView = (TextView) params[1]; 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) { @@ -195,44 +170,13 @@ public class PreviewTextFragment extends FileFragment { sc.close(); } } - //Add the remaining text, if any - while (!accumulatedText.isEmpty()) { - addLine(); - } - return null; + return source; } @Override - protected void onProgressUpdate(StringWriter... values) { - super.onProgressUpdate(values); - final char[] newTextAsCharArray = values[0].toString().toCharArray(); - for (char c : newTextAsCharArray) { - accumulatedText.add(c); - } - addLine(); - } - - private synchronized void addLine() { - StringBuilder textForThisLine = new StringBuilder(); - do { - Character polled = accumulatedText.poll(); - textForThisLine.append(polled); - } - while (!isTooLarge(textForThisLine.toString()) && !accumulatedText.isEmpty()); - String line = textForThisLine.toString(); - ((TextLineAdapter) mTextPreviewList.getAdapter()).add(line.contentEquals("null") ? "" : line); - } - - private boolean isTooLarge(String text) { - paint.getTextBounds(text, 0, text.length(), bounds); - int lineWidth = (int) Math.ceil(bounds.width()); - return lineWidth / TEXTVIEW_WIDTH > 1; - } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - mTextPreviewList.setVisibility(View.VISIBLE); + protected void onPostExecute(final StringWriter stringWriter) { + mTextView.setText(new String(stringWriter.getBuffer())); + mTextView.setVisibility(View.VISIBLE); dismissLoadingDialog(); } @@ -259,84 +203,6 @@ public class PreviewTextFragment extends FileFragment { } } - private class TextLineAdapter extends BaseAdapter { - private static final int LIST_ITEM_LAYOUT = R.layout.text_file_preview_list_item; - private final List items = new ArrayList(); - - private void add(String line) { - items.add(line); - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - notifyDataSetChanged(); - } - }); - } - - private void clear() { - items.clear(); - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - notifyDataSetChanged(); - } - }); - } - - @Override - public int getCount() { - return items.size(); - } - - @Override - public String getItem(int position) { - if (position >= items.size()) - throw new IllegalArgumentException(); - return items.get(position); - } - - @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); - } else { - viewHolder = (ViewHolder) convertView.getTag(); - } - - 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; - } - } - /** * {@inheritDoc} */ @@ -515,6 +381,11 @@ public class PreviewTextFragment extends FileFragment { * Finishes the preview */ private void finish() { - getSherlockActivity().onBackPressed(); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + getSherlockActivity().onBackPressed(); + } + }); } }