1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.replica.replicaisland; 18 19 20import java.lang.reflect.InvocationTargetException; 21import java.lang.reflect.Method; 22 23import android.app.Activity; 24import android.app.AlertDialog; 25import android.app.Dialog; 26import android.content.DialogInterface; 27import android.content.Intent; 28import android.content.SharedPreferences; 29import android.content.pm.PackageManager; 30import android.media.AudioManager; 31import android.os.Build; 32import android.os.Bundle; 33import android.text.Html; 34import android.view.View; 35import android.view.animation.Animation; 36import android.view.animation.AnimationUtils; 37import android.widget.ImageView; 38 39public class MainMenuActivity extends Activity { 40 private boolean mPaused; 41 private View mStartButton; 42 private View mOptionsButton; 43 private View mExtrasButton; 44 private View mBackground; 45 private View mTicker; 46 private Animation mButtonFlickerAnimation; 47 private Animation mFadeOutAnimation; 48 private Animation mAlternateFadeOutAnimation; 49 private Animation mFadeInAnimation; 50 private boolean mJustCreated; 51 private String mSelectedControlsString; 52 53 54 private final static int WHATS_NEW_DIALOG = 0; 55 private final static int TILT_TO_SCREEN_CONTROLS_DIALOG = 1; 56 private final static int CONTROL_SETUP_DIALOG = 2; 57 58 // Create an anonymous implementation of OnClickListener 59 private View.OnClickListener sContinueButtonListener = new View.OnClickListener() { 60 public void onClick(View v) { 61 if (!mPaused) { 62 Intent i = new Intent(getBaseContext(), AndouKun.class); 63 v.startAnimation(mButtonFlickerAnimation); 64 mFadeOutAnimation.setAnimationListener(new StartActivityAfterAnimation(i)); 65 mBackground.startAnimation(mFadeOutAnimation); 66 mOptionsButton.startAnimation(mAlternateFadeOutAnimation); 67 mExtrasButton.startAnimation(mAlternateFadeOutAnimation); 68 mTicker.startAnimation(mAlternateFadeOutAnimation); 69 mPaused = true; 70 } 71 } 72 }; 73 74 private View.OnClickListener sOptionButtonListener = new View.OnClickListener() { 75 public void onClick(View v) { 76 if (!mPaused) { 77 Intent i = new Intent(getBaseContext(), SetPreferencesActivity.class); 78 79 v.startAnimation(mButtonFlickerAnimation); 80 mFadeOutAnimation.setAnimationListener(new StartActivityAfterAnimation(i)); 81 mBackground.startAnimation(mFadeOutAnimation); 82 mStartButton.startAnimation(mAlternateFadeOutAnimation); 83 mExtrasButton.startAnimation(mAlternateFadeOutAnimation); 84 mTicker.startAnimation(mAlternateFadeOutAnimation); 85 mPaused = true; 86 } 87 } 88 }; 89 90 private View.OnClickListener sExtrasButtonListener = new View.OnClickListener() { 91 public void onClick(View v) { 92 if (!mPaused) { 93 Intent i = new Intent(getBaseContext(), ExtrasMenuActivity.class); 94 95 v.startAnimation(mButtonFlickerAnimation); 96 mButtonFlickerAnimation.setAnimationListener(new StartActivityAfterAnimation(i)); 97 mPaused = true; 98 99 } 100 } 101 }; 102 103 private View.OnClickListener sStartButtonListener = new View.OnClickListener() { 104 public void onClick(View v) { 105 if (!mPaused) { 106 Intent i = new Intent(getBaseContext(), DifficultyMenuActivity.class); 107 i.putExtra("newGame", true); 108 v.startAnimation(mButtonFlickerAnimation); 109 mButtonFlickerAnimation.setAnimationListener(new StartActivityAfterAnimation(i)); 110 111 mPaused = true; 112 113 } 114 } 115 }; 116 117 @Override 118 public void onCreate(Bundle savedInstanceState) { 119 super.onCreate(savedInstanceState); 120 setContentView(R.layout.mainmenu); 121 mPaused = true; 122 123 mStartButton = findViewById(R.id.startButton); 124 mOptionsButton = findViewById(R.id.optionButton); 125 mBackground = findViewById(R.id.mainMenuBackground); 126 127 if (mOptionsButton != null) { 128 mOptionsButton.setOnClickListener(sOptionButtonListener); 129 } 130 131 mExtrasButton = findViewById(R.id.extrasButton); 132 mExtrasButton.setOnClickListener(sExtrasButtonListener); 133 134 mButtonFlickerAnimation = AnimationUtils.loadAnimation(this, R.anim.button_flicker); 135 mFadeOutAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out); 136 mAlternateFadeOutAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out); 137 mFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in); 138 139 SharedPreferences prefs = getSharedPreferences(PreferenceConstants.PREFERENCE_NAME, MODE_PRIVATE); 140 final int row = prefs.getInt(PreferenceConstants.PREFERENCE_LEVEL_ROW, 0); 141 final int index = prefs.getInt(PreferenceConstants.PREFERENCE_LEVEL_INDEX, 0); 142 int levelTreeResource = R.xml.level_tree; 143 if (row != 0 || index != 0) { 144 final int linear = prefs.getInt(PreferenceConstants.PREFERENCE_LINEAR_MODE, 0); 145 if (linear != 0) { 146 levelTreeResource = R.xml.linear_level_tree; 147 } 148 } 149 150 if (!LevelTree.isLoaded(levelTreeResource)) { 151 LevelTree.loadLevelTree(levelTreeResource, this); 152 LevelTree.loadAllDialog(this); 153 } 154 155 mTicker = findViewById(R.id.ticker); 156 if (mTicker != null) { 157 mTicker.setFocusable(true); 158 mTicker.requestFocus(); 159 mTicker.setSelected(true); 160 } 161 162 mJustCreated = true; 163 164 // Keep the volume control type consistent across all activities. 165 setVolumeControlStream(AudioManager.STREAM_MUSIC); 166 167 //MediaPlayer mp = MediaPlayer.create(this, R.raw.bwv_115); 168 //mp.start(); 169 170 171 } 172 173 174 @Override 175 protected void onPause() { 176 super.onPause(); 177 mPaused = true; 178 } 179 180 @Override 181 protected void onResume() { 182 super.onResume(); 183 mPaused = false; 184 185 mButtonFlickerAnimation.setAnimationListener(null); 186 187 if (mStartButton != null) { 188 189 // Change "start" to "continue" if there's a saved game. 190 SharedPreferences prefs = getSharedPreferences(PreferenceConstants.PREFERENCE_NAME, MODE_PRIVATE); 191 final int row = prefs.getInt(PreferenceConstants.PREFERENCE_LEVEL_ROW, 0); 192 final int index = prefs.getInt(PreferenceConstants.PREFERENCE_LEVEL_INDEX, 0); 193 if (row != 0 || index != 0) { 194 ((ImageView)mStartButton).setImageDrawable(getResources().getDrawable(R.drawable.ui_button_continue)); 195 mStartButton.setOnClickListener(sContinueButtonListener); 196 } else { 197 ((ImageView)mStartButton).setImageDrawable(getResources().getDrawable(R.drawable.ui_button_start)); 198 mStartButton.setOnClickListener(sStartButtonListener); 199 } 200 201 TouchFilter touch; 202 final int sdkVersion = Integer.parseInt(Build.VERSION.SDK); 203 if (sdkVersion < Build.VERSION_CODES.ECLAIR) { 204 touch = new SingleTouchFilter(); 205 } else { 206 touch = new MultiTouchFilter(); 207 } 208 209 final int lastVersion = prefs.getInt(PreferenceConstants.PREFERENCE_LAST_VERSION, 0); 210 if (lastVersion == 0) { 211 // This is the first time the game has been run. 212 // Pre-configure the control options to match the device. 213 // The resource system can tell us what this device has. 214 // TODO: is there a better way to do this? Seems like a kind of neat 215 // way to do custom device profiles. 216 final String navType = getString(R.string.nav_type); 217 mSelectedControlsString = getString(R.string.control_setup_dialog_trackball); 218 if (navType != null) { 219 if (navType.equalsIgnoreCase("DPad")) { 220 // Turn off the click-to-attack pref on devices that have a dpad. 221 SharedPreferences.Editor editor = prefs.edit(); 222 editor.putBoolean(PreferenceConstants.PREFERENCE_CLICK_ATTACK, false); 223 editor.commit(); 224 mSelectedControlsString = getString(R.string.control_setup_dialog_dpad); 225 } else if (navType.equalsIgnoreCase("None")) { 226 SharedPreferences.Editor editor = prefs.edit(); 227 228 // This test relies on the PackageManager if api version >= 5. 229 if (touch.supportsMultitouch(this)) { 230 // Default to screen controls. 231 editor.putBoolean(PreferenceConstants.PREFERENCE_SCREEN_CONTROLS, true); 232 mSelectedControlsString = getString(R.string.control_setup_dialog_screen); 233 } else { 234 // Turn on tilt controls if there's nothing else. 235 editor.putBoolean(PreferenceConstants.PREFERENCE_TILT_CONTROLS, true); 236 mSelectedControlsString = getString(R.string.control_setup_dialog_tilt); 237 } 238 editor.commit(); 239 240 } 241 } 242 243 } 244 245 if (Math.abs(lastVersion) < Math.abs(AndouKun.VERSION)) { 246 // This is a new install or an upgrade. 247 248 // Check the safe mode option. 249 // Useful reference: http://en.wikipedia.org/wiki/List_of_Android_devices 250 if (Build.PRODUCT.contains("morrison") || // Motorola Cliq/Dext 251 Build.MODEL.contains("Pulse") || // Huawei Pulse 252 Build.MODEL.contains("U8220") || // Huawei Pulse 253 Build.MODEL.contains("U8230") || // Huawei U8230 254 Build.MODEL.contains("MB300") || // Motorola Backflip 255 Build.MODEL.contains("MB501") || // Motorola Quench / Cliq XT 256 Build.MODEL.contains("Behold+II")) { // Samsung Behold II 257 // These are all models that users have complained about. They likely use 258 // the same buggy QTC graphics driver. Turn on Safe Mode by default 259 // for these devices. 260 SharedPreferences.Editor editor = prefs.edit(); 261 editor.putBoolean(PreferenceConstants.PREFERENCE_SAFE_MODE, true); 262 editor.commit(); 263 } 264 265 SharedPreferences.Editor editor = prefs.edit(); 266 267 if (lastVersion > 0 && lastVersion < 14) { 268 // if the user has beat the game once, go ahead and unlock stuff for them. 269 if (prefs.getInt(PreferenceConstants.PREFERENCE_LAST_ENDING, -1) != -1) { 270 editor.putBoolean(PreferenceConstants.PREFERENCE_EXTRAS_UNLOCKED, true); 271 } 272 } 273 274 // show what's new message 275 editor.putInt(PreferenceConstants.PREFERENCE_LAST_VERSION, AndouKun.VERSION); 276 editor.commit(); 277 278 showDialog(WHATS_NEW_DIALOG); 279 280 // screen controls were added in version 14 281 if (lastVersion > 0 && lastVersion < 14 && 282 prefs.getBoolean(PreferenceConstants.PREFERENCE_TILT_CONTROLS, false)) { 283 if (touch.supportsMultitouch(this)) { 284 // show message about switching from tilt to screen controls 285 showDialog(TILT_TO_SCREEN_CONTROLS_DIALOG); 286 } 287 } else if (lastVersion == 0) { 288 // show message about auto-selected control schemes. 289 showDialog(CONTROL_SETUP_DIALOG); 290 } 291 292 } 293 294 } 295 296 297 if (mBackground != null) { 298 mBackground.clearAnimation(); 299 } 300 301 if (mTicker != null) { 302 mTicker.clearAnimation(); 303 mTicker.setAnimation(mFadeInAnimation); 304 } 305 306 if (mJustCreated) { 307 if (mStartButton != null) { 308 mStartButton.startAnimation(AnimationUtils.loadAnimation(this, R.anim.button_slide)); 309 } 310 if (mExtrasButton != null) { 311 Animation anim = AnimationUtils.loadAnimation(this, R.anim.button_slide); 312 anim.setStartOffset(500L); 313 mExtrasButton.startAnimation(anim); 314 } 315 316 if (mOptionsButton != null) { 317 Animation anim = AnimationUtils.loadAnimation(this, R.anim.button_slide); 318 anim.setStartOffset(1000L); 319 mOptionsButton.startAnimation(anim); 320 } 321 mJustCreated = false; 322 323 } else { 324 mStartButton.clearAnimation(); 325 mOptionsButton.clearAnimation(); 326 mExtrasButton.clearAnimation(); 327 } 328 329 330 } 331 332 @Override 333 protected Dialog onCreateDialog(int id) { 334 Dialog dialog; 335 if (id == WHATS_NEW_DIALOG) { 336 dialog = new AlertDialog.Builder(this) 337 .setTitle(R.string.whats_new_dialog_title) 338 .setPositiveButton(R.string.whats_new_dialog_ok, null) 339 .setMessage(R.string.whats_new_dialog_message) 340 .create(); 341 } else if (id == TILT_TO_SCREEN_CONTROLS_DIALOG) { 342 dialog = new AlertDialog.Builder(this) 343 .setTitle(R.string.onscreen_tilt_dialog_title) 344 .setPositiveButton(R.string.onscreen_tilt_dialog_ok, new DialogInterface.OnClickListener() { 345 public void onClick(DialogInterface dialog, int whichButton) { 346 SharedPreferences prefs = getSharedPreferences(PreferenceConstants.PREFERENCE_NAME, MODE_PRIVATE); 347 SharedPreferences.Editor editor = prefs.edit(); 348 editor.putBoolean(PreferenceConstants.PREFERENCE_SCREEN_CONTROLS, true); 349 editor.commit(); 350 } 351 }) 352 .setNegativeButton(R.string.onscreen_tilt_dialog_cancel, null) 353 .setMessage(R.string.onscreen_tilt_dialog_message) 354 .create(); 355 } else if (id == CONTROL_SETUP_DIALOG) { 356 String messageFormat = getResources().getString(R.string.control_setup_dialog_message); 357 String message = String.format(messageFormat, mSelectedControlsString); 358 CharSequence sytledMessage = Html.fromHtml(message); // lame. 359 dialog = new AlertDialog.Builder(this) 360 .setTitle(R.string.control_setup_dialog_title) 361 .setPositiveButton(R.string.control_setup_dialog_ok, null) 362 .setNegativeButton(R.string.control_setup_dialog_change, new DialogInterface.OnClickListener() { 363 public void onClick(DialogInterface dialog, int whichButton) { 364 Intent i = new Intent(getBaseContext(), SetPreferencesActivity.class); 365 i.putExtra("controlConfig", true); 366 startActivity(i); 367 } 368 }) 369 .setMessage(sytledMessage) 370 .create(); 371 } else { 372 dialog = super.onCreateDialog(id); 373 } 374 return dialog; 375 } 376 377 protected class StartActivityAfterAnimation implements Animation.AnimationListener { 378 private Intent mIntent; 379 380 StartActivityAfterAnimation(Intent intent) { 381 mIntent = intent; 382 } 383 384 385 public void onAnimationEnd(Animation animation) { 386 387 startActivity(mIntent); 388 389 if (UIConstants.mOverridePendingTransition != null) { 390 try { 391 UIConstants.mOverridePendingTransition.invoke(MainMenuActivity.this, R.anim.activity_fade_in, R.anim.activity_fade_out); 392 } catch (InvocationTargetException ite) { 393 DebugLog.d("Activity Transition", "Invocation Target Exception"); 394 } catch (IllegalAccessException ie) { 395 DebugLog.d("Activity Transition", "Illegal Access Exception"); 396 } 397 } 398 } 399 400 public void onAnimationRepeat(Animation animation) { 401 // TODO Auto-generated method stub 402 403 } 404 405 public void onAnimationStart(Animation animation) { 406 // TODO Auto-generated method stub 407 408 } 409 410 } 411} 412