Home | History | Annotate | Download | only in lunarlander
      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
     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
     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
    124     protected void onCreate(Bundle savedInstanceState) {
    125         super.onCreate(savedInstanceState);
    126 
    127         // tell system to use the layout defined in our XML file
    128         setContentView(R.layout.lunar_layout);
    129 
    130         // get handles to the LunarView from XML, and its LunarThread
    131         mLunarView = (LunarView) findViewById(R.id.lunar);
    132         mLunarThread = mLunarView.getThread();
    133 
    134         // give the LunarView a handle to the TextView used for messages
    135         mLunarView.setTextView((TextView) findViewById(R.id.text));
    136 
    137         if (savedInstanceState == null) {
    138             // we were just launched: set up a new game
    139             mLunarThread.setState(LunarThread.STATE_READY);
    140             Log.w(this.getClass().getName(), "SIS is null");
    141         } else {
    142             // we are being restored: resume a previous game
    143             mLunarThread.restoreState(savedInstanceState);
    144             Log.w(this.getClass().getName(), "SIS is nonnull");
    145         }
    146     }
    147 
    148     /**
    149      * Invoked when the Activity loses user focus.
    150      */
    151     @Override
    152     protected void onPause() {
    153         super.onPause();
    154         mLunarView.getThread().pause(); // pause game when Activity pauses
    155     }
    156 
    157     /**
    158      * Notification that something is about to happen, to give the Activity a
    159      * chance to save state.
    160      *
    161      * @param outState a Bundle into which this Activity should save its state
    162      */
    163     @Override
    164     protected void onSaveInstanceState(Bundle outState) {
    165         // just have the View's thread save its state into our Bundle
    166         super.onSaveInstanceState(outState);
    167         mLunarThread.saveState(outState);
    168         Log.w(this.getClass().getName(), "SIS called");
    169     }
    170 }
    171