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