Input data into BasicEditField on BlackBerry Torch - blackberry

I'm writing an app which has a function asking users to type a filename into a BasicEditField on a PopupScreen. The app works fine with Blackberry Storm 2 -- both the simulator and a real device.
The problem is that the app doesn't work on a BlackBerry Torch -- neither the simulator nor a device. I can't enter text into the BasicEditField.
Why doesn't the keyboard on the BlackBerry Torch work with a BasicEditField? I've also tried an EditField instead of BasicEditField but it doesn't work either.
private BasicEditField txtFileName =
new BasicEditField("Name: ", "", 50, EditField.EDITABLE | EditField.FILTER_FILENAME);
...
Constructor()
{
add(txtFileName);
}

OK, the mistake I made was to put the wrong return value for the keyChar method.
I put
return true;
at the end of the method,
which should be
return super.keyChar(key,status,time);
Below is the correct implementation for the keyChar method:
public boolean keyChar(char key, int status, int time)
{
..................
/*
return true; // user cannot type in the BasicField on Torch, but can type on Storm
*/
return super.keyChar(key,status,time);// works on both Torch and Storm
}

This isn't an answer to your problem, but I have a code snippet that might help. I tried reproducing what you describe above using the following code. It worked without issue:
private static class AppScreen extends PopupScreen
{
AppScreen() {
super(new VerticalFieldManager(), Field.FOCUSABLE);
BasicEditField txtFileName = new BasicEditField("Name: ", "", 50,
EditField.EDITABLE | EditField.FILTER_FILENAME);
add(txtFileName);
}
}
Sorry I don't have a direct answer to your problem, but hopefully the above code can help you track down the problem in your app.

Related

How to start a file download in vaadin without button?

I know that it is really easy to create a FileDownloader and call extend with a Button. But how do I start a download without the Button?
In my specific situation right now I have a ComboBox and the file I'd like to send to the user is generated after changing its value, based on the input. The file should be sent immediately without waiting for another click. Is that easily possible?
Thanks
raffael
I found a solution myself. Actually two.
The first one uses the deprecated method Page.open()
public class DownloadComponent extends CustomComponent implements ValueChangeListener {
private ComboBox cb = new ComboBox();
public DownloadComponent() {
cb.addValueChangeListener(this);
cb.setNewItemsAllowed(true);
cb.setImmediate(true);
cb.setNullSelectionAllowed(false);
setCompositionRoot(cb);
}
#Override
public void valueChange(ValueChangeEvent event) {
String val = (String) event.getProperty().getValue();
FileResource res = new FileResource(new File(val));
Page.getCurrent().open(res, null, false);
}
}
The javadoc here mentions some memory and security problems as reason for marking it deprecated
In the second I try to go around this deprecated method by registering the resource in the DownloadComponent. I'd be glad if a vaadin expert comments this solution.
public class DownloadComponent extends CustomComponent implements ValueChangeListener {
private ComboBox cb = new ComboBox();
private static final String MYKEY = "download";
public DownloadComponent() {
cb.addValueChangeListener(this);
cb.setNewItemsAllowed(true);
cb.setImmediate(true);
cb.setNullSelectionAllowed(false);
setCompositionRoot(cb);
}
#Override
public void valueChange(ValueChangeEvent event) {
String val = (String) event.getProperty().getValue();
FileResource res = new FileResource(new File(val));
setResource(MYKEY, res);
ResourceReference rr = ResourceReference.create(res, this, MYKEY);
Page.getCurrent().open(rr.getURL(), null);
}
}
Note: I do not really allow the user to open all my files on the server and you should not do that either. It is just for demonstration.
Here is my work-around. It works like a charm for me. Hope it will help you.
Create a button and hide it by Css (NOT by code: button.setInvisible(false))
final Button downloadInvisibleButton = new Button();
downloadInvisibleButton.setId("DownloadButtonId");
downloadInvisibleButton.addStyleName("InvisibleButton");
In your theme, add this rule to hide the downloadInvisibleButton:
.InvisibleButton {
display: none;
}
When the user clicks on menuItem: extend the fileDownloader to the downloadInvisibleButton, then simulate the click on the downloadInvisibleButton by JavaScript.
menuBar.addItem("Download", new MenuBar.Command() {
#Override
public void menuSelected(MenuBar.MenuItem selectedItem) {
FileDownloader fileDownloader = new FileDownloader(...);
fileDownloader.extend(downloadInvisibleButton);
//Simulate the click on downloadInvisibleButton by JavaScript
Page.getCurrent().getJavaScript()
.execute("document.getElementById('DownloadButtonId').click();");
}
});

