Search in sources :

Example 1 with MarkdownView

use of us.feras.mdv.MarkdownView in project Notepad by farmerbb.

the class NoteViewFragment method onActivityCreated.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Set values
    setRetainInstance(true);
    setHasOptionsMenu(true);
    // Get filename of saved note
    filename = getArguments().getString("filename");
    // Change window title
    String title;
    try {
        title = listener.loadNoteTitle(filename);
    } catch (IOException e) {
        title = getResources().getString(R.string.view_note);
    }
    getActivity().setTitle(title);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(title, null, ContextCompat.getColor(getActivity(), R.color.primary));
        getActivity().setTaskDescription(taskDescription);
    }
    // Show the Up button in the action bar.
    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    // Animate elevation change
    if (getActivity().findViewById(R.id.layoutMain).getTag().equals("main-layout-large") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        LinearLayout noteViewEdit = (LinearLayout) getActivity().findViewById(R.id.noteViewEdit);
        LinearLayout noteList = (LinearLayout) getActivity().findViewById(R.id.noteList);
        noteList.animate().z(0f);
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            noteViewEdit.animate().z(getResources().getDimensionPixelSize(R.dimen.note_view_edit_elevation_land));
        else
            noteViewEdit.animate().z(getResources().getDimensionPixelSize(R.dimen.note_view_edit_elevation));
    }
    // Set up content view
    TextView noteContents = (TextView) getActivity().findViewById(R.id.textView);
    markdownView = (MarkdownView) getActivity().findViewById(R.id.markdownView);
    // Apply theme
    SharedPreferences pref = getActivity().getSharedPreferences(getActivity().getPackageName() + "_preferences", Context.MODE_PRIVATE);
    ScrollView scrollView = (ScrollView) getActivity().findViewById(R.id.scrollView);
    String theme = pref.getString("theme", "light-sans");
    int textSize = -1;
    int textColor = -1;
    String fontFamily = null;
    if (theme.contains("light")) {
        if (noteContents != null) {
            noteContents.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_color_primary));
            noteContents.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.window_background));
        }
        if (markdownView != null) {
            markdownView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.window_background));
            textColor = ContextCompat.getColor(getActivity(), R.color.text_color_primary);
        }
        scrollView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.window_background));
    }
    if (theme.contains("dark")) {
        if (noteContents != null) {
            noteContents.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_color_primary_dark));
            noteContents.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.window_background_dark));
        }
        if (markdownView != null) {
            markdownView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.window_background_dark));
            textColor = ContextCompat.getColor(getActivity(), R.color.text_color_primary_dark);
        }
        scrollView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.window_background_dark));
    }
    if (theme.contains("sans")) {
        if (noteContents != null)
            noteContents.setTypeface(Typeface.SANS_SERIF);
        if (markdownView != null)
            fontFamily = "sans-serif";
    }
    if (theme.contains("serif")) {
        if (noteContents != null)
            noteContents.setTypeface(Typeface.SERIF);
        if (markdownView != null)
            fontFamily = "serif";
    }
    if (theme.contains("monospace")) {
        if (noteContents != null)
            noteContents.setTypeface(Typeface.MONOSPACE);
        if (markdownView != null)
            fontFamily = "monospace";
    }
    switch(pref.getString("font_size", "normal")) {
        case "smallest":
            textSize = 12;
            break;
        case "small":
            textSize = 14;
            break;
        case "normal":
            textSize = 16;
            break;
        case "large":
            textSize = 18;
            break;
        case "largest":
            textSize = 20;
            break;
    }
    if (noteContents != null)
        noteContents.setTextSize(textSize);
    String css = "";
    if (markdownView != null) {
        String topBottom = " " + Float.toString(getResources().getDimension(R.dimen.padding_top_bottom) / getResources().getDisplayMetrics().density) + "px";
        String leftRight = " " + Float.toString(getResources().getDimension(R.dimen.padding_left_right) / getResources().getDisplayMetrics().density) + "px";
        String fontSize = " " + Integer.toString(textSize) + "px";
        String fontColor = " #" + StringUtils.remove(Integer.toHexString(textColor), "ff");
        String linkColor = " #" + StringUtils.remove(Integer.toHexString(new TextView(getActivity()).getLinkTextColors().getDefaultColor()), "ff");
        css = "body { " + "margin:" + topBottom + topBottom + leftRight + leftRight + "; " + "font-family:" + fontFamily + "; " + "font-size:" + fontSize + "; " + "color:" + fontColor + "; " + "}" + "a { " + "color:" + linkColor + "; " + "}";
        markdownView.getSettings().setJavaScriptEnabled(false);
        markdownView.getSettings().setLoadsImagesAutomatically(false);
        markdownView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                if (!request.hasGesture())
                    return super.shouldOverrideUrlLoading(view, request);
                Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
                    try {
                        startActivity(intent);
                    } catch (ActivityNotFoundException | FileUriExposedException e) {
                    /* Gracefully fail */
                    }
                else
                    try {
                        startActivity(intent);
                    } catch (ActivityNotFoundException e) {
                    /* Gracefully fail */
                    }
                return true;
            }
        });
    }
    // Load note contents
    try {
        contentsOnLoad = listener.loadNote(filename);
    } catch (IOException e) {
        showToast(R.string.error_loading_note);
        // Add NoteListFragment or WelcomeFragment
        Fragment fragment;
        if (getActivity().findViewById(R.id.layoutMain).getTag().equals("main-layout-normal"))
            fragment = new NoteListFragment();
        else
            fragment = new WelcomeFragment();
        getFragmentManager().beginTransaction().replace(R.id.noteViewEdit, fragment, "NoteListFragment").setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
    }
    // Set TextView contents
    if (noteContents != null)
        noteContents.setText(contentsOnLoad);
    if (markdownView != null)
        markdownView.loadMarkdown(contentsOnLoad, "data:text/css;base64," + Base64.encodeToString(css.getBytes(), Base64.DEFAULT));
    // Show a toast message if this is the user's first time viewing a note
    final SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    firstLoad = sharedPref.getInt("first-load", 0);
    if (firstLoad == 0) {
        // Show dialog with info
        DialogFragment firstLoad = new FirstViewDialogFragment();
        firstLoad.show(getFragmentManager(), "firstloadfragment");
        // Set first-load preference to 1; we don't need to show the dialog anymore
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt("first-load", 1);
        editor.apply();
    }
    // Detect single and double-taps using GestureDetector
    final GestureDetector detector = new GestureDetector(getActivity(), new GestureDetector.OnGestureListener() {

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return false;
        }
    });
    detector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if (sharedPref.getBoolean("show_double_tap_message", true)) {
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putBoolean("show_double_tap_message", false);
                editor.apply();
            }
            Bundle bundle = new Bundle();
            bundle.putString("filename", filename);
            Fragment fragment = new NoteEditFragment();
            fragment.setArguments(bundle);
            getFragmentManager().beginTransaction().replace(R.id.noteViewEdit, fragment, "NoteEditFragment").commit();
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if (sharedPref.getBoolean("show_double_tap_message", true) && showMessage) {
                showToastLong(R.string.double_tap);
                showMessage = false;
            }
            return false;
        }
    });
    if (noteContents != null)
        noteContents.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                detector.onTouchEvent(event);
                return false;
            }
        });
    if (markdownView != null)
        markdownView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                detector.onTouchEvent(event);
                return false;
            }
        });
}
Also used : DialogFragment(android.support.v4.app.DialogFragment) FirstViewDialogFragment(com.farmerbb.notepad.fragment.dialog.FirstViewDialogFragment) GestureDetector(android.view.GestureDetector) ActivityManager(android.app.ActivityManager) DialogFragment(android.support.v4.app.DialogFragment) Fragment(android.support.v4.app.Fragment) FirstViewDialogFragment(com.farmerbb.notepad.fragment.dialog.FirstViewDialogFragment) TextView(android.widget.TextView) FirstViewDialogFragment(com.farmerbb.notepad.fragment.dialog.FirstViewDialogFragment) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient) FileUriExposedException(android.os.FileUriExposedException) WebResourceRequest(android.webkit.WebResourceRequest) SharedPreferences(android.content.SharedPreferences) Bundle(android.os.Bundle) Intent(android.content.Intent) IOException(java.io.IOException) View(android.view.View) WebView(android.webkit.WebView) MarkdownView(us.feras.mdv.MarkdownView) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) MotionEvent(android.view.MotionEvent) ScrollView(android.widget.ScrollView) ActivityNotFoundException(android.content.ActivityNotFoundException) LinearLayout(android.widget.LinearLayout) TargetApi(android.annotation.TargetApi)

