GWT Multi-threaded effect - gwt

I know GWT is single threaded by nature, however I am trying to user deferred command to emulate a multi-threaded effect. I am trying to implement a text animation which is controlled by the isBusy boolean variable, which the text manipulation (animation effect) runs unti isBusy gets false value, when the server responded already. However this code seems to get stuck with the while-loop?
Here is the code:
loginButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
isBusy = true; // boolean
// Show animating effect...
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
while(isBusy == true) {
for (int i=0;i<3;i++) {
if (i==0) { loginButton.setText("."); }
if (i==1) { loginButton.setText(".."); }
if (i==2) { loginButton.setText("..."); }
}
}
}
});
loginService.getUser(usernameBox.getText(), passwordBox.getText(), new AsyncCallback<User>() {
#Override
public void onSuccess(User result) {
isBusy = false;
}
#Override
public void onFailure(Throwable caught) {
isBusy = false;
}
});
}
});

Scheduler was the right tool, but you are using the wrong command. You have to use scheduleFixedPeriod insdead of scheduleDeferred. Here is a small example which showes hopefully helps you.
#Override
public void onModuleLoad() {
final Label l = new Label("");
RootPanel.get().add(l);
Scheduler.get().scheduleFixedPeriod(new RepeatingCommand() {
#Override
public boolean execute() {
int i = l.getText().length();
if (i == 0) {
l.setText(".");
}
else if (i == 1) {
l.setText("..");
}
else if (i == 2) {
l.setText("...");
}
else if (i == 3) {
l.setText("");
}
return true; //as long as it returns true the execute is repealed after XX milliseconds
}
}, 200);
}

Related

Passing ExoPlayer instance from activity to binded service?

I'm trying to make a Video Player using ExoPlayer that can also work in the background and you can control it through the notification. I already created the ExoPlayer and the ForeGround Service for the notification and i binded them. At the moment it works as intended, the only problem is that i don't want the activity player to stop working when i close the notification. This happens because at the moment i'm creating the ExoPlayer instance in the service and then i pass the instance to the Activity, so when i close the notification the instance gets lost. Is there a way to initialize the player instance in the Activity and then pass that instance to the service so i can still control the video from the notification without risking to lose the instance once the notification is closed?
I'm pretty new to android and this is the first time that i'm binding a service to an activity so i don't really know how to do it. I tried searching on Google but that didn't help either.
This is the Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playerView=findViewById(R.id.player_view);
intent=new Intent(this,AudioPlayerService.class);
//here i will add the url that needs to be loaded
//but at the moment this is just a draft
Util.startForegroundService(this,intent);
playerView.setUseController(true);
//playerView.showController();
playerView.setControllerAutoShow(true);
playerView.setControllerHideOnTouch(true);
}
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
AudioPlayerService.LocalBinder binder = (AudioPlayerService.LocalBinder) iBinder;
mService = binder.getService();
mBound = true;
initializePlayer();
}
#Override
public void onServiceDisconnected(ComponentName componentName) {
mBound = false;
}
};
#Override
public void onStart() {
super.onStart();
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
initializePlayer();
}
#Override
protected void onStop() {
unbindService(mConnection);
mBound = false;
super.onStop();
}
private void releasePlayer() {
if (player != null) {
player.release();
player = null;
}
}
private void initializePlayer() {
if (mBound) {
SimpleExoPlayer player = mService.getplayerInstance();
playerView.setPlayer(player);
}
}
}
This is the Service
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public SimpleExoPlayer getplayerInstance() {
if (player == null) {
startPlayer();
}
return player;
}
public class LocalBinder extends Binder {
public AudioPlayerService getService() {
return AudioPlayerService.this;
}
}
#Override
public void onCreate() {
super.onCreate();
final Context context=this;
}
private void startPlayer() {
final Context context = this;
player = ExoPlayerFactory.newSimpleInstance(context, new DefaultTrackSelector());
ProgressiveMediaSource mediaSource = new ProgressiveMediaSource.Factory(new DefaultHttpDataSourceFactory("NotificationSync", 10000, 10000, true))
.createMediaSource(Uri.parse("https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"));
player.prepare(mediaSource);
player.setPlayWhenReady(true);
playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(context, "1",
R.string.app_name,
2,
new PlayerNotificationManager.MediaDescriptionAdapter() {
#Override
public String getCurrentContentTitle(Player player) {
return "title";
}
#Nullable
#Override
public PendingIntent createCurrentContentIntent(Player player) {
Intent intent = new Intent(context, MainActivity.class);
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
#Nullable
#Override
public String getCurrentContentText(Player player) {
return "text";
}
#Nullable
#Override
public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
return null;
}
}, new PlayerNotificationManager.NotificationListener() {
#Override
public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
stopSelf();
}
#Override
public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
mNotification = notification;
mNotificationId = notificationId;
if (ongoing) {
startForeground(notificationId, notification);
}
}
}
);
playerNotificationManager.setPlayer(player);
playerNotificationManager.setUseStopAction(true);
}
#Override
public void onDestroy() {
releasePlayer();
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (player == null) {
//here i will get all the data from the intent that came from the
//activity (title,text,url...)
startPlayer();
}
return START_STICKY;
}
private void releasePlayer() {
if (player != null) {
player.release();
player = null;
}
}
}
In the end all i want to achieve is a video player that you start in the activity and also works/can be controlled through a notification without interruption when i go from activity->background and background->activity. If there are other ways to achieve this i'm open to try.

