StatusShout.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. 
 * See license text file delivered with this project for more information.
 */

package com.nokia.example.statusshout;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.nokia.example.statusshout.engine.ShareApiManager;
import com.nokia.example.statusshout.engine.StatusShoutData;
import com.nokia.example.statusshout.ui.MainView;

/**
 * The main class of the application.
 */
public class StatusShout
    extends MIDlet
    implements CommandListener
{
    // Constants
    private static final String SPLASH_SCREEN_IMAGE_URI = "/splash-screen-240x320.png";
    private static final int SPLASH_SCREEN_BG_COLOR = 0x0066ca;
    private static final long SPLASH_SCREEN_DURATION = 2000; // Milliseconds

    // Members
    private MainView mainView;
    private Command exitCommand;
    private Timer splashScreenTimer;
    private TimerTask splashScreenTimerTask;
    private boolean wasLaunchedAsSharingDestination;

    /**
     * Constructor.
     */
    public StatusShout() {
        // Create and show the splash screen. Since the splash screen is a
        // local variable, the resources it reserves will be released after
        // another Displayable is shown.
        SplashScreen splashScreen = new SplashScreen(SPLASH_SCREEN_IMAGE_URI, SPLASH_SCREEN_BG_COLOR);
        Display.getDisplay(this).setCurrent(splashScreen);
        
        // It is a good idea to enable exiting with back command during splash
        // screen too.
        exitCommand = new Command("Exit", Command.EXIT, 0x01);
        splashScreen.addCommand(exitCommand);
        splashScreen.setCommandListener(this);
        
        // Set the timer to show the main view
        splashScreenTimer = new Timer();
        splashScreenTimerTask = new SplashScreenTimerTask();
        splashScreenTimer.schedule(splashScreenTimerTask, SPLASH_SCREEN_DURATION);
    }

    /**
     * @see javax.microedition.midlet.MIDlet#startApp()
     */
    protected void startApp() throws MIDletStateChangeException {
        System.out.println("StatusShout.startApp()");
        
        if (mainView == null) {
            mainView = new MainView(this);
        }
        
        ShareApiManager shareApiManager = ShareApiManager.getInstance(this);
        shareApiManager.checkForInvocation();
        wasLaunchedAsSharingDestination = shareApiManager.getWasLaunchedAsSharingDestination();
        
        if (splashScreenTimer == null) {
            // No timer running. This might indicate that the app has been
            // resumed. Thus, show the main view immediately.
            showMainView();
        }
    }

    /**
     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
     */
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        System.out.println("StatusShout.destroyApp()");
        
        // Save app state
        StatusShoutData.getInstance().save();
    }

    /**
     * @see javax.microedition.midlet.MIDlet#pauseApp()
     */
    protected void pauseApp() {
        System.out.println("StatusShout.pauseApp()");
    }

    /**
     * @see javax.microedition.lcdui.CommandListener#commandAction(
     * javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == exitCommand) {
            try {
                destroyApp(true);
            }
            catch (MIDletStateChangeException e) {
                e.printStackTrace();
            }
            finally {
                notifyDestroyed();
            }
        }
    }

    /**
     * Quits the app.
     */
    public void quit() {
        commandAction(exitCommand, null);
    }

    /**  
     * @return The Display instance (required by SplashScreenTimerTask).
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Shows the main view.
     */
    private void showMainView() {
        getDisplay().setCurrent(mainView);
        mainView.setCategoryBar(!wasLaunchedAsSharingDestination);
        
        // If there was an image URI in the invocation data, show it in the
        // main view
        final String imageUri = ShareApiManager.getInstance(this).getImageUriFromInvocation();
        
        if (imageUri != null) {
            mainView.setImage(imageUri);
        }
        
        splashScreenTimer = null;
        splashScreenTimerTask = null;
    }

    /**
     * A task for showing the main view.
     */
    private class SplashScreenTimerTask extends TimerTask {
        public void run() {
            System.out.println("SplashScreenTimerTask.run()");
            
            if (mainView != null) {
                showMainView();
            }
        }
    }
}

Last updated 15 October 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×