Fixes bad cast depending of the streaming service

This commit is contained in:
stom79 2017-11-14 15:11:31 +01:00
parent 8608eed2da
commit 1903fc14d5
4 changed files with 53 additions and 56 deletions

View File

@ -1059,22 +1059,22 @@ public abstract class BaseMainActivity extends AppCompatActivity
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras(); Bundle b = intent.getExtras();
LiveNotificationService.EventStreaming eventStreaming = (LiveNotificationService.EventStreaming) intent.getSerializableExtra("eventStreaming"); Helper.EventStreaming eventStreaming = (Helper.EventStreaming) intent.getSerializableExtra("eventStreaming");
assert b != null; assert b != null;
userIdService = b.getString("userIdService", null); userIdService = b.getString("userIdService", null);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
if( userIdService != null && userIdService.equals(userId)) { if( userIdService != null && userIdService.equals(userId)) {
if (eventStreaming == LiveNotificationService.EventStreaming.NOTIFICATION) { if (eventStreaming == Helper.EventStreaming.NOTIFICATION) {
Notification notification = b.getParcelable("data"); Notification notification = b.getParcelable("data");
if (notificationsFragment != null) { if (notificationsFragment != null) {
notificationsFragment.refresh(notification); notificationsFragment.refresh(notification);
} }
} else if (eventStreaming == LiveNotificationService.EventStreaming.UPDATE) { } else if (eventStreaming == Helper.EventStreaming.UPDATE) {
Status status = b.getParcelable("data"); Status status = b.getParcelable("data");
if (homeFragment != null) { if (homeFragment != null) {
homeFragment.refresh(status); homeFragment.refresh(status);
} }
} else if (eventStreaming == LiveNotificationService.EventStreaming.DELETE) { } else if (eventStreaming == Helper.EventStreaming.DELETE) {
//noinspection unused //noinspection unused
String id = b.getString("id"); String id = b.getString("id");
if (notificationsFragment != null) { if (notificationsFragment != null) {

View File

@ -299,6 +299,13 @@ public class Helper {
CW, CW,
SIMPLE SIMPLE
} }
//Event Type
public enum EventStreaming{
UPDATE,
NOTIFICATION,
DELETE,
NONE
}
private static boolean isPerformingSearch = false; private static boolean isPerformingSearch = false;

View File

@ -78,12 +78,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
public class LiveNotificationService extends Service { public class LiveNotificationService extends Service {
public enum EventStreaming{
UPDATE,
NOTIFICATION,
DELETE,
NONE
}
protected Account account; protected Account account;
private boolean restartCalled; private boolean restartCalled;
@ -139,7 +134,7 @@ public class LiveNotificationService extends Service {
HttpsURLConnection httpsURLConnection = null; HttpsURLConnection httpsURLConnection = null;
BufferedReader reader = null; BufferedReader reader = null;
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
EventStreaming lastEvent = null; Helper.EventStreaming lastEvent = null;
if( accountStream != null){ if( accountStream != null){
try { try {
URL url = new URL("https://" + accountStream.getInstance() + "/api/v1/streaming/user"); URL url = new URL("https://" + accountStream.getInstance() + "/api/v1/streaming/user");
@ -156,44 +151,44 @@ public class LiveNotificationService extends Service {
inputStream = new BufferedInputStream(httpsURLConnection.getInputStream()); inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
reader = new BufferedReader(new InputStreamReader(inputStream)); reader = new BufferedReader(new InputStreamReader(inputStream));
String event; String event;
EventStreaming eventStreaming; Helper.EventStreaming eventStreaming;
while((event = reader.readLine()) != null) { while((event = reader.readLine()) != null) {
if( !sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING, true) ) { if( !sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING, true) ) {
stopSelf(); stopSelf();
return; return;
} }
if ((lastEvent == EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) { if ((lastEvent == Helper.EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) {
switch (event.trim()) { switch (event.trim()) {
case "event: update": case "event: update":
lastEvent = EventStreaming.UPDATE; lastEvent = Helper.EventStreaming.UPDATE;
break; break;
case "event: notification": case "event: notification":
lastEvent = EventStreaming.NOTIFICATION; lastEvent = Helper.EventStreaming.NOTIFICATION;
break; break;
case "event: delete": case "event: delete":
lastEvent = EventStreaming.DELETE; lastEvent = Helper.EventStreaming.DELETE;
break; break;
default: default:
lastEvent = EventStreaming.NONE; lastEvent = Helper.EventStreaming.NONE;
} }
} else { } else {
if (!event.startsWith("data: ")) { if (!event.startsWith("data: ")) {
lastEvent = EventStreaming.NONE; lastEvent = Helper.EventStreaming.NONE;
continue; continue;
} }
event = event.substring(6); event = event.substring(6);
if (lastEvent == EventStreaming.UPDATE) { if (lastEvent == Helper.EventStreaming.UPDATE) {
eventStreaming = EventStreaming.UPDATE; eventStreaming = Helper.EventStreaming.UPDATE;
} else if (lastEvent == EventStreaming.NOTIFICATION) { } else if (lastEvent == Helper.EventStreaming.NOTIFICATION) {
eventStreaming = EventStreaming.NOTIFICATION; eventStreaming = Helper.EventStreaming.NOTIFICATION;
} else if (lastEvent == EventStreaming.DELETE) { } else if (lastEvent == Helper.EventStreaming.DELETE) {
eventStreaming = EventStreaming.DELETE; eventStreaming = Helper.EventStreaming.DELETE;
event = "{id:" + event + "}"; event = "{id:" + event + "}";
} else { } else {
eventStreaming = EventStreaming.UPDATE; eventStreaming = Helper.EventStreaming.UPDATE;
} }
lastEvent = EventStreaming.NONE; lastEvent = Helper.EventStreaming.NONE;
try { try {
JSONObject eventJson = new JSONObject(event); JSONObject eventJson = new JSONObject(event);
onRetrieveStreaming(eventStreaming, accountStream, eventJson); onRetrieveStreaming(eventStreaming, accountStream, eventJson);
@ -227,7 +222,7 @@ public class LiveNotificationService extends Service {
} }
} }
public void onRetrieveStreaming(EventStreaming event, final Account account, JSONObject response) { public void onRetrieveStreaming(Helper.EventStreaming event, final Account account, JSONObject response) {
if( response == null ) if( response == null )
return; return;
//No previous notifications in cache, so no notification will be sent //No previous notifications in cache, so no notification will be sent
@ -236,7 +231,7 @@ public class LiveNotificationService extends Service {
String dataId = null; String dataId = null;
Bundle b = new Bundle(); Bundle b = new Bundle();
if( event == EventStreaming.NOTIFICATION){ if( event == Helper.EventStreaming.NOTIFICATION){
notification = API.parseNotificationResponse(getApplicationContext(), response); notification = API.parseNotificationResponse(getApplicationContext(), response);
b.putParcelable("data", notification); b.putParcelable("data", notification);
boolean activityPaused; boolean activityPaused;
@ -346,12 +341,12 @@ public class LiveNotificationService extends Service {
} }
} }
} }
}else if ( event == EventStreaming.UPDATE){ }else if ( event == Helper.EventStreaming.UPDATE){
status = API.parseStatuses(getApplicationContext(), response); status = API.parseStatuses(getApplicationContext(), response);
status.setReplies(new ArrayList<Status>()); status.setReplies(new ArrayList<Status>());
status.setNew(true); status.setNew(true);
b.putParcelable("data", status); b.putParcelable("data", status);
}else if( event == EventStreaming.DELETE){ }else if( event == Helper.EventStreaming.DELETE){
try { try {
dataId = response.getString("id"); dataId = response.getString("id");
} catch (JSONException e) { } catch (JSONException e) {

View File

@ -54,7 +54,7 @@ import fr.gouv.etalab.mastodon.sqlite.Sqlite;
public class StreamingService extends IntentService { public class StreamingService extends IntentService {
private EventStreaming lastEvent; private Helper.EventStreaming lastEvent;
/** /**
* Creates an IntentService. Invoked by your subclass's constructor. * Creates an IntentService. Invoked by your subclass's constructor.
* *
@ -70,12 +70,7 @@ public class StreamingService extends IntentService {
} }
private static HttpsURLConnection httpsURLConnection; private static HttpsURLConnection httpsURLConnection;
public enum EventStreaming{
UPDATE,
NOTIFICATION,
DELETE,
NONE
}
protected Account account; protected Account account;
public void onCreate() { public void onCreate() {
@ -118,41 +113,41 @@ public class StreamingService extends IntentService {
inputStream = new BufferedInputStream(httpsURLConnection.getInputStream()); inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
reader = new BufferedReader(new InputStreamReader(inputStream)); reader = new BufferedReader(new InputStreamReader(inputStream));
String event; String event;
EventStreaming eventStreaming; Helper.EventStreaming eventStreaming;
while((event = reader.readLine()) != null) { while((event = reader.readLine()) != null) {
if( !sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING + accountStream.getId(), true) ) if( !sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING + accountStream.getId(), true) )
stopSelf(); stopSelf();
if ((lastEvent == EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) { if ((lastEvent == Helper.EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) {
switch (event.trim()) { switch (event.trim()) {
case "event: update": case "event: update":
lastEvent = EventStreaming.UPDATE; lastEvent = Helper.EventStreaming.UPDATE;
break; break;
case "event: notification": case "event: notification":
lastEvent = EventStreaming.NOTIFICATION; lastEvent = Helper.EventStreaming.NOTIFICATION;
break; break;
case "event: delete": case "event: delete":
lastEvent = EventStreaming.DELETE; lastEvent = Helper.EventStreaming.DELETE;
break; break;
default: default:
lastEvent = EventStreaming.NONE; lastEvent = Helper.EventStreaming.NONE;
} }
} else { } else {
if (!event.startsWith("data: ")) { if (!event.startsWith("data: ")) {
lastEvent = EventStreaming.NONE; lastEvent = Helper.EventStreaming.NONE;
continue; continue;
} }
event = event.substring(6); event = event.substring(6);
if (lastEvent == EventStreaming.UPDATE) { if (lastEvent == Helper.EventStreaming.UPDATE) {
eventStreaming = EventStreaming.UPDATE; eventStreaming = Helper.EventStreaming.UPDATE;
} else if (lastEvent == EventStreaming.NOTIFICATION) { } else if (lastEvent == Helper.EventStreaming.NOTIFICATION) {
eventStreaming = EventStreaming.NOTIFICATION; eventStreaming = Helper.EventStreaming.NOTIFICATION;
} else if (lastEvent == EventStreaming.DELETE) { } else if (lastEvent == Helper.EventStreaming.DELETE) {
eventStreaming = EventStreaming.DELETE; eventStreaming = Helper.EventStreaming.DELETE;
event = "{id:" + event + "}"; event = "{id:" + event + "}";
} else { } else {
eventStreaming = EventStreaming.UPDATE; eventStreaming = Helper.EventStreaming.UPDATE;
} }
lastEvent = EventStreaming.NONE; lastEvent = Helper.EventStreaming.NONE;
try { try {
JSONObject eventJson = new JSONObject(event); JSONObject eventJson = new JSONObject(event);
onRetrieveStreaming(eventStreaming, accountStream, eventJson); onRetrieveStreaming(eventStreaming, accountStream, eventJson);
@ -177,7 +172,7 @@ public class StreamingService extends IntentService {
} }
} }
public void onRetrieveStreaming(EventStreaming event, Account account, JSONObject response) { public void onRetrieveStreaming(Helper.EventStreaming event, Account account, JSONObject response) {
if( response == null ) if( response == null )
return; return;
//No previous notifications in cache, so no notification will be sent //No previous notifications in cache, so no notification will be sent
@ -187,15 +182,15 @@ public class StreamingService extends IntentService {
String dataId = null; String dataId = null;
Bundle b = new Bundle(); Bundle b = new Bundle();
if( event == EventStreaming.NOTIFICATION){ if( event == Helper.EventStreaming.NOTIFICATION){
notification = API.parseNotificationResponse(getApplicationContext(), response); notification = API.parseNotificationResponse(getApplicationContext(), response);
b.putParcelable("data", notification); b.putParcelable("data", notification);
}else if ( event == EventStreaming.UPDATE){ }else if ( event == Helper.EventStreaming.UPDATE){
status = API.parseStatuses(getApplicationContext(), response); status = API.parseStatuses(getApplicationContext(), response);
status.setReplies(new ArrayList<Status>()); status.setReplies(new ArrayList<Status>());
status.setNew(true); status.setNew(true);
b.putParcelable("data", status); b.putParcelable("data", status);
}else if( event == EventStreaming.DELETE){ }else if( event == Helper.EventStreaming.DELETE){
try { try {
//noinspection UnusedAssignment //noinspection UnusedAssignment
dataId = response.getString("id"); dataId = response.getString("id");