From 4a5718a445194b6f9bbb4b992dd1e8f2b3a23c59 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Fri, 17 Oct 2014 16:29:38 +0200 Subject: [PATCH 01/33] Added text file preview --- res/layout/file_preview.xml | 66 +- res/layout/text_file_preview.xml | 29 + .../owncloud/android/datamodel/OCFile.java | 145 ++-- .../ui/activity/FileDisplayActivity.java | 686 +++++++++--------- .../ui/fragment/OCFileListFragment.java | 6 +- .../ui/preview/PreviewTextFragment.java | 341 +++++++++ 6 files changed, 845 insertions(+), 428 deletions(-) create mode 100644 res/layout/text_file_preview.xml create mode 100644 src/com/owncloud/android/ui/preview/PreviewTextFragment.java diff --git a/res/layout/file_preview.xml b/res/layout/file_preview.xml index 483a3691b1..0da5363461 100644 --- a/res/layout/file_preview.xml +++ b/res/layout/file_preview.xml @@ -1,5 +1,4 @@ - - + \ 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 d0a678c8af..790cfde649 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -1,13 +1,15 @@ package com.owncloud.android.ui.preview; import android.accounts.Account; +import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ListView; import android.widget.ProgressBar; -import android.widget.ScrollView; import android.widget.TextView; import com.actionbarsherlock.view.Menu; @@ -26,6 +28,8 @@ import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.IOException; import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; import java.util.Scanner; public class PreviewTextFragment extends FileFragment { @@ -34,9 +38,8 @@ public class PreviewTextFragment extends FileFragment { private static final String TAG = PreviewTextFragment.class.getSimpleName(); private Account mAccount; - private TextView mTextPreview; + private ListView mTextPreviewList; private ProgressBar mProgressBar; - private ScrollView mScrollView; /** * Creates an empty fragment for previews. @@ -64,8 +67,8 @@ 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); + mTextPreviewList = (ListView) ret.findViewById(R.id.text_preview_list); + mTextPreviewList.setAdapter(new TextLineAdapter()); mProgressBar = (ProgressBar) ret.findViewById(R.id.progress_bar); return ret; @@ -118,44 +121,41 @@ public class PreviewTextFragment extends FileFragment { public void onStart() { super.onStart(); Log_OC.e(TAG, "onStart"); - - loadAndShowTextPreview(getFile().getStoragePath(), mTextPreview); } - private void loadAndShowTextPreview(String location, TextView textView) { - new TextLoadAsyncTask().execute(location, textView); + private void loadAndShowTextPreview() { + new TextLoadAsyncTask().execute(getFile().getStoragePath()); } /** * Reads the file to preview and shows its contents. Too critical to be anonymous. */ - private class TextLoadAsyncTask extends AsyncTask { - TextView mTextView; + private class TextLoadAsyncTask extends AsyncTask { @Override protected void onPreExecute() { - mProgressBar.setVisibility(View.VISIBLE); + ((TextLineAdapter) mTextPreviewList.getAdapter()).clear(); } @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"); + 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"); 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()) { - bufferedWriter.append(sc.nextLine()); - if (sc.hasNextLine()) bufferedWriter.append("\n"); + StringWriter target = new StringWriter(); + BufferedWriter bufferedWriter = new BufferedWriter(target); + if (sc.hasNextLine()) + bufferedWriter.write(sc.nextLine()); + bufferedWriter.close(); + publishProgress(target); } - bufferedWriter.close(); IOException exc = sc.ioException(); if (exc != null) throw exc; } catch (IOException e) { @@ -172,15 +172,90 @@ public class PreviewTextFragment extends FileFragment { sc.close(); } } - return source; + return null; } @Override - protected void onPostExecute(final StringWriter stringWriter) { - super.onPostExecute(stringWriter); + 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()); + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); mProgressBar.setVisibility(View.GONE); - mScrollView.setVisibility(View.VISIBLE); - mTextView.setText(new String(stringWriter.getBuffer())); + 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 items = new ArrayList<>(); + + private void add(String line) { + items.add(line); + notifyDataSetChanged(); + } + + private void clear() { + items.clear(); + 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; } } @@ -300,6 +375,8 @@ public class PreviewTextFragment extends FileFragment { public void onResume() { super.onResume(); Log_OC.e(TAG, "onResume"); + + loadAndShowTextPreview(); } @Override From cbf2f96a83b3525bc7a8cf23a7dc24ebaaa391aa Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sun, 19 Oct 2014 08:10:54 +0200 Subject: [PATCH 03/33] Fixed a mistake. --- src/com/owncloud/android/ui/preview/PreviewTextFragment.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index 790cfde649..e66cc41d52 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -193,7 +193,7 @@ 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 final List items = new ArrayList(); private void add(String line) { items.add(line); @@ -351,7 +351,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); From bc7bfeec91cc5fd36fc175350f052fcc4e4ba5d4 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sun, 19 Oct 2014 08:32:35 +0200 Subject: [PATCH 04/33] Removed any remains of list appearance --- res/layout/text_file_preview_list_item.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/res/layout/text_file_preview_list_item.xml b/res/layout/text_file_preview_list_item.xml index 269765ead2..0ff7570983 100644 --- a/res/layout/text_file_preview_list_item.xml +++ b/res/layout/text_file_preview_list_item.xml @@ -21,4 +21,5 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/black" + android:maxLines="1" android:layout_gravity="center" /> \ No newline at end of file From 4e6abd4f0ad9758180c3918ebef2c9ff0afc266c Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sat, 25 Oct 2014 20:43:11 +0200 Subject: [PATCH 05/33] Minor updates --- res/layout/text_file_preview.xml | 29 ++++++---------------- res/layout/text_file_preview_list_item.xml | 1 - 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/res/layout/text_file_preview.xml b/res/layout/text_file_preview.xml index d34045c197..1b526bcd9e 100644 --- a/res/layout/text_file_preview.xml +++ b/res/layout/text_file_preview.xml @@ -1,23 +1,10 @@ - - - - - - \ No newline at end of file + android:layout_height="wrap_content" + android:visibility="gone" + android:divider="@null" + android:dividerHeight="0dp" + android:focusable="false" + android:focusableInTouchMode="false" /> \ 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 index 0ff7570983..269765ead2 100644 --- a/res/layout/text_file_preview_list_item.xml +++ b/res/layout/text_file_preview_list_item.xml @@ -21,5 +21,4 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/black" - android:maxLines="1" android:layout_gravity="center" /> \ No newline at end of file From 7644f0b5db335b3f285dee3362f676f21a97e196 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sat, 25 Oct 2014 20:43:23 +0200 Subject: [PATCH 06/33] Fixed line breaks --- .../ui/preview/PreviewTextFragment.java | 97 +++++++++++++++++-- 1 file changed, 88 insertions(+), 9 deletions(-) diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index e66cc41d52..dcdb1d48b4 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -1,15 +1,22 @@ 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.util.Log; import android.view.LayoutInflater; 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; @@ -21,6 +28,7 @@ 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; @@ -29,7 +37,9 @@ 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 { @@ -39,7 +49,6 @@ public class PreviewTextFragment extends FileFragment { private Account mAccount; private ListView mTextPreviewList; - private ProgressBar mProgressBar; /** * Creates an empty fragment for previews. @@ -69,7 +78,6 @@ public class PreviewTextFragment extends FileFragment { mTextPreviewList = (ListView) ret.findViewById(R.id.text_preview_list); mTextPreviewList.setAdapter(new TextLineAdapter()); - mProgressBar = (ProgressBar) ret.findViewById(R.id.progress_bar); return ret; } @@ -131,10 +139,26 @@ public class PreviewTextFragment extends FileFragment { * 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 final String DIALOG_WAIT_TAG = "DIALOG_WAIT"; + private final Rect bounds = new Rect(); + private final Paint paint = new Paint(); + @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 @@ -172,22 +196,67 @@ public class PreviewTextFragment extends FileFragment { sc.close(); } } + //Add the remaining text, if any + while (!accumulatedText.isEmpty()) { + addLine(); + } 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()); + 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); - mProgressBar.setVisibility(View.GONE); mTextPreviewList.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); + } + + /** + * Dismiss loading dialog + */ + public void dismissLoadingDialog() { + Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG); + if (frag != null) { + LoadingDialog loading = (LoadingDialog) frag; + loading.dismiss(); + } } } @@ -197,12 +266,22 @@ public class PreviewTextFragment extends FileFragment { private void add(String line) { items.add(line); - notifyDataSetChanged(); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + notifyDataSetChanged(); + } + }); } private void clear() { items.clear(); - notifyDataSetChanged(); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + notifyDataSetChanged(); + } + }); } @Override From 554cd3300f124bc24e276d1b9810f41b6b1b2373 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sat, 25 Oct 2014 20:43:45 +0200 Subject: [PATCH 07/33] Added Java 1.5 restriction to match the application standards --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 75231ed9f8..9807d7f270 100644 --- a/build.gradle +++ b/build.gradle @@ -63,8 +63,8 @@ android { compileOptions { - sourceCompatibility JavaVersion.VERSION_1_7 - targetCompatibility JavaVersion.VERSION_1_7 + sourceCompatibility JavaVersion.VERSION_1_5 + targetCompatibility JavaVersion.VERSION_1_5 } productFlavors { From 2b9176a9cd312e58c429c26a59aa5dab456ef6d3 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sat, 25 Oct 2014 21:32:21 +0200 Subject: [PATCH 08/33] Added some margins. --- res/layout/text_file_preview.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/res/layout/text_file_preview.xml b/res/layout/text_file_preview.xml index 1b526bcd9e..f33b3d8046 100644 --- a/res/layout/text_file_preview.xml +++ b/res/layout/text_file_preview.xml @@ -7,4 +7,6 @@ android:divider="@null" android:dividerHeight="0dp" android:focusable="false" - android:focusableInTouchMode="false" /> \ No newline at end of file + android:focusableInTouchMode="false" + android:layout_marginStart="8dp" + android:layout_marginEnd="8dp" /> \ No newline at end of file From 96232b1cceb8d2fbf152dd978ad2f323e499dd5b Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sat, 25 Oct 2014 21:32:38 +0200 Subject: [PATCH 09/33] Removed unnecessary actions --- .../ui/preview/PreviewTextFragment.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index dcdb1d48b4..07445db55b 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -11,7 +11,6 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.util.DisplayMetrics; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -384,6 +383,30 @@ public class PreviewTextFragment extends FileFragment { item.setVisible(false); 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); + item.setEnabled(false); + } + + item = menu.findItem(R.id.action_sync_account); + if (item != null) { + item.setVisible(false); + item.setEnabled(false); + } } /** From 7414d163f00fa02e15dc15cd774266d105df8050 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sun, 26 Oct 2014 09:09:59 +0100 Subject: [PATCH 10/33] Solved 'Share with copied link' flow --- .../owncloud/android/ui/activity/FileDisplayActivity.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index 63ca229bb9..60636df479 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -1411,9 +1411,11 @@ public class FileDisplayActivity extends HookActivity implements if (details instanceof PreviewMediaFragment) { // Refresh OCFile of the fragment ((PreviewMediaFragment) details).updateFile(file); - } else { + } else if (details instanceof PreviewTextFragment) { + // Refresh OCFile of the fragment + ((PreviewTextFragment) details).updateFile(file); + } else showDetails(file); - } } invalidateOptionsMenu(); } From afefef9ae3194024dc82c590d632aed5c76e3fd5 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sun, 26 Oct 2014 10:45:06 +0100 Subject: [PATCH 11/33] Reverted the text preview system to a single TextView --- res/layout/text_file_preview.xml | 41 ++-- res/layout/text_file_preview_list_item.xml | 24 --- .../ui/preview/PreviewTextFragment.java | 179 +++--------------- 3 files changed, 55 insertions(+), 189 deletions(-) delete mode 100644 res/layout/text_file_preview_list_item.xml diff --git a/res/layout/text_file_preview.xml b/res/layout/text_file_preview.xml index f33b3d8046..aa6cf76c53 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 269765ead2..0000000000 --- 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 07445db55b..1be63998a5 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(); + } + }); } } From 46ff60a9e760a760b478609bac68d45f8728488f Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sun, 26 Oct 2014 15:38:57 +0100 Subject: [PATCH 12/33] Fixed a bug that would cause the details view to be shown upon turning off and on the screen --- .../owncloud/android/ui/activity/FileDisplayActivity.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index 60636df479..1bf8365a70 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -303,6 +303,8 @@ public class FileDisplayActivity extends HookActivity implements } else { cleanSecondFragment(); + if (file.isDown() && PreviewTextFragment.canBePreviewed(file)) + startTextPreview(file); } } else { @@ -326,9 +328,9 @@ public class FileDisplayActivity extends HookActivity implements boolean autoplay = getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true); secondFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition, autoplay); - } else { - secondFragment = new FileDetailFragment(file, getAccount()); - } + } else if (file.isDown() && PreviewTextFragment.canBePreviewed(file)) { + secondFragment = null; + } else secondFragment = new FileDetailFragment(file, getAccount()); } return secondFragment; } From 7cc88a22d183a5bc82ddbd30945789d58e0a753b Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Fri, 17 Oct 2014 16:29:38 +0200 Subject: [PATCH 13/33] Added text file preview --- res/layout/file_preview.xml | 66 +- res/layout/text_file_preview.xml | 29 + .../owncloud/android/datamodel/OCFile.java | 11 +- .../ui/activity/FileDisplayActivity.java | 751 +++++++++--------- .../ui/fragment/OCFileListFragment.java | 6 +- .../ui/preview/PreviewTextFragment.java | 341 ++++++++ 6 files changed, 809 insertions(+), 395 deletions(-) create mode 100644 res/layout/text_file_preview.xml create mode 100644 src/com/owncloud/android/ui/preview/PreviewTextFragment.java diff --git a/res/layout/file_preview.xml b/res/layout/file_preview.xml index 483a3691b1..0da5363461 100644 --- a/res/layout/file_preview.xml +++ b/res/layout/file_preview.xml @@ -1,5 +1,4 @@ - - + \ 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 d0a678c8af..790cfde649 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -1,13 +1,15 @@ package com.owncloud.android.ui.preview; import android.accounts.Account; +import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ListView; import android.widget.ProgressBar; -import android.widget.ScrollView; import android.widget.TextView; import com.actionbarsherlock.view.Menu; @@ -26,6 +28,8 @@ import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.IOException; import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; import java.util.Scanner; public class PreviewTextFragment extends FileFragment { @@ -34,9 +38,8 @@ public class PreviewTextFragment extends FileFragment { private static final String TAG = PreviewTextFragment.class.getSimpleName(); private Account mAccount; - private TextView mTextPreview; + private ListView mTextPreviewList; private ProgressBar mProgressBar; - private ScrollView mScrollView; /** * Creates an empty fragment for previews. @@ -64,8 +67,8 @@ 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); + mTextPreviewList = (ListView) ret.findViewById(R.id.text_preview_list); + mTextPreviewList.setAdapter(new TextLineAdapter()); mProgressBar = (ProgressBar) ret.findViewById(R.id.progress_bar); return ret; @@ -118,44 +121,41 @@ public class PreviewTextFragment extends FileFragment { public void onStart() { super.onStart(); Log_OC.e(TAG, "onStart"); - - loadAndShowTextPreview(getFile().getStoragePath(), mTextPreview); } - private void loadAndShowTextPreview(String location, TextView textView) { - new TextLoadAsyncTask().execute(location, textView); + private void loadAndShowTextPreview() { + new TextLoadAsyncTask().execute(getFile().getStoragePath()); } /** * Reads the file to preview and shows its contents. Too critical to be anonymous. */ - private class TextLoadAsyncTask extends AsyncTask { - TextView mTextView; + private class TextLoadAsyncTask extends AsyncTask { @Override protected void onPreExecute() { - mProgressBar.setVisibility(View.VISIBLE); + ((TextLineAdapter) mTextPreviewList.getAdapter()).clear(); } @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"); + 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"); 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()) { - bufferedWriter.append(sc.nextLine()); - if (sc.hasNextLine()) bufferedWriter.append("\n"); + StringWriter target = new StringWriter(); + BufferedWriter bufferedWriter = new BufferedWriter(target); + if (sc.hasNextLine()) + bufferedWriter.write(sc.nextLine()); + bufferedWriter.close(); + publishProgress(target); } - bufferedWriter.close(); IOException exc = sc.ioException(); if (exc != null) throw exc; } catch (IOException e) { @@ -172,15 +172,90 @@ public class PreviewTextFragment extends FileFragment { sc.close(); } } - return source; + return null; } @Override - protected void onPostExecute(final StringWriter stringWriter) { - super.onPostExecute(stringWriter); + 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()); + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); mProgressBar.setVisibility(View.GONE); - mScrollView.setVisibility(View.VISIBLE); - mTextView.setText(new String(stringWriter.getBuffer())); + 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 items = new ArrayList<>(); + + private void add(String line) { + items.add(line); + notifyDataSetChanged(); + } + + private void clear() { + items.clear(); + 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; } } @@ -300,6 +375,8 @@ public class PreviewTextFragment extends FileFragment { public void onResume() { super.onResume(); Log_OC.e(TAG, "onResume"); + + loadAndShowTextPreview(); } @Override From fac333290088b5650e01a9600aa81ce4309b8c9c Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sun, 19 Oct 2014 08:10:54 +0200 Subject: [PATCH 15/33] Fixed a mistake. --- src/com/owncloud/android/ui/preview/PreviewTextFragment.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index 790cfde649..e66cc41d52 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -193,7 +193,7 @@ 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 final List items = new ArrayList(); private void add(String line) { items.add(line); @@ -351,7 +351,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); From 65e82bff36af6fbfb640b4f77f8c1a9c2a1b5097 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sun, 19 Oct 2014 08:32:35 +0200 Subject: [PATCH 16/33] Removed any remains of list appearance --- res/layout/text_file_preview_list_item.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/res/layout/text_file_preview_list_item.xml b/res/layout/text_file_preview_list_item.xml index 269765ead2..0ff7570983 100644 --- a/res/layout/text_file_preview_list_item.xml +++ b/res/layout/text_file_preview_list_item.xml @@ -21,4 +21,5 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/black" + android:maxLines="1" android:layout_gravity="center" /> \ No newline at end of file From 3877ddf85aa4edf190b76f7ac6ba07e34cd659c1 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sat, 25 Oct 2014 20:43:11 +0200 Subject: [PATCH 17/33] Minor updates --- res/layout/text_file_preview.xml | 29 ++++++---------------- res/layout/text_file_preview_list_item.xml | 1 - 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/res/layout/text_file_preview.xml b/res/layout/text_file_preview.xml index d34045c197..1b526bcd9e 100644 --- a/res/layout/text_file_preview.xml +++ b/res/layout/text_file_preview.xml @@ -1,23 +1,10 @@ - - - - - - \ No newline at end of file + android:layout_height="wrap_content" + android:visibility="gone" + android:divider="@null" + android:dividerHeight="0dp" + android:focusable="false" + android:focusableInTouchMode="false" /> \ 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 index 0ff7570983..269765ead2 100644 --- a/res/layout/text_file_preview_list_item.xml +++ b/res/layout/text_file_preview_list_item.xml @@ -21,5 +21,4 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/black" - android:maxLines="1" android:layout_gravity="center" /> \ No newline at end of file From 1629981256c61b862caa63bf7a1e9c8e2fe0b59d Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sat, 25 Oct 2014 20:43:23 +0200 Subject: [PATCH 18/33] Fixed line breaks --- .../ui/preview/PreviewTextFragment.java | 97 +++++++++++++++++-- 1 file changed, 88 insertions(+), 9 deletions(-) diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index e66cc41d52..dcdb1d48b4 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -1,15 +1,22 @@ 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.util.Log; import android.view.LayoutInflater; 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; @@ -21,6 +28,7 @@ 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; @@ -29,7 +37,9 @@ 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 { @@ -39,7 +49,6 @@ public class PreviewTextFragment extends FileFragment { private Account mAccount; private ListView mTextPreviewList; - private ProgressBar mProgressBar; /** * Creates an empty fragment for previews. @@ -69,7 +78,6 @@ public class PreviewTextFragment extends FileFragment { mTextPreviewList = (ListView) ret.findViewById(R.id.text_preview_list); mTextPreviewList.setAdapter(new TextLineAdapter()); - mProgressBar = (ProgressBar) ret.findViewById(R.id.progress_bar); return ret; } @@ -131,10 +139,26 @@ public class PreviewTextFragment extends FileFragment { * 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 final String DIALOG_WAIT_TAG = "DIALOG_WAIT"; + private final Rect bounds = new Rect(); + private final Paint paint = new Paint(); + @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 @@ -172,22 +196,67 @@ public class PreviewTextFragment extends FileFragment { sc.close(); } } + //Add the remaining text, if any + while (!accumulatedText.isEmpty()) { + addLine(); + } 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()); + 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); - mProgressBar.setVisibility(View.GONE); mTextPreviewList.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); + } + + /** + * Dismiss loading dialog + */ + public void dismissLoadingDialog() { + Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG); + if (frag != null) { + LoadingDialog loading = (LoadingDialog) frag; + loading.dismiss(); + } } } @@ -197,12 +266,22 @@ public class PreviewTextFragment extends FileFragment { private void add(String line) { items.add(line); - notifyDataSetChanged(); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + notifyDataSetChanged(); + } + }); } private void clear() { items.clear(); - notifyDataSetChanged(); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + notifyDataSetChanged(); + } + }); } @Override From c9105c61d91f5b9a04fd432d93d84943707b9421 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sat, 25 Oct 2014 20:43:45 +0200 Subject: [PATCH 19/33] Added Java 1.5 restriction to match the application standards --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 75231ed9f8..9807d7f270 100644 --- a/build.gradle +++ b/build.gradle @@ -63,8 +63,8 @@ android { compileOptions { - sourceCompatibility JavaVersion.VERSION_1_7 - targetCompatibility JavaVersion.VERSION_1_7 + sourceCompatibility JavaVersion.VERSION_1_5 + targetCompatibility JavaVersion.VERSION_1_5 } productFlavors { From 0c384167c1fae57b23032269c4e61a9856616c4b Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sat, 25 Oct 2014 21:32:21 +0200 Subject: [PATCH 20/33] Added some margins. --- res/layout/text_file_preview.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/res/layout/text_file_preview.xml b/res/layout/text_file_preview.xml index 1b526bcd9e..f33b3d8046 100644 --- a/res/layout/text_file_preview.xml +++ b/res/layout/text_file_preview.xml @@ -7,4 +7,6 @@ android:divider="@null" android:dividerHeight="0dp" android:focusable="false" - android:focusableInTouchMode="false" /> \ No newline at end of file + android:focusableInTouchMode="false" + android:layout_marginStart="8dp" + android:layout_marginEnd="8dp" /> \ No newline at end of file From 5aa4d0e6c88940a88069494e5a4d1678ebaac146 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sat, 25 Oct 2014 21:32:38 +0200 Subject: [PATCH 21/33] Removed unnecessary actions --- .../ui/preview/PreviewTextFragment.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index dcdb1d48b4..07445db55b 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -11,7 +11,6 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.util.DisplayMetrics; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -384,6 +383,30 @@ public class PreviewTextFragment extends FileFragment { item.setVisible(false); 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); + item.setEnabled(false); + } + + item = menu.findItem(R.id.action_sync_account); + if (item != null) { + item.setVisible(false); + item.setEnabled(false); + } } /** From 9bdb2cd17a518059692c2cb2a0408303393ab91f Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sun, 26 Oct 2014 09:09:59 +0100 Subject: [PATCH 22/33] Solved 'Share with copied link' flow --- .../owncloud/android/ui/activity/FileDisplayActivity.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index 820ed3e698..58b39967b1 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -1446,9 +1446,11 @@ public class FileDisplayActivity extends HookActivity implements if (details instanceof PreviewMediaFragment) { // Refresh OCFile of the fragment ((PreviewMediaFragment) details).updateFile(file); - } else { + } else if (details instanceof PreviewTextFragment) { + // Refresh OCFile of the fragment + ((PreviewTextFragment) details).updateFile(file); + } else showDetails(file); - } } invalidateOptionsMenu(); } From 093bf4fb5caf9045209ed5cf6c6fb70e4eb67da2 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sun, 26 Oct 2014 10:45:06 +0100 Subject: [PATCH 23/33] Reverted the text preview system to a single TextView --- res/layout/text_file_preview.xml | 41 ++-- res/layout/text_file_preview_list_item.xml | 24 --- .../ui/preview/PreviewTextFragment.java | 179 +++--------------- 3 files changed, 55 insertions(+), 189 deletions(-) delete mode 100644 res/layout/text_file_preview_list_item.xml diff --git a/res/layout/text_file_preview.xml b/res/layout/text_file_preview.xml index f33b3d8046..aa6cf76c53 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 269765ead2..0000000000 --- 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 07445db55b..1be63998a5 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(); + } + }); } } From b1ef5d94d4c430be3bf7839eff0e7b37a03ed2b7 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Sun, 26 Oct 2014 15:38:57 +0100 Subject: [PATCH 24/33] Fixed a bug that would cause the details view to be shown upon turning off and on the screen --- .../owncloud/android/ui/activity/FileDisplayActivity.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index 58b39967b1..627d40cf2f 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -304,6 +304,8 @@ public class FileDisplayActivity extends HookActivity implements } else { cleanSecondFragment(); + if (file.isDown() && PreviewTextFragment.canBePreviewed(file)) + startTextPreview(file); } } else { @@ -327,9 +329,9 @@ public class FileDisplayActivity extends HookActivity implements boolean autoplay = getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true); secondFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition, autoplay); - } else { - secondFragment = new FileDetailFragment(file, getAccount()); - } + } else if (file.isDown() && PreviewTextFragment.canBePreviewed(file)) { + secondFragment = null; + } else secondFragment = new FileDetailFragment(file, getAccount()); } return secondFragment; } From 244758e8506a438fceb678885ce7e998308e934c Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Tue, 4 Nov 2014 11:20:25 +0100 Subject: [PATCH 25/33] Update Gradle plugin to 0.14 for compatibility with Android Studio 0.9.0 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 9807d7f270..51544bee90 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:0.13.0' + classpath 'com.android.tools.build:gradle:0.14.0' } } From f2020942861b0167ea01edc621bc51f571f0d4b9 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Tue, 4 Nov 2014 16:23:14 +0100 Subject: [PATCH 26/33] Track right branch --- .gitmodules | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitmodules b/.gitmodules index c069d93b4a..1756096016 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,3 +2,4 @@ [submodule "owncloud-android-library"] path = owncloud-android-library url = git://github.com/owncloudChalmers/android-library.git + branch = buildsystem/gradle_develop From 1c5416e4cd0db177c92682471a34e7840a17a184 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Tue, 4 Nov 2014 16:25:55 +0100 Subject: [PATCH 27/33] Cleanup --- src/com/owncloud/android/datamodel/OCFile.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/com/owncloud/android/datamodel/OCFile.java b/src/com/owncloud/android/datamodel/OCFile.java index d568ea3ca7..5e1bd5facd 100644 --- a/src/com/owncloud/android/datamodel/OCFile.java +++ b/src/com/owncloud/android/datamodel/OCFile.java @@ -541,8 +541,7 @@ public class OCFile implements Parcelable, Comparable { * @return 'True' if the file is simple text (e.g. not application-dependent, like .doc or .docx) */ public boolean isText() { - return !isFolder() && !isAudio() && !isVideo() && !isImage() && ((mMimeType != null && mMimeType.startsWith("text/")) || - getMimeTypeFromName().startsWith("text/")); + return ((mMimeType != null && mMimeType.startsWith("text/")) || getMimeTypeFromName().startsWith("text/")); } public String getMimeTypeFromName() { From 6cc391748e6403f7ac17a7ba079c5398900d70b0 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Wed, 3 Dec 2014 15:11:59 +0100 Subject: [PATCH 28/33] Update submodule --- owncloud-android-library | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/owncloud-android-library b/owncloud-android-library index 7ff0bc0d83..8261865ff2 160000 --- a/owncloud-android-library +++ b/owncloud-android-library @@ -1 +1 @@ -Subproject commit 7ff0bc0d837edea90b075140f8caba308ce7d378 +Subproject commit 8261865ff24c1bfc05be19ae9364a66dac8f26c3 From 9a2f6c6c4b2428350eaa52979e816e1cebf08530 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Wed, 24 Jun 2015 22:12:36 +0200 Subject: [PATCH 29/33] Support margins for API levels pre-17 --- res/layout/text_file_preview.xml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/res/layout/text_file_preview.xml b/res/layout/text_file_preview.xml index aa6cf76c53..b144875a89 100644 --- a/res/layout/text_file_preview.xml +++ b/res/layout/text_file_preview.xml @@ -17,15 +17,17 @@ --> + android:layout_width="match_parent" + android:layout_height="match_parent" + android:fillViewport="true"> + android:layout_marginLeft="8dp" + android:layout_marginRight="8dp" + android:layout_marginStart="8dp" + android:visibility="gone"/> \ No newline at end of file From 4c17d77a6cfb386c6c36348d9d9860d79863a882 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Wed, 24 Jun 2015 22:14:58 +0200 Subject: [PATCH 30/33] Added braces to single-line if statements --- .../android/ui/preview/PreviewTextFragment.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index 1be63998a5..107b2a2f56 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -81,12 +81,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) { @@ -137,8 +138,8 @@ public class PreviewTextFragment extends FileFragment { @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 != 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]; @@ -195,7 +196,7 @@ public class PreviewTextFragment extends FileFragment { * Dismiss loading dialog */ public void dismissLoadingDialog() { - Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG); + final Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG); if (frag != null) { LoadingDialog loading = (LoadingDialog) frag; loading.dismiss(); From 1508e99e9ab6e43de4686ce521e22487ccccfb69 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Wed, 24 Jun 2015 22:24:39 +0200 Subject: [PATCH 31/33] Added exception logging --- src/com/owncloud/android/ui/preview/PreviewTextFragment.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index 107b2a2f56..04641198fa 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -158,12 +158,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(); } } From cc0627db93fbf4566681f0de1dea8c1705524162 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Wed, 24 Jun 2015 22:38:39 +0200 Subject: [PATCH 32/33] Implementation of the requested changes to the asynctask that loads the text --- .../ui/preview/PreviewTextFragment.java | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index 04641198fa..285a56ac16 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -9,7 +9,6 @@ import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ScrollView; import android.widget.TextView; import com.actionbarsherlock.view.Menu; @@ -29,6 +28,7 @@ import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.IOException; import java.io.StringWriter; +import java.lang.ref.WeakReference; import java.util.Scanner; public class PreviewTextFragment extends FileFragment { @@ -38,6 +38,7 @@ public class PreviewTextFragment extends FileFragment { private Account mAccount; private TextView mTextPreview; + private TextLoadAsyncTask mTextLoadTask; /** * Creates an empty fragment for previews. @@ -81,9 +82,9 @@ public class PreviewTextFragment extends FileFragment { Bundle args = getArguments(); - if (file == null){ + if (file == null) { file = args.getParcelable(FileDisplayActivity.EXTRA_FILE); - } + } if (mAccount == null) { mAccount = args.getParcelable(FileDisplayActivity.EXTRA_ACCOUNT); @@ -121,15 +122,22 @@ public class PreviewTextFragment extends FileFragment { } private void loadAndShowTextPreview() { - new TextLoadAsyncTask().execute(getFile().getStoragePath(), mTextPreview); + mTextLoadTask = new TextLoadAsyncTask(new WeakReference(mTextPreview)); + mTextLoadTask.execute(getFile().getStoragePath()); } + /** * Reads the file to preview and shows its contents. Too critical to be anonymous. */ private class TextLoadAsyncTask extends AsyncTask { private final String DIALOG_WAIT_TAG = "DIALOG_WAIT"; - private TextView mTextView; + private final WeakReference mTextViewReference; + + private TextLoadAsyncTask(WeakReference textView) { + mTextViewReference = textView; + } + @Override protected void onPreExecute() { @@ -138,10 +146,10 @@ public class PreviewTextFragment extends FileFragment { @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; @@ -178,8 +186,13 @@ public class PreviewTextFragment extends FileFragment { @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(); } @@ -360,6 +373,8 @@ public class PreviewTextFragment extends FileFragment { public void onStop() { super.onStop(); Log_OC.e(TAG, "onStop"); + if (mTextLoadTask != null) + mTextLoadTask.cancel(Boolean.TRUE); } /** From efbab82a6e21eeebe2f25a86d48fd56a4812e26f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Antonio=20D=C3=ADaz-Benito=20Soriano?= Date: Thu, 25 Jun 2015 12:49:45 +0200 Subject: [PATCH 33/33] Moved text load call to onStart --- src/com/owncloud/android/ui/preview/PreviewTextFragment.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index 285a56ac16..bae32e131c 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -119,6 +119,8 @@ public class PreviewTextFragment extends FileFragment { public void onStart() { super.onStart(); Log_OC.e(TAG, "onStart"); + + loadAndShowTextPreview(); } private void loadAndShowTextPreview() { @@ -359,8 +361,6 @@ public class PreviewTextFragment extends FileFragment { public void onResume() { super.onResume(); Log_OC.e(TAG, "onResume"); - - loadAndShowTextPreview(); } @Override