Example 2 with MarkdownView

use of us.feras.mdv.MarkdownView in project Notepad by farmerbb.

the class MainActivity method printNote.

@SuppressLint("SetJavaScriptEnabled")
@TargetApi(Build.VERSION_CODES.KITKAT)
public void printNote(String contentToPrint) {
    SharedPreferences pref = getSharedPreferences(getPackageName() + "_preferences", MODE_PRIVATE);
    // Create a WebView object specifically for printing
    boolean generateHtml = !(pref.getBoolean("markdown", false) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
    WebView webView = generateHtml ? new WebView(this) : new MarkdownView(this);
    // Apply theme
    String theme = pref.getString("theme", "light-sans");
    int textSize = -1;
    String fontFamily = null;
    if (theme.contains("sans")) {
        fontFamily = "sans-serif";
    }
    if (theme.contains("serif")) {
        fontFamily = "serif";
    }
    if (theme.contains("monospace")) {
        fontFamily = "monospace";
    }
    switch(pref.getString("font_size", "normal")) {
        case "smallest":
            textSize = 12;
            break;
        case "small":
            textSize = 14;
            break;
        case "normal":
            textSize = 16;
            break;
        case "large":
            textSize = 18;
            break;
        case "largest":
            textSize = 20;
            break;
    }
    String topBottom = " " + Float.toString(getResources().getDimension(R.dimen.padding_top_bottom_print) / getResources().getDisplayMetrics().density) + "px";
    String leftRight = " " + Float.toString(getResources().getDimension(R.dimen.padding_left_right_print) / getResources().getDisplayMetrics().density) + "px";
    String fontSize = " " + Integer.toString(textSize) + "px";
    String css = "body { " + "margin:" + topBottom + topBottom + leftRight + leftRight + "; " + "font-family:" + fontFamily + "; " + "font-size:" + fontSize + "; " + "}";
    webView.getSettings().setJavaScriptEnabled(false);
    webView.getSettings().setLoadsImagesAutomatically(false);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(final WebView view, String url) {
            createWebPrintJob(view);
        }
    });
    // Load content into WebView
    if (generateHtml) {
        webView.loadDataWithBaseURL(null, "<link rel='stylesheet' type='text/css' href='data:text/css;base64," + Base64.encodeToString(css.getBytes(), Base64.DEFAULT) + "' /><html><body><p>" + StringUtils.replace(contentToPrint, "\n", "<br>") + "</p></body></html>", "text/HTML", "UTF-8", null);
    } else
        ((MarkdownView) webView).loadMarkdown(contentToPrint, "data:text/css;base64," + Base64.encodeToString(css.getBytes(), Base64.DEFAULT));
}
Also used : MarkdownView(us.feras.mdv.MarkdownView) SharedPreferences(android.content.SharedPreferences) WebView(android.webkit.WebView) SuppressLint(android.annotation.SuppressLint) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint) TargetApi(android.annotation.TargetApi)

