Skip to content

Commit dac906f

Browse files
s7092910rohanjain1
authored andcommitted
Proper handling of removal and update of pokemon on the map (#226)
* Updated the MarkerUpdate to use a CountdownTimer and when a pokemon is created on the map it is posted to a handler that removes it after it is expired. * Added switch statement for later * Removed some logging * Removed the runOnUIThread * Removed passing Marker to the MarkerRefreshController for starting the countdowntimer. Added a fade out animation when a pokemon marker expires.
1 parent db58749 commit dac906f

File tree

5 files changed

+201
-102
lines changed

5 files changed

+201
-102
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.omkarmoghe.pokemap.controllers;
22

3+
import android.os.CountDownTimer;
34
import android.os.Handler;
45
import android.os.HandlerThread;
5-
import android.support.design.widget.TabLayout;
6+
import android.os.Message;
67
import android.util.Log;
78

8-
import com.omkarmoghe.pokemap.models.events.MarkerUpdateEvent;
9+
import com.omkarmoghe.pokemap.models.events.MarkerExpired;
10+
import com.omkarmoghe.pokemap.models.events.MarkerUpdate;
11+
import com.omkarmoghe.pokemap.models.map.PokemonMarkerExtended;
912

1013
import org.greenrobot.eventbus.EventBus;
1114

15+
import java.sql.Time;
16+
1217
/**
1318
* Created by Rohan on 26-07-2016.
1419
*/
@@ -18,38 +23,31 @@ public class MarkerRefreshController {
1823

1924
final private String TAG = MarkerRefreshController.class.getName();
2025

21-
private HandlerThread mThread;
26+
private static final int DEFAULT_UPDATE_INTERVAL = 1000;//1 seconds : heartbeat
27+
private static final int MARKER_EXPIRED = 1;
28+
2229
private Handler mHandler;
23-
private static final int DEFAULT_UPDATE_INTERVAL = 10 * 1000;//10 seconds : heartbeat
24-
private long updateInterval = DEFAULT_UPDATE_INTERVAL;
25-
private MarkerUpdateEvent mEvent;
26-
private Runnable markerExpiryUpdate;
30+
private CountDownTimer mTimer;
2731

2832
private static MarkerRefreshController mInstance;
2933

3034
private MarkerRefreshController() {
31-
if (mThread == null) {
32-
33-
mThread = new HandlerThread("MarkerRefreshThread");
34-
mThread.start();
35-
mHandler = new Handler(mThread.getLooper());
36-
mEvent = new MarkerUpdateEvent();
37-
markerExpiryUpdate = new Runnable() {
38-
@Override
39-
public void run()
40-
{
41-
/**
42-
* Talking to:
43-
* {@link com.omkarmoghe.pokemap.views.map.MapWrapperFragment,}
44-
*/
45-
EventBus.getDefault().post(new MarkerUpdateEvent());
35+
Handler.Callback callback = new Handler.Callback() {
36+
@Override
37+
public boolean handleMessage(Message message) {
38+
switch (message.what) {
39+
case MARKER_EXPIRED:
40+
//If Marker Expired
41+
if (message.obj instanceof PokemonMarkerExtended) {
42+
EventBus.getDefault().post(new MarkerExpired((PokemonMarkerExtended) message.obj));
43+
return true;
44+
}
45+
break;
4646
}
47-
};
48-
49-
} else {
50-
throw new RuntimeException("Invalid state of Marker Refresh Thread.");
51-
}
52-
47+
return false;
48+
}
49+
};
50+
mHandler = new Handler(callback);
5351
}
5452

5553
/*
@@ -64,44 +62,58 @@ public static MarkerRefreshController getInstance() {
6462

6563

6664
/**
67-
* Cleanup thread state.
68-
* Must ensure thread is properly quitting to allow safe reInit when needed
65+
* Cleanup Messages and cancels the timer if it is running.
6966
*/
7067
public void clear() {
71-
if(mThread!=null) {
72-
try {
73-
mThread.join();
74-
mThread.quit(); //I don't need to process the scheduled messages in queue.
75-
76-
} catch (InterruptedException e) {
77-
Log.d(TAG, "Quitting too soon.. doing final cleanup");
78-
} finally {
79-
mThread = null;
80-
mInstance = null;
81-
}
68+
if(mTimer != null){
69+
mTimer.cancel();
70+
mTimer = null;
8271
}
72+
mHandler.removeMessages(MARKER_EXPIRED);
73+
8374
}
8475

85-
/**
86-
* Combo call for starting, resetting, refreshing, scheduling next update
87-
* Must be called separately after [@link notifyTimeToExpiry]
88-
*/
89-
public void reset() {
90-
getInstance().mHandler.removeCallbacks(markerExpiryUpdate);
91-
getInstance().mHandler.postDelayed(markerExpiryUpdate, updateInterval);
92-
updateInterval = DEFAULT_UPDATE_INTERVAL;
76+
public void startTimer(long duration){
77+
if(mTimer != null){
78+
mTimer.cancel();
79+
}
80+
81+
if(duration <= 0) {
82+
return;
83+
}
84+
85+
final MarkerUpdate event = new MarkerUpdate();
86+
mTimer = new CountDownTimer(duration, DEFAULT_UPDATE_INTERVAL) {
87+
@Override
88+
public void onTick(long l) {
89+
EventBus.getDefault().post(event);
90+
}
91+
92+
@Override
93+
public void onFinish() {
94+
mTimer = null;
95+
EventBus.getDefault().post(event);
96+
}
97+
};
98+
mTimer.start();
9399
}
94100

95-
/**
96-
* Hook to determine next closest expiration time.
97-
* Can be scheduled at regular intervals to use as a heartbeat.
98-
*
99-
* @param timeToExpiry
100-
*/
101-
public void notifyTimeToExpiry(long timeToExpiry) {
102-
if (updateInterval > timeToExpiry) {
103-
updateInterval = timeToExpiry;
101+
public void stopTimer(){
102+
if(mTimer != null){
103+
mTimer.cancel();
104+
mTimer = null;
104105
}
105106
}
106107

108+
public void clearMessages(){
109+
mHandler.removeMessages(MARKER_EXPIRED);
110+
}
111+
112+
public void postMarker(PokemonMarkerExtended markerData){
113+
long time = markerData.getCatchablePokemon().getExpirationTimestampMs() - System.currentTimeMillis();
114+
if(time > 0) {
115+
Message message = mHandler.obtainMessage(MARKER_EXPIRED, markerData);
116+
mHandler.sendMessageDelayed(message, time);
117+
}
118+
}
107119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.omkarmoghe.pokemap.models.events;
2+
3+
import com.google.android.gms.maps.model.Marker;
4+
import com.omkarmoghe.pokemap.models.map.PokemonMarkerExtended;
5+
import com.pokegoapi.api.map.pokemon.CatchablePokemon;
6+
7+
/**
8+
* Created by chris on 7/26/2016.
9+
*/
10+
11+
public class MarkerExpired {
12+
13+
private PokemonMarkerExtended mData;
14+
15+
public MarkerExpired(PokemonMarkerExtended markerData){
16+
mData = markerData;
17+
}
18+
19+
public PokemonMarkerExtended getData(){
20+
return mData;
21+
}
22+
23+
public Marker getMarker(){
24+
return mData.getMarker();
25+
}
26+
27+
public CatchablePokemon getPokemon(){
28+
return mData.getCatchablePokemon();
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.omkarmoghe.pokemap.models.events;
2+
3+
/**
4+
* Created by Rohan on 26-07-2016.
5+
*/
6+
7+
import com.google.android.gms.maps.model.Marker;
8+
import com.omkarmoghe.pokemap.models.map.PokemonMarkerExtended;
9+
import com.pokegoapi.api.map.pokemon.CatchablePokemon;
10+
11+
/**
12+
* Empty event.
13+
*/
14+
public class MarkerUpdate {
15+
}

app/src/main/java/com/omkarmoghe/pokemap/models/events/MarkerUpdateEvent.java

-12
This file was deleted.

0 commit comments

Comments
 (0)