1 |
/* |
2 |
* Copyright (C) 2007 Google Inc. |
3 |
* |
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
5 |
* use this file except in compliance with the License. You may obtain a copy of |
6 |
* 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, WITHOUT |
12 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 |
* License for the specific language governing permissions and limitations under |
14 |
* the License. |
15 |
*/ |
16 |
|
17 |
package com.example.admob.lunarlander; |
18 |
|
19 |
import android.app.Activity; |
20 |
import android.os.Bundle; |
21 |
import android.util.Log; |
22 |
import android.view.Menu; |
23 |
import android.view.MenuItem; |
24 |
import android.view.Window; |
25 |
import android.widget.TextView; |
26 |
import com.admob.android.ads.*; |
27 |
import com.example.admob.lunarlander.LunarView.*; |
28 |
|
29 |
/** |
30 |
* This is a simple LunarLander activity that houses a single LunarView. It |
31 |
* demonstrates... |
32 |
* <ul> |
33 |
* <li>animating by calling invalidate() from draw() |
34 |
* <li>loading and drawing resources |
35 |
* <li>handling onPause() in an animation |
36 |
* </ul> |
37 |
*/ |
38 |
public class LunarLander extends Activity { |
39 |
private static final int MENU_EASY = 1; |
40 |
|
41 |
private static final int MENU_HARD = 2; |
42 |
|
43 |
private static final int MENU_MEDIUM = 3; |
44 |
|
45 |
private static final int MENU_PAUSE = 4; |
46 |
|
47 |
private static final int MENU_RESUME = 5; |
48 |
|
49 |
private static final int MENU_START = 6; |
50 |
|
51 |
private static final int MENU_STOP = 7; |
52 |
|
53 |
// TODO Sensors don't work in the emulator. Every comment flush left below needs to be undone. |
54 |
// private SensorManager mSensorManager; |
55 |
|
56 |
/** A handle to the thread that's actually running the animation. */ |
57 |
private LunarThread mLunarThread; |
58 |
|
59 |
/** A handle to the View in which the game is running. */ |
60 |
private LunarView mLunarView; |
61 |
|
62 |
/** |
63 |
* Invoked during init to give the Activity a chance to set up its Menu. |
64 |
* |
65 |
* @param menu the Menu to which entries may be added |
66 |
* @return true |
67 |
*/ |
68 |
@Override |
69 |
public boolean onCreateOptionsMenu(Menu menu) { |
70 |
super.onCreateOptionsMenu(menu); |
71 |
|
72 |
menu.add(0, MENU_START, 0, R.string.menu_start); |
73 |
menu.add(0, MENU_STOP, 0, R.string.menu_stop); |
74 |
menu.add(0, MENU_PAUSE, 0, R.string.menu_pause); |
75 |
menu.add(0, MENU_RESUME, 0, R.string.menu_resume); |
76 |
menu.add(0, MENU_EASY, 0, R.string.menu_easy); |
77 |
menu.add(0, MENU_MEDIUM, 0, R.string.menu_medium); |
78 |
menu.add(0, MENU_HARD, 0, R.string.menu_hard); |
79 |
|
80 |
return true; |
81 |
} |
82 |
|
83 |
/** |
84 |
* Invoked when the user selects an item from the Menu. |
85 |
* |
86 |
* @param item the Menu entry which was selected |
87 |
* @return true if the Menu item was legit (and we consumed it), false |
88 |
* otherwise |
89 |
*/ |
90 |
@Override |
91 |
public boolean onOptionsItemSelected(MenuItem item) { |
92 |
switch (item.getItemId()) { |
93 |
case MENU_START: |
94 |
mLunarThread.doStart(); |
95 |
return true; |
96 |
case MENU_STOP: |
97 |
mLunarThread.setState(LunarThread.STATE_LOSE, |
98 |
getText(R.string.message_stopped)); |
99 |
return true; |
100 |
case MENU_PAUSE: |
101 |
mLunarThread.pause(); |
102 |
return true; |
103 |
case MENU_RESUME: |
104 |
mLunarThread.unpause(); |
105 |
return true; |
106 |
case MENU_EASY: |
107 |
mLunarThread.setDifficulty(LunarThread.DIFFICULTY_EASY); |
108 |
return true; |
109 |
case MENU_MEDIUM: |
110 |
mLunarThread.setDifficulty(LunarThread.DIFFICULTY_MEDIUM); |
111 |
return true; |
112 |
case MENU_HARD: |
113 |
mLunarThread.setDifficulty(LunarThread.DIFFICULTY_HARD); |
114 |
return true; |
115 |
} |
116 |
|
117 |
return false; |
118 |
} |
119 |
|
120 |
/** |
121 |
* Invoked when the Activity is created. |
122 |
* |
123 |
* @param savedInstanceState a Bundle containing state saved from a previous |
124 |
* execution, or null if this is a new execution |
125 |
*/ |
126 |
@Override |
127 |
protected void onCreate(Bundle savedInstanceState) { |
128 |
super.onCreate(savedInstanceState); |
129 |
|
130 |
// turn off the window's title bar |
131 |
requestWindowFeature(Window.FEATURE_NO_TITLE); |
132 |
|
133 |
// Use the phone's orientation to control the rocket ship. |
134 |
// mSensorManager = (SensorManager) getSystemService( SENSOR_SERVICE ); |
135 |
|
136 |
// tell system to use the layout defined in our XML file |
137 |
setContentView(R.layout.lunar_layout); |
138 |
|
139 |
// get handles to the LunarView from XML, and its LunarThread |
140 |
mLunarView = (LunarView) findViewById(R.id.lunar); |
141 |
mLunarThread = mLunarView.getThread(); |
142 |
|
143 |
// give the LunarView a handle to the TextView used for messages |
144 |
TextView text = (TextView) findViewById(R.id.text); |
145 |
AdView ad = (AdView) findViewById(R.id.ad); |
146 |
mLunarView.setTextView( text, ad ); |
147 |
|
148 |
if (savedInstanceState == null) { |
149 |
// we were just launched: set up a new game |
150 |
mLunarThread.setState(LunarThread.STATE_READY); |
151 |
Log.w(this.getClass().getName(), "SIS is null"); |
152 |
} else { |
153 |
// we are being restored: resume a previous game |
154 |
mLunarThread.restoreState(savedInstanceState); |
155 |
Log.w(this.getClass().getName(), "SIS is nonnull"); |
156 |
} |
157 |
} |
158 |
|
159 |
/** |
160 |
* Invoked when the Activity loses user focus. |
161 |
*/ |
162 |
@Override |
163 |
protected void onPause() { |
164 |
super.onPause(); |
165 |
// mSensorManager.unregisterListener( mLunarView ); |
166 |
mLunarView.getThread().pause(); // pause game when Activity pauses |
167 |
} |
168 |
|
169 |
/** |
170 |
* Invoked when the Activity gains focus. |
171 |
*/ |
172 |
@Override |
173 |
protected void onResume() { |
174 |
super.onResume(); |
175 |
// Sensor orientation = mSensorManager.getSensorList( Sensor.TYPE_ORIENTATION ).get( 0 ); |
176 |
// mSensorManager.registerListener( |
177 |
// mLunarView, |
178 |
// orientation, |
179 |
// SensorManager.SENSOR_DELAY_FASTEST); |
180 |
} |
181 |
|
182 |
/** |
183 |
* Notification that something is about to happen, to give the Activity a |
184 |
* chance to save state. |
185 |
* |
186 |
* @param outState a Bundle into which this Activity should save its state |
187 |
*/ |
188 |
@Override |
189 |
protected void onSaveInstanceState(Bundle outState) { |
190 |
// just have the View's thread save its state into our Bundle |
191 |
super.onSaveInstanceState(outState); |
192 |
mLunarThread.saveState(outState); |
193 |
Log.w(this.getClass().getName(), "SIS called"); |
194 |
} |
195 |
} |