Example 3 with MarkdownView

use of us.feras.mdv.MarkdownView in project AgentWeb by Justson.

the class CustomWebViewFragment method onViewCreated.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
    // MarkdownView 是 WebView  的一个子类
    mMarkdownWebView = new MarkdownView(getActivity());
    markdownEditText = (EditText) view.findViewById(R.id.markdownText);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
    lp.weight = 1f;
    mAgentWeb = // 
    AgentWeb.with(this).setAgentWebParent((ViewGroup) view, // 
    lp).closeIndicator().setWebViewClient(mWebViewClient).setWebView(mMarkdownWebView).setSecurityType(AgentWeb.SecurityType.STRICT_CHECK).createAgentWeb().ready().go(null);
    String text = "## AgentWeb 功能\n" + "***\n\n" + "1. 支持进度条以及自定义进度条\n" + "2. 支持文件下载\n" + "3. 支持文件下载断点续传\n" + "4. 支持下载通知形式提示进度\n" + "5. 简化 Javascript 通信 \n" + "6. 支持 Android 4.4 Kitkat 以及其他版本文件上传\n" + "7. 支持注入 Cookies\n" + "8. 加强 Web 安全\n" + "9. 支持全屏播放视频\n" + "10. 兼容低版本 Js 安全通信\n" + "11. 更省电 。\n" + "12. 支持调起微信支付\n" + "13. 支持调起支付宝(请参照sample)\n" + "14. 默认支持定位";
    markdownEditText.setText(text);
    updateMarkdownView();
    markdownEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            updateMarkdownView();
        }
    });
    initView(view);
}
Also used : MarkdownView(us.feras.mdv.MarkdownView) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) LinearLayout(android.widget.LinearLayout)

Aggregations

MarkdownView (us.feras.mdv.MarkdownView)3 TargetApi (android.annotation.TargetApi)2 SharedPreferences (android.content.SharedPreferences)2 WebView (android.webkit.WebView)2 WebViewClient (android.webkit.WebViewClient)2 LinearLayout (android.widget.LinearLayout)2 SuppressLint (android.annotation.SuppressLint)1 ActivityManager (android.app.ActivityManager)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 FileUriExposedException (android.os.FileUriExposedException)1 DialogFragment (android.support.v4.app.DialogFragment)1 Fragment (android.support.v4.app.Fragment)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 GestureDetector (android.view.GestureDetector)1 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 WebResourceRequest (android.webkit.WebResourceRequest)1