commit 31bed24bd8795d69c710b35132bdd67d2394b12a Author: Billy Brawner Date: Mon Jul 24 23:37:23 2017 -0500 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..39fb081 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..d8d8196 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,35 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 26 + buildToolsVersion "26.0.0" + defaultConfig { + applicationId "com.wbrawner.simplemarkdown" + minSdkVersion 15 + targetSdkVersion 26 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:26.+' + compile 'com.android.support.constraint:constraint-layout:1.0.2' + compile 'com.android.support:design:26.+' + compile 'us.feras.mdv:markdownview:1.1.0' + compile 'com.jakewharton:butterknife:8.7.0' + compile 'com.android.support:support-v4:26.+' + testCompile 'junit:junit:4.12' + annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..4581c5f --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,25 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /home/billy/Android/Sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/com/wbrawner/simplemarkdown/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/wbrawner/simplemarkdown/ExampleInstrumentedTest.java new file mode 100644 index 0000000..aeecc22 --- /dev/null +++ b/app/src/androidTest/java/com/wbrawner/simplemarkdown/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.wbrawner.simplemarkdown; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.wbrawner.simplemarkdown", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..526b077 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/EditFragment.java b/app/src/main/java/com/wbrawner/simplemarkdown/EditFragment.java new file mode 100644 index 0000000..07ac42c --- /dev/null +++ b/app/src/main/java/com/wbrawner/simplemarkdown/EditFragment.java @@ -0,0 +1,171 @@ +package com.wbrawner.simplemarkdown; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.support.v4.content.LocalBroadcastManager; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.WindowManager; +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; + +import static android.content.ContentValues.TAG; + + +public class EditFragment extends Fragment { + public static final String SAVE_ACTION = "com.wbrawner.simplemarkdown.ACTION_SAVE"; + @BindView(R.id.markdown_edit) EditText markdownEditor; + + private File mTmpFile; + + public EditFragment() { + // Required empty public constructor + } + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + IntentFilter filter = new IntentFilter(); + filter.addAction(SAVE_ACTION); + LocalBroadcastManager.getInstance(getContext()).registerReceiver( + new EditFragment.MarkdownBroadcastSaveReceiver(), + filter + ); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View view = inflater.inflate(R.layout.fragment_edit, container, false); + ButterKnife.bind(this, view); + if (markdownEditor.requestFocus()) { + getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); + }; + markdownEditor.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + updatePreview(markdownEditor.getText()); + } + + @Override + public void afterTextChanged(Editable editable) { + } + }); + return view; + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + Log.d(TAG, "Markdown from beginning: " + markdownEditor.getText().toString()); + BufferedReader reader = null; + try { + File tmpFile = new File(getActivity().getFilesDir() + MainActivity.getTempFileName()); + if (tmpFile.exists()) { + Log.d(TAG, "Temp file size: " + tmpFile.length()); + 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) { + } + } + } + if (markdownEditor.getText().length() > 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()); + manager.sendBroadcast(broadcastIntent); + } + + public void save(String data, String filePath) { + if (filePath == null) + filePath = MainActivity.getFilePath() + MainActivity.getFileName(); + FileOutputStream out = null; + try { + File tmpFile = new File(filePath); + out = new FileOutputStream(tmpFile); + out.write(data.getBytes()); + } catch (Exception e) { + Log.e(TAG, "Error saving temp file:", e); + } finally { + try { + if (out != null) + out.close(); + } catch (IOException e) { + Log.e(TAG, "Error closing write stream", e); + } + } + } + + public void save(String data) { + save(data, null); + } + + public void save(Editable data) { + save(data.toString(), null); + } + + @Override + public void onPause() { + save(markdownEditor.getText().toString(), + MainActivity.getTempFilePath() + MainActivity.getFileName()); + super.onPause(); + } + + + 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); + } + } + } +} diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/MainActivity.java b/app/src/main/java/com/wbrawner/simplemarkdown/MainActivity.java new file mode 100644 index 0000000..c57261c --- /dev/null +++ b/app/src/main/java/com/wbrawner/simplemarkdown/MainActivity.java @@ -0,0 +1,163 @@ +package com.wbrawner.simplemarkdown; + +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.support.design.widget.TabLayout; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.content.FileProvider; +import android.support.v4.content.LocalBroadcastManager; +import android.support.v4.view.ViewPager; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.text.InputType; +import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.EditText; + +import java.io.File; + +import butterknife.BindView; +import butterknife.ButterKnife; + +public class MainActivity extends AppCompatActivity { + + private static final String AUTHORITY = "com.wbrawner.simplemarkdown.fileprovider"; + private static File mFilesDir; + @BindView(R.id.pager) ViewPager pager; + @BindView(R.id.layout_tab) TabLayout tabLayout; + + private static final String TAG = MainActivity.class.getSimpleName(); + private static String fileName; + + public static String getTempFileName() { + return "com_wbrawner_simplemarkdown_tmp.md"; + } + + public static String getFileName() { + if (fileName == null) { + return getTempFileName(); + } + return fileName; + } + + public static String getTempFilePath() { + return mFilesDir + File.pathSeparator + "tmp" + File.pathSeparator; + } + + public static String getFilePath() { + return mFilesDir + File.pathSeparator + "saved_files" + File.pathSeparator; + } + + public static void setFileName(String fileName) { + MainActivity.fileName = fileName; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + ButterKnife.bind(this); + pager.setAdapter(new EditPagerAdapter(getSupportFragmentManager())); + mFilesDir = getFilesDir(); + } + +// @Override +// public boolean onCreateOptionsMenu(Menu menu) { +// getMenuInflater().inflate(R.menu.menu_edit, menu); +// return true; +// } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.action_save: + //TODO: Create popup for file name + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.action_save); + + final EditText input = new EditText(this); + input.setInputType(InputType.TYPE_CLASS_TEXT); + input.setHint(R.string.hint_filename); + 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); + } + }); + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + + builder.show(); + 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) { + Intent shareIntent = new Intent(Intent.ACTION_SEND); + shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri); + shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + startActivity(shareIntent); + } + } + break; + } + return super.onOptionsItemSelected(item); + } + + public class EditPagerAdapter extends FragmentPagerAdapter { + private static final int FRAGMENT_EDIT = 0; + private static final int FRAGMENT_PREVIEW = 1; + private static final int NUM_PAGES = 2; + + public EditPagerAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public Fragment getItem(int position) { + switch (position) { + case FRAGMENT_EDIT: + return new EditFragment(); + case FRAGMENT_PREVIEW: + return new PreviewFragment(); + } + return null; + } + + @Override + public int getCount() { + return NUM_PAGES; + } + + @Override + public CharSequence getPageTitle(int position) { + int stringId = 0; + switch (position) { + case FRAGMENT_EDIT: + stringId = R.string.action_edit; + break; + case FRAGMENT_PREVIEW: + stringId = R.string.action_preview; + break; + } + return getString(stringId); + } + } +} diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/PreviewFragment.java b/app/src/main/java/com/wbrawner/simplemarkdown/PreviewFragment.java new file mode 100644 index 0000000..e0432b0 --- /dev/null +++ b/app/src/main/java/com/wbrawner/simplemarkdown/PreviewFragment.java @@ -0,0 +1,63 @@ +package com.wbrawner.simplemarkdown; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.net.Uri; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.support.v4.content.LocalBroadcastManager; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import butterknife.BindView; +import butterknife.ButterKnife; +import us.feras.mdv.MarkdownView; + +public class PreviewFragment extends Fragment { + private static final String TAG = PreviewFragment.class.getSimpleName(); + @BindView(R.id.markdown_view) + MarkdownView markdownView; + + public static final String PREVIEW_ACTION = "com.wbrawner.simplemarkdown.preview"; + + public PreviewFragment() { + // Required empty public constructor + } + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + IntentFilter filter = new IntentFilter(); + filter.addAction(PREVIEW_ACTION); + LocalBroadcastManager.getInstance(getContext()).registerReceiver( + new MarkdownBroadcastSender(), + filter + ); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View view = inflater.inflate(R.layout.fragment_preview, container, false); + ButterKnife.bind(this, view); + return view; + } + + private class MarkdownBroadcastSender extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + if (intent.hasExtra("markdownData")) { + String data = intent.getStringExtra("markdownData"); + Log.d(TAG, "Markdown Data: " + data); + markdownView.loadMarkdown(data); + } + } + } + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..d9ec428 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,28 @@ + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_edit.xml b/app/src/main/res/layout/fragment_edit.xml new file mode 100644 index 0000000..02f2669 --- /dev/null +++ b/app/src/main/res/layout/fragment_edit.xml @@ -0,0 +1,16 @@ + + + + diff --git a/app/src/main/res/layout/fragment_preview.xml b/app/src/main/res/layout/fragment_preview.xml new file mode 100644 index 0000000..4196a56 --- /dev/null +++ b/app/src/main/res/layout/fragment_preview.xml @@ -0,0 +1,12 @@ + + + + + diff --git a/app/src/main/res/menu/menu_edit.xml b/app/src/main/res/menu/menu_edit.xml new file mode 100644 index 0000000..cef694e --- /dev/null +++ b/app/src/main/res/menu/menu_edit.xml @@ -0,0 +1,14 @@ + + + + + \ 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 new file mode 100644 index 0000000..9ac9b30 --- /dev/null +++ b/app/src/main/res/menu/menu_main.xml @@ -0,0 +1,15 @@ + + + + diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000..cde69bc Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 0000000..9a078e3 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000..c133a0c Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 0000000..efc028a Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000..bfa42f0 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 0000000..3af2608 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..324e72c Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..9bec2e6 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..aee44e1 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..34947cd Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..3ab3e9c --- /dev/null +++ b/app/src/main/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #3F51B5 + #303F9F + #FF4081 + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml new file mode 100644 index 0000000..59a0b0c --- /dev/null +++ b/app/src/main/res/values/dimens.xml @@ -0,0 +1,3 @@ + + 16dp + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..455e62f --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,14 @@ + + Simple Markdown + Settings + Help + Edit + Preview + + + Hello blank fragment + Markdown here... + Save + Share + File name + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml new file mode 100644 index 0000000..545b9c6 --- /dev/null +++ b/app/src/main/res/values/styles.xml @@ -0,0 +1,20 @@ + + + + + + + +