net.toload.main.hd.ui.NewsDialog.java Source code

Java tutorial

Introduction

Here is the source code for net.toload.main.hd.ui.NewsDialog.java

Source

/*
 *
 *  *
 *  **    Copyright 2015, The LimeIME Open Source Project
 *  **
 *  **    Project Url: http://github.com/lime-ime/limeime/
 *  **                 http://android.toload.net/
 *  **
 *  **    This program is free software: you can redistribute it and/or modify
 *  **    it under the terms of the GNU General Public License as published by
 *  **    the Free Software Foundation, either version 3 of the License, or
 *  **    (at your option) any later version.
 *  *
 *  **    This program is distributed in the hope that it will be useful,
 *  **    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  **    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  **    GNU General Public License for more details.
 *  *
 *  **    You should have received a copy of the GNU General Public License
 *  **    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  *
 *
 */

package net.toload.main.hd.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;

import net.toload.main.hd.Lime;
import net.toload.main.hd.R;
import net.toload.main.hd.global.LIMEPreferenceManager;

public class NewsDialog extends DialogFragment {

    Activity activity;
    View view;

    Button btnHelpDialog;

    private LIMEPreferenceManager mLIMEPref;

    public static NewsDialog newInstance() {
        NewsDialog btd = new NewsDialog();
        btd.setCancelable(true);
        return btd;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onAttach(Activity act) {
        super.onAttach(act);
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.setCancelable(false);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, android.view.KeyEvent event) {
                if ((keyCode == android.view.KeyEvent.KEYCODE_BACK)) {
                    // To dismiss the fragment when the back-button is pressed.
                    dismiss();
                    return true;
                }
                // Otherwise, do nothing else
                else
                    return false;
            }
        });
    }

    public void cancelDialog() {
        this.dismiss();
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle) {

        getDialog().getWindow().setTitle(getResources().getString(R.string.action_news));

        mLIMEPref = new LIMEPreferenceManager(getActivity());

        view = inflater.inflate(R.layout.fragment_dialog_news, container, false);

        String html_value = mLIMEPref.getParameterString(Lime.LIME_NEWS_CONTENT, "");
        if (!html_value.isEmpty()) {
            WebView newsContentArea = (WebView) view.findViewById(R.id.newsContentArea);
            newsContentArea.getSettings().setJavaScriptEnabled(true);
            newsContentArea.loadData(html_value, "text/html", "UTF-8");
        }

        btnHelpDialog = (Button) view.findViewById(R.id.btnNewsDialog);
        btnHelpDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        return view;

    }

    @Override
    public void onSaveInstanceState(Bundle icicle) {
        super.onSaveInstanceState(icicle);
    }

}