Blackberry Notification Message Issue - java

I am trying to make a notification message appear when I click the menu item, it shows me the indicator but I don't see the message, can someone explain where i did wrong :
private MenuItem AMenu = new MenuItem("Notify", 101, 10)
{
public void run()
{
ReadableListImpl mylist= new ReadableListImpl();
ApplicationMessageFolder folder = null;
if(ApplicationMessageFolderRegistry.getInstance().getApplicationFolder(0x33c7ce29883abe5fL)==null){
folder = ApplicationMessageFolderRegistry.getInstance().registerFolder(
0x33c7ce29883abe5fL, "Test Folder", mylist );
}else {
folder = ApplicationMessageFolderRegistry.getInstance().getApplicationFolder(0x33c7ce29883abe5fL);
}
//DemoMessage source is available in the messagelistdemo.
DemoMessage msg = new DemoMessage("me#here.com", "Pizza Toppings","What would you like on your pizza?", System.currentTimeMillis());
mylist.addMessage(msg);
folder.fireElementAdded(msg,true);
System.out.println("nr of messages"+folder.hasNewMessages());
ApplicationIndicatorRegistry reg =
ApplicationIndicatorRegistry.getInstance();
EncodedImage image = EncodedImage.getEncodedImageResource("new.png" );
ApplicationIcon icon = new ApplicationIcon( image );
ApplicationIndicator indicator = reg.register( icon, false, true);
ApplicationIndicator appIndicator = reg.getApplicationIndicator();
appIndicator.setIcon(icon);
appIndicator.setValue(appIndicator.getValue() + 1);
appIndicator.setNotificationState(true);
appIndicator.setVisible(true);;
}
};

I noticed two things looking at your code:
First, you create a new ReadableListImpl each time the menu item gets invoked. This means the ReadableListImpl instance you add the message to is not always the same as the one that was used when registering the folder. So your code should work on first invocation but not on subsequent ones.
Second, with BB OS 6 a message can appear in two places, the home screen (notification bar) and the message list (the 'Messages' app). It might be possible that your message did actually show up in the message list but not in the notification bar. From my experience messages show up in the notification bar only if the message's status is ApplicationMessage.Status.UNOPENED.
Use ApplicationFolderIntegrationConfig if you want to have control over where your message should show up.

Related

How to get button press event from camera

I've got a dental camera and iam try to get windows to press space when the camera button is pressed
I have the OEM software and driver installed, it works perfect, gets the feed and makes a snapshot when camera button is pressed. I need to use another software for the feed and the snapshot, the software gets the feed but doesn't react to camera button, it only reacts to space key press(part of the oem driver), so my way of solving this was getting the device by product id and listening the button press event and remapping it space press.
I am pretty much stuck at this point.
How can I listen on events coming from the device I've got?
public static Device findDCam(){
// Create the libusb context
Context context = new Context();
// Initialize the libusb context
int result = LibUsb.init(context);
if (result < 0)
{
throw new LibUsbException("Unable to initialize libusb", result);
}
// Read the USB device list
DeviceList list = new DeviceList();
result = LibUsb.getDeviceList(context, list);
if (result < 0)
{
throw new LibUsbException("Unable to get device list", result);
}
try
{
// Iterate over all devices and list them
for (Device device: list)
{
DeviceDescriptor descriptor = new DeviceDescriptor();
result = LibUsb.getDeviceDescriptor(device, descriptor);
if (result < 0)
{
throw new LibUsbException(
"Unable to read device descriptor", result);
}
if(descriptor.idProduct()== -3810){
System.out.println("D cam found");
return device;
}
}
}
finally
{
// Ensure the allocated device list is freed
LibUsb.freeDeviceList(list, true);
}
// Deinitialize the libusb context
LibUsb.exit(context);
return null;
}
I've also thought that maybe it's impossible using usb4java since as far as i understood, if i want to listen on the usb port i need to take control from the driver and then its pointless.
Maybe iam going all wrong and i should use the driver instead?
Or maybe there is an app that can read button presses from a specific device and remap it?
If the camera has a standard driver, this should work through this video capture SDK. To quick test it, run the demo executable included in the package, select the camera in the list, check the "webcam snapshot button" checkbox and start the camera. Then press the camera button to test the snapshot.

Telegram BOT JAVA

