Skip to content

Commit

Permalink
Add read task
Browse files Browse the repository at this point in the history
  • Loading branch information
billthefarmer committed Jul 22, 2017
1 parent e1a6ae9 commit 3841084
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 41 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ Android simple text editor

![Editor](https://github.com/billthefarmer/billthefarmer.github.io/raw/master/images/Editor.png) ![Editor](https://github.com/billthefarmer/billthefarmer.github.io/raw/master/images/Editor-styles.png)

This is a very simple text editor which may be used standalone or to
show or edit any sort of text file from another app. If you select a
text file in a file manager or any similar app you will be offered the
option of using this editor along with whatever other apps you have
installed that can show or edit a text file.
This is a very simple generic text editor which may be used standalone
or to show or edit any sort of text file from another app. If you
select a text file in a file manager or similar app you will be
offered the option of using this editor along with whatever other apps
you have installed that can show or edit a text file.

There are two toolbar items which may appear:
* **Open** - Open a text file using a chooser
* **Save** - Save the current file if modified

If you touch the back button, and the file has been modified, you will
be prompted whether you want to exit, else the editor will just exit.
If you touch the back button, and the current file has been modified,
you will be prompted whether you want to exit, else the editor will
just exit.
78 changes: 44 additions & 34 deletions src/main/java/org/billthefarmer/editor/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
Expand Down Expand Up @@ -79,18 +80,7 @@ protected void onCreate(Bundle savedInstanceState)
if (savedInstanceState == null)
{
if (uri != null)
{
if (uri.getScheme().equalsIgnoreCase(CONTENT))
uri = resolveContent(uri);

String title = uri.getLastPathSegment();
setTitle(title);

path = uri.getPath();
file = new File(path);
String text = read(file);
textView.setText(text);
}
readFile(uri);
}

else
Expand Down Expand Up @@ -253,16 +243,16 @@ private void openFile()
// readFile
private void readFile(Uri uri)
{
if (uri.getScheme().equalsIgnoreCase("content"))
if (uri.getScheme().equalsIgnoreCase(CONTENT))
uri = resolveContent(uri);

String title = uri.getLastPathSegment();
setTitle(title);

path = uri.getPath();
file = new File(path);
String text = read(file);
textView.setText(text);
ReadTask read = new ReadTask();
read.execute(file);

dirty = false;
invalidateOptionsMenu();
Expand Down Expand Up @@ -292,25 +282,6 @@ private void saveFile()
invalidateOptionsMenu();
}

// read
private static String read(File file)
{
StringBuilder text = new StringBuilder();
try
{
FileReader fileReader = new FileReader(file);
char buffer[] = new char[BUFFER_SIZE];
int n;
while ((n = fileReader.read(buffer)) != -1)
text.append(String.valueOf(buffer, 0, n));
fileReader.close();
}

catch (Exception e) {}

return text.toString();
}

// write
private void write(String text, File file)
{
Expand All @@ -323,4 +294,43 @@ private void write(String text, File file)
}
catch (Exception e) {}
}

// ReadTask
private class ReadTask
extends AsyncTask<File, Integer, String>
{
// doInBackground
@Override
protected String doInBackground(File... params)
{
StringBuilder text = new StringBuilder();
try
{
FileReader fileReader = new FileReader(params[0]);
char buffer[] = new char[BUFFER_SIZE];
int n;
while ((n = fileReader.read(buffer)) != -1)
text.append(String.valueOf(buffer, 0, n));
fileReader.close();
}

catch (Exception e) {}

return text.toString();
}

// onProgressUpdate
@Override
protected void onProgressUpdate(Integer... progress)
{
// no-op
}

// onPostExecute
@Override
protected void onPostExecute(String result)
{
textView.setText(result);
}
}
}

0 comments on commit 3841084

Please sign in to comment.