1package com.replica.replicaisland;
2
3import java.lang.reflect.InvocationTargetException;
4import java.lang.reflect.Method;
5
6import android.app.Activity;
7import android.app.AlertDialog;
8import android.app.Dialog;
9import android.content.DialogInterface;
10import android.content.Intent;
11import android.content.SharedPreferences;
12import android.media.AudioManager;
13import android.os.Bundle;
14import android.view.KeyEvent;
15import android.view.View;
16import android.view.animation.Animation;
17import android.view.animation.AnimationUtils;
18
19public class ExtrasMenuActivity extends Activity {
20	private View mLinearModeButton;
21	private View mLevelSelectButton;
22    private View mControlsButton;
23    private View mBackground;
24    private View mLevelSelectLocked;
25    private View mLinearModeLocked;
26    private Animation mButtonFlickerAnimation;
27    private Animation mFadeOutAnimation;
28    private Animation mAlternateFadeOutAnimation;
29    private Animation mLockedAnimation;
30
31    private int mPendingGameStart;
32
33
34    public static final int NEW_GAME_DIALOG = 0;
35    public static final int EXTRAS_LOCKED_DIALOG = 1;
36
37    private static final int START_LINEAR_MODE = 0;
38    private static final int START_LEVEL_SELECT = 1;
39
40
41    private View.OnClickListener sLinearModeButtonListener = new View.OnClickListener() {
42        public void onClick(View v) {
43        	SharedPreferences prefs = getSharedPreferences(PreferenceConstants.PREFERENCE_NAME, MODE_PRIVATE);
44            final int row = prefs.getInt(PreferenceConstants.PREFERENCE_LEVEL_ROW, 0);
45            final int index = prefs.getInt(PreferenceConstants.PREFERENCE_LEVEL_INDEX, 0);
46            if (row != 0 || index != 0) {
47            	mPendingGameStart = START_LINEAR_MODE;
48            	showDialog(NEW_GAME_DIALOG);
49            } else {
50            	startGame(START_LINEAR_MODE);
51            }
52        }
53    };
54
55    private View.OnClickListener sLevelSelectButtonListener = new View.OnClickListener() {
56        public void onClick(View v) {
57        	SharedPreferences prefs = getSharedPreferences(PreferenceConstants.PREFERENCE_NAME, MODE_PRIVATE);
58            final int row = prefs.getInt(PreferenceConstants.PREFERENCE_LEVEL_ROW, 0);
59            final int index = prefs.getInt(PreferenceConstants.PREFERENCE_LEVEL_INDEX, 0);
60            if (row != 0 || index != 0) {
61            	mPendingGameStart = START_LEVEL_SELECT;
62            	showDialog(NEW_GAME_DIALOG);
63            } else {
64            	startGame(START_LEVEL_SELECT);
65            }
66        }
67    };
68
69    private View.OnClickListener sLockedSelectButtonListener = new View.OnClickListener() {
70        public void onClick(View v) {
71        	showDialog(EXTRAS_LOCKED_DIALOG);
72        }
73    };
74
75    private View.OnClickListener sControlsButtonListener = new View.OnClickListener() {
76        public void onClick(View v) {
77
78            Intent i = new Intent(getBaseContext(), SetPreferencesActivity.class);
79            i.putExtra("controlConfig", true);
80
81            v.startAnimation(mButtonFlickerAnimation);
82            mFadeOutAnimation.setAnimationListener(new StartActivityAfterAnimation(i));
83            mBackground.startAnimation(mFadeOutAnimation);
84            mLinearModeButton.startAnimation(mAlternateFadeOutAnimation);
85            mLevelSelectButton.startAnimation(mAlternateFadeOutAnimation);
86        }
87    };
88
89	 @Override
90    public void onCreate(Bundle savedInstanceState) {
91        super.onCreate(savedInstanceState);
92        setContentView(R.layout.extras_menu);
93
94        SharedPreferences prefs = getSharedPreferences(PreferenceConstants.PREFERENCE_NAME, MODE_PRIVATE);
95        final boolean extrasUnlocked = prefs.getBoolean(PreferenceConstants.PREFERENCE_EXTRAS_UNLOCKED, false);
96
97        mLinearModeButton = findViewById(R.id.linearModeButton);
98        mLevelSelectButton = findViewById(R.id.levelSelectButton);
99        mControlsButton = findViewById(R.id.controlsButton);
100        mLinearModeLocked = findViewById(R.id.linearModeLocked);
101        mLevelSelectLocked = findViewById(R.id.levelSelectLocked);
102
103        mBackground = findViewById(R.id.mainMenuBackground);
104
105        mButtonFlickerAnimation = AnimationUtils.loadAnimation(this, R.anim.button_flicker);
106        mFadeOutAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
107        mAlternateFadeOutAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
108
109
110        if (extrasUnlocked) {
111        	mLinearModeButton.setOnClickListener(sLinearModeButtonListener);
112        	mLevelSelectButton.setOnClickListener(sLevelSelectButtonListener);
113        	mLinearModeLocked.setVisibility(View.GONE);
114        	mLevelSelectLocked.setVisibility(View.GONE);
115        } else {
116            mLockedAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in_out);
117
118        	mLinearModeButton.setOnClickListener(sLockedSelectButtonListener);
119        	mLevelSelectButton.setOnClickListener(sLockedSelectButtonListener);
120        	mLinearModeLocked.startAnimation(mLockedAnimation);
121        	mLevelSelectLocked.startAnimation(mLockedAnimation);
122        }
123        mControlsButton.setOnClickListener(sControlsButtonListener);
124
125
126
127        // Keep the volume control type consistent across all activities.
128        setVolumeControlStream(AudioManager.STREAM_MUSIC);
129    }
130
131	@Override
132	public boolean onKeyDown(int keyCode, KeyEvent event) {
133		boolean result = true;
134		if (keyCode == KeyEvent.KEYCODE_BACK) {
135			finish();
136
137			if (UIConstants.mOverridePendingTransition != null) {
138 		       try {
139 		    	  UIConstants.mOverridePendingTransition.invoke(ExtrasMenuActivity.this, R.anim.activity_fade_in, R.anim.activity_fade_out);
140 		       } catch (InvocationTargetException ite) {
141 		           DebugLog.d("Activity Transition", "Invocation Target Exception");
142 		       } catch (IllegalAccessException ie) {
143 		    	   DebugLog.d("Activity Transition", "Illegal Access Exception");
144 		       }
145             }
146		} else {
147			result = super.onKeyDown(keyCode, event);
148		}
149		return result;
150	}
151
152	@Override
153    protected Dialog onCreateDialog(int id) {
154        Dialog dialog = null;
155        if (id == NEW_GAME_DIALOG) {
156
157            dialog = new AlertDialog.Builder(this)
158                .setTitle(R.string.new_game_dialog_title)
159                .setPositiveButton(R.string.new_game_dialog_ok, new DialogInterface.OnClickListener() {
160                    public void onClick(DialogInterface dialog, int whichButton) {
161                    	startGame(mPendingGameStart);
162                    }
163                })
164                .setNegativeButton(R.string.new_game_dialog_cancel, null)
165                .setMessage(R.string.new_game_dialog_message)
166                .create();
167        } else if (id == EXTRAS_LOCKED_DIALOG) {
168            dialog = new AlertDialog.Builder(this)
169                .setTitle(R.string.extras_locked_dialog_title)
170                .setPositiveButton(R.string.extras_locked_dialog_ok, null)
171                .setMessage(R.string.extras_locked_dialog_message)
172                .create();
173        }
174        return dialog;
175    }
176
177	protected void startGame(int type) {
178		 if (type == START_LINEAR_MODE) {
179            Intent i = new Intent(getBaseContext(), DifficultyMenuActivity.class);
180            i.putExtra("linearMode", true);
181            i.putExtra("newGame", true);
182            mLinearModeButton.startAnimation(mButtonFlickerAnimation);
183            mButtonFlickerAnimation.setAnimationListener(new StartActivityAfterAnimation(i));
184
185        } else if (type == START_LEVEL_SELECT) {
186            Intent i = new Intent(getBaseContext(), DifficultyMenuActivity.class);
187            i.putExtra("startAtLevelSelect", true);
188            i.putExtra("newGame", true);
189            mLevelSelectButton.startAnimation(mButtonFlickerAnimation);
190            mButtonFlickerAnimation.setAnimationListener(new StartActivityAfterAnimation(i));
191
192        }
193	}
194
195	protected class StartActivityAfterAnimation implements Animation.AnimationListener {
196	        private Intent mIntent;
197
198	        StartActivityAfterAnimation(Intent intent) {
199	            mIntent = intent;
200	        }
201
202
203	        public void onAnimationEnd(Animation animation) {
204	        	mLinearModeButton.setVisibility(View.INVISIBLE);
205	        	mLinearModeButton.clearAnimation();
206	        	mLevelSelectButton.setVisibility(View.INVISIBLE);
207	        	mLevelSelectButton.clearAnimation();
208	        	mControlsButton.setVisibility(View.INVISIBLE);
209	        	mControlsButton.clearAnimation();
210	            startActivity(mIntent);
211	            finish();
212	            if (UIConstants.mOverridePendingTransition != null) {
213	 		       try {
214	 		    	  UIConstants.mOverridePendingTransition.invoke(ExtrasMenuActivity.this, R.anim.activity_fade_in, R.anim.activity_fade_out);
215	 		       } catch (InvocationTargetException ite) {
216	 		           DebugLog.d("Activity Transition", "Invocation Target Exception");
217	 		       } catch (IllegalAccessException ie) {
218	 		    	   DebugLog.d("Activity Transition", "Illegal Access Exception");
219	 		       }
220	             }
221	        }
222
223	        public void onAnimationRepeat(Animation animation) {
224	            // TODO Auto-generated method stub
225
226	        }
227
228	        public void onAnimationStart(Animation animation) {
229	            // TODO Auto-generated method stub
230
231	        }
232
233	    }
234
235}
236