WearableListenerService: calling putDataItem() multiple times - android

I have a list of 10 objects that I need to send from the handheld to the wearable. The sending works perfect for all of them, but onDataChanged() only gets triggered 2 times and it seems like the first one is random. So i recreated the exact same problem with the same results:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.button);
mDataClient = Wearable.getDataClient(this);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i = 0; i < 10; i++) {
//own mock up class with string id of i to differ them
Model model = new Model(String.valueOf(i)
, "title"
, new Date().getTime());
sendDataToWear(model);
}
}
});
}
private void sendDataToWear(Model model) {
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/my_path");
putDataMapRequest.getDataMap().putString("id", model.getId());
putDataMapRequest.getDataMap().putString("title", model.getTitle());
putDataMapRequest.getDataMap().putLong("timestamp", model.getTimeStamp());
PutDataRequest putDataRequest = putDataMapRequest.asPutDataRequest();
Task<DataItem> putDataTask = mDataClient.putDataItem(putDataRequest);
putDataTask.addOnSuccessListener(new OnSuccessListener<DataItem>() {
#Override
public void onSuccess(DataItem dataItem) {
Log.d(TAG, "onSuccess: dataitem");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d(TAG, "onFailure: dataitem " + e.getMessage());
}
});
}
The onSuccess logs are called 10 times with the correct payload.
The wearable activity with implemented OnDataChangeListener:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Wearable.getDataClient(this).addListener(this);
}
#Override
public void onDataChanged(#NonNull DataEventBuffer dataEventBuffer) {
for(DataEvent event : dataEventBuffer) {
if(event.getType() == DataEvent.TYPE_CHANGED) {
DataItem item = event.getDataItem();
if(item.getUri().getPath().compareTo("/my_path") == 0) {
DataMap map = DataMapItem.fromDataItem(item).getDataMap();
Log.d(TAG, "onDataChanged: name: " + map.getString("id"));
}
}
}
}
onDataChanged logs are called 2 times with the first one always different and the second one always 9. I have no idea whats going on. I also tested it with 1 second wait time between the putDataItem() calls and it works as intended. The onDataChanged method is called 10 times and the 10 payloads are transfferred. But i cant wait one second between each call. Does somebody know whats going on here?

All your requests are being created with the same path, so later requests overwrite the earlier - and if this happens before the earlier requests have been sent to the phone (as is likely), it'll only receive the last one.
To send different items, use different paths in your PutDataMapRequest.create() calls. Preferably, create a path that sensibly differentiates each item (such as one that ends in a unique ID, like "/my_path/[item_id]").

Related

Making multiple asynchronous request using Retrofit

In my android application I have a screen where I have 3 spinners that need to be
filled from APIs call.
static List<TripCode> tripCodeList = new ArrayList<>();
static List<Fleet> truckList = new ArrayList<>();
static List<Trailer> trailerList = new ArrayList<>();
And I don't want to inflate the layout unless I get the response from all the 3 different API calls so this is what I'm doing
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
if (MyApplication.isConnected()) {
getTripCodes();
} else {
Toast.makeText(this, "No internet Connection", Toast.LENGTH_LONG).show();
setContentView(R.layout.no_internet_connection);
}
}
Basically , I removed setContentView(R.layout.activity_create_trip);
from onCreate() And I called getTripCodes()
here's the code for getTripCodes()
public void getTripCodes() {
MyApplication.showProgressDialog(getString(R.string.please_wait), this);
IMyAPI iMyAPI = MyApplication.getIMyAPI();
Call<List<TripCode>> call = iMyAPI.getTripCodes();
call.enqueue(new Callback<List<TripCode>>() {
#Override
public void onResponse(Call<List<TripCode>> call, Response<List<TripCode>> response) {
if (response.isSuccessful() && response.body() != null) {
tripCodeList = response.body();
Log.d("test", "getTripCodes success = " + tripCodeList.size());
getTrucks();
} else {
MyApplication.dismissProgressDialog();
}
}
#Override
public void onFailure(Call<List<TripCode>> call, Throwable t) {
MyApplication.dismissProgressDialog();
}
});
}
So in the success of the call I'm calling the other function getTrucks() which also get result from API and in the success it will call getTrailers()
But I think it's a waste of time, because I can call the three function all together in parallel, and then check if all the list are filled or not.
But I don't know how to do it. How can I check if all the calls are success? And if one of them has failed, how will I know which one exactly failed?
I Believe for your problem you can easily use Retrofit 2.6.0 which has coroutine support and you can declare all the function's as suspended function's and dispatch them with async/launch dispatcher and if you want to wait for some result in some case use await() to wait for the result.
And use RxJava/liveData for responsive UI
sample code for you will look like
//maybe from Activity for ViewModel you can use ViewModelScope
GlobalScope.launch{
result1= async{ getTripCodes() }
result2= async{ getTrucks() }
result3= async{ getTrailers() }
doSomethingWithTripCodes(result1.await())
doSomethingWIthTrucks(result2.await())
doSomethingTrailers(result3.await())
}
Reference:
post1

