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
17
18package com.replica.replicaisland;
19
20import java.lang.reflect.InvocationTargetException;
21
22import android.app.Activity;
23import android.content.Intent;
24import android.graphics.drawable.AnimationDrawable;
25import android.os.Bundle;
26import android.os.Handler;
27import android.os.Message;
28import android.util.DisplayMetrics;
29import android.view.MotionEvent;
30import android.view.View;
31import android.view.animation.Animation;
32import android.view.animation.AnimationUtils;
33import android.view.animation.TranslateAnimation;
34import android.widget.ImageView;
35
36
37public class AnimationPlayerActivity extends Activity {
38	public static final int KYLE_DEATH = 0;
39	public static final int WANDA_ENDING = 1;
40	public static final int KABOCHA_ENDING = 2;
41	public static final int ROKUDOU_ENDING = 3;
42
43	private AnimationDrawable mAnimation;
44	private int mAnimationType;
45	private long mAnimationEndTime;
46
47	private KillActivityHandler mKillActivityHandler = new KillActivityHandler();
48
49    class KillActivityHandler extends Handler {
50
51        @Override
52        public void handleMessage(Message msg) {
53        	AnimationPlayerActivity.this.finish();
54        	if (UIConstants.mOverridePendingTransition != null) {
55  		       try {
56  		    	  UIConstants.mOverridePendingTransition.invoke(AnimationPlayerActivity.this, R.anim.activity_fade_in, R.anim.activity_fade_out);
57  		       } catch (InvocationTargetException ite) {
58  		           DebugLog.d("Activity Transition", "Invocation Target Exception");
59  		       } catch (IllegalAccessException ie) {
60  		    	   DebugLog.d("Activity Transition", "Illegal Access Exception");
61  		       }
62        	}
63        }
64
65        public void sleep(long delayMillis) {
66        	this.removeMessages(0);
67            sendMessageDelayed(obtainMessage(0), delayMillis);
68        }
69    };
70
71	@Override
72	public void onCreate(Bundle savedInstanceState) {
73	  super.onCreate(savedInstanceState);
74
75	  final Intent callingIntent = getIntent();
76	  mAnimationType = callingIntent.getIntExtra("animation", KYLE_DEATH);
77
78	  if (mAnimationType == KYLE_DEATH) {
79		  setContentView(R.layout.animation_player);
80
81		  ImageView canvasImage = (ImageView) findViewById(R.id.animation_canvas);
82		  canvasImage.setImageResource(R.anim.kyle_fall);
83		  mAnimation = (AnimationDrawable) canvasImage.getDrawable();
84	  } else {
85
86
87		  if (mAnimationType == WANDA_ENDING || mAnimationType == KABOCHA_ENDING) {
88			  float startX = 0.0f;
89			  DisplayMetrics metrics = new DisplayMetrics();
90			  getWindowManager().getDefaultDisplay().getMetrics(metrics);
91			  if (mAnimationType == WANDA_ENDING) {
92				  setContentView(R.layout.good_ending_animation);
93				  startX = 200 * metrics.density;
94
95			  } else {
96				  setContentView(R.layout.kabocha_ending_animation);
97				  startX = -200 * metrics.density;
98			  }
99
100			  // HACK
101			  // the TranslateAnimation system doesn't support device independent pixels.
102			  // So for the Wanda ending and Kabocha endings, in which the game over text
103			  // scrolls in horizontally, compute the size based on the actual density of
104			  // the display and just generate the anim in code.  The Rokudou animation
105			  // can be safely loaded from a file.
106			  Animation gameOverAnim = new TranslateAnimation(startX, 0, 0, 0);
107			  gameOverAnim.setDuration(6000);
108			  gameOverAnim.setFillAfter(true);
109			  gameOverAnim.setFillEnabled(true);
110			  gameOverAnim.setStartOffset(8000);
111
112			  View background = findViewById(R.id.animation_background);
113			  View foreground = findViewById(R.id.animation_foreground);
114			  View gameOver = findViewById(R.id.game_over);
115
116			  Animation foregroundAnim = AnimationUtils.loadAnimation(this, R.anim.horizontal_layer2_slide);
117			  Animation backgroundAnim = AnimationUtils.loadAnimation(this, R.anim.horizontal_layer1_slide);
118
119			  background.startAnimation(backgroundAnim);
120			  foreground.startAnimation(foregroundAnim);
121			  gameOver.startAnimation(gameOverAnim);
122
123			  mAnimationEndTime = gameOverAnim.getDuration() + System.currentTimeMillis();
124		  } else if (mAnimationType == ROKUDOU_ENDING) {
125			  setContentView(R.layout.rokudou_ending_animation);
126			  View background = findViewById(R.id.animation_background);
127			  View sphere = findViewById(R.id.animation_sphere);
128			  View cliffs = findViewById(R.id.animation_cliffs);
129			  View rokudou = findViewById(R.id.animation_rokudou);
130			  View gameOver = findViewById(R.id.game_over);
131
132
133			  Animation backgroundAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_bg);
134			  Animation sphereAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_sphere);
135			  Animation cliffsAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_cliffs);
136			  Animation rokudouAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_rokudou);
137			  Animation gameOverAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_game_over);
138
139			  background.startAnimation(backgroundAnim);
140			  sphere.startAnimation(sphereAnim);
141			  cliffs.startAnimation(cliffsAnim);
142			  rokudou.startAnimation(rokudouAnim);
143			  gameOver.startAnimation(gameOverAnim);
144			  mAnimationEndTime = gameOverAnim.getDuration() + System.currentTimeMillis();
145		  } else {
146			  assert false;
147		  }
148
149	  }
150
151	  // Pass the calling intent back so that we can figure out which animation just played.
152	  setResult(RESULT_OK, callingIntent);
153
154	}
155
156	@Override
157	public boolean onTouchEvent(MotionEvent event) {
158		long time = System.currentTimeMillis();
159		if (time > mAnimationEndTime) {
160			finish();
161		} else {
162			try {
163				Thread.sleep(32);
164			} catch (InterruptedException e) {
165				// Safe to ignore.
166			}
167		}
168		return true;
169	}
170
171	@Override
172	public void onWindowFocusChanged(boolean hasFocus) {
173	  if (hasFocus && mAnimation != null) {
174		  mAnimation.start();
175		  mKillActivityHandler.sleep(mAnimation.getDuration(0) * mAnimation.getNumberOfFrames());
176	  }
177	}
178
179}
180