project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java
changeset 10017 de822cd3df3a
parent 7584 7831c84cc644
child 15511 fb23e49b2d4e
equal deleted inserted replaced
10015:4feced261c68 10017:de822cd3df3a
    47 import android.view.View.OnClickListener;
    47 import android.view.View.OnClickListener;
    48 import android.widget.Button;
    48 import android.widget.Button;
    49 import android.widget.Toast;
    49 import android.widget.Toast;
    50 
    50 
    51 public class MainActivity extends FragmentActivity {
    51 public class MainActivity extends FragmentActivity {
    52 	private static final int DIALOG_NO_SDCARD = 0;
    52     private static final int DIALOG_NO_SDCARD = 0;
    53 	
    53 
    54 	private LocalBroadcastManager broadcastManager;
    54     private LocalBroadcastManager broadcastManager;
    55 	private ProgressDialog assetsDialog;
    55     private ProgressDialog assetsDialog;
    56 
    56 
    57 	public void onCreate(Bundle sis){
    57     public void onCreate(Bundle sis){
    58 		super.onCreate(sis);
    58         super.onCreate(sis);
    59 		setContentView(R.layout.activity_main);
    59         setContentView(R.layout.activity_main);
    60 
    60 
    61 		broadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
    61         broadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
    62 		Button startLocalGame = (Button)findViewById(R.id.startGame);
    62         Button startLocalGame = (Button)findViewById(R.id.startGame);
    63 		Button startNetGame = (Button)findViewById(R.id.joinLobby);
    63         Button startNetGame = (Button)findViewById(R.id.joinLobby);
    64 
    64 
    65 		startLocalGame.setOnClickListener(startGameListener);
    65         startLocalGame.setOnClickListener(startGameListener);
    66 		startNetGame.setOnClickListener(startNetGameListener);
    66         startNetGame.setOnClickListener(startNetGameListener);
    67 
    67 
    68 		if(!FileUtils.isDataPathAvailable()){
    68         if(!FileUtils.isDataPathAvailable()){
    69 			showDialog(DIALOG_NO_SDCARD);
    69             showDialog(DIALOG_NO_SDCARD);
    70 		} else {
    70         } else {
    71 			String existingVersion = "";
    71             String existingVersion = "";
    72 			try {
    72             try {
    73 				File versionFile = new File(FileUtils.getCachePath(this), "assetsversion.txt");
    73                 File versionFile = new File(FileUtils.getCachePath(this), "assetsversion.txt");
    74 				existingVersion = FileUtils.readToString(new FileInputStream(versionFile));
    74                 existingVersion = FileUtils.readToString(new FileInputStream(versionFile));
    75 			} catch(IOException e) {
    75             } catch(IOException e) {
    76 			}
    76             }
    77 			
    77 
    78 			String newVersion = "";
    78             String newVersion = "";
    79 			try {
    79             try {
    80 				newVersion = FileUtils.readToString(getAssets().open("assetsversion.txt"));
    80                 newVersion = FileUtils.readToString(getAssets().open("assetsversion.txt"));
    81 			} catch(IOException e) {
    81             } catch(IOException e) {
    82 			}
    82             }
    83 			
    83 
    84 			if(!existingVersion.equals(newVersion)) {
    84             if(!existingVersion.equals(newVersion)) {
    85 				DownloadAssets assetsAsyncTask = new DownloadAssets(this);
    85                 DownloadAssets assetsAsyncTask = new DownloadAssets(this);
    86 				assetsDialog = ProgressDialog.show(this, "Please wait a moment", "Moving assets to SD card...");
    86                 assetsDialog = ProgressDialog.show(this, "Please wait a moment", "Moving assets to SD card...");
    87 				assetsAsyncTask.execute();
    87                 assetsAsyncTask.execute();
    88 			}
    88             }
    89 		}
    89         }
    90 	}
    90     }
    91 
    91 
    92 	@Override
    92     @Override
    93 	protected void onResume() {
    93     protected void onResume() {
    94 		super.onResume();
    94         super.onResume();
    95 		broadcastManager.registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
    95         broadcastManager.registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
    96 		broadcastManager.registerReceiver(connectionFailedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
    96         broadcastManager.registerReceiver(connectionFailedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
    97 		broadcastManager.registerReceiver(passwordRequestedReceiver, new IntentFilter(Netplay.ACTION_PASSWORD_REQUESTED));
    97         broadcastManager.registerReceiver(passwordRequestedReceiver, new IntentFilter(Netplay.ACTION_PASSWORD_REQUESTED));
    98 	}
    98     }
    99 	
    99 
   100 	@Override
   100     @Override
   101 	protected void onPause() {
   101     protected void onPause() {
   102 		super.onPause();
   102         super.onPause();
   103 		broadcastManager.unregisterReceiver(connectedReceiver);
   103         broadcastManager.unregisterReceiver(connectedReceiver);
   104 		broadcastManager.unregisterReceiver(connectionFailedReceiver);
   104         broadcastManager.unregisterReceiver(connectionFailedReceiver);
   105 		broadcastManager.unregisterReceiver(passwordRequestedReceiver);
   105         broadcastManager.unregisterReceiver(passwordRequestedReceiver);
   106 		Netplay netplay = Netplay.getAppInstance(getApplicationContext());
   106         Netplay netplay = Netplay.getAppInstance(getApplicationContext());
   107 		if(netplay.getState() == State.CONNECTING) {
   107         if(netplay.getState() == State.CONNECTING) {
   108 			netplay.disconnect();
   108             netplay.disconnect();
   109 		}
   109         }
   110 	}
   110     }
   111 	
   111 
   112 	@Override
   112     @Override
   113 	public boolean onCreateOptionsMenu(Menu menu) {
   113     public boolean onCreateOptionsMenu(Menu menu) {
   114 		super.onCreateOptionsMenu(menu);
   114         super.onCreateOptionsMenu(menu);
   115 		getMenuInflater().inflate(R.menu.main_options, menu);
   115         getMenuInflater().inflate(R.menu.main_options, menu);
   116 		return true;
   116         return true;
   117 	}
   117     }
   118 	
   118 
   119 	@Override
   119     @Override
   120 	public boolean onOptionsItemSelected(MenuItem item) {
   120     public boolean onOptionsItemSelected(MenuItem item) {
   121 		switch(item.getItemId()) {
   121         switch(item.getItemId()) {
   122 		case R.id.download:
   122         case R.id.download:
   123 			startActivityForResult(new Intent(this, DownloadListActivity.class), 0);
   123             startActivityForResult(new Intent(this, DownloadListActivity.class), 0);
   124 			return true;
   124             return true;
   125 		case R.id.preferences:
   125         case R.id.preferences:
   126 			Toast.makeText(this, R.string.not_implemented_yet, Toast.LENGTH_SHORT).show();
   126             Toast.makeText(this, R.string.not_implemented_yet, Toast.LENGTH_SHORT).show();
   127 			return true;
   127             return true;
   128 		case R.id.edit_weaponsets:
   128         case R.id.edit_weaponsets:
   129 			startActivity(new Intent(this, WeaponsetListActivity.class));
   129             startActivity(new Intent(this, WeaponsetListActivity.class));
   130 			return true;
   130             return true;
   131 		case R.id.edit_teams:
   131         case R.id.edit_teams:
   132 			startActivity(new Intent(this, TeamListActivity.class));
   132             startActivity(new Intent(this, TeamListActivity.class));
   133 			return true;
   133             return true;
   134 		default:
   134         default:
   135 			return super.onOptionsItemSelected(item);
   135             return super.onOptionsItemSelected(item);
   136 		}
   136         }
   137 	}
   137     }
   138 	
   138 
   139 	public Dialog onCreateDialog(int id, Bundle args){
   139     public Dialog onCreateDialog(int id, Bundle args){
   140 		switch(id) {
   140         switch(id) {
   141 		case DIALOG_NO_SDCARD:
   141         case DIALOG_NO_SDCARD:
   142 			return createNoSdcardDialog();
   142             return createNoSdcardDialog();
   143 		default:
   143         default:
   144 			throw new IndexOutOfBoundsException();
   144             throw new IndexOutOfBoundsException();
   145 		}
   145         }
   146 	}
   146     }
   147 
   147 
   148 	private Dialog createNoSdcardDialog() {
   148     private Dialog createNoSdcardDialog() {
   149 		AlertDialog.Builder builder = new AlertDialog.Builder(this);
   149         AlertDialog.Builder builder = new AlertDialog.Builder(this);
   150 		builder.setTitle(R.string.sdcard_not_mounted_title);
   150         builder.setTitle(R.string.sdcard_not_mounted_title);
   151 		builder.setMessage(R.string.sdcard_not_mounted);
   151         builder.setMessage(R.string.sdcard_not_mounted);
   152 		builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener(){
   152         builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener(){
   153 			public void onClick(DialogInterface dialog, int which) {
   153             public void onClick(DialogInterface dialog, int which) {
   154 				finish();				
   154                 finish();
   155 			}
   155             }
   156 		});
   156         });
   157 
   157 
   158 		return builder.create();
   158         return builder.create();
   159 	}
   159     }
   160 	
   160 
   161 	public void onAssetsDownloaded(boolean result){
   161     public void onAssetsDownloaded(boolean result){
   162 		if(!result){
   162         if(!result){
   163 			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG).show();
   163             Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG).show();
   164 		}
   164         }
   165 		assetsDialog.dismiss();
   165         assetsDialog.dismiss();
   166 	}
   166     }
   167 
   167 
   168 	private final OnClickListener startGameListener = new OnClickListener(){
   168     private final OnClickListener startGameListener = new OnClickListener(){
   169 		public void onClick(View v){
   169         public void onClick(View v){
   170 			startActivity(new Intent(getApplicationContext(), LocalRoomActivity.class));
   170             startActivity(new Intent(getApplicationContext(), LocalRoomActivity.class));
   171 		}
   171         }
   172 	};
   172     };
   173 	
   173 
   174 	private final OnClickListener startNetGameListener = new OnClickListener() {
   174     private final OnClickListener startNetGameListener = new OnClickListener() {
   175 		public void onClick(View v) {
   175         public void onClick(View v) {
   176 			State state = Netplay.getAppInstance(getApplicationContext()).getState();
   176             State state = Netplay.getAppInstance(getApplicationContext()).getState();
   177 			switch(state) {
   177             switch(state) {
   178 			case NOT_CONNECTED:
   178             case NOT_CONNECTED:
   179 		        FragmentManager fm = getSupportFragmentManager();
   179                 FragmentManager fm = getSupportFragmentManager();
   180 		        StartNetgameDialog startNetgameDialog = new StartNetgameDialog();
   180                 StartNetgameDialog startNetgameDialog = new StartNetgameDialog();
   181 		        startNetgameDialog.show(fm, "start_netgame_dialog");
   181                 startNetgameDialog.show(fm, "start_netgame_dialog");
   182 				break;
   182                 break;
   183 			case CONNECTING:
   183             case CONNECTING:
   184 				onNetConnectingStarted();
   184                 onNetConnectingStarted();
   185 				break;
   185                 break;
   186 			default:
   186             default:
   187 				startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
   187                 startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
   188 				break;
   188                 break;
   189 			}
   189             }
   190 		}
   190         }
   191 	};
   191     };
   192 	
   192 
   193 	private BroadcastReceiver connectedReceiver = new BroadcastReceiver() {
   193     private BroadcastReceiver connectedReceiver = new BroadcastReceiver() {
   194 		@Override
   194         @Override
   195 		public void onReceive(Context context, Intent intent) {
   195         public void onReceive(Context context, Intent intent) {
   196 			startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
   196             startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
   197 		}
   197         }
   198 	};
   198     };
   199 	
   199 
   200 	private BroadcastReceiver connectionFailedReceiver = new BroadcastReceiver() {
   200     private BroadcastReceiver connectionFailedReceiver = new BroadcastReceiver() {
   201 		@Override
   201         @Override
   202 		public void onReceive(Context context, Intent intent) {
   202         public void onReceive(Context context, Intent intent) {
   203 			if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) {
   203             if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) {
   204 				Toast.makeText(getApplicationContext(), intent.getStringExtra(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show();
   204                 Toast.makeText(getApplicationContext(), intent.getStringExtra(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show();
   205 			}
   205             }
   206 		}
   206         }
   207 	};
   207     };
   208 	
   208 
   209 	private BroadcastReceiver passwordRequestedReceiver = new BroadcastReceiver() {
   209     private BroadcastReceiver passwordRequestedReceiver = new BroadcastReceiver() {
   210 		@Override
   210         @Override
   211 		public void onReceive(Context context, Intent intent) {
   211         public void onReceive(Context context, Intent intent) {
   212 	        FragmentManager fm = getSupportFragmentManager();
   212             FragmentManager fm = getSupportFragmentManager();
   213 	        PasswordDialog passwordDialog = new PasswordDialog(intent.getStringExtra(Netplay.EXTRA_PLAYERNAME));
   213             PasswordDialog passwordDialog = new PasswordDialog(intent.getStringExtra(Netplay.EXTRA_PLAYERNAME));
   214 	        passwordDialog.show(fm, "fragment_password_dialog");
   214             passwordDialog.show(fm, "fragment_password_dialog");
   215 		}
   215         }
   216 	};
   216     };
   217 
   217 
   218 	public void onNetConnectingStarted() {
   218     public void onNetConnectingStarted() {
   219         FragmentManager fm = getSupportFragmentManager();
   219         FragmentManager fm = getSupportFragmentManager();
   220         ConnectingDialog connectingDialog = new ConnectingDialog();
   220         ConnectingDialog connectingDialog = new ConnectingDialog();
   221         connectingDialog.show(fm, "fragment_connecting_dialog");
   221         connectingDialog.show(fm, "fragment_connecting_dialog");
   222 	}
   222     }
   223 }
   223 }