package net.mvla.mvhs.ui; import android.annotation.SuppressLint; import android.app.Dialog; import android.app.DialogFragment; import android.os.Bundle; import android.support.annotation.NonNull; import android.view.InflateException; import android.view.LayoutInflater; import android.view.View; import android.webkit.WebView; import com.afollestad.materialdialogs.MaterialDialog; import net.mvla.mvhs.R; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class ChangelogDialog extends DialogFragment { public static ChangelogDialog newInstance() { ChangelogDialog dialog = new ChangelogDialog(); return dialog; } @SuppressLint("InflateParams") @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final View customView; try { customView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_webview, null); } catch (InflateException e) { e.printStackTrace(); return new MaterialDialog.Builder(getActivity()) .title(R.string.error) .content("This device doesn't support web view. It is missing a system component.") .positiveText(android.R.string.ok) .build(); } MaterialDialog dialog = new MaterialDialog.Builder(getActivity()) .title(R.string.changelog) .customView(customView, false) .positiveText(android.R.string.ok) .build(); final WebView webView = (WebView) customView.findViewById(R.id.webview); try { // Load from changelog.html in the assets folder StringBuilder buf = new StringBuilder(); InputStream json = getActivity().getAssets().open("changelog.html"); BufferedReader in = new BufferedReader(new InputStreamReader(json, "UTF-8")); String str; while ((str = in.readLine()) != null) buf.append(str); in.close(); // Inject color values for WebView body background and links webView.loadData(buf.toString(), "text/html", "UTF-8"); } catch (Throwable e) { webView.loadData("<h1>Unable to load</h1><p>" + e.getLocalizedMessage() + "</p>", "text/html", "UTF-8"); } return dialog; } }