project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/ConnectingDialog.java
changeset 7582 714310efad8f
parent 7444 2e31f114f57e
child 7584 7831c84cc644
equal deleted inserted replaced
7580:c92596feac0d 7582:714310efad8f
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU General Public License
       
     7  * as published by the Free Software Foundation; either version 2
       
     8  * of the License, or (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    18  */
       
    19 
     1 package org.hedgewars.hedgeroid;
    20 package org.hedgewars.hedgeroid;
     2 
    21 
     3 import org.hedgewars.hedgeroid.netplay.Netplay;
    22 import org.hedgewars.hedgeroid.netplay.Netplay;
     4 import org.hedgewars.hedgeroid.netplay.Netplay.State;
    23 import org.hedgewars.hedgeroid.netplay.Netplay.State;
     5 
    24 
    10 import android.content.DialogInterface;
    29 import android.content.DialogInterface;
    11 import android.content.Intent;
    30 import android.content.Intent;
    12 import android.content.IntentFilter;
    31 import android.content.IntentFilter;
    13 import android.os.Bundle;
    32 import android.os.Bundle;
    14 import android.support.v4.content.LocalBroadcastManager;
    33 import android.support.v4.content.LocalBroadcastManager;
       
    34 import android.widget.Toast;
    15 
    35 
       
    36 /**
       
    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
       
    39  * lobby state), an error toast with the disconnect message is shown.
       
    40  * 
       
    41  */
    16 public class ConnectingDialog extends ConnectionDependendDialogFragment {
    42 public class ConnectingDialog extends ConnectionDependendDialogFragment {
    17 	@Override
    43 	@Override
    18 	public void onStart() {
    44 	public void onStart() {
    19 		super.onStart();
    45 		super.onStart();
    20 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
    46 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
    21 
    47 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(disconnectedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
       
    48 		
    22 		if(Netplay.getAppInstance(getActivity().getApplicationContext()).getState() != State.CONNECTING) {
    49 		if(Netplay.getAppInstance(getActivity().getApplicationContext()).getState() != State.CONNECTING) {
    23 			dismiss();
    50 			dismiss();
    24 		}
    51 		}
    25 	}
    52 	}
    26 	
    53 	
    27 	@Override
    54 	@Override
    28 	public void onStop() {
    55 	public void onStop() {
    29 		super.onStop();
    56 		super.onStop();
    30 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(connectedReceiver);
    57 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(connectedReceiver);
       
    58 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(disconnectedReceiver);
    31 	}
    59 	}
    32 	
    60 	
    33 	@Override
    61 	@Override
    34 	public Dialog onCreateDialog(Bundle savedInstanceState) {
    62 	public Dialog onCreateDialog(Bundle savedInstanceState) {
    35 		ProgressDialog dialog = new ProgressDialog(getActivity());
    63 		ProgressDialog dialog = new ProgressDialog(getActivity());
    50 				dismiss();
    78 				dismiss();
    51 			}
    79 			}
    52 		}
    80 		}
    53 	};
    81 	};
    54 	
    82 	
       
    83 	private BroadcastReceiver disconnectedReceiver = new BroadcastReceiver() {
       
    84 		@Override
       
    85 		public void onReceive(Context context, Intent intent) {
       
    86 			Toast.makeText(getActivity(), intent.getExtras().getString(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show();
       
    87 		}
       
    88 	};
       
    89 	
    55 	public void onCancel(DialogInterface dialog) {
    90 	public void onCancel(DialogInterface dialog) {
    56 		super.onCancel(dialog);
    91 		super.onCancel(dialog);
    57 		Netplay.getAppInstance(getActivity().getApplicationContext()).disconnect();
    92 		Netplay.getAppInstance(getActivity().getApplicationContext()).disconnect();
    58 	};
    93 	};
    59 }
    94 }