+ 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);
+ 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();
+ }
+ }
+ }
+
+ 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);
+ 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;