// Copyright 2001-2005, Danger, Inc.  All Rights Reserved.
// This file is subject to the Danger, Inc. Sample Code License,
// which is provided in the file SAMPLE_CODE_LICENSE.
// Copies are also available from http://developer.danger.com/

package com.example.AudioDemo;

import danger.app.Application;
import danger.app.Event;
import danger.ui.ScreenWindow;
import danger.ui.StaticText;
import danger.util.DEBUG;

/**
 * Implements the MainWindow class. It is created from a Screen resource. The
 * layout merely shows the current play state as a StaticText item that is
 * updated to properly reflect that value.
 * 
 * This object has different code for the 1.1 version, as compared to the 2.0
 * and greater version. In general, the goal is to have one source base, but
 * sometimes there are improvements in the OS that are compelling enough to
 * break that goal. Properly handling the Pause of the soundtrack is one of
 * these cases.
 * 
 * The SDK has a code file override feature which helps to avoid duplicating
 * the entire source tree. In this case, the single file that varies between
 * versions, MainWindow.java has a 1.1 specific file in the 'common' directory.
 * The 2.0 directory includes just the MainWindow.java file that compiles for
 * the 2.0 version of the OS, and uses the rest of the application code from
 * common.
 * 
 * 
 * In the 2.0 variant here, handling Pause is much simpler because it can
 * override the onDeactivate() and onActivate() methods to be informed when a
 * system dialog or menu has come up over the MainWindow. When this happens, in
 * order to follow the proper UI, the audio needs to be paused. It is also
 * resumed when the dialog leaves.
 * 
 * Since this is the 2.0 variant, the UI event of Event.DEVICE_BUTTON_CANCEL is
 * used directly, instead of using the Hiptop2Event external file variant.
 */
public class MainWindow extends ScreenWindow implements Resources, Commands {

	/**
	 * local reference for the application, to avoid the need for coercion.
	 */
	private AudioDemo mApplication;

	/**
	 * reference variables for the StaticText item in the window, to allow for
	 * updating the string.
	 */
	private StaticText mPlayingText;

	// ------------------------------------------------------------------------
	/**
	 * This is called after the window has been created from resources. The
	 * reason to override this routine is to allow access to screen widgets,
	 * so they can be updated when the play state changes.
	 */
	public void onDecoded() {
		// get coerced reference to current App, it's used to update all window
		// widgets.
		mApplication = (AudioDemo) Application.getCurrentApp();

		// After the window is created, the StaticText field has a default
		// value that will be updated upon user menu choices.
		// Therefore, find and save a reference to the object of interest.
		mPlayingText = (StaticText) getDescendantWithID(ID_PLAYING_TEXT);

		super.onDecoded();
	}

	/**
	 * Handles events. Called automatically whenever this window receives an
	 * event. The only events handled are related to the menu.
	 */
	public boolean receiveEvent(Event e) {
		switch (e.type) {
		case EVENT_MUSIC_OFF:
			DemoSounds.stopLoop();
			mPlayingText.setText(mApplication.getString(ID_STOPPED));

			DEBUG.p("MainWindow: EVENT_MUSIC_OFF");
			return true;

		case EVENT_MUSIC_ON:
			DemoSounds.startLoop();
			mPlayingText.setText(mApplication.getString(ID_PLAYING));

			DEBUG.p("MainWindow: EVENT_MUSIC_ON");
			return true;
		}
		return super.receiveEvent(e);
	}

	/**
	 * Upon the Down Widget hit, play a Torpedo sound for JoyPad events. This is
	 * preferable to the Up Widget hit for something where the user expects an
	 * immediate response.
	 */
	public boolean eventWidgetDown(int inWhichWidget, Event inEvent) {
		switch (inWhichWidget) {

		// For the keypad hits, launch the torpedo sounds.
		case Event.DEVICE_ARROW_LEFT:
		case Event.DEVICE_ARROW_UP:
		case Event.DEVICE_ARROW_RIGHT:
		case Event.DEVICE_ARROW_DOWN:
			DemoSounds.startTorpedo(inWhichWidget);
			return true;

		case Event.DEVICE_BUTTON_JUMP:
			// Always make sure to call super.eventWidgetUp for this control so
			// the system can execute the correct behavior (returning to the
			// Jump screen with phone selected).
			break;
		}
		return super.eventWidgetDown(inWhichWidget, inEvent);
	}

	/**
	 * Handles device buttons, specifically the Cancel, and Done buttons.
	 */
	public boolean eventWidgetUp(int inWhichWidget, Event inEvent) {
		switch (inWhichWidget) {

		// On original Sidekick devices, this event is never sent. On Sidekick2
		// devices, this event will be received and handled here. Since this
		// is the 2.0 code variant, we use the standard event code.
		case Event.DEVICE_BUTTON_CANCEL:
			mApplication.returnToLauncher();
			return true;

		// For both the Cancel or Back/Done button from the MainWindow, it will
		// jump back to the launcher screen to provide a better experience.
		case Event.DEVICE_BUTTON_BACK:
			mApplication.returnToLauncher();
			return true;
		}

		// Always make sure to call super.eventWidgetUp for other events so the
		// system can execute the correct behavior (e.g. returning to the Jump
		// screen with phone selected).
		return super.eventWidgetUp(inWhichWidget, inEvent);
	}

	/**
	 * This routine does not exist in the 1.1 version of the OS. It will be
	 * called whenever this MainWindow is deactivated. Of particular interest,
	 * this will be called when a System Dialog comes up in front of the
	 * MainWindow, but the app is still running. The Sound profile selector is
	 * one such dialog, as well as phone call alerts, or Calendar events, or
	 * even just the menu.
	 * 
	 * This will merely Pause the sounds, to implement the proper UI during
	 * these System Dialogs.
	 */
	protected void onDeactivate() {
		DemoSounds.pause();

		DEBUG.p("MainWindow: Deactivate");
	}

	/**
	 * This routine does not exist in the 1.1 version of the OS. It is called
	 * whenever a system dialog is done. Upon having the dialog removed, it is
	 * necessary to resume playback of the Audio.
	 */
	protected void onActivate() {
		DemoSounds.resume();

		DEBUG.p("MainWindow: Activate");
	}
}
