Permalink
Browse files

BlackBerry Samples for Java release 6.0.0.

An Open Source version of the samples as they were released
with the 6.0.0 Java Development Environment release.

The BlackBerry Samples for Java project is an Open Source
repository of samples that in the past were released through
the BlackBerry Developer Knowledge Base or within the
BlackBerry Java Tools.  The samples range in scope from Hello
World, to Advanced UI components and include both samples and
libraries that offer extended functionality.

Copyright (c) 2011 Research In Motion Limited

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
  • Loading branch information...
slegge committed Dec 1, 2011
1 parent b3db4c1 commit ea584537f125a95e27000dfca596344ea11301cd
Showing 488 changed files with 29,211 additions and 10,848 deletions.
@@ -28,6 +28,9 @@
import java.util.Random;
+import net.rim.device.api.command.Command;
+import net.rim.device.api.command.CommandHandler;
+import net.rim.device.api.command.ReadOnlyCommandMetadata;
import net.rim.device.api.system.AccelerometerSensor;
import net.rim.device.api.system.AccelerometerSensor.Channel;
import net.rim.device.api.system.Bitmap;
@@ -39,7 +42,9 @@
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
+import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
+import net.rim.device.api.util.StringProvider;
/**
* This sample demonstrates the Accelerometer API. The DrawThread opens the
@@ -75,8 +80,11 @@
private int _tick = 0;
+ private MenuItem _startMenuItem;
+ private MenuItem _stopMenuItem;
+
/**
- * Entry point for application.
+ * Entry point for application
*
* @param args
* Command line arguments (not used)
@@ -88,13 +96,51 @@ public static void main(final String[] args) {
app.enterEventDispatcher();
}
- // Constructor
+ /**
+ * Creates a new AccelerometerDemo object
+ */
public AccelerometerDemo() {
if (AccelerometerSensor.isSupported()) {
- // Initialize UI
+
+ // Menu item to start the ball moving
+ _startMenuItem =
+ new MenuItem(new StringProvider("Start"), 0x230010, 0);
+ _startMenuItem.setCommand(new Command(new CommandHandler() {
+ /**
+ * @see net.rim.device.api.command.CommandHandler#execute(ReadOnlyCommandMetadata,
+ * Object)
+ */
+ public void execute(final ReadOnlyCommandMetadata metadata,
+ final Object context) {
+ if (_thread == null) {
+ // Start drawing
+ _thread = new DrawThread();
+ _thread.start();
+ }
+ }
+ }));
+
+ // Menu item to stop the ball moving
+ _stopMenuItem =
+ new MenuItem(new StringProvider("Stop"), 0x230010, 0);
+ _stopMenuItem.setCommand(new Command(new CommandHandler() {
+ /**
+ * @see net.rim.device.api.command.CommandHandler#execute(ReadOnlyCommandMetadata,
+ * Object)
+ */
+ public void execute(final ReadOnlyCommandMetadata metadata,
+ final Object context) {
+ if (_thread != null) {
+ synchronized (_thread) {
+ _thread._running = false;
+ _thread.notifyAll();
+ _thread = null;
+ }
+ }
+ }
+ }));
+
_screen = new AccelerometerDemoScreen();
- _screen.addMenuItem(_startMenuItem);
- _screen.addMenuItem(_stopMenuItem);
pushScreen(_screen);
_ball = Bitmap.getBitmapResource("img/ball.png");
@@ -105,7 +151,7 @@ public AccelerometerDemo() {
_ballHeight = _ball.getHeight();
}
- // Prevent UI from rotating our screen.
+ // Prevent UI from rotating the screen
Ui.getUiEngineInstance().setAcceptableDirections(
DEFAULT_ORIENTATION);
} else {
@@ -119,23 +165,23 @@ public void run() {
}
/**
- * Calculates ball position.
+ * Calculates ball position
*
* @param xAcc
* x axis acceleration
* @param yAcc
* y axis acceleration
*/
private void applyForce(final int xAcc, final int yAcc) {
- // Calculate new speed.
+ // Calculate new speed
_xSpeed += xAcc * G_NORM;
_ySpeed += yAcc * G_NORM;
- // Apply table friction.
+ // Apply table friction
_xSpeed *= TABLE_FRICTION;
_ySpeed *= TABLE_FRICTION;
- // Move the ball.
+ // Move the ball
_x += _xSpeed;
_y += _ySpeed;
@@ -163,24 +209,24 @@ private void applyForce(final int xAcc, final int yAcc) {
}
/**
- * A thread class to handle screen updates.
+ * A thread class to handle screen updates
*/
private class DrawThread extends Thread {
private boolean _running;
public void run() {
_running = true;
- // Start querying the accelerometer sensor.
+ // Start querying the accelerometer sensor
openAccelerometerConnection();
while (_running) {
_tick++;
- // Get current acceleration.
+ // Get current acceleration
readAcceleration();
- // Apply force to the ball.
+ // Apply force to the ball
applyForce(-_xyz[0], _xyz[1]);
try {
@@ -203,13 +249,22 @@ public void run() {
_screen.invalidate();
}
- // Stop querying the sensor to save battery charge.
+ // Stop querying the sensor to save battery charge
closeAccelerometerConnection();
}
+
+ /**
+ * Returns running state of thread
+ *
+ * @return True if this thread is running, otherwise false
+ */
+ public boolean isRunning() {
+ return _running;
+ }
}
/**
- * Opens the data channel.
+ * Opens the data channel
*/
private void openAccelerometerConnection() {
if (DeviceInfo.isSimulator()) {
@@ -223,23 +278,23 @@ private void openAccelerometerConnection() {
}
/**
- * Gets the latest acceleromenter data.
+ * Gets the latest acceleromenter data
*/
private void readAcceleration() {
if (_simulated) {
- // Running in a simulator, simulate random.
+ // Running in a simulator, simulate random
if (_tick % 10 == 0) {
_xyz[0] = (short) (_r.nextInt(400) - 200);
_xyz[1] = (short) (_r.nextInt(400) - 200);
}
} else {
- // Real device, call the API for samples.
+ // Real device, call the API for samples
_accChannel.getLastAccelerationData(_xyz);
}
}
/**
- * Closes the data channel.
+ * Closes the data channel
*/
private void closeAccelerometerConnection() {
if (_accChannel != null) {
@@ -249,36 +304,7 @@ private void closeAccelerometerConnection() {
}
/**
- * Menu item to start the ball moving.
- */
- private final MenuItem _startMenuItem = new MenuItem("Start", 0, 0) {
- public void run() {
- if (_thread == null) {
- // Start drawing
- _thread = new DrawThread();
- _thread.start();
- }
-
- }
- };
-
- /**
- * Menu item to stop the ball moving.
- */
- private final MenuItem _stopMenuItem = new MenuItem("Stop", 0, 0) {
- public void run() {
- if (_thread != null) {
- synchronized (_thread) {
- _thread._running = false;
- _thread.notifyAll();
- _thread = null;
- }
- }
- }
- };
-
- /**
- * A screen on which to display the ball.
+ * A screen on which to display the ball
*/
private class AccelerometerDemoScreen extends MainScreen {
/**
@@ -291,6 +317,19 @@ protected void paint(final Graphics graphics) {
}
}
+ /**
+ * @see MainScreen#makeMenu(Menu, int)
+ */
+ protected void makeMenu(final Menu menu, final int instance) {
+ if (_thread == null || !_thread.isRunning()) {
+ menu.add(_startMenuItem);
+ } else {
+ menu.add(_stopMenuItem);
+ }
+
+ super.makeMenu(menu, instance);
+ }
+
/**
* @see Screen#onClose()
*/
@@ -59,7 +59,9 @@ public static void main(final String[] args) {
new CustomComponentsDemo().enterEventDispatcher();
}
- // Constructor
+ /**
+ * Creates a new CustomComponentsDemo object
+ */
public CustomComponentsDemo() {
try {
// Only one listener can be registered
@@ -91,7 +93,9 @@ public void run() {
private final TextComponent _textComponent;
private final ValueComponent _valueComponent;
- // Constructor
+ /**
+ * Creates a new CustomComponentsDemoScreen object
+ */
private CustomComponentsDemoScreen() {
setTitle("Accessibility Demo");
@@ -46,13 +46,14 @@ img\accessibilitydemo_jde.png
]
Listing=0
Options=-quiet
-OutputFileName=accessibilitydemo
+OutputFileName=CustomComponentsDemo
[PackageProtection
]
RibbonPosition=0
[RolloverIcons
]
RunOnStartup=0
+SkipCompile=0
StartupTier=7
SystemModule=0
Title=Accessibility Demo
@@ -53,7 +53,9 @@
private int _iconHeight;
private int _state;
- // Constructor
+ /**
+ * Creates a new IconToolbarComponent object
+ */
public IconToolbarComponent() {
super(Field.FOCUSABLE);
@@ -337,7 +339,14 @@ public AccessibleValue getAccessibleValue() {
private final Bitmap _image;
private int _state;
- // Constructor
+ /**
+ * Creates a new Icon object
+ *
+ * @param label
+ * Label for the icon
+ * @param image
+ * Bitmap image for the icon
+ */
public Icon(final String label, final Bitmap image) {
_label = label;
_image = image;
@@ -176,7 +176,14 @@ public boolean isAccessibleSelected(final int r, final int c) {
private final boolean _selected; // True if the label is selected -
// applicable for table cells only
- // Constructor
+ /**
+ * Creates a new AccessibleLabel object
+ *
+ * @param label
+ * Text to display on the label
+ * @param selected
+ * True if the label is selected, otherwise false
+ */
private AccessibleLabel(final String label, final boolean selected) {
_label = label;
_selected = selected;
@@ -38,13 +38,14 @@ HaveImports=0
]
Listing=0
Options=-quiet
-OutputFileName=screenreaderdemo
+OutputFileName=ScreenReaderDemo
[PackageProtection
]
RibbonPosition=0
[RolloverIcons
]
RunOnStartup=0
+SkipCompile=0
StartupTier=7
SystemModule=0
Type=2
@@ -91,7 +91,7 @@ public static void speak(final String text) {
*
* @param stateSet
* Flag containing bits representing supported orientation(s)
- * @return <description>
+ * @return String to indicate supported orientation
*/
public static String getOrientation(final int stateSet) {
final boolean horizontal = (stateSet & AccessibleState.HORIZONTAL) != 0;
Oops, something went wrong.

0 comments on commit ea58453

Please sign in to comment.