diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 526b077..9c38352 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + + + + + + + + + + + + 0) { - Log.d(TAG, "We have text"); - } else { - Log.d(TAG, "Blank slate"); - } updatePreview(markdownEditor.getText()); } private void updatePreview(Editable data) { - Log.d(TAG, "Data to send: " + data.toString()); Intent broadcastIntent = new Intent(PreviewFragment.PREVIEW_ACTION); broadcastIntent.putExtra("markdownData", data.toString()); LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getContext()); @@ -161,11 +162,25 @@ public class EditFragment extends Fragment { private class MarkdownBroadcastSaveReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - if (intent.hasExtra("fileName")) { - String fileName = intent.getStringExtra("fileName"); - Log.d(TAG, "File: " + fileName); - save(markdownEditor.getText().toString(), fileName); + Log.d(TAG, "Intent received: " + intent.getAction()); + switch (intent.getAction()) { + case SAVE_ACTION: + if (intent.hasExtra("fileName")) { + String fileName = intent.getStringExtra("fileName"); + save(markdownEditor.getText().toString(), fileName); + } + break; + case LOAD_ACTION: + if (intent.hasExtra("fileUri")) { + load(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 new file mode 100644 index 0000000..4600f55 --- /dev/null +++ b/app/src/main/java/com/wbrawner/simplemarkdown/FileLoadTask.java @@ -0,0 +1,64 @@ +package com.wbrawner.simplemarkdown; + +import android.content.Context; +import android.net.Uri; +import android.os.AsyncTask; +import android.util.Log; +import android.widget.EditText; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; + +import static android.content.ContentValues.TAG; + +/** + * Created by billy on 7/25/17. + */ + +public class FileLoadTask extends AsyncTask { + + private Context mContext; + private EditText mMarkdownEditor; + + public FileLoadTask(Context context, EditText markdownEditor) { + mContext = context; + mMarkdownEditor = markdownEditor; + } + + @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(); + try { + InputStream in = mContext.getContentResolver().openInputStream(uris[0]); + reader = new BufferedReader(new InputStreamReader(in)); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line); + sb.append("\r\n"); + } + } catch (Exception e) { + Log.e(TAG, "Error opening file:", e); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (Exception e) { + } + } + } + return sb.toString(); + } + + @Override + protected void onPostExecute(String s) { + mMarkdownEditor.setText(s); + super.onPostExecute(s); + } +} diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/MainActivity.java b/app/src/main/java/com/wbrawner/simplemarkdown/MainActivity.java index c57261c..f0aa932 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/MainActivity.java +++ b/app/src/main/java/com/wbrawner/simplemarkdown/MainActivity.java @@ -1,12 +1,18 @@ package com.wbrawner.simplemarkdown; +import android.Manifest; +import android.content.ActivityNotFoundException; +import android.content.ClipData; import android.content.DialogInterface; import android.content.Intent; +import android.content.pm.PackageManager; import android.net.Uri; import android.support.design.widget.TabLayout; +import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.content.ContextCompat; import android.support.v4.content.FileProvider; import android.support.v4.content.LocalBroadcastManager; import android.support.v4.view.ViewPager; @@ -17,7 +23,9 @@ import android.text.InputType; import android.util.Log; import android.view.Menu; import android.view.MenuItem; +import android.webkit.MimeTypeMap; import android.widget.EditText; +import android.widget.Toast; import java.io.File; @@ -27,6 +35,7 @@ import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity { private 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; @BindView(R.id.layout_tab) TabLayout tabLayout; @@ -35,22 +44,24 @@ public class MainActivity extends AppCompatActivity { private static String fileName; public static String getTempFileName() { - return "com_wbrawner_simplemarkdown_tmp.md"; + return "tmp_" + getFileName(); } public static String getFileName() { if (fileName == null) { - return getTempFileName(); + return "untitled.md"; } + if (!fileName.endsWith(".md")) + return fileName + ".md"; return fileName; } public static String getTempFilePath() { - return mFilesDir + File.pathSeparator + "tmp" + File.pathSeparator; + return mFilesDir + "/tmp/"; } public static String getFilePath() { - return mFilesDir + File.pathSeparator + "saved_files" + File.pathSeparator; + return mFilesDir + "/saved_files/"; } public static void setFileName(String fileName) { @@ -64,13 +75,33 @@ public class MainActivity extends AppCompatActivity { ButterKnife.bind(this); pager.setAdapter(new EditPagerAdapter(getSupportFragmentManager())); mFilesDir = getFilesDir(); + checkDirectories(); + Intent intent = getIntent(); + if (intent != null && intent.getData() != null) { + Intent loadIntent = new Intent(EditFragment.LOAD_ACTION); + loadIntent.putExtra("fileUri", intent.getData().toString()); + LocalBroadcastManager.getInstance(getApplicationContext()) + .sendBroadcast(loadIntent); + } } -// @Override -// public boolean onCreateOptionsMenu(Menu menu) { -// getMenuInflater().inflate(R.menu.menu_edit, menu); -// return true; -// } + private void checkDirectories() { + File tmpDir = new File(getTempFilePath()); + if (!tmpDir.exists()) { + tmpDir.mkdir(); + } + File outDir = new File(getFilePath()); + if (!outDir.exists()) { + outDir.mkdir(); + } + + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu_edit, menu); + return true; + } @Override public boolean onOptionsItemSelected(MenuItem item) { @@ -83,15 +114,19 @@ public class MainActivity extends AppCompatActivity { final EditText input = new EditText(this); input.setInputType(InputType.TYPE_CLASS_TEXT); input.setHint(R.string.hint_filename); + input.setText(getFileName()); builder.setView(input); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - Intent saveIntent = new Intent(EditFragment.SAVE_ACTION); - saveIntent.putExtra("fileName", input.getText()); - LocalBroadcastManager.getInstance(getApplicationContext()) - .sendBroadcast(saveIntent); + if (input.getText().length() > 0) { + setFileName(input.getText().toString()); + Intent saveIntent = new Intent(EditFragment.SAVE_ACTION); + saveIntent.putExtra("fileName", input.getText()); + LocalBroadcastManager.getInstance(getApplicationContext()) + .sendBroadcast(saveIntent); + } } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @@ -105,22 +140,75 @@ public class MainActivity extends AppCompatActivity { break; case R.id.action_share: //TODO: Fix this - File tmpFile = new File(getFilesDir() + getTempFileName()); - if (tmpFile.exists()) { - Log.d(TAG, "Temp file size: " + tmpFile.length()); - Uri fileUri = FileProvider.getUriForFile(MainActivity.this, AUTHORITY, tmpFile); - if (fileUri != null) { + File tmpFile = new File(getTempFilePath() + getFileName()); + if (!tmpFile.exists()) { + Intent saveIntent = new Intent(EditFragment.SAVE_ACTION); + saveIntent.putExtra("fileName", getTempFilePath() + getFileName()); + LocalBroadcastManager.getInstance(getApplicationContext()) + .sendBroadcast(saveIntent); + } + Log.d(TAG, "Temp file size: " + tmpFile.length()); + Uri fileUri = FileProvider.getUriForFile(MainActivity.this, AUTHORITY, tmpFile); +// Uri fileUri = Uri.fromFile(tmpFile); + if (fileUri != null) { + Log.d(TAG, "MIME type: " + getContentResolver().getType(fileUri)); + try { Intent shareIntent = new Intent(Intent.ACTION_SEND); + shareIntent.setDataAndType( + fileUri, + getContentResolver().getType(fileUri) + ); shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri); shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - startActivity(shareIntent); + startActivity( + Intent.createChooser( + shareIntent, + getString(R.string.share_file) + ) + ); + } catch (ActivityNotFoundException e) { + Log.e(TAG, "Error sharing file", e); + Toast.makeText( + MainActivity.this, + R.string.no_shareable_apps, + Toast.LENGTH_SHORT + ).show(); } } break; +// case R.id.action_export: +// //TODO: Implement export functionality +// if (ContextCompat.checkSelfPermission(MainActivity.this, +// Manifest.permission.WRITE_EXTERNAL_STORAGE) +// != PackageManager.PERMISSION_GRANTED){ +// ActivityCompat.requestPermissions(MainActivity.this, +// new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, +// REQUEST_WRITE_STORAGE); +// break; +// } } return super.onOptionsItemSelected(item); } + @Override + public void onRequestPermissionsResult(int requestCode, + String permissions[], int[] grantResults) { + //TODO: Implement export functionality + switch (requestCode) { + case REQUEST_WRITE_STORAGE: { + // If request is cancelled, the result arrays are empty. + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + // Permission granted, open file chooser dialog + + } else { + // Permission denied, do nothing + } + return; + } + } + } + public class EditPagerAdapter extends FragmentPagerAdapter { private static final int FRAGMENT_EDIT = 0; private static final int FRAGMENT_PREVIEW = 1; diff --git a/app/src/main/res/menu/menu_edit.xml b/app/src/main/res/menu/menu_edit.xml index cef694e..a326d7e 100644 --- a/app/src/main/res/menu/menu_edit.xml +++ b/app/src/main/res/menu/menu_edit.xml @@ -11,4 +11,19 @@ android:id="@+id/action_share" android:icon="@android:drawable/ic_menu_share" app:showAsAction="always" /> + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml deleted file mode 100644 index 9ac9b30..0000000 --- a/app/src/main/res/menu/menu_main.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 455e62f..35ff05a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -11,4 +11,7 @@ Save Share File name + Export + Unable to share file - no capable apps installed + Share file to... diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml index 42e9419..908efde 100644 --- a/app/src/main/res/xml/file_paths.xml +++ b/app/src/main/res/xml/file_paths.xml @@ -1,4 +1,5 @@ - + + \ No newline at end of file