i want after editing of tableview cell textfield ,when i type i want auto suggestion of word

this code contain basically how to edit the textfield of tableview column ,like tableview column is in textfield format and i need to edit it and when i will type i need auto suggestion but my auto suggestion code is not working ,so can anyone suggest me or help to overcome my auto suggestion of word problem with the same procedure ,in createfield after commitedit i called learnword function as well but that is not happening.
class EditingCell extends TableCell<File, String> {
Set<String> possibleWordSet= new HashSet<>();
private AutoCompletionBinding<String> autoCompletionBinding;
public TextField textField;
//private Object possibleWordSet;
#Override
public void startEdit() {
if (!isEmpty()) {
super.startEdit();
createTextField();
setText(null);
setGraphic(textField);
System.out.println(textField.getCaretPosition() + "caret position");
}
}
#Override
public void cancelEdit() {
super.cancelEdit();
setText(getItem());
setGraphic(null);
}
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
System.out.println("I am in update function");
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
if (textField != null) {
textField.setText(item);
}
setText(null);
setGraphic(textField);
} else {
setText(item);
setGraphic(null);
}
}
}
// Instantiates the text field.
public void createTextField() {
textField = new TextField(getItem());
System.out.println("hello i am inside the createtextfield");
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
autoCompletionBinding = TextFields.bindAutoCompletion(textField, possibleWordSet); /tion/for auto suuges
textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
commitEdit(textField.getText());//on enter event editing of textfield
learnword(textField.getText());//for auto suggestion call learnword method.
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
});
textField.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
int caretPosition = textField.getCaretPosition();
GUIFXMLDocumentController fx = new GUIFXMLDocumentController();
fx.setCaretPos(caretPosition);
System.out.println(caretPosition + "caretPosition");
fx.setTextField(textField);
}
});
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
#Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) {
if (!arg2) {
System.out.println(textField.getText() + "Inside changeListener------------------");
GUIFXMLDocumentController.tv_new.getFocusModel().focusNext();
commitEdit(textField.getText());
learnword(textField.getText());
}
}
});
}
public void learnword(String text) {
possibleWordSet.add(text);
if(autoCompletionBinding!=null){
autoCompletionBinding.dispose();
}
autoCompletionBinding = TextFields.bindAutoCompletion(textField, possibleWordSet);
}
}
enter code here

runOnUiThread not working properly

