project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/ConnectionDependendDialogFragment.java
author unC0Rr
Sat, 17 Mar 2018 23:55:32 +0300
changeset 13245 950186baa764
parent 10017 de822cd3df3a
permissions -rw-r--r--
Fix crasher
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     1
/*
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     4
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     5
 * This program is free software; you can redistribute it and/or
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     6
 * modify it under the terms of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     8
 * of the License, or (at your option) any later version.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     9
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    13
 * GNU General Public License for more details.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    14
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    15
 * You should have received a copy of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    16
 * along with this program; if not, write to the Free Software
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    18
 */
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    19
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import org.hedgewars.hedgeroid.netplay.Netplay;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import org.hedgewars.hedgeroid.netplay.Netplay.State;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import android.app.Dialog;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import android.content.BroadcastReceiver;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import android.content.Context;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import android.content.Intent;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.content.IntentFilter;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.support.v4.app.DialogFragment;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.support.v4.content.LocalBroadcastManager;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    33
/**
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    34
 * Helper class for DialogFragments that are supposed to be dismissed when the
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    35
 * network connection is lost. This is used for some dialog fragments that
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    36
 * appear during connecting (e.g. username input)
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    37
 */
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
public class ConnectionDependendDialogFragment extends DialogFragment {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    39
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
    public void onStart() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    41
        super.onStart();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    42
        LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(dismissReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    43
        if(Netplay.getAppInstance(getActivity().getApplicationContext()).getState() == State.NOT_CONNECTED) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    44
            dismiss();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
    public void onStop() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
        super.onStop();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
        LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(dismissReceiver);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
    private BroadcastReceiver dismissReceiver = new BroadcastReceiver() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
        @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
        public void onReceive(Context context, Intent intent) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
            Dialog dialog = getDialog();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
            if(dialog != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
                dialog.dismiss();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
                dismiss();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
    };
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
}