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
19import java.lang.reflect.InvocationTargetException;
20
21import android.app.Activity;
22import android.content.Context;
23import android.content.SharedPreferences;
24import android.graphics.Canvas;
25import android.os.Bundle;
26import android.os.SystemClock;
27import android.util.AttributeSet;
28import android.view.View;
29import android.widget.Button;
30import android.widget.TextView;
31
32public class GameOverActivity extends Activity {
33	private float mPearlPercent = 100.0f;
34	private float mEnemiesDestroyedPercent = 100.0f;
35	private float mPlayTime = 0.0f;
36	private int mEnding = AnimationPlayerActivity.KABOCHA_ENDING;
37
38	private IncrementingTextView mPearlView;
39	private IncrementingTextView mEnemiesDestroyedView;
40	private IncrementingTextView mPlayTimeView;
41	private TextView mEndingView;
42
43    public static class IncrementingTextView extends TextView {
44    	private static final int INCREMENT_DELAY_MS = 2 * 1000;
45    	private static final int MODE_NONE = 0;
46    	private static final int MODE_PERCENT = 1;
47    	private static final int MODE_TIME = 2;
48
49    	private float mTargetValue;
50    	private float mIncrement = 1.0f;
51    	private float mCurrentValue = 0.0f;
52    	private long mLastTime = 0;
53    	private int mMode = MODE_NONE;
54
55    	public IncrementingTextView(Context context) {
56            super(context);
57        }
58
59        public IncrementingTextView(Context context, AttributeSet attrs) {
60            super(context, attrs);
61        }
62
63        public IncrementingTextView(Context context, AttributeSet attrs, int defStyle) {
64            super(context, attrs, defStyle);
65        }
66
67        public void setTargetValue(float target) {
68        	mTargetValue = target;
69        	postInvalidate();
70        }
71
72        public void setMode(int mode) {
73        	mMode = mode;
74        }
75
76        public void setIncrement(float increment) {
77        	mIncrement = increment;
78        }
79
80        @Override
81        public void onDraw(Canvas canvas) {
82            final long time = SystemClock.uptimeMillis();
83            final long delta = time - mLastTime;
84            if (delta > INCREMENT_DELAY_MS) {
85            	if (mCurrentValue < mTargetValue) {
86            		mCurrentValue += mIncrement;
87            		mCurrentValue = Math.min(mCurrentValue, mTargetValue);
88            		String value;
89            		if (mMode == MODE_PERCENT) {
90            			value = mCurrentValue + "%";
91            		} else if (mMode == MODE_TIME) {
92            			float seconds = mCurrentValue;
93            			float minutes = seconds / 60.0f;
94            			float hours = minutes / 60.0f;
95
96            			int totalHours = (int)Math.floor(hours);
97            			float totalHourMinutes = totalHours * 60.0f;
98            			int totalMinutes = (int)(minutes - totalHourMinutes);
99            			float totalMinuteSeconds = totalMinutes * 60.0f;
100            			float totalHourSeconds = totalHourMinutes * 60.0f;
101            			int totalSeconds = (int)(seconds - (totalMinuteSeconds + totalHourSeconds));
102
103            			value = totalHours + ":" + totalMinutes + ":" + totalSeconds;
104            		} else {
105            			value = mCurrentValue + "";
106            		}
107            		setText(value);
108                    postInvalidateDelayed(INCREMENT_DELAY_MS);
109            	}
110            }
111            super.onDraw(canvas);
112        }
113    }
114
115    private View.OnClickListener sOKClickListener = new View.OnClickListener() {
116        public void onClick(View v) {
117        	finish();
118        	if (UIConstants.mOverridePendingTransition != null) {
119   		       try {
120   		    	  UIConstants.mOverridePendingTransition.invoke(GameOverActivity.this, R.anim.activity_fade_in, R.anim.activity_fade_out);
121   		       } catch (InvocationTargetException ite) {
122   		           DebugLog.d("Activity Transition", "Invocation Target Exception");
123   		       } catch (IllegalAccessException ie) {
124   		    	   DebugLog.d("Activity Transition", "Illegal Access Exception");
125   		       }
126         	}
127        }
128    };
129
130	@Override
131    public void onCreate(Bundle savedInstanceState) {
132        super.onCreate(savedInstanceState);
133        setContentView(R.layout.game_over);
134
135        mPearlView = (IncrementingTextView)findViewById(R.id.pearl_percent);
136        mEnemiesDestroyedView = (IncrementingTextView)findViewById(R.id.enemy_percent);
137        mPlayTimeView = (IncrementingTextView)findViewById(R.id.total_play_time);
138        mEndingView = (TextView)findViewById(R.id.ending);
139
140        SharedPreferences prefs = getSharedPreferences(PreferenceConstants.PREFERENCE_NAME, MODE_PRIVATE);
141        final float playTime = prefs.getFloat(PreferenceConstants.PREFERENCE_TOTAL_GAME_TIME, 0.0f);
142        final int ending = prefs.getInt(PreferenceConstants.PREFERENCE_LAST_ENDING, -1);
143        final int pearlsCollected = prefs.getInt(PreferenceConstants.PREFERENCE_PEARLS_COLLECTED, 0);
144        final int pearlsTotal = prefs.getInt(PreferenceConstants.PREFERENCE_PEARLS_TOTAL, 0);
145
146        final int enemies = prefs.getInt(PreferenceConstants.PREFERENCE_ROBOTS_DESTROYED, 0);
147
148        if (pearlsCollected > 0 && pearlsTotal > 0) {
149        	mPearlView.setTargetValue((int)((pearlsCollected / (float)pearlsTotal) * 100.0f));
150        } else {
151        	mPearlView.setText("--");
152        }
153        mPearlView.setMode(IncrementingTextView.MODE_PERCENT);
154        mEnemiesDestroyedView.setTargetValue(enemies);
155        mPlayTimeView.setTargetValue(playTime);
156        mPlayTimeView.setIncrement(90.0f);
157        mPlayTimeView.setMode(IncrementingTextView.MODE_TIME);
158
159        if (ending == AnimationPlayerActivity.KABOCHA_ENDING) {
160        	mEndingView.setText(R.string.game_results_kabocha_ending);
161        } else if (ending == AnimationPlayerActivity.ROKUDOU_ENDING) {
162        	mEndingView.setText(R.string.game_results_rokudou_ending);
163        } else {
164        	mEndingView.setText(R.string.game_results_wanda_ending);
165        }
166
167        Button okButton = (Button)findViewById(R.id.ok);
168        okButton.setOnClickListener(sOKClickListener);
169
170	}
171
172
173}
174