Fixed file loading and app icon not showing up in app drawer
This commit is contained in:
parent
7a39fd947c
commit
90ae21724a
4 changed files with 48 additions and 47 deletions
|
@ -14,6 +14,8 @@
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN"></action>
|
<action android:name="android.intent.action.MAIN"></action>
|
||||||
<category android:name="android.intent.category.LAUNCHER"></category>
|
<category android:name="android.intent.category.LAUNCHER"></category>
|
||||||
|
</intent-filter>
|
||||||
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.EDIT" />
|
<action android:name="android.intent.action.EDIT" />
|
||||||
<action android:name="android.intent.action.VIEW" />
|
<action android:name="android.intent.action.VIEW" />
|
||||||
<category android:name="android.intent.category.DEFAULT"/>
|
<category android:name="android.intent.category.DEFAULT"/>
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package com.wbrawner.simplemarkdown;
|
package com.wbrawner.simplemarkdown;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
@ -9,6 +8,7 @@ import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
|
import android.support.v4.content.FileProvider;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
import android.text.Editable;
|
import android.text.Editable;
|
||||||
import android.text.TextWatcher;
|
import android.text.TextWatcher;
|
||||||
|
@ -21,11 +21,8 @@ import android.widget.EditText;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
|
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
|
@ -36,12 +33,15 @@ import static android.content.ContentValues.TAG;
|
||||||
public class EditFragment extends Fragment {
|
public class EditFragment extends Fragment {
|
||||||
public static final String SAVE_ACTION = "com.wbrawner.simplemarkdown.ACTION_SAVE";
|
public static final String SAVE_ACTION = "com.wbrawner.simplemarkdown.ACTION_SAVE";
|
||||||
public static final String LOAD_ACTION = "com.wbrawner.simplemarkdown.ACTION_LOAD";
|
public static final String LOAD_ACTION = "com.wbrawner.simplemarkdown.ACTION_LOAD";
|
||||||
|
private static EditText mMarkdownEditor;
|
||||||
|
|
||||||
@BindView(R.id.markdown_edit)
|
@BindView(R.id.markdown_edit)
|
||||||
EditText markdownEditor;
|
EditText markdownEditor;
|
||||||
|
|
||||||
private Activity mContext;
|
private Context mContext;
|
||||||
|
|
||||||
private File mTmpFile;
|
private File mTmpFile;
|
||||||
|
private boolean loadTmpFile = true;
|
||||||
|
|
||||||
public EditFragment() {
|
public EditFragment() {
|
||||||
// Required empty public constructor
|
// Required empty public constructor
|
||||||
|
@ -66,11 +66,12 @@ public class EditFragment extends Fragment {
|
||||||
// Inflate the layout for this fragment
|
// Inflate the layout for this fragment
|
||||||
View view = inflater.inflate(R.layout.fragment_edit, container, false);
|
View view = inflater.inflate(R.layout.fragment_edit, container, false);
|
||||||
ButterKnife.bind(this, view);
|
ButterKnife.bind(this, view);
|
||||||
if (markdownEditor.requestFocus()) {
|
mMarkdownEditor = markdownEditor;
|
||||||
|
if (mMarkdownEditor.requestFocus()) {
|
||||||
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
||||||
}
|
}
|
||||||
;
|
|
||||||
markdownEditor.addTextChangedListener(new TextWatcher() {
|
mMarkdownEditor.addTextChangedListener(new TextWatcher() {
|
||||||
@Override
|
@Override
|
||||||
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
||||||
|
|
||||||
|
@ -78,7 +79,7 @@ public class EditFragment extends Fragment {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
||||||
updatePreview(markdownEditor.getText());
|
updatePreview(mMarkdownEditor.getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -91,29 +92,14 @@ public class EditFragment extends Fragment {
|
||||||
@Override
|
@Override
|
||||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
BufferedReader reader = null;
|
if (getActivity().getIntent().getAction().equals(Intent.ACTION_MAIN)) {
|
||||||
try {
|
File tmpFile = new File(getActivity().getFilesDir() + "/" + MainActivity.getTempFileName());
|
||||||
File tmpFile = new File(getActivity().getFilesDir() + MainActivity.getTempFileName());
|
|
||||||
if (tmpFile.exists()) {
|
if (tmpFile.exists()) {
|
||||||
InputStream in = new FileInputStream(tmpFile);
|
FileLoadTask loadTask = new FileLoadTask(mContext, EditFragment.this);
|
||||||
reader = new BufferedReader(new InputStreamReader(in));
|
loadTask.execute(FileProvider.getUriForFile(mContext, MainActivity.AUTHORITY, tmpFile));
|
||||||
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) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
updatePreview(markdownEditor.getText());
|
updatePreview(mMarkdownEditor.getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updatePreview(Editable data) {
|
private void updatePreview(Editable data) {
|
||||||
|
@ -124,6 +110,7 @@ public class EditFragment extends Fragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(String data, String filePath) {
|
public void save(String data, String filePath) {
|
||||||
|
// TODO: move this to AsyncTask
|
||||||
if (filePath == null)
|
if (filePath == null)
|
||||||
filePath = MainActivity.getFilePath() + MainActivity.getFileName();
|
filePath = MainActivity.getFilePath() + MainActivity.getFileName();
|
||||||
FileOutputStream out = null;
|
FileOutputStream out = null;
|
||||||
|
@ -153,11 +140,14 @@ public class EditFragment extends Fragment {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
save(markdownEditor.getText().toString(),
|
save(mMarkdownEditor.getText().toString(),
|
||||||
MainActivity.getTempFilePath() + MainActivity.getFileName());
|
MainActivity.getTempFilePath() + MainActivity.getFileName());
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setEditorText(String s) {
|
||||||
|
mMarkdownEditor.setText(s);
|
||||||
|
}
|
||||||
|
|
||||||
private class MarkdownBroadcastSaveReceiver extends BroadcastReceiver {
|
private class MarkdownBroadcastSaveReceiver extends BroadcastReceiver {
|
||||||
@Override
|
@Override
|
||||||
|
@ -167,20 +157,16 @@ public class EditFragment extends Fragment {
|
||||||
case SAVE_ACTION:
|
case SAVE_ACTION:
|
||||||
if (intent.hasExtra("fileName")) {
|
if (intent.hasExtra("fileName")) {
|
||||||
String fileName = intent.getStringExtra("fileName");
|
String fileName = intent.getStringExtra("fileName");
|
||||||
save(markdownEditor.getText().toString(), fileName);
|
save(mMarkdownEditor.getText().toString(), fileName);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LOAD_ACTION:
|
case LOAD_ACTION:
|
||||||
if (intent.hasExtra("fileUri")) {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void load(Uri fileUri) {
|
|
||||||
FileLoadTask loadTask = new FileLoadTask(mContext, markdownEditor);
|
|
||||||
loadTask.execute(fileUri);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,8 @@ import android.util.Log;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
@ -16,32 +18,37 @@ import static android.content.ContentValues.TAG;
|
||||||
* Created by billy on 7/25/17.
|
* Created by billy on 7/25/17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class FileLoadTask extends AsyncTask<Uri, Void, String> {
|
public class FileLoadTask extends AsyncTask<Uri, String, String> {
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private EditText mMarkdownEditor;
|
private EditFragment mEditFragment;
|
||||||
|
|
||||||
public FileLoadTask(Context context, EditText markdownEditor) {
|
public FileLoadTask(Context context, EditFragment editFragment) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mMarkdownEditor = markdownEditor;
|
mEditFragment = editFragment;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String doInBackground(Uri... uris) {
|
protected String doInBackground(Uri... uris) {
|
||||||
if (mContext == null) {
|
if (mContext == null) {
|
||||||
Log.d(TAG, "No context, abort");
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Log.d(TAG, "Begin loading file");
|
|
||||||
BufferedReader reader = null;
|
BufferedReader reader = null;
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
FileOutputStream out = null;
|
||||||
try {
|
try {
|
||||||
InputStream in = mContext.getContentResolver().openInputStream(uris[0]);
|
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));
|
reader = new BufferedReader(new InputStreamReader(in));
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
sb.append(line);
|
sb.append(line);
|
||||||
sb.append("\r\n");
|
sb.append("\r\n");
|
||||||
|
out.write(line.getBytes());
|
||||||
|
out.write("\r\n".getBytes());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(TAG, "Error opening file:", e);
|
Log.e(TAG, "Error opening file:", e);
|
||||||
|
@ -52,13 +59,18 @@ public class FileLoadTask extends AsyncTask<Uri, Void, String> {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (out != null) {
|
||||||
|
try {
|
||||||
|
out.close();
|
||||||
|
} catch (Exception e) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(String s) {
|
protected void onPostExecute(String s) {
|
||||||
mMarkdownEditor.setText(s);
|
|
||||||
super.onPostExecute(s);
|
super.onPostExecute(s);
|
||||||
|
mEditFragment.setEditorText(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package com.wbrawner.simplemarkdown;
|
package com.wbrawner.simplemarkdown;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
|
import android.app.ProgressDialog;
|
||||||
import android.content.ActivityNotFoundException;
|
import android.content.ActivityNotFoundException;
|
||||||
import android.content.ClipData;
|
import android.content.ClipData;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
|
@ -34,7 +35,7 @@ import butterknife.ButterKnife;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
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 final int REQUEST_WRITE_STORAGE = 0;
|
||||||
private static File mFilesDir;
|
private static File mFilesDir;
|
||||||
@BindView(R.id.pager) ViewPager pager;
|
@BindView(R.id.pager) ViewPager pager;
|
||||||
|
@ -77,7 +78,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
mFilesDir = getFilesDir();
|
mFilesDir = getFilesDir();
|
||||||
checkDirectories();
|
checkDirectories();
|
||||||
Intent intent = getIntent();
|
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);
|
Intent loadIntent = new Intent(EditFragment.LOAD_ACTION);
|
||||||
loadIntent.putExtra("fileUri", intent.getData().toString());
|
loadIntent.putExtra("fileUri", intent.getData().toString());
|
||||||
LocalBroadcastManager.getInstance(getApplicationContext())
|
LocalBroadcastManager.getInstance(getApplicationContext())
|
||||||
|
|
Loading…
Reference in a new issue