Android Open Source - APO-DZ-Android Web Fragment






From Project

Back to project page APO-DZ-Android.

License

The source code is released under:

MIT License

If you think the Android project APO-DZ-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 */*from   ww w  . ja v a  2  s . c  om*/
 */
package org.upennapo.app.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import org.upennapo.app.R;
import org.upennapo.app.model.DataManager;

/**
 * WebFragment uses a WebView to load a webpage. Classic.
 * This Fragment has JavaScript enabled by default.
 *
 * @author Ronald Martin
 */
public class WebFragment extends Fragment {

    public static final String URL_KEY = "url";
    private String mUrl;
    private WebView mWebView;

    public WebFragment() {
    }

    /**
     * Get an instance of WebFragment that loads the specified URL.
     *
     * @param urlString to load in the WebView
     * @return new WebFragment set to load the URL
     */
    public static WebFragment newInstance(String urlString) {
        WebFragment instance = new WebFragment();

        Bundle args = new Bundle();
        args.putString(URL_KEY, urlString);
        instance.setArguments(args);

        return instance;
    }

    /**
     * Get an instance of WebFragment that loads the Google Calendars.
     *
     * @param context in which to load the WebView
     * @return Google Calendar Fragment
     */
    public static WebFragment newCalendarInstance(Context context) {
        final String url = context.getString(R.string.calendar_url);
        return newInstance(url);
    }

    /**
     * Get an instance of WebFragment that loads the APO 2048 game.
     *
     * @param context in which to load the WebView
     * @return 2048 Fragment
     */
    public static WebFragment new2048Instance(Context context) {
        final String url = context.getString(R.string.apo_2048_url);
        return newInstance(url);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        this.mUrl = getArguments().getString(URL_KEY);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (DataManager.isNetworkAvailable(getActivity())) {
            mWebView = new WebView(getActivity());
            mWebView.setWebViewClient(new NoRedirectWebViewClient());
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.loadUrl(mUrl);

            return mWebView;
        } else {
            Toast.makeText(getActivity(), R.string.no_internet_toast_msg, Toast.LENGTH_SHORT).show();
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.fragment_web, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_reload:
                mWebView.clearCache(true);
                mWebView.loadUrl(mUrl);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onDestroy() {
        if (mWebView != null) {
            // Prevent memory leaks.
            mWebView.clearCache(true);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }

    /**
     * Custom WebViewClient prevents hosts from overriding loads, thus circumventing Google
     * Calendar's tendency to prevent loading embeddable calendar feeds outside of iframes.
     */
    private class NoRedirectWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }

        @Override
        public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
            return false;
        }
    }
}




Java Source Code List

org.upennapo.app.activity.AlumModeActivity.java
org.upennapo.app.activity.DirectoryActivity.java
org.upennapo.app.activity.DirectoryDetailsActivity.java
org.upennapo.app.activity.EasterEggActivity.java
org.upennapo.app.activity.LoginActivity.java
org.upennapo.app.activity.MainActivity.java
org.upennapo.app.activity.WebActivity.java
org.upennapo.app.adapter.AlphabeticalAdapter.java
org.upennapo.app.adapter.LinkAdapter.java
org.upennapo.app.fragment.AlumDirectoryFragment.java
org.upennapo.app.fragment.BrotherStatusFragment.java
org.upennapo.app.fragment.DirectoryFragment.java
org.upennapo.app.fragment.LinkListFragment.java
org.upennapo.app.fragment.NavigationDrawerFragment.java
org.upennapo.app.fragment.WebFragment.java
org.upennapo.app.model.Brother.java
org.upennapo.app.model.DataManager.java
org.upennapo.app.model.User.java