I have a bot with inline keyboards.
The bot has a text with a button to make an order. After pressing the bot asks the name of the person and below there is a button to cancel the operation.
If I enter the name of the person, I rightly load the new text with the new buttons, but the old cancel button is not deleted and remains visible.
I would like to be able to remove the button after writing the text.
this code is for insert the name:
String answer = "Insert the Name";
EditMessageText new_message = new EditMessageText()
.setChatId(chat_id)
.setMessageId(message_id)
.setText(answer).setParseMode("HTML");
markupInline = new InlineKeyboardMarkup();
List<List<InlineKeyboardButton>> bottoni_totali = new ArrayList<>();
List<InlineKeyboardButton> riga1 = new ArrayList<>();
riga1.add(createButton("annulla", emoji_annulla+" Annulla"));
bottoni_totali.add(riga1);
// Add it to the message
markupInline.setKeyboard(bottoni_totali);
new_message.setReplyMarkup(markupInline);
try {
execute(new_message);
} catch (TelegramApiException e) {
e.printStackTrace();
}
When I insert the name, I show the surname but the old button CANCEL is visible.
String answer = "Now insert the surname";
sendMessage = new SendMessage().setChatId(update.getMessage().getChatId());
sendMessage.setText(answer).setParseMode("HTML");
markupInline = new InlineKeyboardMarkup();
List<List<InlineKeyboardButton>> bottoni_totali = new ArrayList<>();
List<InlineKeyboardButton> riga1 = new ArrayList<>();
riga1.add(createButton("annulla", emoji_annulla+" Annulla"));
bottoni_totali.add(riga1);
// Add it to the message
markupInline.setKeyboard(bottoni_totali);
sendMessage.setReplyMarkup(markupInline);
try {
execute(sendMessage);
} catch (TelegramApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Firstly I would say about this link:
How to hide ReplyKeyboardMarkup after user click in Telegram Bot API
Question above is about InlineKeyboardMarkup and question by link is about ReplyKeyboardMarkup. These two are different things, don't mix them.
Secondly, you have to clarify your question.
but the old cancel button is not deleted and remains visible
What do you mean by that? (screenshots is the best way to explain)
In second snippet of code you use SendMessage instead of EditMessageText (as in first snippet). It means that message with text Now insert the surname will not replace message with text Insert the Name. Now insert the surname will appear as new message.
Seems like you should use EditMessageText in second case too.

Java : Determine if String is an URL(without www or http), take its screenshot

I am working on a Spring-MVC webapplication in which we are trying to get a screenshot of an URL. Currently I am using PhantomJS for that task, but it's too slow(>10seconds). Also, the URL's have to be with http/https and www for detecting that it's an URL. As this is a chat application, there can be simple URL's which users add like helloworld.com. Any help would be nice. Thank you.
Code:
String[] words = message.split(" ");
for( String item : words ){
boolean val = ResourceUtils.isUrl(item);
if(val){
urlIdentifier = calcUrl(item);
break;
}else {
System.out.println("Url false is "+item);
}
}
if(urlIdentifier!=null){
replies.setPreviewIdentifier(urlIdentifier);
input.put("preview_identifier",urlIdentifier);
}
Method to calculate screenshot :
private String calcUrl(String website){
try {
String identifier = String.valueOf(new BigInteger(130, random).toString(32));
String previewLocation = msg + "chatthumbs/" + identifier ;
Process proc = Runtime.getRuntime().exec("phantomjs --ssl-protocol=any " +
"/home/deploy/phantom/rasterizepdf.js " +" "+website+" " +previewLocation);
proc.waitFor();
BufferedImage image = ImageIO.read(new File("/home/akshay/testme.png"));
if(image!=null){
if (image.getWidth() > image.getHeight()) {
image = Scalr.resize(image, Scalr.Mode.FIT_TO_HEIGHT, 250, 250);
} else {
image = Scalr.resize(image, Scalr.Mode.FIT_TO_WIDTH, 250, 250);
}
image = Scalr.crop(image, 250, 250);
ImageIO.write(image, "png", new File(previewLocation));
}
return identifier;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
Any help would be nice. Thank you.
(a) I think the process of taking a screen shot is taking time. Is this code running on the same device as the chat screen? Why not use java.awt.Robot to take the screen shot ? or just save the text why do you need a screen shot?
(b) Is the system too busy/ Use on a SSD to see if faster?
(c) But curious as to the end application, is this part of a web app? How will your code run on the client systems? Or will you java agent be installed on all user systems that monitors the web page and takes screen shots? Then why use a web page, use a java app to chat, and parse the text as typed.
Parsing the text. What if user types/ pastes a long message? Are you parsing everything once or on change? Think of ways to improve that if it seems to be a problem. Ignore if not the immediate issue now.
Also if the msg is too long the parsing can take a lot of time. maybe process after every key press or change event (if paste) keep local js copy of previous text to get diff?

What is the best way to attach multiple images?

Several of my timeline item designs require multiple images, yet I am having difficulty attaching them all reliably. The timeline.insert function only seems to allow for one attachment and inserting attachments after the timeline item is inserted sometimes results in the images not being rendered.
I also tried using setAttachments on the timeline item itself but it didn't seem to actually upload the attachments when inserting the item. Using the code below I tend to get mixed results. Sometimes it works and other times it fails to render the image. There seems to be a correlation with how long I wait to view the notification after receiving it, if I view it too quickly it never fully renders.
Does anyone have any thoughts or suggestions on how I could overcome this or see anything I'm doing wrong?
//CardFactory.java - Create TimelineItem with attachment list
public static TimelineItem getConceptCard(String conceptImage) {
TimelineItem timelineItem = new TimelineItem();
timelineItem.setHtml("<article class=\"photo\">\n <img src=\"attachment:0\" width=\"100%\" height=\"100%\">\n <div class=\"photo-overlay\"/>\n <section>\n <p class=\"text-auto-size\">Test</p>\n </section>\n</article>\n");
List<Attachment> attachments = new ArrayList<Attachment>();
Attachment img1 = new Attachment();
img1.setId("backImage");
img1.setContentType("image/jpeg");
img1.setContentUrl(WebUtil.buildStaticImgUrl("cardconcepts/" + conceptImage + ".JPG"));
attachments.add(img1);
timelineItem.setAttachments(attachments);
timelineItem.setNotification(new NotificationConfig().setLevel("DEFAULT"));
return timelineItem;
}
//MainServlet.java - Send TimelineItem on button press
} else if (req.getParameter("operation").equals("insertConceptCard")) {
TimelineItem timelineItem = CardFactory.getConceptCard(req.getParameter("conceptCard"));
MirrorClient.insertTimelineCard(credential, timelineItem);
//MirrorClient.java - Insert TimelineItem with multiple attachments
public static void insertTimelineCard(Credential credential, TimelineItem item) throws IOException {
Mirror.Timeline timeline = getMirror(credential).timeline();
TimelineItem timelineItem = timeline.insert(item).execute();
for(Attachment TAttach : item.getAttachments()){
InputStreamContent mediaContent = new InputStreamContent(TAttach.getContentType(), new URL(TAttach.getContentUrl()).openStream());
timeline.attachments().insert(timelineItem.getId(), mediaContent).execute();
}
I am not sure if it is possible given your requirements, but if the attachments are public images, you don't actually need to attach them. You can use the img tag with a normal http URL. My experience has been that these get fetched fairly quickly, are cached if you use them frequently, and render correctly even if they don't render immediately.
(Even if your requirements need to keep these more private, you may wish to use standard image fetching with some kind of nonce instead of trying to attach them. I realize this doesn't quite answer your question, but it may be a useful workaround.)

How do I send bundled cards all at the same time?

I've set up a Java application where I'm creating a bundle of 4 cards. The problem is that all the cards do not come in at once. Some times just one shows up, then a few seconds or minute later the other cards show up. How do I get them to all show up on the headset at the same time?
edit:
I tried HTML paging and that didn't work and now I think I'm more confused. So in my senario here I want to send a bunch of landmarks to the user that they can navigate to. I want all the landmarks in a bundle, I want a cover to the bundle that isn't an option in the bundle saying "here are your landmarks", and I'd like the bundle to get to the user all at the same time. How can I achieve this?
TimelineItem timelineItemEmpire = new TimelineItem();
timelineItemEmpire.setText("Empire State Building");
// Triggers an audible tone when the timeline item is received
timelineItemEmpire.setNotification(new NotificationConfig().setLevel("DEFAULT"));
Location empireLoc = new Location();
empireLoc.setLatitude(40.748492);
empireLoc.setLongitude(-73.985868);
timelineItemEmpire.setLocation(empireLoc);
// Attach an image, if we have one
URL url = new URL(WebUtil.buildUrl(req, "/static/images/empirestate.jpg"));
timelineItemEmpire.setBundleId(bundleId);
List<MenuItem> menuItemList = new ArrayList<MenuItem>();
menuItemList.add(new MenuItem().setAction("NAVIGATE"));
timelineItemEmpire.setMenuItems(menuItemList);
MirrorClient.insertTimelineItem(credential, timelineItemEmpire, contentType, url.openStream());
TimelineItem timelineItemCP = new TimelineItem();
timelineItemCP.setText("Central Park");
// Triggers an audible tone when the timeline item is received
timelineItemCP.setNotification(new NotificationConfig().setLevel("DEFAULT"));
// Attach an image, if we have one
URL url3 = new URL(WebUtil.buildUrl(req, "/static/images/central_park.jpg"));
timelineItemCP.setBundleId(bundleId);
Location cpLoc = new Location();
cpLoc.setLatitude(40.772263);
cpLoc.setLongitude(-73.974488);
timelineItemCP.setLocation(cpLoc);
timelineItemCP.setMenuItems(menuItemList);
MirrorClient.insertTimelineItem(credential, timelineItemCP, contentType, url3.openStream());
TimelineItem timelineCover = new TimelineItem();
timelineCover.setText("Nearby Landmarks");
timelineCover.setBundleId(bundleId);
// Triggers an audible tone when the timeline item is received
timelineCover.setNotification(new NotificationConfig().setLevel("DEFAULT"));
// Attach an image, if we have one
URL url4 = new URL(WebUtil.buildUrl(req, "/static/images/bundle_cover.jpg"));
MirrorClient.insertTimelineItem(credential, timelineCover, contentType, url4.openStream());
You need to set the isBundleCover resource to true for your cover; i.e.:
timelineCover.setIsBundleCover(true);
This will make it the entry point into the bundle, and prevent it from being displayed within the bundle, as described here.
Furthermore, you can use BatchRequest to make sure they're sent together; e.g.,:
BatchRequest batch = MirrorClient.getMirror(null).batch();
BatchCallback callback = new BatchCallback();
for (TimelineItem item : items) {
MirrorClient.getMirror(userCredential).timeline().insert(item).queue(batch, callback);
}
batch.execute();

Categories

Resources