34 * Helper class for DialogFragments that are supposed to be dismissed when the |
34 * Helper class for DialogFragments that are supposed to be dismissed when the |
35 * network connection is lost. This is used for some dialog fragments that |
35 * network connection is lost. This is used for some dialog fragments that |
36 * appear during connecting (e.g. username input) |
36 * appear during connecting (e.g. username input) |
37 */ |
37 */ |
38 public class ConnectionDependendDialogFragment extends DialogFragment { |
38 public class ConnectionDependendDialogFragment extends DialogFragment { |
39 @Override |
39 @Override |
40 public void onStart() { |
40 public void onStart() { |
41 super.onStart(); |
41 super.onStart(); |
42 LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(dismissReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED)); |
42 LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(dismissReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED)); |
43 if(Netplay.getAppInstance(getActivity().getApplicationContext()).getState() == State.NOT_CONNECTED) { |
43 if(Netplay.getAppInstance(getActivity().getApplicationContext()).getState() == State.NOT_CONNECTED) { |
44 dismiss(); |
44 dismiss(); |
45 } |
45 } |
46 } |
46 } |
47 |
47 |
48 @Override |
48 @Override |
49 public void onStop() { |
49 public void onStop() { |
50 super.onStop(); |
50 super.onStop(); |
51 LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(dismissReceiver); |
51 LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(dismissReceiver); |
52 } |
52 } |
53 |
53 |
54 private BroadcastReceiver dismissReceiver = new BroadcastReceiver() { |
54 private BroadcastReceiver dismissReceiver = new BroadcastReceiver() { |
55 @Override |
55 @Override |
56 public void onReceive(Context context, Intent intent) { |
56 public void onReceive(Context context, Intent intent) { |
57 Dialog dialog = getDialog(); |
57 Dialog dialog = getDialog(); |
58 if(dialog != null) { |
58 if(dialog != null) { |
59 dialog.dismiss(); |
59 dialog.dismiss(); |
60 } else { |
60 } else { |
61 dismiss(); |
61 dismiss(); |
62 } |
62 } |
63 } |
63 } |
64 }; |
64 }; |
65 } |
65 } |