martes, 13 de diciembre de 2011

Blackberry: Getting Info from the App World


Third-party applications can interface with the BlackBerry App World™ storefront. Using this feature, your application can obtain information about itself, such as the current version that is available and the license key it uses. This is useful for keeping the application current and making sure that it runs on a specific BlackBerry smartphone model. You can also launch the BlackBerry App World and point it directly to the web page for a specific application, making it easier to sell other applications or upgrades to the current application.

To test functionality that is dependent on accessing these properties (such as license validation) before your application is available on BlackBerry App World, you may load values into the properties yourself as part of a wireless installation. For more informationt, see this article.

The following example demonstrates how to interface to the BlackBerry App World. The complete project for this sample can be downloaded here.


package com.rim.samples.AppWorld;

import java.io.*;
import javax.microedition.content.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

/**
 * SampleApplication.java
 *
 * Sample application that shows how an application can
 * interface with BlackBerry App World.
 *
 * Note: For the sake of simplicity, this sample application may
 * not leverage resource bundles and resource strings. However,
 * it is STRONGLY recommended that application developers make
 * use of the localization features available within the
 * BlackBerry development platform to ensure a seamless application
 * experience across a variety of languages and geographies. For more
 * information on localizing your application, please refer to
 * the BlackBerry Java Development Environment Development Guide
 * associated with this release.
 *
 */
public class SampleApplication extends UiApplication {
    /**
     * The App World specific ID of this application.
     */
    private static final String RIM_APP_WORLD_ID = "RIM_APP_WORLD_ID";
    /**
     * A flag (true or false) that specifies whether there is
     * currently an update for this app available.
     */
    private static final String RIM_APP_WORLD_UPDATE_AVAIL = "RIM_APP_WORLD_UPDATE_AVAIL";
    /**
     * The license key for this application, as specified by the
     * App World purchase.
     */
    private static final String RIM_APP_WORLD_LICENSE_KEY = "RIM_APP_WORLD_LICENSE_KEY";
    /**
     * The name of the application, as specified in the App World
     * ISV Portal.
     */
    public static final String RIM_APP_WORLD_NAME = "RIM_APP_WORLD_NAME";
    /**
     * The email address of the purchaser. This will be their
     * PayPal email address.
     */
    public static final String RIM_APP_WORLD_EMAIL = "RIM_APP_WORLD_EMAIL";
    /**
     * The PIN of the purchaser.
     */
    public static final String RIM_APP_WORLD_PIN = "RIM_APP_WORLD_PIN";
    /**
     * The version of the application, as specified in the App
     * World ISV Portal.
     */
    public static final String RIM_APP_WORLD_VERSION = "RIM_APP_WORLD_VERSION";

    private MainScreen screen;
    private BasicEditField contentIdField;
    public SampleApplication() {

        // Get name of your app
        String myAppName = ApplicationDescriptor.currentApplicationDescriptor().getName();
        // It must be exact name of your application as
        // registered with the App World ISV portal
        //String myAppName = "AppName:Vendor";

        // If you are targeting 4.3+, use this:
        CodeModuleGroup group = CodeModuleGroupManager.load( myAppName );

        // On 4.2 you would need to use the following:
        /*
        CodeModuleGroup group = null;
        if( myAppName != null ) {
            CodeModuleGroup[] groups = CodeModuleGroupManager.loadAll();
            if( groups != null ) {
                for( int i = 0; i < groups.length; ++i ) {
                    if( groups[ i ].containsModule( myAppName ) ) {
                        group = groups[ i ];
                        break;
                    }
                }
            }
        } else {
            myAppName = "unknown";
        }
        */

        // Pull out the App World data from the CodeModuleGroup
        final String key = group == null ? "unknown" : group.getProperty( RIM_APP_WORLD_LICENSE_KEY );
        final String update = group == null ? "unknown" : group.getProperty( RIM_APP_WORLD_UPDATE_AVAIL );
        final String myContentId = group == null ? "" : group.getProperty( RIM_APP_WORLD_ID );
        final String name = group == null ? "unknown" : group.getProperty( RIM_APP_WORLD_NAME );
        final String email = group == null ? "unknown" : group.getProperty( RIM_APP_WORLD_EMAIL );
        final String pin = group == null ? "unknown" : group.getProperty( RIM_APP_WORLD_PIN );
        final String version = group == null ? "unknown" : group.getProperty( RIM_APP_WORLD_VERSION );
        screen = new MainScreen() {
            public boolean isDirty() {
                return false;
            }
        };

        //Display the data on the screen
        VerticalFieldManager manager = new VerticalFieldManager();
        screen.add( manager );
        manager.add( new BasicEditField( "App Name : ", myAppName, 100, Field.READONLY ) );
        manager.add( new BasicEditField( "License Key : ", key, 100, Field.READONLY ) );
        manager.add( new BasicEditField( "Update Available : ", update, 100, Field.READONLY ) );
        manager.add( new BasicEditField( "Name : ", name, 100, Field.READONLY ) );
        manager.add( new BasicEditField( "Email : ", email, 100, Field.READONLY ) );
        manager.add( new BasicEditField( "PIN : ", pin, 100, Field.READONLY ) );
         manager.add( new BasicEditField( "Version : ", version, 100, Field.READONLY ) );
        manager.add( new SeparatorField() );

        //Create the resources needed to launch the App World
        //pointing at our app's page
        contentIdField = new BasicEditField( "Content ID : ", myContentId );
        manager.add( contentIdField );
        ButtonField button = new ButtonField( "Open BlackBerry App World", ButtonField.CONSUME_CLICK );
        button.setChangeListener( new FieldChangeListener() {
            public void fieldChanged( Field field, int context ) {
                try {
                    openAppWorld( contentIdField.getText() );
                } catch( final Exception e ) {
                    SampleApplication.this.invokeLater( new Runnable() {
                        public void run() {
                            Dialog.alert( "Problems opening App World: " + e.getMessage() );
                        }
                    } );
                }
            }
        } );
        manager.add( button );
        pushScreen( screen );
    }

    /***
     * openAppWorld
     * <p>
     * Opens the App World pointing to a specific application. <p>
     * Note: This method takes advantage of
     * javax.microedition.content, which was introduced in 4.3.
     * There is no way to open the BlackBerry App World on an older OS.
     * <p>
     * @param myContentId App World ID of application to open.
     * @throws IllegalArgumentException if myContentId is invalid
     * @throws ContentHandlerException if App World is not installed
     * @throws SecurityException if access is not permitted
     * @throws IOException if access to the registry fails
     */

    protected void openAppWorld( String myContentId ) throws IllegalArgumentException, ContentHandlerException,
            SecurityException, IOException {
        Registry registry = Registry.getRegistry( SampleApplication.class.getName() );
        Invocation invocation = new Invocation( null, null, "net.rim.bb.appworld.Content", true, ContentHandler.ACTION_OPEN );
        invocation.setArgs( new String[] { myContentId } );
        boolean mustExit = registry.invoke( invocation );
        if( mustExit ) // For strict compliance - this should never happen on a BlackBerry
            screen.close();

        // Please note that this response won't be generated
        // until the BlackBerry App World exits
        // This method will block until that time
        Invocation response = registry.getResponse( true );
        if( response.getStatus() != Invocation.OK )
            Dialog.alert( "Problem invoking BlackBerry App World. Error code " + response.getStatus() );
        else
            Dialog.inform( "BlackBerry App World successfully opened." );
    }

    public static void main( String[] args ) {
        new SampleApplication().enterEventDispatcher();
    }
}