I am trying to create textview in android which display the remaining time(seconds).
Have used runOnUiThread and Hadnler for this and everything seems working fine(sysout and debug shows that both threads are executed and value is updated properly).
However, on UI the textview value is not updated properly. It gets updated with last value when the thread completes.
I am writing the below code inside private method of the fragment.
final TextView timerText = (TextView) mainView.findViewById(R.id.timerText);
timerText.setText(R.string.maxAllowedSeconds);
handler.post(new Runnable() {
#Override
public void run() {
while(true)
{
System.out.println("Text:"+ ""+maxAllowedSeconds);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
maxAllowedSeconds--;
if(maxAllowedSeconds <= 0)
break;
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
System.out.println("Running on UI Thread : " + maxAllowedSeconds);
timerText.setText("" + maxAllowedSeconds);
}
});
}
}
});
Gone through many of the previous questions in this area but none seems to have concrete solutions for this problem.
Thanks in advance.
SOLUTOIN:
Finally I used AsynchTask which worked perfectly as expected.
private class RapidFireAsyncTimerTask extends AsyncTask<Integer, Integer, Void> {
private Context context;
private View rootView;
public RapidFireAsyncTimerTask(Context ctx, View rootView) {
this.context = ctx;
this.rootView = rootView;
}
#Override
protected Void doInBackground(Integer... params) {
int maxSec= params[0];
while (true) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(--maxSec);
if (maxSec <= 0)
break;
}
return null;
}
#Override
protected void onProgressUpdate(final Integer... values) {
((TextView) rootView.findViewById(R.id.timerText)).setText("" + values[0]);
}
#Override
protected void onPostExecute(Void aVoid) {
//next task
}
}
Instead of Handler, it worked with AsynchTask(No runOnUiThread needed).
public RapidFireAsyncTimerTask(Context ctx, View rootView) {
this.context = ctx;
this.rootView = rootView;
}
#Override
protected Void doInBackground(Integer... params) {
int maxSec= params[0];
while (true) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(--maxSec);
if (maxSec <= 0)
break;
}
return null;
}
#Override
protected void onProgressUpdate(final Integer... values) {
((TextView) rootView.findViewById(R.id.timerText)).setText("" + values[0]);
}
#Override
protected void onPostExecute(Void aVoid) {
//next task
}
}

Items does not sets disabled after first click on ListBox in Chrome

Here is my code:
listbox.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
serv.getSlotStatusesStrings(clientFactory.getWorkspaceId(), new AsyncCallback<SlotStatusGwtStruct>() {
#Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
#Override
public void onSuccess(SlotStatusGwtStruct result) {
List<String> statusList = Arrays.asList(result.styleName);
int emtyCounter = 0;
boolean passFlag = false;
boolean failFlag = false;
for(String status : statusList){
if(status.equals("empty")){
emtyCounter++;
}else if(status.equals("pass")){
passFlag = true;
}else if(status.equals("fail")){
failFlag = true;
}
}
if(emtyCounter == statusList.size()){
listbox.getElement().<SelectElement>cast().getOptions().getItem(0).setDisabled(true);
}
if(passFlag == false){
listbox.getElement().<SelectElement>cast().getOptions().getItem(1).setDisabled(true);
}
if(failFlag == false){
listbox.getElement().<SelectElement>cast().getOptions().getItem(2).setDisabled(true);
}
}
});
}
});
}
In Firefox it works ok, but in Chrome browser when I click on listbox at the first time all my items are enabled (by condition they should be disabled), and after I made one more click I have the expected result.
Could yo please give me some advice how to resolve this issue.

Google Maps API v3 - Buttons and TextBoxes inside InfoWindow?

