package xyz.devspace.md;

import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.BaseTransientBottomBar;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.sufficientlysecure.htmltextview.HtmlTextView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MarkdownViewerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_markdown_viewer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        Intent intent = getIntent();
        Uri data = intent.getData();
        if (data != null) {
            new ParseMarkdownTask().execute(data);
        } else {
            displayErrorAndExit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void displayErrorAndExit() {
        View view = findViewById(R.id.coordinator_markdown_viewer);
        Snackbar
            .make(view, R.string.no_markdown, Snackbar.LENGTH_SHORT)
            .addCallback(new BaseTransientBottomBar.BaseCallback<Snackbar>() {
                @Override
                public void onDismissed(Snackbar transientBottomBar, int event) {
                    finish();
                }
            })
            .show();
    }

    private class ParseMarkdownTask extends AsyncTask<Uri, Void, String> {
        @Override
        protected String doInBackground(Uri... params) {
            String markdown = getStringFromFile(params[0]);
            Parser parser = Parser.builder().build();
            HtmlRenderer renderer = HtmlRenderer.builder().build();
            return renderer.render(parser.parse(markdown));
        }

        protected void onPostExecute(String markdown) {
            HtmlTextView htmlTextView = (HtmlTextView) findViewById(R.id.rendered_markdown);
            htmlTextView.setHtml(markdown);
        }

        private String getStringFromFile(Uri uri) {
            try {
                InputStream fileInputStream = getContentResolver().openInputStream(uri);
                if (fileInputStream != null) {
                    String string = convertStreamToString(fileInputStream);
                    fileInputStream.close();
                    return string;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        private String convertStreamToString(InputStream is) throws Exception {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
            return sb.toString();
        }
    }

}

@@ -50,16 +50,48 @@
 Intent;%0A
+import android.database.Cursor;%0A
 import a
@@ -161,16 +161,57 @@
 Bundle;%0A
+import android.provider.OpenableColumns;%0A
 import a
@@ -1326,32 +1326,199 @@
 data != null) %7B%0A
+            String fileName = readFileName(data);%0A            if (fileName != null && actionBar != null) %7B%0A                actionBar.setTitle(fileName);%0A            %7D%0A
             new 
@@ -1852,32 +1852,504 @@
 d(item);%0A    %7D%0A%0A
+    private String readFileName(Uri uri) %7B%0A        Cursor cursor = getContentResolver().query(uri, null, null, null, null);%0A        if (cursor == null) %7B%0A            return null;%0A        %7D%0A%0A        String result = null;%0A        try %7B%0A            if (cursor.moveToFirst()) %7B%0A                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));%0A            %7D%0A        %7D finally %7B%0A            cursor.close();%0A        %7D%0A        return result;%0A    %7D%0A%0A
     private void

Use file name as title of the markdown viewer activity