project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/ConnectingDialog.java
changeset 10017 de822cd3df3a
parent 7584 7831c84cc644
equal deleted inserted replaced
10015:4feced261c68 10017:de822cd3df3a
    35 
    35 
    36 /**
    36 /**
    37  * Indeterminate progress dialog that is shown in the MainActivity while trying
    37  * Indeterminate progress dialog that is shown in the MainActivity while trying
    38  * to connect to the server. If the connection fails (disconnect before we reach
    38  * to connect to the server. If the connection fails (disconnect before we reach
    39  * lobby state), an error toast with the disconnect message is shown.
    39  * lobby state), an error toast with the disconnect message is shown.
    40  * 
    40  *
    41  */
    41  */
    42 public class ConnectingDialog extends ConnectionDependendDialogFragment {
    42 public class ConnectingDialog extends ConnectionDependendDialogFragment {
    43 	@Override
    43     @Override
    44 	public void onStart() {
    44     public void onStart() {
    45 		super.onStart();
    45         super.onStart();
    46 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
    46         LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
    47 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(disconnectedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
    47         LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(disconnectedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
    48 		
    48 
    49 		if(Netplay.getAppInstance(getActivity().getApplicationContext()).getState() != State.CONNECTING) {
    49         if(Netplay.getAppInstance(getActivity().getApplicationContext()).getState() != State.CONNECTING) {
    50 			dismiss();
    50             dismiss();
    51 		}
    51         }
    52 	}
    52     }
    53 	
    53 
    54 	@Override
    54     @Override
    55 	public void onStop() {
    55     public void onStop() {
    56 		super.onStop();
    56         super.onStop();
    57 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(connectedReceiver);
    57         LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(connectedReceiver);
    58 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(disconnectedReceiver);
    58         LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(disconnectedReceiver);
    59 	}
    59     }
    60 	
    60 
    61 	@Override
    61     @Override
    62 	public Dialog onCreateDialog(Bundle savedInstanceState) {
    62     public Dialog onCreateDialog(Bundle savedInstanceState) {
    63 		ProgressDialog dialog = new ProgressDialog(getActivity());
    63         ProgressDialog dialog = new ProgressDialog(getActivity());
    64 		dialog.setIndeterminate(true);
    64         dialog.setIndeterminate(true);
    65 		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    65         dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    66 		dialog.setTitle(R.string.dialog_connecting_title);
    66         dialog.setTitle(R.string.dialog_connecting_title);
    67 		dialog.setMessage(getString(R.string.dialog_connecting_message));
    67         dialog.setMessage(getString(R.string.dialog_connecting_message));
    68 		return dialog;
    68         return dialog;
    69 	}
    69     }
    70 	
    70 
    71 	private BroadcastReceiver connectedReceiver = new BroadcastReceiver() {
    71     private BroadcastReceiver connectedReceiver = new BroadcastReceiver() {
    72 		@Override
    72         @Override
    73 		public void onReceive(Context context, Intent intent) {
    73         public void onReceive(Context context, Intent intent) {
    74 			Dialog dialog = getDialog();
    74             Dialog dialog = getDialog();
    75 			if(dialog != null) {
    75             if(dialog != null) {
    76 				dialog.dismiss();
    76                 dialog.dismiss();
    77 			} else {
    77             } else {
    78 				dismiss();
    78                 dismiss();
    79 			}
    79             }
    80 		}
    80         }
    81 	};
    81     };
    82 	
    82 
    83 	private BroadcastReceiver disconnectedReceiver = new BroadcastReceiver() {
    83     private BroadcastReceiver disconnectedReceiver = new BroadcastReceiver() {
    84 		@Override
    84         @Override
    85 		public void onReceive(Context context, Intent intent) {
    85         public void onReceive(Context context, Intent intent) {
    86 			Toast.makeText(getActivity(), intent.getExtras().getString(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show();
    86             Toast.makeText(getActivity(), intent.getExtras().getString(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show();
    87 		}
    87         }
    88 	};
    88     };
    89 	
    89 
    90 	public void onCancel(DialogInterface dialog) {
    90     public void onCancel(DialogInterface dialog) {
    91 		super.onCancel(dialog);
    91         super.onCancel(dialog);
    92 		Netplay.getAppInstance(getActivity().getApplicationContext()).disconnect();
    92         Netplay.getAppInstance(getActivity().getApplicationContext()).disconnect();
    93 	};
    93     };
    94 }
    94 }