de.persoapp.android.activity.dialog.NoInternetConnectionDialog.java Source code

Java tutorial

Introduction

Here is the source code for de.persoapp.android.activity.dialog.NoInternetConnectionDialog.java

Source

/**
 *
 * COPYRIGHT (C) 2010, 2011, 2012, 2013, 2014 AGETO Innovation GmbH
 *
 * Authors Christian Kahlo, Ralf Wondratschek
 *
 * All Rights Reserved.
 *
 * Contact: PersoApp, http://www.persoapp.de
 *
 * @version 1.0, 30.07.2013 13:50:47
 *
 *          This file is part of PersoApp.
 *
 *          PersoApp is free software: you can redistribute it and/or modify it
 *          under the terms of the GNU Lesser General Public License as
 *          published by the Free Software Foundation, either version 3 of the
 *          License, or (at your option) any later version.
 *
 *          PersoApp 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
 *          Lesser General Public License for more details.
 *
 *          You should have received a copy of the GNU Lesser General Public
 *          License along with PersoApp. If not, see
 *          <http://www.gnu.org/licenses/>.
 *
 *          Diese Datei ist Teil von PersoApp.
 *
 *          PersoApp ist Freie Software: Sie knnen es unter den Bedingungen der
 *          GNU Lesser General Public License, wie von der Free Software
 *          Foundation, Version 3 der Lizenz oder (nach Ihrer Option) jeder
 *          spteren verffentlichten Version, weiterverbreiten und/oder
 *          modifizieren.
 *
 *          PersoApp wird in der Hoffnung, dass es ntzlich sein wird, aber OHNE
 *          JEDE GEWHRLEISTUNG, bereitgestellt; sogar ohne die implizite
 *          Gewhrleistung der MARKTFHIGKEIT oder EIGNUNG FR EINEN BESTIMMTEN
 *          ZWECK. Siehe die GNU Lesser General Public License fr weitere
 *          Details.
 *
 *          Sie sollten eine Kopie der GNU Lesser General Public License
 *          zusammen mit diesem Programm erhalten haben. Wenn nicht, siehe
 *          <http://www.gnu.org/licenses/>.
 *
 */
package de.persoapp.android.activity.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.DialogFragment;
import android.view.View;

import net.vrallev.android.base.BaseActivitySupport;
import net.vrallev.android.base.util.NetworkHelper;

import javax.inject.Inject;

import de.greenrobot.event.EventBus;
import de.persoapp.android.R;
import de.persoapp.android.activity.fragment.InitializeAppFragment;

/**
 * The <tt>NoInternetConnectionDialog</tt> informs the user that the used
 * smartphone has no connection to the internet or a similiar network.
 * 
 * @author Ralf Wondratschek
 * @author Rico Klimsa - added javadoc comments.
 */
public class NoInternetConnectionDialog extends DialogFragment {

    @Inject
    NetworkHelper mNetworkHelper;

    @Inject
    EventBus mEventBus;

    private boolean mRegistered;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((BaseActivitySupport) activity).inject(this);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                case DialogInterface.BUTTON_NEGATIVE:
                    getActivity().finish();
                    break;
                }
            }
        };

        return new AlertDialog.Builder(getActivity()).setTitle(R.string.no_internet_connection)
                .setMessage(R.string.no_internet_connection_message).setPositiveButton(R.string.settings, null)
                .setNegativeButton(android.R.string.cancel, onClickListener).create();
    }

    @SuppressWarnings("ConstantConditions")
    @Override
    public void onResume() {
        super.onResume();

        if (mNetworkHelper.isNetworkAvailable()) {
            closeDialog();

        } else {
            AlertDialog dialog = (AlertDialog) getDialog();

            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                }
            });

            getActivity().registerReceiver(mBroadcastReceiver,
                    new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
            mRegistered = true;
        }
    }

    @Override
    public void onPause() {
        if (mRegistered) {
            getActivity().unregisterReceiver(mBroadcastReceiver);
            mRegistered = false;
        }
        super.onPause();
    }

    private void closeDialog() {
        dismiss();
        mEventBus.post(new InitializeAppFragment.OnAppInitialized(true));
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mNetworkHelper.isNetworkAvailable()) {
                closeDialog();
            }
        }
    };
}