project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/NetplayStateFragment.java
author Medo <smaxein@googlemail.com>
Mon, 06 Aug 2012 22:39:36 +0200
changeset 7476 2fb781bbdd51
parent 7461 38acbfdb484f
permissions -rw-r--r--
Hedgeroid: Start using the frontlib for more operations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
package org.hedgewars.hedgeroid.netplay;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
     3
import org.hedgewars.hedgeroid.R;
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7461
diff changeset
     4
import org.hedgewars.hedgeroid.frontlib.Frontlib;
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
import org.hedgewars.hedgeroid.netplay.Netplay.State;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
     7
import android.app.Activity;
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
import android.content.BroadcastReceiver;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
import android.content.Context;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
import android.content.Intent;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
import android.content.IntentFilter;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
import android.os.Bundle;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
import android.support.v4.app.Fragment;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
import android.support.v4.content.LocalBroadcastManager;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
import android.widget.Toast;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
/**
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
 * Fragment for use by an activity that depends on the state of the network
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    19
 * connection. The activity must implement the NetplayStateListener interface.
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
 * 
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
 * This fragment manages a few aspects of the netplay connection: Requesting
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
 * the network system loop to run at high frequency while the activity is in
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    23
 * the foreground, and reacting to changes in the networking state by calling
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    24
 * a callback method on the activity.
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
 */
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
public class NetplayStateFragment extends Fragment {
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
    private Netplay netplay;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
    private Context appContext;
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
    private LocalBroadcastManager broadcastManager;
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    30
    private NetplayStateListener listener;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    31
    private State knownState;
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
    
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    33
    interface NetplayStateListener {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    34
    	/**
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    35
    	 * This is called while the activity is running, and every time during resume, if
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    36
    	 * a change in the networking state is detected. It is also called once
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    37
    	 * with the initial state (which could be called a change from the "unknown" state).
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    38
    	 */
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    39
    	void onNetplayStateChanged(State newState);
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    40
    }
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    41
    
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    42
    @Override
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    43
	public void onAttach(Activity activity) {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    44
		super.onAttach(activity);
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    45
		try {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    46
			listener = (NetplayStateListener) activity;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    47
		} catch(ClassCastException e) {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    48
			throw new ClassCastException("Activity " + activity + " must implement NetplayStateListener to use NetplayStateFragment.");
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
		}
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    50
	}
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    51
	
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    52
	@Override
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    53
	public void onDetach() {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    54
		super.onDetach();
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    55
		listener = null;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    56
	}
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    57
	
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
    @Override
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
    public void onCreate(Bundle icicle) {
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
        super.onCreate(icicle);
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
        appContext = getActivity().getApplicationContext();
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
        broadcastManager = LocalBroadcastManager.getInstance(appContext);
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
        netplay = Netplay.getAppInstance(appContext);
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
    }    
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
    @Override
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
    public void onResume() {
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
    	super.onResume();
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
    	broadcastManager.registerReceiver(disconnectReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    70
    	broadcastManager.registerReceiver(leaveRoomReceiver, new IntentFilter(Netplay.ACTION_LEFT_ROOM));
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    71
    	broadcastManager.registerReceiver(stateChangeReceiver, new IntentFilter(Netplay.ACTION_STATE_CHANGED));
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
    	netplay.requestFastTicks();
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
    	
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    74
    	State newState = netplay.getState();
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    75
		if(knownState != newState) {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    76
    		listener.onNetplayStateChanged(newState);
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    77
    		knownState = newState;
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
    	}
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
    }
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
    
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
    @Override
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
    public void onPause() {
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
    	super.onPause();
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
    	broadcastManager.unregisterReceiver(disconnectReceiver);
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    85
    	broadcastManager.unregisterReceiver(leaveRoomReceiver);
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    86
    	broadcastManager.unregisterReceiver(stateChangeReceiver);
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
    	netplay.unrequestFastTicks();
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
    }
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    89
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    90
	private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    91
		@Override
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    92
		public void onReceive(Context context, Intent intent) {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    93
			if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    94
				String message = intent.getStringExtra(Netplay.EXTRA_MESSAGE);
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    95
				String toastText = getString(R.string.toast_disconnected, message);
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    96
				Toast.makeText(appContext, toastText, Toast.LENGTH_LONG).show();
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    97
			}
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    98
		}
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    99
	};
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   100
	
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   101
	private final BroadcastReceiver leaveRoomReceiver = new BroadcastReceiver() {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   102
		@Override
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   103
		public void onReceive(Context context, Intent intent) {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   104
			int reason = intent.getIntExtra(Netplay.EXTRA_REASON, -1);
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7461
diff changeset
   105
			if(reason == Frontlib.NETCONN_ROOMLEAVE_ABANDONED) {
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   106
				Toast.makeText(appContext, R.string.toast_room_abandoned, Toast.LENGTH_LONG).show();
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7461
diff changeset
   107
			} else if(reason == Frontlib.NETCONN_ROOMLEAVE_KICKED) {
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   108
				Toast.makeText(appContext, R.string.toast_kicked, Toast.LENGTH_LONG).show();
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   109
			}
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   110
		}
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   111
	};
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   112
	
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   113
	private final BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   114
		@Override
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   115
		public void onReceive(Context context, Intent intent) {
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   116
			State newState = netplay.getState();
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   117
			listener.onNetplayStateChanged(newState);
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   118
			knownState = newState;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   119
		}
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   120
	};
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
}