zack.yovel.clear.controller.foreground.HelpFragment.java Source code

Java tutorial

Introduction

Here is the source code for zack.yovel.clear.controller.foreground.HelpFragment.java

Source

/*
 * Copyright (c) 2014 Yehezkel (Zack) Yovel
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package zack.yovel.clear.controller.foreground;

import android.app.AlertDialog;
import android.app.Dialog;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import zack.yovel.clear.R;

public class HelpFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog result;

        View customContent = getCustomContent();

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.title_help_dialog).setIcon(R.drawable.system_help).setView(customContent);
        result = builder.create();
        return result;
    }

    private View getCustomContent() {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View customContent = inflater.inflate(R.layout.fragment_help, null, false);
        WebView wvHelp = (WebView) customContent.findViewById(R.id.wvHelp);
        String helpHtmlString = getHelpHtmlEvenIfException();
        wvHelp.loadData(helpHtmlString, "text/html", "UTF-8");
        return customContent;
    }

    private String getHelpHtmlEvenIfException() {
        String helpHtmlString;
        try {
            helpHtmlString = getHelpHtmlString();
        } catch (IOException e) {
            helpHtmlString = getActivity().getString(R.string.html_alt_help);
        }
        return helpHtmlString;
    }

    private String getHelpHtmlString() throws IOException {
        InputStream inputStream = getActivity().getResources().openRawResource(R.raw.help);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String helpHtmlString = "";
        while (reader.ready()) {
            helpHtmlString += reader.readLine();
        }
        return helpHtmlString;
    }
}