I ported my midlet to Blackberry and I can't assign a listener to the ESCAPE key

Here is some of the code in my midlet:
the addKeyListener method presents an error as the function is not recognized.
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Keypad;
public class PhraZApp extends javax.microedition.midlet.MIDlet implements ActionListener{
public PhraZApp {
addKeyListener (new KeyPadListener());
}
protected void keyPressed(int key) {
System.out.println(key);
}
public void actionPerformed(ActionEvent evt) {
System.out.println(evt.getKeyEvent());
}
public final class KeyPadListener implements KeyListener {
public boolean keyChar(char key, int status, int time) {
return false;
}
public boolean keyDown(int keycode, int time) {
if (Keypad.KEY_ESCAPE == Keypad.key(keycode)) {
System.out.println("key: " + keycode);
return true;
}
//let the system to pass the event to another listener.
return false;
}
public boolean keyUp(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean keyRepeat(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean keyStatus(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
The keyPressed action is not heard by any of those listeners.
Ive been told to add the keylistner to a GUI component, but none that I try it with accept it.
Furthermore, one of the possible issues is that the addKeyListener method is not declared, but in that case I don't know how to declare it.
If i change extends javax.microedition.midlet.MIDlet to extends UiApplication, the addKeyListener becomes accepted but the entire midlet falls to a RuntimeErrorException.
How can I get my Midlet to hear the escape key? I have searched through many forums and none of the suggestions have worked so far.
Thanks in advance.
You need to create a LWUIT Command and assign it to the parent form using the setBackCommand method. You can handle the command event like you handle every other command in LWUIT. E.g. through a command listener or even just by subclassing it and overriding actionPerformed(ActionEvent).
Thanks to Shai pointing me in the right direction, I solved it.
Here is how I did it.
Command backCommand = new Command("",Keypad.KEY_ESCAPE);
form.setBackCommand(backCommand);
then
public void actionPerformed(ActionEvent evt) {
if (evt.getCommand().getId() ==Keypad.KEY_ESCAPE){
//execution code
}
I didn't try, but if I had included text in the command I imagine it would appear as such when I push the menu button. The important thing is that I finally got the MIDlet to hear out the escape button after MANY hours of trying and searching for solutions.

How to silence an incoming call

I am trying to silence an incoming call and prevent the BlackBerry device from ringing. I tried Alert.setVolume(0) and some EventInjector keys but this didn't work.
So how to silence an incoming call?
I was puzzled by your question and decided to take up the challenge. I tried different thing including
Playing a "silence" audio file hoping to overlap the device's ringing or occupy the media player
Hacking the phone screen via UiApplication.getUiApplication().getActiveScreen()
Injecting keyboard events
Eventually, injecting VOLUME UP key (VOLUME DOWN key works as well) event worked for me and muted the device ringing on incoming call. The drawback with this approach is that sometimes the device did ring for a fraction of second before muting.
import net.rim.blackberry.api.phone.AbstractPhoneListener;
import net.rim.blackberry.api.phone.Phone;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.EventInjector;
import net.rim.device.api.ui.Keypad;
class Muter extends AbstractPhoneListener {
public void callIncoming(int callId) {
Thread muterThread = new Thread(new Runnable() {
public void run() {
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, (char) Keypad.KEY_VOLUME_UP, 0));
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_UP, (char) Keypad.KEY_VOLUME_UP, 0));
}
});
muterThread.setPriority(Thread.MAX_PRIORITY);
muterThread.start();
}
}
public class MuterApp extends Application {
public static void main(String[] args){
Phone.addPhoneListener(new Muter());
new MyApp().enterEventDispatcher();
}
}
The following also works (replace Muter thread in callIncoming() method with the following code).
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, (char) Keypad.KEY_VOLUME_UP, 0));
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_UP, (char) Keypad.KEY_VOLUME_UP, 0));
}
});
You won't be able to disable the sound programmatically (found a couple other sources that said the same thing). The best workaround people have seemed to come up with was to use the EventInjector to change the phone's sound profile to silent.
Some Blackberry phones have a mute key. You may try the following idea:
public void callIncoming(int callId) {
if (KeyPad.hasMuteKey()) {
/* Inject KEY_SPEAKERPHONE event */
}
else {
/* Inject KEY_VOLUME_DOWN event N times, so that you get the mute effect */
}
}
i am quite new to all this...but i thought i might as well put in my 2 cents worth...
i have been trying to find ways to programatically change the profile settings...
i have found that, while we cannot(yet) change the profile settings, we can change the setting that we are using( change the profile thats in use, i think)- this is something i came across searching for info-though i should give credit to alishaik786 for the code.
public final class LoadingScreen extends MainScreen implements FieldChangeListener
{
public LoadingScreen()
{
createGUI();
}
private void createGUI()
{
try
{
ApplicationManager.getApplicationManager().launch("net_rim_bb_profiles_app");
}
catch (Exception e)
{
//Exception
}
}
public void fieldChanged(Field field, int context)
{
}
}