Pinning live items (subclass ParseObject) and immediately retrieving via fromPin(pinName) - the list is empty

I have a very simple fragment that basically calls a method that tries to retrieve custom Parse objects from the internet, pin them and then reload a UI ListView with the pinned items whether or not the internet call succeeded (so there is a fallback caching mechanism in case there is no internet connection).
Here are the two key methods:
// This is called in onViewCreated() and onResume() of the fragment
private void reloadWalletsFromInternet() {
ParseQuery<Wallet> queryLiveData = ParseQuery.getQuery(Wallet.class);
queryLiveData.findInBackground(new FindCallback<Wallet>() {
#Override
public void done(final List<com.hasmobi.money.models.Wallet> list, ParseException e) {
if (list != null && list.size() > 0) {
for (final Wallet w : list) {
w.pinInBackground("wallets", new SaveCallback() {
#Override
public void done(ParseException e) {
reloadWalletsFromLocalstore();
}
});
}
} else {
Log.d(getClass().getSimpleName(), "no wallets retrieved from internet");
reloadWalletsFromLocalstore();
}
}
});
}
private void reloadWalletsFromLocalstore() {
final ParseQuery<Wallet> queryLocalData = ParseQuery.getQuery(Wallet.class);
queryLocalData.fromLocalDatastore();
queryLocalData.fromPin("wallets");
queryLocalData.findInBackground(new FindCallback<Wallet>() {
#Override
public void done(List<Wallet> list, ParseException e) {
// Here I am receiving 0 items in "list" which is wrong
// (e is "null")
}
});
}
Inside App.java (my custom Application base class) I've subclassed the Wallet class to be registered in Parse:
ParseObject.registerSubclass(Wallet.class);
Parse.enableLocalDatastore(this);
Parse.initialize(this, "my keys", "my keys");
I've put a couple of strategic Log.d() lines on a few places and the code successfully runs through both methods and the for() loop inside reloadWalletsFromInternet() successfully runs and appears to pin each of the received Wallet objects in the pin group "wallets". However, the subsequent query for the pins in that group, made by reloadWalletsFromLocalstore() don't seem to be able to retrieve those Wallet objects.
Below code will do the job.
get rid of queryLocalData.fromPin("wallets");
private void reloadWalletsFromLocalstore() {
final ParseQuery<Wallet> queryLocalData = ParseQuery.getQuery(Wallet.class);
queryLocalData.fromLocalDatastore();
queryLocalData.findInBackground(new FindCallback<Wallet>() {
#Override
public void done(List<Wallet> list, ParseException e) {
// Here I am receiving 0 items in "list" which is wrong
// (e is "null")
}
});
}

Connect a client app Android to a server written with Sails.js using socket.io

Here's my situation: i have a server written with sails.js where i have a user model. I have a dashboard where i can see all the users, create new, delete them and so on...Now i want to create an android app where i can get, using socket.io, notification about the events that occour to the user model.
Example: if i delete a user from my dashboard i want the app to recive a notification from the server that the user has been deleted.
Here's my code for the app:
public class MainActivity extends Activity {
//socket instance
private Socket mSocket;
{
try {
mSocket = IO.socket("http://server_url:port/user");
} catch (URISyntaxException e) {
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //set the layout for this activity
final TextView tv = (TextView) findViewById(R.id.textView);
mSocket.on("user", new Emitter.Listener() {
#Override
public void call(Object... args) {
tv.setVisibility(View.INVISIBLE);
}
});
mSocket.connect();
final Button btn_createUser = (Button)findViewById(R.id.button);
btn_createUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mSocket.connected()) {
//tv.setVisibility(View.INVISIBLE);
}
}
});
}
}
Like i did with the dashboard to connect the socket to the server i did the same thing here, but it seems like the socket doesn't connect, in fact, when i delete a user from the dashboard i get no notification.
Here's the code (that works) i used in my dashboard to connect my socket to the server and listen to updates from the user model:
//connect to the server and listen to updates from user model
io.socket.get('/user', function(data, jwres) {
$scope.users = data;
$scope.$apply(); //write users in the table...
});
//wait for updates...
io.socket.on('user', function serverResponded (data) {
if(data.verb == "updated")
{
//get the user index in the array and update it
var index = getIndex(data.id, $scope.users);
$scope.users[index].online = data.data.online;
$scope.$apply();
}
if(data.verb == "destroyed")
{
//get the user index in the array and remove it
var index = getIndex(data.id, $scope.users);
$scope.users.splice(index, 1);
$scope.$apply();
}
if(data.verb == "created")
{
//simply update the array
$scope.users.push(data.data);
$scope.$apply();
}
});
Now, i think all i'm missing out in the android app is the GET request which automatically subscribe my socket to the model and get notified if something happen...but i don't know hot to do it.
I hope i was clear enough...thank to everyone who will answer me!
PS: i don't want to use AndroidAsync because i need the ultimate version of socket.io!
In case someone need it, i found the solution: https://github.com/balderdashy/sails/issues/2640 here you can find out how to solve the issue! :)

