fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java

205 lines
8.3 KiB
Java
Raw Normal View History

2017-08-28 19:04:13 +02:00
package fr.gouv.etalab.mastodon.services;
/* Copyright 2017 Thomas Schneider
*
* This file is a part of Mastalab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
2017-09-22 19:49:25 +02:00
import android.app.IntentService;
2017-08-28 19:04:13 +02:00
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
2017-09-22 19:49:25 +02:00
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
2017-08-29 15:57:51 +02:00
import android.os.SystemClock;
2017-08-28 19:04:13 +02:00
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
2017-09-23 08:41:13 +02:00
2017-08-28 19:04:13 +02:00
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
2017-09-02 17:44:59 +02:00
import java.util.ArrayList;
2017-09-22 19:49:25 +02:00
import javax.net.ssl.HttpsURLConnection;
2017-09-22 19:49:25 +02:00
2017-08-28 19:04:13 +02:00
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Notification;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.TLSSocketFactory;
2017-08-28 19:04:13 +02:00
import fr.gouv.etalab.mastodon.helper.Helper;
2017-09-22 19:49:25 +02:00
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
2017-08-28 19:04:13 +02:00
/**
* Created by Thomas on 28/08/2017.
2017-08-29 07:30:25 +02:00
* Manage service for streaming api and new notifications
2017-08-28 19:04:13 +02:00
*/
2017-09-22 19:49:25 +02:00
public class StreamingService extends IntentService {
2017-08-28 19:04:13 +02:00
2017-09-12 18:41:13 +02:00
2017-09-09 19:05:43 +02:00
private EventStreaming lastEvent;
2017-09-22 19:49:25 +02:00
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public StreamingService(String name) {
super(name);
}
public StreamingService() {
super("StreamingService");
}
2017-09-23 08:41:13 +02:00
2017-09-22 19:49:25 +02:00
private static HttpsURLConnection httpsURLConnection;
2017-09-09 19:05:43 +02:00
public enum EventStreaming{
UPDATE,
NOTIFICATION,
DELETE,
NONE
2017-08-30 15:24:18 +02:00
}
2017-09-21 07:21:04 +02:00
protected Account account;
2017-08-30 15:24:18 +02:00
2017-09-22 19:49:25 +02:00
public void onCreate() {
super.onCreate();
}
2017-09-09 11:38:26 +02:00
2017-09-09 19:05:43 +02:00
@Override
2017-09-22 19:49:25 +02:00
protected void onHandleIntent(@Nullable Intent intent) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2017-09-17 15:34:41 +02:00
InputStream inputStream;
BufferedReader reader = null;
2017-09-22 19:49:25 +02:00
Account accountStream = null;
if( httpsURLConnection != null)
httpsURLConnection.disconnect();
if( userId != null) {
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
accountStream = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
}
if( accountStream != null){
try {
URL url = new URL("https://" + accountStream.getInstance() + "/api/v1/streaming/user");
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + accountStream.getToken());
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpsURLConnection.setRequestProperty("Keep-Alive", "header");
httpsURLConnection.setRequestProperty("Connection", "close");
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.setConnectTimeout(70000);
httpsURLConnection.setReadTimeout(70000);
inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
reader = new BufferedReader(new InputStreamReader(inputStream));
String event;
EventStreaming eventStreaming;
while((event = reader.readLine()) != null) {
if ((lastEvent == EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) {
switch (event.trim()) {
case "event: update":
lastEvent = EventStreaming.UPDATE;
break;
case "event: notification":
lastEvent = EventStreaming.NOTIFICATION;
break;
case "event: delete":
lastEvent = EventStreaming.DELETE;
break;
default:
lastEvent = EventStreaming.NONE;
}
} else {
if (!event.startsWith("data: ")) {
2017-09-17 15:34:41 +02:00
lastEvent = EventStreaming.NONE;
2017-09-22 19:49:25 +02:00
continue;
}
event = event.substring(6);
if (lastEvent == EventStreaming.UPDATE) {
eventStreaming = EventStreaming.UPDATE;
} else if (lastEvent == EventStreaming.NOTIFICATION) {
eventStreaming = EventStreaming.NOTIFICATION;
} else if (lastEvent == EventStreaming.DELETE) {
eventStreaming = EventStreaming.DELETE;
event = "{id:" + event + "}";
} else {
eventStreaming = EventStreaming.UPDATE;
}
2017-09-17 15:34:41 +02:00
lastEvent = EventStreaming.NONE;
2017-09-22 19:49:25 +02:00
try {
JSONObject eventJson = new JSONObject(event);
2017-09-23 08:05:52 +02:00
onRetrieveStreaming(eventStreaming, accountStream, eventJson);
2017-09-22 19:49:25 +02:00
} catch (JSONException e) {
e.printStackTrace();
}
2017-09-17 15:34:41 +02:00
}
2017-09-22 19:49:25 +02:00
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(reader != null){
try{
reader.close();
}catch (IOException e){
2017-09-17 15:34:41 +02:00
e.printStackTrace();
}
}
2017-09-22 19:49:25 +02:00
SystemClock.sleep(1000);
sendBroadcast(new Intent("RestartStreamingService"));
2017-09-17 15:34:41 +02:00
}
}
}
2017-09-23 08:05:52 +02:00
public void onRetrieveStreaming(EventStreaming event, Account account, JSONObject response) {
2017-08-28 19:04:13 +02:00
if( response == null )
return;
//No previous notifications in cache, so no notification will be sent
2017-09-12 18:41:13 +02:00
Status status ;
Notification notification;
2017-08-28 19:04:13 +02:00
String dataId = null;
Bundle b = new Bundle();
if( event == EventStreaming.NOTIFICATION){
2017-08-28 19:04:13 +02:00
notification = API.parseNotificationResponse(getApplicationContext(), response);
b.putParcelable("data", notification);
}else if ( event == EventStreaming.UPDATE){
2017-08-28 19:04:13 +02:00
status = API.parseStatuses(getApplicationContext(), response);
2017-09-12 18:41:13 +02:00
status.setReplies(new ArrayList<Status>());
2017-09-08 18:30:49 +02:00
status.setNew(true);
b.putParcelable("data", status);
}else if( event == EventStreaming.DELETE){
2017-08-28 19:04:13 +02:00
try {
dataId = response.getString("id");
} catch (JSONException e) {
e.printStackTrace();
}
}
2017-09-23 08:05:52 +02:00
if( account != null)
b.putString("userIdService",account.getId());
2017-09-12 18:41:13 +02:00
Intent intentBC = new Intent(Helper.RECEIVE_DATA);
intentBC.putExtra("eventStreaming", event);
intentBC.putExtras(b);
2017-09-12 18:41:13 +02:00
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentBC);
2017-08-29 08:32:09 +02:00
}
2017-09-21 07:21:04 +02:00
2017-08-28 19:04:13 +02:00
}