Gluon position service is taking too long to display the coordinates - gps

I tried the following code, it's working, but making the first click on the button after launching the app, the message with the coordinates show right when the button is clicked, but for the next clicks, the message takes long time unfortunately to display the message.
My code is as follows:
button.setOnAction(e->{
PositionService positionService = Services.get(PositionService.class).orElseThrow(() -> new RuntimeException("PositionService not available."));
positionService.positionProperty().addListener((obs, ov, nv) -> MobileApplication.getInstance().showMessage("Latest known GPS coordinates from device: " + nv.getLatitude() + ", " + nv.getLongitude()));
});
Thanks in advance.

Let's say you have a single view project created with the Gluon plugin for your IDE, and you have added the required location service and the permission to the Android manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
In the BasicView you can do for instance:
public BasicView(String name) {
super(name);
Label label = new Label("GPS is not active");
Button button = new Button("Active GPS");
button.setGraphic(new Icon(MaterialDesignIcon.LOCATION_ON));
button.setOnAction(e -> {
Services.get(PositionService.class).ifPresent(service -> {
// if there is GPS service, disable button to avoid adding more
// listeners
button.setDisable(true);
label.setText("Waiting for GPS signal");
service.positionProperty().addListener((obs, ov, nv) ->
label.setText("Latest known GPS coordinates:\n" + nv.getLatitude() + ", " + nv.getLongitude()));
});
});
VBox controls = new VBox(15.0, label, button);
controls.setAlignment(Pos.CENTER);
setCenter(controls);
}
Or you can initialize the service when you create the view, without the need to activate it:
public BasicView(String name) {
super(name);
Label label = new Label("GPS is not active");
Services.get(PositionService.class).ifPresent(service -> {
label.setText("Waiting for GPS signal");
service.positionProperty().addListener((obs, ov, nv) ->
label.setText("Latest known GPS coordinates:\n" + nv.getLatitude() + ", " + nv.getLongitude()));
});
}
Either way, you can deploy the app to your Android/iOS device and test.
EDIT
Optionally, if you want to include a button, so you can click on it as many time as you want.
public BasicView(String name) {
super(name);
Label label = new Label("GPS is not active");
Button button = new Button("Get GPS coordinates");
button.setGraphic(new Icon(MaterialDesignIcon.LOCATION_ON));
button.setDisable(true);
Services.get(PositionService.class).ifPresent(service -> {
label.setText("Waiting for GPS signal");
service.positionProperty().addListener((obs, ov, nv) -> {
label.setText("Latest known GPS coordinates:\n" + nv.getLatitude() + ", " + nv.getLongitude());
});
// enable button and add listener to retrieve coordinates
button.setDisable(false);
button.setOnAction(e -> {
Position position = service.getPosition();
MobileApplication.getInstance().showMessage("Latest known GPS coordinates from device:\n" +
position.getLatitude() + ", " + position.getLongitude());
});
});
VBox controls = new VBox(15.0, label, button);
controls.setAlignment(Pos.CENTER);
setCenter(controls);
}
Note that when clicking the button you should use service.getPosition().

Related

How to create a camera view + a button to capture the photo in Xamarin.Forms?

I am trying to find a solution for an android app which I can have both camera preview and a button created with Xamarin Forms (XAML) that when I click that button photo should automatically save in device gallery. After 2 days of research the only best solution I found is this. Can someone please help me achieve this?
Refer to official sample , it implements creating camera preview , i just add the function of take picture and save into gallery .Call messaging center in forms project and get camera instance in android project ,
Call back of Picture Taken
public void OnPictureTaken(byte[] data, Camera camera)
{
camera.StopPreview();
FileOutputStream outStream = null;
Java.IO.File dataDir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
if (data != null)
{
try
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
var s = ts.TotalMilliseconds;
outStream = new FileOutputStream(dataDir + "/" + s + ".jpg");
outStream.Write(data);
outStream.Close();
}
catch (Java.IO.FileNotFoundException e)
{
System.Console.Out.WriteLine(e.Message);
}
catch (Java.IO.IOException ie)
{
System.Console.Out.WriteLine(ie.Message);
}
}
camera.StartPreview();
}
Check my sample on github.

How to handle the click on the sticky header decor in a RecyclerView?

I have a RecyclerView with a StickyHeaderDecor (with Button, ImageView, and TextView inside).
How do I handle the clicks on these components within the StickyHeader?
The library used is UltimateRecyclerView.
This is the code where I setup my recyclerView:
StickyRecyclerHeadersDecoration headersDecoration =
new StickyRecyclerHeadersDecoration(adapter);
recyclerView.addItemDecoration(headersDecoration);
StickyRecyclerHeadersTouchListener headersTouchListener =
new StickyRecyclerHeadersTouchListener(recyclerView, headersDecoration);
headersTouchListener.setOnHeaderClickListener(new StickyRecyclerHeadersTouchListener.OnHeaderClickListener() {
#Override
public void onHeaderClick(View headerView, int position, long headerId) {
Log.d(TAG, "clicked view " + v.getId() + " position:" + position);
// my code here to handle click (*)
}
});
recyclerView.addOnItemTouchListener(headersTouchListener);
(*) I don't have the possibility to handle click on headerView.
Unfortunately it is not possible to easy handle click of part of item decor. Here is an explanation why.
For Sticky Headers is better use:
FlexibleAdapter
SuperSlim

How to handle a new window pop up with selenium