Blackberry log file

Is there a method for creating a log file using a blackberry api ?
Something like log4j ?
I'd like to save this log file on pc running the emulator, is this possible ?
At Antair, during development of our BlackBerry applications, we often include a debug console in the dev builds of our apps.
With the debug console, all debug output hits the output screen when the dev build is running in the simulator, and when the dev build runs on a physical test device, the debug output is automatically persisted and is available to view on a dedicated screen that can be pulled up via a menu option or button. With a little code modification, you can easily have the debug log be rerouted to a file, emailed or sent over a network connection.
The code below is a stripped-down version of the debug console we use at our company.
Using the console is easy. Include the code in your project, fill out the PERSISTENCE_GUID for your application, set the TAGID string to identify your application name in the debug logs, and when you want to output a debug statement, simply call Debug.print(”Something happened here…“);
Each line of the debug output, both in the output window when running in a simulator, and in the debug console screen when viewed on a device, will contain your debug message, the thread number on which the call was made (useful for thread/ui debugging), and the date/time of the log statement, with a millisecond timestamp for performance profiling.
To view the debug console on a real device, simple put in a call to pushScreen(new AntairLogScreen()). The screen has a built-in menu item to clear the persisted log messages, and will dismiss itself like a regular application screen.
If you’re running the RIM compiler preprocessor to switch between development, QA, and production builds, you can simply put in a call to set Debug.ENABLED = false for everything but the development builds, and the debug console will be there when you need to debug and go away quietly when you don’t need it.
The code is below.
// ---------------------------------------------------------------------------
// Antair Debug Log (for the BlackBerry API)
// http://www.antair.com
// ---------------------------------------------------------------------------
package com.antair.examples.debug;
import net.rim.device.api.i18n.SimpleDateFormat;
import java.util.Date;
import net.rim.device.api.collection.util.BigVector;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.Persistable;
final class Debug implements Persistable
{
final static boolean ENABLED = true;
final static String TAGID = "MY_PROJECT";
final static long PERSISTENCE_GUID = /* YOUR OWN PERSISTENCE GUID */;
private BigVector _messages = new BigVector();
static String print(String str)
{
if ( Debug.ENABLED )
{
StringBuffer sb = new StringBuffer();
sb.append(TAGID);
sb.append("\n");
sb.append(Thread.currentThread().toString());
sb.append("\n");
sb.append(new SimpleDateFormat("MM/dd/yy HH:mm:ss:SSS").format(
new Date()));
sb.append("\n");
sb.append(str); sb.append("\n");
str = sb.toString();
System.out.println(str);
System.out.flush();
Debug d = load();
d._messages.addElement(str);
save(d);
}
return str;
}
static BigVector getPersistedMessages()
{
return load()._messages;
}
static void clearPersistedMessages()
{
save(new Debug());
}
private static Debug load()
{
Debug d = null;
try
{
PersistentObject po =
PersistentStore.getPersistentObject(Debug.PERSISTENCE_GUID);
synchronized(po)
{
Object obj = po.getContents();
d = (obj == null) ? new Debug() : (Debug)obj;
}
}
catch ( Exception e )
{
d = new Debug();
}
return d;
}
private static void save(Debug d)
{
try
{
PersistentObject po =
PersistentStore.getPersistentObject(Debug.PERSISTENCE_GUID);
synchronized(po)
{
po.setContents(d);
po.commit();
}
}
catch ( Exception e )
{
}
}
}
final class ClearAntairLogScreenMenuItem extends MenuItem
{
ClearAntairLogScreenMenuItem(int position)
{
super("Clear Log", position, 0);
}
public void run()
{
Debug.clearPersistedMessages();
}
}
final class AntairLogScreen extends MainScreen
{
AntairLogScreen()
{
super(MainScreen.DEFAULT_CLOSE|MainScreen.DEFAULT_MENU);
StringBuffer text = new StringBuffer();
BigVector logItems = Debug.getPersistedMessages();
for ( int i = 0 ; i < logItems.size() ; ++i )
{
text.append((String)logItems.elementAt(i) + "\n");
}
add(new RichTextField(text.toString()));
}
protected void makeMenu ( Menu menu, int instance )
{
menu.add(new ClearAntairLogScreenMenuItem(100000));
}
}
BlackBerry has its own log facility - EventLogger, but I find it to be ugly. You can write to EventLogger, but you can not view it as easily as you might with other logs or for example you can not extract the content programmatically to send over http/email.
I think it is much more comfortable to use your custom logger class that writes log entries directly to a log file on the SD card. Since the SD card on the emulator is a folder on your PC, you can easily monitor the app with any PC log viewer in real time.
Also as a bonus with such approach you can have some code to send the log over http/email. Such logging on a real device will slow down the app significantly, so it should not be normally used in production, but your app could have such option (to enable the debug mose) so it'll allow to know the reason which otherwise would remain unknown.

