diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 9c38352..adcc9e1 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -14,6 +14,8 @@
+
+
diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/EditFragment.java b/app/src/main/java/com/wbrawner/simplemarkdown/EditFragment.java
index 97a2482..2240011 100644
--- a/app/src/main/java/com/wbrawner/simplemarkdown/EditFragment.java
+++ b/app/src/main/java/com/wbrawner/simplemarkdown/EditFragment.java
@@ -1,6 +1,5 @@
package com.wbrawner.simplemarkdown;
-import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -9,6 +8,7 @@ import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
+import android.support.v4.content.FileProvider;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextWatcher;
@@ -21,11 +21,8 @@ import android.widget.EditText;
import java.io.BufferedReader;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
import butterknife.BindView;
import butterknife.ButterKnife;
@@ -36,12 +33,15 @@ import static android.content.ContentValues.TAG;
public class EditFragment extends Fragment {
public static final String SAVE_ACTION = "com.wbrawner.simplemarkdown.ACTION_SAVE";
public static final String LOAD_ACTION = "com.wbrawner.simplemarkdown.ACTION_LOAD";
+ private static EditText mMarkdownEditor;
+
@BindView(R.id.markdown_edit)
EditText markdownEditor;
- private Activity mContext;
+ private Context mContext;
private File mTmpFile;
+ private boolean loadTmpFile = true;
public EditFragment() {
// Required empty public constructor
@@ -66,11 +66,12 @@ public class EditFragment extends Fragment {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_edit, container, false);
ButterKnife.bind(this, view);
- if (markdownEditor.requestFocus()) {
+ mMarkdownEditor = markdownEditor;
+ if (mMarkdownEditor.requestFocus()) {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
- ;
- markdownEditor.addTextChangedListener(new TextWatcher() {
+
+ mMarkdownEditor.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
@@ -78,7 +79,7 @@ public class EditFragment extends Fragment {
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- updatePreview(markdownEditor.getText());
+ updatePreview(mMarkdownEditor.getText());
}
@Override
@@ -91,29 +92,14 @@ public class EditFragment extends Fragment {
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
- BufferedReader reader = null;
- try {
- File tmpFile = new File(getActivity().getFilesDir() + MainActivity.getTempFileName());
+ if (getActivity().getIntent().getAction().equals(Intent.ACTION_MAIN)) {
+ File tmpFile = new File(getActivity().getFilesDir() + "/" + MainActivity.getTempFileName());
if (tmpFile.exists()) {
- InputStream in = new FileInputStream(tmpFile);
- reader = new BufferedReader(new InputStreamReader(in));
- String line;
- while ((line = reader.readLine()) != null) {
- markdownEditor.append(line);
- markdownEditor.append("\r\n");
- }
- }
- } catch (Exception e) {
- Log.e(TAG, "Error reading temp file: ", e);
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- }
+ FileLoadTask loadTask = new FileLoadTask(mContext, EditFragment.this);
+ loadTask.execute(FileProvider.getUriForFile(mContext, MainActivity.AUTHORITY, tmpFile));
}
}
- updatePreview(markdownEditor.getText());
+ updatePreview(mMarkdownEditor.getText());
}
private void updatePreview(Editable data) {
@@ -124,6 +110,7 @@ public class EditFragment extends Fragment {
}
public void save(String data, String filePath) {
+ // TODO: move this to AsyncTask
if (filePath == null)
filePath = MainActivity.getFilePath() + MainActivity.getFileName();
FileOutputStream out = null;
@@ -153,11 +140,14 @@ public class EditFragment extends Fragment {
@Override
public void onPause() {
- save(markdownEditor.getText().toString(),
+ save(mMarkdownEditor.getText().toString(),
MainActivity.getTempFilePath() + MainActivity.getFileName());
super.onPause();
}
+ public void setEditorText(String s) {
+ mMarkdownEditor.setText(s);
+ }
private class MarkdownBroadcastSaveReceiver extends BroadcastReceiver {
@Override
@@ -167,20 +157,16 @@ public class EditFragment extends Fragment {
case SAVE_ACTION:
if (intent.hasExtra("fileName")) {
String fileName = intent.getStringExtra("fileName");
- save(markdownEditor.getText().toString(), fileName);
+ save(mMarkdownEditor.getText().toString(), fileName);
}
break;
case LOAD_ACTION:
if (intent.hasExtra("fileUri")) {
- load(Uri.parse(intent.getStringExtra("fileUri")));
+ FileLoadTask loadTask = new FileLoadTask(mContext, EditFragment.this);
+ loadTask.execute(Uri.parse(intent.getStringExtra("fileUri")));
}
break;
}
}
}
-
- private void load(Uri fileUri) {
- FileLoadTask loadTask = new FileLoadTask(mContext, markdownEditor);
- loadTask.execute(fileUri);
- }
}
diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/FileLoadTask.java b/app/src/main/java/com/wbrawner/simplemarkdown/FileLoadTask.java
index 4600f55..8d1dc0d 100644
--- a/app/src/main/java/com/wbrawner/simplemarkdown/FileLoadTask.java
+++ b/app/src/main/java/com/wbrawner/simplemarkdown/FileLoadTask.java
@@ -7,6 +7,8 @@ import android.util.Log;
import android.widget.EditText;
import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -16,32 +18,37 @@ import static android.content.ContentValues.TAG;
* Created by billy on 7/25/17.
*/
-public class FileLoadTask extends AsyncTask {
+public class FileLoadTask extends AsyncTask {
private Context mContext;
- private EditText mMarkdownEditor;
+ private EditFragment mEditFragment;
- public FileLoadTask(Context context, EditText markdownEditor) {
+ public FileLoadTask(Context context, EditFragment editFragment) {
mContext = context;
- mMarkdownEditor = markdownEditor;
+ mEditFragment = editFragment;
}
@Override
protected String doInBackground(Uri... uris) {
if (mContext == null) {
- Log.d(TAG, "No context, abort");
return null;
}
- Log.d(TAG, "Begin loading file");
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
+ FileOutputStream out = null;
try {
InputStream in = mContext.getContentResolver().openInputStream(uris[0]);
+ File tmpFile = new File(mContext.getFilesDir() + "/" + MainActivity.getTempFileName());
+ if (tmpFile.exists())
+ tmpFile.delete();
+ out = new FileOutputStream(tmpFile);
reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\r\n");
+ out.write(line.getBytes());
+ out.write("\r\n".getBytes());
}
} catch (Exception e) {
Log.e(TAG, "Error opening file:", e);
@@ -52,13 +59,18 @@ public class FileLoadTask extends AsyncTask {
} catch (Exception e) {
}
}
+ if (out != null) {
+ try {
+ out.close();
+ } catch (Exception e) {}
+ }
}
return sb.toString();
}
@Override
protected void onPostExecute(String s) {
- mMarkdownEditor.setText(s);
super.onPostExecute(s);
+ mEditFragment.setEditorText(s);
}
-}
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/MainActivity.java b/app/src/main/java/com/wbrawner/simplemarkdown/MainActivity.java
index f0aa932..b3da09f 100644
--- a/app/src/main/java/com/wbrawner/simplemarkdown/MainActivity.java
+++ b/app/src/main/java/com/wbrawner/simplemarkdown/MainActivity.java
@@ -1,6 +1,7 @@
package com.wbrawner.simplemarkdown;
import android.Manifest;
+import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.DialogInterface;
@@ -34,7 +35,7 @@ import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
- private static final String AUTHORITY = "com.wbrawner.simplemarkdown.fileprovider";
+ public static final String AUTHORITY = "com.wbrawner.simplemarkdown.fileprovider";
private static final int REQUEST_WRITE_STORAGE = 0;
private static File mFilesDir;
@BindView(R.id.pager) ViewPager pager;
@@ -77,7 +78,7 @@ public class MainActivity extends AppCompatActivity {
mFilesDir = getFilesDir();
checkDirectories();
Intent intent = getIntent();
- if (intent != null && intent.getData() != null) {
+ if (intent != null && !intent.getAction().equals(Intent.ACTION_MAIN) && intent.getData() != null) {
Intent loadIntent = new Intent(EditFragment.LOAD_ACTION);
loadIntent.putExtra("fileUri", intent.getData().toString());
LocalBroadcastManager.getInstance(getApplicationContext())