I'm currently automating an application using selenium ,and when a button is clicked a new pop up window appears.I have to switch to that window and do certain operation such as searching of record.
Note The Parent window and child window have same title .
You can use this for which you have to provide the index of the window you want to switch
switchToWindowByIndex(1);
public void switchToWindowByIndex(int wndIndex) {
Set<String> handles = Driver.getWindowHandles();
if (handles.size() > wndIndex) {
String handle = handles.toArray()[wndIndex].toString();
Driver.switchTo().window(handle);
} else {
throw new RuntimeException("There are only [" + handles.size() + "] windows present at the moment.Requested window is [" + wndIndex + "] which is out of range");
}
}

Can't get mouse event from any other javafx 8 node after getting MOUSE_PRESSED event from one node

I'm creating rich text component with selection capabilities for JavaFX project and facing some difficulties.
I'm trying to catch on which TextFlow object user presses mouse button and on which another TextFlow he releases it. But after MOUSE_PRESSED event i can interact only with that TextFlow, who fired it, until i release the mouse.
Here is similar example with Labels:
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
AnchorPane root = new AnchorPane();
primaryStage.setTitle("Events Problem Example");
primaryStage.setScene(new Scene(root, 800, 600));
VBox mainVB = new VBox();
root.getChildren().add(mainVB);
//########## Code is here:
for (int i = 0; i < 5; i++) {
final Label label = new Label("labelâ„–"+i);
mainVB.getChildren().addAll(label);
label.setOnMouseEntered(mouseEvent -> System.out.println("entering " + label.getText()));
label.setOnMousePressed(mouseEvent -> System.out.println("press mouse button on " + label.getText()));
label.setOnMouseReleased(mouseEvent -> System.out.println("release mouse button on " + label.getText()));
}
//########################
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Try to move mouse over different Labels and watch messages in command line. After that press and hold mouse primary button on any Label and move it again. You'll see that no other Labels will fire any event until you releases the button.
I spend some time searching for the solution but got nothing.
I also tried to manually fire MOUSE_RELEASED for corresponding Label but it didn't help also.
Appreciate your support.
The documentation for MouseEvent details three different modes for handling mouse drag. In the default mode ("simple press-drag-release gesture"), as you've observed, mouse events are delivered only to the node on which the gesture originated.
In "full press-drag-release gesture" mode, MouseDragEvents are delivered to other nodes during the drag. This is the mode you need, and you activate it by calling startFullDrag on the originating node.
(The third mode is "drag-and-drop" gesture, which is for transferring data between nodes and is typically supported by the underlying platform, so you can drag and drop between your JavaFX application and other applications, as well as within the application.)
Try the following code for your event handlers:
label.setOnDragDetected(mouseEvent -> label.startFullDrag());
label.setOnMouseDragEntered(mouseEvent -> System.out.println("entering " + label.getText()));
label.setOnMouseDragReleased(mouseEvent -> System.out.println("release mouse button on " + label.getText()));

windows 8 metro app - toast notification

I am developing a Windows 8 metro-style application using toast notification. (C# + xaml combination)
I looked into MS metro style sample code and tried to apply it to my project,
looks like I used the code exactly the same way, but I don't know why it is not working..
(There is no error, it builds successfully but just doesn't work.)
What I'm trying to do is very simple:
There is a button.
When the button_click event occurs, I'd like to pop a toast notification.
This is what I did:
namespace Application1
{
public sealed partial class BlankPage : Page
{
public BlankPage()
{
this.InitializeComponent();
Scenario2Init();
}
void Scenario2Init()
{
toastTest.Click += (sender, e) => { ToastAlarm(true); };
}
void ToastAlarm(bool loopAudio)
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
// Toasts can optionally be set to long duration by adding the 'duration' attribute
IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("duration", "long");
// This XmlNodeList will have two items since the template we are using has two text fields.
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements.Item(0).AppendChild(toastXml.CreateTextNode("Long Duration Toast"));
XmlElement audioElement = toastXml.CreateElement("audio");
if (loopAudio)
{
// Long-duration Toasts can optionally loop audio using the 'loop' attribute
audioElement.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Alarm");
audioElement.SetAttribute("loop", "true");
stringElements.Item(1).AppendChild(toastXml.CreateTextNode("Looping audio"));
}
else
{
audioElement.SetAttribute("src", "ms-winsoundevent:Notification.IM");
}
toastNode.AppendChild(audioElement);
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
//Scenario2OutputText.Text = toastXml.GetXml();
}
}
}
If I click the button, nothing happens. Why?
Your code looks correct to me; I don't have Win8 with me here right now so I can't test it. However, you may want to check your app's manifest if you enabled Toast or not in the "Toast Capable" field in VS. Hope this helps.
Did you enable "Toast capable" in Package.appxmanifest?
I think, there are two reasons,
First may be relating to toast capability of your application. For this set ToastCapable="true" in your Package.appxmanifest
Second one is run application in Local Machine rather than Simulator. I found that Simulator is not able to produce Toast notification.
i think you can just use Xml String
// Create the toast content by direct string manipulation.
// See the Toasts SDK Sample for other ways of generating toasts.
string toastXmlString =
"<toast duration=\"long\">\n" +
"<visual>\n" +
"<binding template=\"ToastText02\">\n" +
"<text id=\"1\">Alarms Notifications SDK Sample App</text>\n" +
"<text id=\"2\">" + alarmName + "</text>\n" +
"</binding>\n" +
"</visual>\n" +
"<commands scenario=\"alarm\">\n" +
"<command id=\"snooze\"/>\n" +
"<command id=\"dismiss\"/>\n" +
"</commands>\n" +
"<audio src=\"ms-winsoundevent:Notification.Looping.Alarm5\" loop=\"true\" />\n" +
"</toast>\n";