How to change the BlackBerry volume, or mute it?

My current code is this:
int volume = Alert.getVolume(); // reads 100
Alert.setVolume(0);
It DOESN'T change the volume setting, like it would be supposed to do
Even calling Alert.mute(true); doesn't produce any good effect.
Audio.setVolume(0); also doesn't work!
I am running this on a Curve 8310. I have another software installed though that successfully manages to lower the volume setting a lot. o I suppose I'm doing something wrong. Any idea ?
If you want to play sound with Alert:
class Scr extends MainScreen implements FieldChangeListener {
ButtonField mVolumeUp;
ButtonField mVolumeDown;
ButtonField mPlay;
LabelField mVolumeLabel;
int mVolumeValue = 50;
private static final short[] tune = new short[] { 466, 125, 10, 466 };
public Scr() {
mVolumeLabel = new LabelField("Volume: " + mVolumeValue);
add(mVolumeLabel);
mVolumeUp = new ButtonField("Vol Up", ButtonField.CONSUME_CLICK);
mVolumeUp.setChangeListener(this);
add(mVolumeUp);
mVolumeDown = new ButtonField("Vol Down", ButtonField.CONSUME_CLICK);
mVolumeDown.setChangeListener(this);
add(mVolumeDown);
mPlay = new ButtonField("Play", ButtonField.CONSUME_CLICK);
mPlay.setChangeListener(this);
add(mPlay);
}
public void fieldChanged(Field field, int context) {
if (mVolumeUp == field) {
if (mVolumeValue <= 90)
mVolumeValue += 10;
mVolumeLabel.setText("Volume: " + mVolumeValue);
} else if (mVolumeDown == field) {
if (mVolumeValue >= 10)
mVolumeValue -= 10;
mVolumeLabel.setText("Volume: " + mVolumeValue);
} else if (mPlay == field) {
Alert.startAudio(tune, mVolumeValue);
}
}
}
Tested on RIM 4.5 8310 simulator
If you're using the class javax.microedition.lcdui.Alert, that may be your problem.
Try taking a look at the net.rim.device.api.notification.NotificationsManager class and its other package classes/interfaces.
Though the simple/polite way is just to ask the user to change the user profiles manually. If I set my blackberry to silent and some application makes a crazy noise (or doesn't make a noise at all if I'm expecting an important call), I'll be removing that application asap.
Certain functions on the blackberry (but not the emulator) only work with signed code. I'm not sure if it is the case for volume, but I wouldn't be surprised when it was.

Resources