project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/NetplayStateFragment.java
changeset 7508 763d3961400b
child 7584 7831c84cc644
equal deleted inserted replaced
7504:ed1d52c5aa94 7508:763d3961400b
       
     1 package org.hedgewars.hedgeroid;
       
     2 
       
     3 import org.hedgewars.hedgeroid.R;
       
     4 import org.hedgewars.hedgeroid.frontlib.Frontlib;
       
     5 import org.hedgewars.hedgeroid.netplay.Netplay;
       
     6 import org.hedgewars.hedgeroid.netplay.Netplay.State;
       
     7 
       
     8 import android.app.Activity;
       
     9 import android.content.BroadcastReceiver;
       
    10 import android.content.Context;
       
    11 import android.content.Intent;
       
    12 import android.content.IntentFilter;
       
    13 import android.os.Bundle;
       
    14 import android.support.v4.app.Fragment;
       
    15 import android.support.v4.content.LocalBroadcastManager;
       
    16 import android.widget.Toast;
       
    17 
       
    18 /**
       
    19  * Fragment for use by an activity that depends on the state of the network
       
    20  * connection. The activity must implement the NetplayStateListener interface.
       
    21  * 
       
    22  * This fragment manages a few aspects of the netplay connection: Requesting
       
    23  * the network system loop to run at high frequency while the activity is in
       
    24  * the foreground, and reacting to changes in the networking state by calling
       
    25  * a callback method on the activity. 
       
    26  */
       
    27 public class NetplayStateFragment extends Fragment {
       
    28     private Netplay netplay;
       
    29     private Context appContext;
       
    30     private LocalBroadcastManager broadcastManager;
       
    31     private NetplayStateListener listener;
       
    32     private State knownState;
       
    33     
       
    34     interface NetplayStateListener {
       
    35     	/**
       
    36     	 * This is called while the activity is running, and every time during resume, if
       
    37     	 * a change in the networking state is detected. It is also called once
       
    38     	 * with the initial state (which could be called a change from the "unknown" state).
       
    39     	 */
       
    40     	void onNetplayStateChanged(State newState);
       
    41     } 
       
    42     
       
    43     @Override
       
    44 	public void onAttach(Activity activity) {
       
    45 		super.onAttach(activity);
       
    46 		try {
       
    47 			listener = (NetplayStateListener) activity;
       
    48 		} catch(ClassCastException e) {
       
    49 			throw new ClassCastException("Activity " + activity + " must implement NetplayStateListener to use NetplayStateFragment.");
       
    50 		}
       
    51 	}
       
    52 	
       
    53 	@Override
       
    54 	public void onDetach() {
       
    55 		super.onDetach();
       
    56 		listener = null;
       
    57 	}
       
    58 	
       
    59     @Override
       
    60     public void onCreate(Bundle icicle) {
       
    61         super.onCreate(icicle);
       
    62         appContext = getActivity().getApplicationContext();
       
    63         broadcastManager = LocalBroadcastManager.getInstance(appContext);
       
    64         netplay = Netplay.getAppInstance(appContext);
       
    65     }    
       
    66 
       
    67     @Override
       
    68     public void onResume() {
       
    69     	super.onResume();
       
    70     	broadcastManager.registerReceiver(disconnectReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
       
    71     	broadcastManager.registerReceiver(leaveRoomReceiver, new IntentFilter(Netplay.ACTION_LEFT_ROOM));
       
    72     	broadcastManager.registerReceiver(stateChangeReceiver, new IntentFilter(Netplay.ACTION_STATE_CHANGED));
       
    73     	
       
    74     	State newState = netplay.getState();
       
    75 		if(knownState != newState) {
       
    76     		listener.onNetplayStateChanged(newState);
       
    77     		knownState = newState;
       
    78     	}
       
    79     }
       
    80     
       
    81     @Override
       
    82     public void onPause() {
       
    83     	super.onPause();
       
    84     	broadcastManager.unregisterReceiver(disconnectReceiver);
       
    85     	broadcastManager.unregisterReceiver(leaveRoomReceiver);
       
    86     	broadcastManager.unregisterReceiver(stateChangeReceiver);
       
    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 == Frontlib.NETCONN_ROOMLEAVE_ABANDONED) {
       
   105 				Toast.makeText(appContext, R.string.toast_room_abandoned, Toast.LENGTH_LONG).show();
       
   106 			} else if(reason == Frontlib.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 	};
       
   120 }