jp.co.conit.sss.sp.ex1.fragment.BookViewerFragment.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.conit.sss.sp.ex1.fragment.BookViewerFragment.java

Source

/*
 * Copyright (C) 2012 CONIT Co., Ltd.
 *
 * 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 jp.co.conit.sss.sp.ex1.fragment;

import jp.co.conit.sss.sp.ex1.R;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
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.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * ????Fragment??
 * 
 * @author conit
 */
public class BookViewerFragment extends Fragment {

    private WebView mWeb;

    private static BookViewerFragment mSelf;

    private OnCancelLoadLisntener mOnCancelLoadLisntener;

    /**
     * ?????????
     * 
     * @author conit
     */
    public interface OnCancelLoadLisntener {
        void cancelLoad();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mOnCancelLoadLisntener = (OnCancelLoadLisntener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnCancelLoadLisntener");
        }
    };

    public static BookViewerFragment newInstance(String url) {

        BookViewerFragment fragment = new BookViewerFragment();
        Bundle args = new Bundle();
        args.putString("url", url);
        fragment.setArguments(args);

        return fragment;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mWeb = (WebView) inflater.inflate(R.layout.fragment_bookviewer, null);
        mWeb.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                removeDialogFragment();
            }
        });
        mSelf = this;
        return mWeb;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Bundle arg = getArguments();
        String url = arg.getString("url");

        FragmentManager fm = getFragmentManager();
        ProgresssDialogFragment pdf = ProgresssDialogFragment.newInstance();
        pdf.show(fm, "loading");

        mWeb.loadUrl(url);
    }

    /**
     * ???????
     * 
     * @author conit
     */
    private static class ProgresssDialogFragment extends DialogFragment {

        public static ProgresssDialogFragment newInstance() {
            ProgresssDialogFragment frag = new ProgresssDialogFragment();
            return frag;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            ProgressDialog progressDialog = new ProgressDialog(getActivity());
            progressDialog.setCancelable(false);
            progressDialog.setMessage(getActivity().getString(R.string.dialog_loading_book));
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setOnKeyListener(new OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (KeyEvent.KEYCODE_BACK == keyCode) {
                        mSelf.removeDialogFragment();
                        mSelf.cancelLoad();
                        return true;
                    }
                    return false;
                }
            });
            return progressDialog;
        }
    }

    /**
     * ?????????
     */
    private void removeDialogFragment() {

        FragmentManager fm = getFragmentManager();
        if (fm == null) {
            return;
        }
        FragmentTransaction ft = fm.beginTransaction();
        DialogFragment df = (DialogFragment) getFragmentManager().findFragmentByTag("loading");
        if (df != null) {
            df.dismiss();
            ft.remove(df);
            ft.commit();
        }
    }

    /**
     * WebView??????{@code BookViewerActivity}????????
     */
    private void cancelLoad() {
        if (mWeb != null) {
            mWeb.stopLoading();
        }
        mOnCancelLoadLisntener.cancelLoad();
    }
}