AboutView.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.TXT for license information.
 */

package com.nokia.example.statusshout.ui;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;

/**
 * About view displaying the application information.
 */
public class AboutView extends Canvas {
    // Constants
    private static final String APPLICATION_NAME = "Status Shout!";
    private static final String VERSION = "Version ";
    private static final String LINK_TEXT = "developer.nokia.com";
    private static final String LINK_URL = "http://developer.nokia.com";
    
    private static final String ABOUT_TEXT =
        "This Nokia example demonstrates how to incorporate services using the "
        + "OAuth authentication to Nokia Asha applications and how to utilise "
        + "the Share API to share images and messages to various destinations. "
        + "For more information, visit Nokia Developer website: ";
    
    private static final int TEXT_COLOR = 0xf06a10;
    private static final int MARGIN = 5;

    // Members
    private MIDlet midlet;
    private TextWrapper textWrapper;
    private Image backgroundImage;
    private Font titleFont;
    private Font versionFont;
    private Font linkFont;
    private int linkY;

    /**
     * Constructor.
     * 
     * @param midlet The MIDlet instance (used for opening the web browser and
     * retrieving the MIDlet version number.
     * @param backgroundImage The background image to use.
     * @throws NullPointerException If the MIDlet instance is null.
     */
    public AboutView(MIDlet midlet, Image backgroundImage)
        throws NullPointerException
    {
        super();
        
        if (midlet == null) {
            throw new NullPointerException("MIDlet instance is null!");
        }
        
        this.midlet = midlet;
        this.backgroundImage = backgroundImage;
        
        textWrapper = TextWrapper.instance(Font.getDefaultFont(), getWidth() - MARGIN * 2);
        
        Font defaultFont = Font.getDefaultFont();
        titleFont = Font.getFont(defaultFont.getFace(), Font.STYLE_BOLD, Font.SIZE_LARGE);
        versionFont = Font.getFont(defaultFont.getFace(), defaultFont.getStyle(), Font.SIZE_SMALL);
        linkFont = Font.getFont(defaultFont.getFace(), Font.STYLE_UNDERLINED, defaultFont.getSize());
    }

    /**
     * @see javax.microedition.lcdui.Canvas#pointerPressed(int, int)
     */
    protected void pointerPressed(int x, int y) {
        if (y >= linkY - MARGIN && y <= linkY + Font.getDefaultFont().getHeight() + MARGIN) {
            // Link tapped, open browser
            try {
                midlet.platformRequest(LINK_URL);
            }
            catch (ConnectionNotFoundException e) {
                System.out.println("AboutView.pointerPressed(): " + e.toString());
            }
            catch (Exception e) {}
        }
    }

    /**
     * @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
     */
    protected void paint(Graphics graphics) {
        final int anchor = Graphics.TOP | Graphics.LEFT;
        
        if (backgroundImage != null) {
            graphics.drawImage(backgroundImage, 0, 0, Graphics.TOP | Graphics.LEFT);
        }
        
        graphics.setColor(TEXT_COLOR);
        graphics.setFont(titleFont);
        
        int y = MARGIN;
        
        graphics.drawString(APPLICATION_NAME, MARGIN, y, anchor);
        
        graphics.setFont(versionFont);
        y += titleFont.getHeight();
        
        graphics.drawString(VERSION + midlet.getAppProperty("MIDlet-Version"), MARGIN, y, anchor);
        
        graphics.setFont(Font.getDefaultFont());
        y += versionFont.getHeight() + MARGIN;
        
        try {
            textWrapper.setText(ABOUT_TEXT);
            
            while (textWrapper.hasMoreLines()) {
                graphics.drawString(textWrapper.nextLine(), MARGIN, y, anchor);
                y += Font.getDefaultFont().getHeight() - 4;
            }
        }
        catch (IllegalArgumentException e) {}
        
        linkY = y + MARGIN;
        graphics.setFont(linkFont);
        graphics.drawString(LINK_TEXT, (getWidth() - linkFont.stringWidth(LINK_TEXT)) / 2, linkY, anchor);
    }
}