Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Protect BlackBerry applications with a password screen

by BlackBerry Development Advisor ‎02-16-2010 01:49 PM - edited ‎09-16-2010 02:32 PM

Summary

 

This article applies to the following:

  • BlackBerry® wireless devices based on Java™
  • BlackBerry Java Development Environment (JDE)

Description

 

Passwords are implemented in one of the following ways:

  1. A pop-up screen that presents a small password entry box to the user;
  2. a full screen that completely blocks the underlying application screen and allows the password to be entered.

In order to create a pop-up password screen for a BlackBerry application, the PopupScreen class must be extended. Implementation of both a TrackwheelListener and KeyListener is also needed, such that whenever the trackwheel is clicked or the Enter key is pressed on the BlackBerry device, the password is verified.

 

A pop-up password screen is implemented using the PopupScreen() UI object, whereas the full screen approach is implemented using the MainScreen() UI object. The example below illustrates the PopupScreen() approach.

 

 

import net.rim.device.api.i18n.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
public class PasswordPopupScreen extends PopupScreen implements
   KeyListener, TrackwheelListener {
   private String _response;
   // This EditField in RIM API provides built in password
   // masking so the password does not appear on the screen
   // as the user enters it
   private PasswordEditField answer;

   // Hard coded password, normally it is a better idea to have
   // the password stored in an internal RecordStore but here
   // it is hard coded for simplification
   private String password = "password";
   public PasswordPopupScreen()
   {
       super(new VerticalFieldManager(),Field.FOCUSABLE);
       LabelField question = new LabelField("Please enter password");
       answer = new PasswordEditField(" ","");
       add(question);
       add(new SeparatorField());
       add(answer);
   }
   // This function gets called if the password gets called
   // it pops the password screen and pushes the apps main screen
   public void accept() {
       UiApplication.getUiApplication().popScreen(this);
   }
   public void close() {
       UiApplication.getUiApplication().popScreen(this);
   }
   public String getResponse() {
       return _response;
   }
   ////////////////////////////////////////////
   /// implementation of TrackwheelListener
   ////////////////////////////////////////////
   public boolean trackwheelClick(int status, int time) {
       _response = answer.getText();
       if (_response.equals(password)) {
           accept();
       }
       else {
           Dialog.alert("Invalid Password !");
       }
       return true;
   }
   /** Invoked when the trackwheel is released */
   public boolean trackwheelUnclick(int status, int time) {
       return false;
   }
   /** Invoked when the trackwheel is rolled. */
   public boolean trackwheelRoll(int amount, int status, int time) {
       return true;
   }
   /////////////////////////////////////
   /// implementation of Keylistener
   /////////////////////////////////////
   public boolean keyChar(char key, int status, int time) {
       //intercept the ESC key - exit the app on its receipt
       boolean retval = false;
       switch (key) {
           case Characters.ENTER:
               _response = answer.getText();
               if (_response.equals(password)) {
                   accept();
               }
               // an alert is displayed if the password is incorrect
               else {
                   Dialog.alert("Invalid Password !");
               }
               retval = true;
               break;
           case Characters.ESCAPE:
               close();
               break;
           default:
               retval = super.keyChar(key,status,time);
       }
       return retval;
   }
   /** Implementation of KeyListener.keyDown */
   public boolean keyDown(int keycode, int time) {
       return false;
   }
   /** Implementation of KeyListener.keyRepeat */
   public boolean keyRepeat(int keycode, int time) {
       return false;
   }
   /** Implementation of KeyListener.keyStatus */
   public boolean keyStatus(int keycode, int time) {
       return false;
   }
   /** Implementation of KeyListener.keyUp */
   public boolean keyUp(int keycode, int time) {
       return false;
   }
}

 

In the code shown above, the PasswordPopupScreen class extends the PopupScreen class. The accept() function is responsible for popping the PasswordScreen and replacing it with the application's main screen, which is represented by the next() function. The accept() function only gets called if the password is correct; otherwise, an Invalid Password alert appears.

 

In this example, the correct password is “password” and it is hard-coded for simplification purposes. Generally, it is a much better idea to store the password in persistent storage (for more details, please see RecordStore class in the application programming interface (API) documentation).

 

Note: PasswordEditField is used in the code sample shown above instead of the EditField for password entry because it provides built-in password masking (for example, the user's password does not appear on the screen as the user types it).

Contributors