I'm using the new maps v3 API from gwt-google-apis.
Is it possible to capture events from GWT widgets that are inside InfoWindow? Am I missing something?
Tried code above (button.addClickHandler) and it doesn't show the alert:
Marker m = Marker.create();
m.setIcon(MarkerImage.create(icone));
m.setPosition(LatLng.create(posicao.lat(), posicao.lng()));
m.setMap(map);
m.addClickHandler(new ClickHandler() {
#Override
public void handle(MouseEvent event) {
InfoWindow info = InfoWindow.create();
Button button = new Button("Desativar");
button.addClickHandler(new com.google.gwt.event.dom.client.ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Window.alert("click");
}
});
final HTMLPanel html = new HTMLPanel(("<div id='button'></div>"));
html.add(button, "button");
info.setContent(html.getElement());
info.setPosition(posicao);
info.open(map);
}
});
Thanks.
The problem is result of a broken hierarchy between the widgets, the normal way to do it is by attach / detach widget. You do it by setting of the widget's element. This is also matter of Google Maps API.
This can be resolved by using fake panel which will be part of the InfoWindow, so when you make setContent(Widget widget) the fake panel will be updated and the element of the widget will be set to the content (as previous).
Please take a look at this class:
public class MyInfoWindow extends InfoWindow {
static class FakePanel extends ComplexPanel {
public FakePanel(Widget w) {
w.removeFromParent();
getChildren().add(w);
adopt(w);
}
#Override
public boolean isAttached() {
return true;
}
public void detachWidget() {
this.remove(0);
}
}
private IsWidget widgetContent = null;
FakePanel widgetAttacher;
public MyInfoWindow() {
super(InfoWindowImpl.impl.construct());
}
private void detachWidget() {
if (this.widgetAttacher != null) {
this.widgetAttacher.detachWidget();
this.widgetAttacher = null;
}
}
public void close() {
super.close();
detachWidget();
}
public void setContent(String content) {
this.widgetContent = null;
this.detachWidget();
super.setContent(content);
}
/** */
public void setContent(Widget value) {
this.widgetContent = value;
setContent(value.getElement());
if (this.widgetAttacher == null) {
addListener(getJso(), "closeclick", new Runnable() {
#Override
public void run() {
detachWidget();
}
});
this.widgetAttacher = new FakePanel(value);
} else if (this.widgetAttacher.getWidget(0) != value) {
this.widgetAttacher.detachWidget();
this.widgetAttacher = new FakePanel(value);
}
}
private void setContent(Element element) {
InfoWindowImpl.impl.setContent(getJso(), element);
}
public IsWidget getContentWidget() {
return widgetContent;
}
public final native void addListener(JavaScriptObject jso, String whichEvent, Runnable handler)
/*-{
var that = jso;
$wnd.google.maps.event.addListener(jso, whichEvent, function() {
handler.#java.lang.Runnable::run()();
});
}-*/;
}
I had to build a wrapper over InfoWindow to make it work.
public class NXInfoWindow {
static class FakePanel extends ComplexPanel {
public FakePanel(Widget w) {
w.removeFromParent();
getChildren().add(w);
adopt(w);
}
#Override
public boolean isAttached() {
return true;
}
public void detachWidget() {
this.remove(0);
}
}
private InfoWindow info;
private IsWidget widgetContent = null;
private Long id;
FakePanel widgetAttacher;
public static NXInfoWindow create(Long id){
NXInfoWindow myInfo = new NXInfoWindow();
myInfo.info = InfoWindow.create();
myInfo.id = id;
return myInfo;
};
private void detachWidget() {
if (this.widgetAttacher != null) {
this.widgetAttacher.detachWidget();
this.widgetAttacher = null;
}
}
public void close() {
info.close();
detachWidget();
}
public void setPosition(LatLng posicao) {
info.setPosition(posicao);
}
public void open(GoogleMap map) {
info.open(map);
}
public void setContent(Widget value) {
this.widgetContent = value;
info.setContent(value.getElement());
if (this.widgetAttacher == null) {
addListener(info, "closeclick", new Runnable() {
#Override
public void run() {
detachWidget();
}
});
this.widgetAttacher = new FakePanel(value);
} else if (this.widgetAttacher.getWidget(0) != value) {
this.widgetAttacher.detachWidget();
this.widgetAttacher = new FakePanel(value);
}
}
private void setContent(Element element) {
this.setContent(element);
}
public IsWidget getContentWidget() {
return widgetContent;
}
public final native void addListener(JavaScriptObject jso, String whichEvent, Runnable handler)
/*-{
var that = jso;
$wnd.google.maps.event.addListener(jso, whichEvent, function() {
handler.#java.lang.Runnable::run()();
});
}-*/;
}