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