project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/NetplayStateFragment.java
author koda
Tue, 21 Jan 2014 22:43:06 +0100
changeset 10017 de822cd3df3a
parent 7584 7831c84cc644
permissions -rw-r--r--
fixwhitespace and dos2unix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     4
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     5
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     6
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     8
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     9
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    13
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    14
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    15
 * You should have received a copy of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    16
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    18
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    19
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import org.hedgewars.hedgeroid.R;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import org.hedgewars.hedgeroid.frontlib.Frontlib;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import org.hedgewars.hedgeroid.netplay.Netplay;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import org.hedgewars.hedgeroid.netplay.Netplay.State;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import android.app.Activity;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import android.content.BroadcastReceiver;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.content.Context;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.content.Intent;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.content.IntentFilter;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.os.Bundle;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.support.v4.app.Fragment;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.support.v4.content.LocalBroadcastManager;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import android.widget.Toast;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
/**
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
 * Fragment for use by an activity that depends on the state of the network
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
 * connection. The activity must implement the NetplayStateListener interface.
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
 *
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    41
 * This fragment manages reacting to changes in the networking state by calling
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    42
 * a callback method on the activity.
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
 */
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
public class NetplayStateFragment extends Fragment {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
    private Netplay netplay;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
    private Context appContext;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
    private LocalBroadcastManager broadcastManager;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
    private NetplayStateListener listener;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
    private State knownState;
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
    interface NetplayStateListener {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
        /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
         * This is called while the activity is running, and every time during resume, if
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
         * a change in the networking state is detected. It is also called once
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
         * with the initial state (which could be called a change from the "unknown" state).
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
         */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
        void onNetplayStateChanged(State newState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
    @Override
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
    public void onAttach(Activity activity) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
        super.onAttach(activity);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
            listener = (NetplayStateListener) activity;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
        } catch(ClassCastException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
            throw new ClassCastException("Activity " + activity + " must implement NetplayStateListener to use NetplayStateFragment.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
    public void onDetach() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
        super.onDetach();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
        listener = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
    @Override
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
    public void onCreate(Bundle icicle) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
        super.onCreate(icicle);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
        appContext = getActivity().getApplicationContext();
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
        broadcastManager = LocalBroadcastManager.getInstance(appContext);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
        netplay = Netplay.getAppInstance(appContext);
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
    }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
    @Override
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
    public void onResume() {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
        super.onResume();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
        broadcastManager.registerReceiver(disconnectReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
        broadcastManager.registerReceiver(leaveRoomReceiver, new IntentFilter(Netplay.ACTION_LEFT_ROOM));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
        broadcastManager.registerReceiver(stateChangeReceiver, new IntentFilter(Netplay.ACTION_STATE_CHANGED));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
        State newState = netplay.getState();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
        if(knownState != newState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
            listener.onNetplayStateChanged(newState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
            knownState = newState;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
        }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    96
    }
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
    @Override
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
    public void onPause() {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
        super.onPause();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   101
        broadcastManager.unregisterReceiver(disconnectReceiver);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   102
        broadcastManager.unregisterReceiver(leaveRoomReceiver);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
        broadcastManager.unregisterReceiver(stateChangeReceiver);
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
    }
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   105
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
    private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
        @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
        public void onReceive(Context context, Intent intent) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
            if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
                String message = intent.getStringExtra(Netplay.EXTRA_MESSAGE);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   111
                String toastText = getString(R.string.toast_disconnected, message);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   112
                Toast.makeText(appContext, toastText, Toast.LENGTH_LONG).show();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   113
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   114
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   115
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   116
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   117
    private final BroadcastReceiver leaveRoomReceiver = new BroadcastReceiver() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   118
        @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   119
        public void onReceive(Context context, Intent intent) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   120
            int reason = intent.getIntExtra(Netplay.EXTRA_REASON, -1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   121
            if(reason == Frontlib.NETCONN_ROOMLEAVE_ABANDONED) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   122
                Toast.makeText(appContext, R.string.toast_room_abandoned, Toast.LENGTH_LONG).show();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   123
            } else if(reason == Frontlib.NETCONN_ROOMLEAVE_KICKED) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   124
                Toast.makeText(appContext, R.string.toast_kicked, Toast.LENGTH_LONG).show();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   125
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   126
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   127
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   128
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   129
    private final BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   130
        @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   131
        public void onReceive(Context context, Intent intent) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   132
            State newState = netplay.getState();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   133
            listener.onNetplayStateChanged(newState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   134
            knownState = newState;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   135
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   136
    };
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   137
}