project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/ConnectingDialog.java
branchhedgeroid
changeset 7857 2bc61f8841a1
parent 7584 7831c84cc644
child 10017 de822cd3df3a
equal deleted inserted replaced
7855:ddcdedd3330b 7857:2bc61f8841a1
       
     1 /*
       
     2  * Hedgewars for Android. An Android port of 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 
       
    20 package org.hedgewars.hedgeroid;
       
    21 
       
    22 import org.hedgewars.hedgeroid.netplay.Netplay;
       
    23 import org.hedgewars.hedgeroid.netplay.Netplay.State;
       
    24 
       
    25 import android.app.Dialog;
       
    26 import android.app.ProgressDialog;
       
    27 import android.content.BroadcastReceiver;
       
    28 import android.content.Context;
       
    29 import android.content.DialogInterface;
       
    30 import android.content.Intent;
       
    31 import android.content.IntentFilter;
       
    32 import android.os.Bundle;
       
    33 import android.support.v4.content.LocalBroadcastManager;
       
    34 import android.widget.Toast;
       
    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  */
       
    42 public class ConnectingDialog extends ConnectionDependendDialogFragment {
       
    43 	@Override
       
    44 	public void onStart() {
       
    45 		super.onStart();
       
    46 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
       
    47 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(disconnectedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
       
    48 		
       
    49 		if(Netplay.getAppInstance(getActivity().getApplicationContext()).getState() != State.CONNECTING) {
       
    50 			dismiss();
       
    51 		}
       
    52 	}
       
    53 	
       
    54 	@Override
       
    55 	public void onStop() {
       
    56 		super.onStop();
       
    57 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(connectedReceiver);
       
    58 		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(disconnectedReceiver);
       
    59 	}
       
    60 	
       
    61 	@Override
       
    62 	public Dialog onCreateDialog(Bundle savedInstanceState) {
       
    63 		ProgressDialog dialog = new ProgressDialog(getActivity());
       
    64 		dialog.setIndeterminate(true);
       
    65 		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
       
    66 		dialog.setTitle(R.string.dialog_connecting_title);
       
    67 		dialog.setMessage(getString(R.string.dialog_connecting_message));
       
    68 		return dialog;
       
    69 	}
       
    70 	
       
    71 	private BroadcastReceiver connectedReceiver = new BroadcastReceiver() {
       
    72 		@Override
       
    73 		public void onReceive(Context context, Intent intent) {
       
    74 			Dialog dialog = getDialog();
       
    75 			if(dialog != null) {
       
    76 				dialog.dismiss();
       
    77 			} else {
       
    78 				dismiss();
       
    79 			}
       
    80 		}
       
    81 	};
       
    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 	
       
    90 	public void onCancel(DialogInterface dialog) {
       
    91 		super.onCancel(dialog);
       
    92 		Netplay.getAppInstance(getActivity().getApplicationContext()).disconnect();
       
    93 	};
       
    94 }