AMMSTuner.java

/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 */

package com.nokia.example.ammstuner;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;

/**
 * The main class of the MIDlet.
 */
public class AMMSTuner
    extends MIDlet
    implements CommandListener, ItemStateListener, Tuner.Listener
{
    // Members
    private Form form;
    private Command startCommand;
    private Command stopCommand;
    private Command exitCommand;
    private Tuner tuner;
    private FrequencyItem frequencyItem;
    private UpDownButtonsItem upDownButtonsItem;
    private StringItem infoItem;
    private boolean radioIsStarting;

    /**
     * @see javax.microedition.midlet.MIDlet#startApp()
     */
    protected void startApp() {
        form = new Form("Tuner test");
        frequencyItem = new FrequencyItem();
        infoItem = new StringItem("Status", "Ready.");
        upDownButtonsItem = new UpDownButtonsItem();
        form.append(frequencyItem);
        form.append(infoItem);
        form.append(upDownButtonsItem);
        
        startCommand = new Command("Start", Command.SCREEN, 1);
        stopCommand = new Command("Stop", Command.SCREEN, 4);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        
        form.addCommand(startCommand);
        form.addCommand(exitCommand);
        form.setCommandListener(this);
        form.setItemStateListener(this);
        Display.getDisplay(this).setCurrent(form);
        
        radioIsStarting = false;
        initRadio();
    }

    /**
     * @see javax.microedition.midlet.MIDlet#pauseApp()
     */
    protected void pauseApp() {
    }

    /**
     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
     */
    protected void destroyApp(boolean unconditional) {
        tuner.killRadio();
    }

    /**
     * @see javax.microedition.lcdui.CommandListener#commandAction(Command, Displayable)
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == startCommand) {
            radioIsStarting = true;
            tuner.startRadio();
        }
        else if (command == stopCommand) {
            tuner.stopRadio();
            
            form.addCommand(startCommand);
            form.removeCommand(stopCommand);
        }
        else if (command == exitCommand) {
            notifyDestroyed();
        }
    }

    /**
     * @see javax.microedition.lcdui.ItemStateListener#itemStateChanged(Item)
     */
    public void itemStateChanged(Item item) {
        if (item.equals(upDownButtonsItem)) {
            if (!tuner.getIsStarted()) {
                return;
            }
            
            if (upDownButtonsItem.getButtonPressed() == UpDownButtonsItem.DOWN) {
                System.out.println("AMMSTuner::itemStateChanged(): Down");
                
                if (!tuner.seekNextFrequency(tuner.getCurrentFrequency(), false)) {
                    log("Seek down: RadioPlayer = null!");
                }
            }
            else if (upDownButtonsItem.getButtonPressed() == UpDownButtonsItem.UP) {
                System.out.println("AMMSTuner::itemStateChanged(): Up");
                
                if (!tuner.seekNextFrequency(tuner.getCurrentFrequency(), true)) {
                    log("Seek up: RadioPlayer = null!");
                }
            }
        }
    }

    /**
     * Appends the form with the given message.
     * @param message The message to show on the form.
     */
    protected void log(String message) {
        form.append(message);
    }

    /**
     * Creates an instance of Tuner class and initializes the radio.
     */
    private void initRadio() {
        if (tuner == null) { 
            tuner = new Tuner(this);
        }
        
        tuner.initializeRadio();
    }

    /**
     * Method for adding Commands to the Form for controlling the radio.
     */
    private void addCommandsAfterRadioIsStarted() {
        form.removeCommand(startCommand);
        form.addCommand(stopCommand);
    }


    // Call backs from Tuner.Listener

    /**
     * @see Tuner.Listener#onInitialized()
     */
    public void onInitialized() {
        infoItem.setText("Radio initialized.");
    }

    /**
     * @see Tuner.Listener#onStarted()
     */
    public void onStarted() {
        radioIsStarting = false;
        infoItem.setText("Radio started.");
        addCommandsAfterRadioIsStarted();
    }

    /**
     * @see Tuner.Listener#onStopped()
     */
    public void onStopped() {
        frequencyItem.updateFrequency(Tuner.MIN_FREQUENCY);
        infoItem.setText("Radio stopped.");
    }

    /**
     * @see Tuner.Listener#onFrequencyChanged(int)
     */
    public void onFrequencyChanged(int frequency) {
        frequencyItem.updateFrequency(frequency);
        infoItem.setText("Frequency set to: " + ((double)frequency / 10000) + " MHz");
    }

    /**
     * @see Tuner.Listener#onSeekFailed()
     */
    public void onSeekFailed() {
        infoItem.setText("No FM broadcast found.");
    }

    /**
     * @see Tuner.Listener#onError(String)
     */
    public void onError(String errorMessage) {
        if (radioIsStarting) {
            // Something went wrong whilst starting the radio. This could easily
            // been caused by the lack of headphones i.e. no antenna.
            radioIsStarting = false;
            
            Alert alert = new Alert("Failed to start",
                "Make sure you have headphones connected to your phone.",
                null, AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            alert.addCommand(new Command("Ok", Command.OK, 1));
            Display.getDisplay(this).setCurrent(alert);
        }
        else {
            log(errorMessage);
        }
    }
}