clearCachedResult() not working as expected

I have this query located in my ParseQueryBuilder object:
public ParseQuery<Event> eventsTypes() {
ParseQuery<Event> query = Event.getQuery();
query.setCachePolicy(ParseQuery.CachePolicy.CACHE_ELSE_NETWORK);
query.setMaxCacheAge(TimeUnit.DAYS.toMillis(1));
query.whereEqualTo(Event.owner, parse.getParseUser());
query.orderByDescending(Event.timesUsed);
return query;
}
I use it to populate a ParseQueryAdapter
and at some point I would like to add an Event and immediately show it:
#OnClick(R.id.add)
public void add(Button button) {
final Event new_type = new Event();
new_type.setOwner(parse.getParseUser());
new_type.setName("atest");
new_type.saveEventually(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
// on successfull save, clear cache
parseQueryBuilder.eventsTypes().clearCachedResult();
// and show newly added object
mAdapter.loadObjects();
Toast.makeText(getActivity(), new_type.getName(), Toast.LENGTH_SHORT).show();
}
}
});
}
I expected clearing the cache would result in a new network query, revealing the newly added item but no matter what I try, it seems it will only show the initially cached result.
Even if I try to restart my app, it shows the result from the first cache.

Google Game Services onPeersDisconnected/onP2PDisconnected not being called

As in the title I've been able to connect to Google Game Services, exchange data between two devices and everything is running fine, except one thing: disconnection callbacks.
I tried to intercept both onPeersDisconnected and onP2PDisconnected without any success. The onP2PDisconnected method is being called in the device that get disconnected from Internet but not into device that is still online (so there is no way to tell the player that the other one got disconnected).
After the match is started it seems that the second device is never notified of the accidental disconnection. If the user close the game properly the onPeersLeft method is being called thought.
Is a ping between the two devices really necessary to overcome this "bug"? Am I doing something wrong?
Here is the code I use:
void startQuickGame() {
// quick-start a game with 1 randomly selected opponent
final int MIN_OPPONENTS = 1, MAX_OPPONENTS = 1;
Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(MIN_OPPONENTS,
MAX_OPPONENTS, 0);
RoomConfig.Builder rtmConfigBuilder = RoomConfig.builder(this);
rtmConfigBuilder.setMessageReceivedListener(this);
rtmConfigBuilder.setRoomStatusUpdateListener(this);
rtmConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
mListener.switchToScreen(R.id.screen_wait);
keepScreenOn();
resetGameVars();
getGamesClient().createRoom(rtmConfigBuilder.build());
}
And here the simple listeners:
#Override
public void onPeersDisconnected(Room room, List<String> peers) {
Log.d(TAG, "onPeersDisconnected");
updateRoom(room);
}
void updateRoom(Room room) {
Log.d(TAG, "UpdateRoom: "+room.getParticipants().size());
mParticipants = room.getParticipants();
}
#Override
public void onP2PDisconnected(String participantId) {
Log.d(TAG, "onP2PDisconnected");
}
public int getPartecipantsInRooom(){
if(mRoom != null)
return mRoom.getParticipants().size();
else
return -123456;
}
Note that calling getPartecipantsInRooom() after one of the two devices disconnects always return 2, and updateRoom never get called.
Just to be sure this might not work for you, for my applications I use this to let me know when another Participant has left the Room, and it is called immediately :
#Override
public void onPeerLeft(Room room, final List<String> participantIds) {
this.mRoomCurrent = room;
this.mRoomId = this.mRoomCurrent.getRoomId();
this.mParticipants = this.mRoomCurrent.getParticipants();
int connected = 0;
for (Participant p : room.getParticipants()) {
if(p.getStatus() == Participant.STATUS_JOINED) {
connected += 1;
}
}
final int fconnected = connected;
for (String s : listIgnoreTheseIDs) {
//checkint to see if we care anymore about this ID.. if out of game already.. nope
if(s.equals(participantIds.get(0))){
return;
}
}
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
mGHInterface.onPeerLeft(fconnected, participantIds.size());
}
});
}
No idea why there are two items, but like you, I realized the onPeersDisconnected() isn't that reliable, but onPeerLeft() normally gets back to the other devices in under 1 second.
onPeerDisconnected() handles disconnects. So if somebody is still in the application but the network connection is lost, this is called for him.
onPeerLeft() handles participants who leave a room. This is called when somebody explizit leaves the room in the application or the application is minimized, and the room is left on the androids onStop() or onDestroy() callback.
I'm making two player game. So I use this approach
#Override
public void onPeerLeft(Room room, List<String> peersWhoLeft) {
updateRoom(room);
Toast.makeText(MyLauncherActivity.this, "Other player left the game", Toast.LENGTH_LONG).show();
quitGame();
}

Categories

Resources