7449
|
1 |
package org.hedgewars.hedgeroid.netplay;
|
|
2 |
|
|
3 |
import org.hedgewars.hedgeroid.netplay.Netplay.State;
|
|
4 |
|
|
5 |
import android.content.BroadcastReceiver;
|
|
6 |
import android.content.Context;
|
|
7 |
import android.content.Intent;
|
|
8 |
import android.content.IntentFilter;
|
|
9 |
import android.os.Bundle;
|
|
10 |
import android.support.v4.app.Fragment;
|
|
11 |
import android.support.v4.content.LocalBroadcastManager;
|
|
12 |
import android.widget.Toast;
|
|
13 |
|
|
14 |
/**
|
|
15 |
* Fragment for use by an activity that depends on the state of the network
|
|
16 |
* connection.
|
|
17 |
*
|
|
18 |
* 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
|
|
20 |
* the foreground, and reacting to changes in the networking state by switching
|
|
21 |
* to the appropriate activity or finishing the activity if the network connection
|
|
22 |
* is closed.
|
|
23 |
*/
|
|
24 |
public class NetplayStateFragment extends Fragment {
|
|
25 |
private Netplay netplay;
|
|
26 |
private Context appContext;
|
|
27 |
private LocalBroadcastManager broadcastManager;
|
|
28 |
|
|
29 |
private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() {
|
|
30 |
@Override
|
|
31 |
public void onReceive(Context context, Intent intent) {
|
|
32 |
if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) {
|
|
33 |
String message = intent.getStringExtra(Netplay.EXTRA_MESSAGE);
|
|
34 |
Toast.makeText(appContext, "Disconnected: "+message, Toast.LENGTH_LONG).show();
|
|
35 |
}
|
|
36 |
getActivity().finish();
|
|
37 |
}
|
|
38 |
};
|
|
39 |
|
|
40 |
@Override
|
|
41 |
public void onCreate(Bundle icicle) {
|
|
42 |
super.onCreate(icicle);
|
|
43 |
appContext = getActivity().getApplicationContext();
|
|
44 |
broadcastManager = LocalBroadcastManager.getInstance(appContext);
|
|
45 |
netplay = Netplay.getAppInstance(appContext);
|
|
46 |
}
|
|
47 |
|
|
48 |
@Override
|
|
49 |
public void onResume() {
|
|
50 |
super.onResume();
|
|
51 |
broadcastManager.registerReceiver(disconnectReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
|
|
52 |
netplay.requestFastTicks();
|
|
53 |
|
|
54 |
if(netplay.getState() == State.NOT_CONNECTED) {
|
|
55 |
getActivity().finish();
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
|
59 |
@Override
|
|
60 |
public void onPause() {
|
|
61 |
super.onPause();
|
|
62 |
broadcastManager.unregisterReceiver(disconnectReceiver);
|
|
63 |
netplay.unrequestFastTicks();
|
|
64 |
}
|
|
65 |
}
|