1 /* 2 * Copyright (C) 2007 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 package com.example.android.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 27 import com.example.android.lunarlander.LunarView.LunarThread; 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 /** A handle to the thread that's actually running the animation. */ 54 private LunarThread mLunarThread; 55 56 /** A handle to the View in which the game is running. */ 57 private LunarView mLunarView; 58 59 /** 60 * Invoked during init to give the Activity a chance to set up its Menu. 61 * 62 * @param menu the Menu to which entries may be added 63 * @return true 64 */ 65 @Override onCreateOptionsMenu(Menu menu)66 public boolean onCreateOptionsMenu(Menu menu) { 67 super.onCreateOptionsMenu(menu); 68 69 menu.add(0, MENU_START, 0, R.string.menu_start); 70 menu.add(0, MENU_STOP, 0, R.string.menu_stop); 71 menu.add(0, MENU_PAUSE, 0, R.string.menu_pause); 72 menu.add(0, MENU_RESUME, 0, R.string.menu_resume); 73 menu.add(0, MENU_EASY, 0, R.string.menu_easy); 74 menu.add(0, MENU_MEDIUM, 0, R.string.menu_medium); 75 menu.add(0, MENU_HARD, 0, R.string.menu_hard); 76 77 return true; 78 } 79 80 /** 81 * Invoked when the user selects an item from the Menu. 82 * 83 * @param item the Menu entry which was selected 84 * @return true if the Menu item was legit (and we consumed it), false 85 * otherwise 86 */ 87 @Override onOptionsItemSelected(MenuItem item)88 public boolean onOptionsItemSelected(MenuItem item) { 89 switch (item.getItemId()) { 90 case MENU_START: 91 mLunarThread.doStart(); 92 return true; 93 case MENU_STOP: 94 mLunarThread.setState(LunarThread.STATE_LOSE, 95 getText(R.string.message_stopped)); 96 return true; 97 case MENU_PAUSE: 98 mLunarThread.pause(); 99 return true; 100 case MENU_RESUME: 101 mLunarThread.unpause(); 102 return true; 103 case MENU_EASY: 104 mLunarThread.setDifficulty(LunarThread.DIFFICULTY_EASY); 105 return true; 106 case MENU_MEDIUM: 107 mLunarThread.setDifficulty(LunarThread.DIFFICULTY_MEDIUM); 108 return true; 109 case MENU_HARD: 110 mLunarThread.setDifficulty(LunarThread.DIFFICULTY_HARD); 111 return true; 112 } 113 114 return false; 115 } 116 117 /** 118 * Invoked when the Activity is created. 119 * 120 * @param savedInstanceState a Bundle containing state saved from a previous 121 * execution, or null if this is a new execution 122 */ 123 @Override onCreate(Bundle savedInstanceState)124 protected void onCreate(Bundle savedInstanceState) { 125 super.onCreate(savedInstanceState); 126 127 // turn off the window's title bar 128 requestWindowFeature(Window.FEATURE_NO_TITLE); 129 130 // tell system to use the layout defined in our XML file 131 setContentView(R.layout.lunar_layout); 132 133 // get handles to the LunarView from XML, and its LunarThread 134 mLunarView = (LunarView) findViewById(R.id.lunar); 135 mLunarThread = mLunarView.getThread(); 136 137 // give the LunarView a handle to the TextView used for messages 138 mLunarView.setTextView((TextView) findViewById(R.id.text)); 139 140 if (savedInstanceState == null) { 141 // we were just launched: set up a new game 142 mLunarThread.setState(LunarThread.STATE_READY); 143 Log.w(this.getClass().getName(), "SIS is null"); 144 } else { 145 // we are being restored: resume a previous game 146 mLunarThread.restoreState(savedInstanceState); 147 Log.w(this.getClass().getName(), "SIS is nonnull"); 148 } 149 } 150 151 /** 152 * Invoked when the Activity loses user focus. 153 */ 154 @Override onPause()155 protected void onPause() { 156 super.onPause(); 157 mLunarView.getThread().pause(); // pause game when Activity pauses 158 } 159 160 /** 161 * Notification that something is about to happen, to give the Activity a 162 * chance to save state. 163 * 164 * @param outState a Bundle into which this Activity should save its state 165 */ 166 @Override onSaveInstanceState(Bundle outState)167 protected void onSaveInstanceState(Bundle outState) { 168 // just have the View's thread save its state into our Bundle 169 super.onSaveInstanceState(outState); 170 mLunarThread.saveState(outState); 171 Log.w(this.getClass().getName(), "SIS called"); 172 } 173 } 174