/* * VITacademics * Copyright (C) 2015 Aneesh Neelam <neelam.aneesh@gmail.com> * Copyright (C) 2015 Saurabh Joshi <saurabhjoshi94@outlook.com> * Copyright (C) 2015 Gaurav Agerwala <gauravagerwala@gmail.com> * Copyright (C) 2015 Karthik Balakrishnan <karthikb351@gmail.com> * Copyright (C) 2015 Pulkit Juneja <pulkit.16296@gmail.com> * Copyright (C) 2015 Hemant Jain <hemanham@gmail.com> * Copyright (C) 2015 Darshan Mehta <darshanmehta17@gmail.com> * * This file is part of VITacademics. * * VITacademics 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. * * VITacademics 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 VITacademics. If not, see <http://www.gnu.org/licenses/>. */ package com.karthikb351.vitinfo2.fragment; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.webkit.WebView; import android.widget.ProgressBar; import com.karthikb351.vitinfo2.R; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * Created by Adam Speakman on 24/09/13. * http://speakman.net.nz */ public class LicensesFragment extends DialogFragment { private static final String FRAGMENT_TAG = LicensesFragment.class.getCanonicalName(); private static final String KEY_SHOW_CLOSE_BUTTON = "keyShowCloseButton"; private AsyncTask<Void, Void, String> mLicenseLoader; private WebView mWebView; private ProgressBar mIndeterminateProgress; /** * Creates a new instance of LicensesFragment with no Close button. * * @return A new licenses fragment. */ public static LicensesFragment newInstance() { return new LicensesFragment(); } /** * Creates a new instance of LicensesFragment with an optional Close button. * * @param showCloseButton Whether to show a Close button at the bottom of the dialog. * @return A new licenses fragment. */ public static LicensesFragment newInstance(boolean showCloseButton) { LicensesFragment fragment = new LicensesFragment(); Bundle bundle = new Bundle(); bundle.putBoolean(KEY_SHOW_CLOSE_BUTTON, showCloseButton); fragment.setArguments(bundle); return fragment; } /** * Builds and displays a licenses fragment with no Close button. Requires * "/res/raw/licenses.html" and "/res/layout/licenses_fragment.xml" to be * present. * * @param fm A fragment manager instance used to display this LicensesFragment. */ public static void displayLicensesFragment(FragmentManager fm) { FragmentTransaction ft = fm.beginTransaction(); Fragment prev = fm.findFragmentByTag(FRAGMENT_TAG); if (prev != null) { ft.remove(prev); } ft.addToBackStack(null); // Create and show the dialog. DialogFragment newFragment = LicensesFragment.newInstance(); newFragment.show(ft, FRAGMENT_TAG); } /** * Builds and displays a licenses fragment with or without a Close button. * Requires "/res/raw/licenses.html" and "/res/layout/licenses_fragment.xml" * to be present. * * @param fm A fragment manager instance used to display this LicensesFragment. * @param showCloseButton Whether to show a Close button at the bottom of the dialog. */ public static void displayLicensesFragment(FragmentManager fm, boolean showCloseButton) { FragmentTransaction ft = fm.beginTransaction(); Fragment prev = fm.findFragmentByTag(FRAGMENT_TAG); if (prev != null) { ft.remove(prev); } ft.addToBackStack(null); // Create and show the dialog. DialogFragment newFragment = LicensesFragment.newInstance(showCloseButton); newFragment.show(ft, FRAGMENT_TAG); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); loadLicenses(); } @Override public void onDestroy() { super.onDestroy(); if (mLicenseLoader != null) { mLicenseLoader.cancel(true); } } @SuppressLint("InflateParams") @Override public Dialog onCreateDialog(Bundle savedInstanceState) { View content = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_licenses, null); mWebView = (WebView) content.findViewById(R.id.licensesFragmentWebView); mIndeterminateProgress = (ProgressBar) content.findViewById(R.id.licensesFragmentIndeterminateProgress); boolean showCloseButton = false; Bundle arguments = getArguments(); if (arguments != null) { showCloseButton = arguments.getBoolean(KEY_SHOW_CLOSE_BUTTON); } AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(getActivity().getString(R.string.fragment_licenses_title)); builder.setView(content); if (showCloseButton) { builder.setNegativeButton(getActivity().getString(R.string.button_close_dialog), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); } return builder.create(); } private void loadLicenses() { // Load asynchronously in case of a very large file. mLicenseLoader = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... params) { InputStream rawResource = getActivity().getResources().openRawResource(R.raw.licenses); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(rawResource)); String line; StringBuilder sb = new StringBuilder(); try { while ((line = bufferedReader.readLine()) != null) { sb.append(line); sb.append("\n"); } bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } @Override protected void onPostExecute(String licensesBody) { super.onPostExecute(licensesBody); if (getActivity() == null || isCancelled()) { return; } mIndeterminateProgress.setVisibility(View.INVISIBLE); mWebView.setVisibility(View.VISIBLE); mWebView.loadDataWithBaseURL(null, licensesBody, "text/html", "utf-8", null); mLicenseLoader = null; } }.execute(); } }