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

278 lines
11 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-08-29 15:57:51 +02:00
import android.app.AlarmManager;
import android.app.PendingIntent;
2017-09-09 19:05:43 +02:00
import android.app.Service;
2017-08-28 19:04:13 +02:00
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
2017-09-12 18:14:41 +02:00
import android.os.Binder;
2017-09-09 19:05:43 +02:00
import android.os.IBinder;
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;
import android.util.Log;
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;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
2017-09-02 17:44:59 +02:00
import java.util.ArrayList;
import javax.net.ssl.HttpsURLConnection;
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;
/**
* 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-09 19:05:43 +02:00
public class StreamingService extends Service {
2017-08-28 19:04:13 +02:00
2017-09-12 18:41:13 +02:00
private boolean isConnectingHashMap = false;
private HttpsURLConnection httpsURLConnection;
2017-09-09 19:05:43 +02:00
private EventStreaming lastEvent;
public enum EventStreaming{
UPDATE,
NOTIFICATION,
DELETE,
NONE
2017-08-30 15:24:18 +02:00
}
2017-09-12 18:14:41 +02:00
private final IBinder iBinder = new StreamingServiceBinder();
2017-08-30 15:24:18 +02:00
2017-09-12 18:14:41 +02:00
public class StreamingServiceBinder extends Binder {
public StreamingService getService() {
return StreamingService.this;
2017-09-09 11:38:26 +02:00
}
2017-09-09 19:05:43 +02:00
}
2017-09-09 11:38:26 +02:00
2017-09-09 19:05:43 +02:00
@Nullable
@Override
public IBinder onBind(Intent intent) {
2017-09-12 18:14:41 +02:00
return iBinder;
2017-08-28 19:04:13 +02:00
}
2017-09-12 18:41:13 +02:00
public void disconnect(){
Thread readThread = new Thread(new Runnable() {
@Override
public void run() {
if( httpsURLConnection != null){
httpsURLConnection.disconnect();
}
}});
readThread.start();
2017-09-12 18:41:13 +02:00
}
2017-08-28 19:04:13 +02:00
/**
* Task in background starts here.
*/
2017-09-12 18:41:13 +02:00
public void connect(final Account account) {
2017-08-28 19:04:13 +02:00
//If an Internet connection and user agrees with notification refresh
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
//Check which notifications the user wants to see
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
boolean notif_ask = sharedpreferences.getBoolean(Helper.SET_NOTIF_ASK, true);
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
//User disagree with all notifications
if( !notif_follow && !notif_add && !notif_ask && !notif_mention && !notif_share)
return; //Nothing is done
//No account connected, the service is stopped
if(!Helper.isLoggedIn(getApplicationContext()))
return;
//If WIFI only and on WIFI OR user defined any connections to use the service.
2017-09-12 18:41:13 +02:00
if( isConnectingHashMap)
2017-09-09 19:05:43 +02:00
return;
2017-08-28 19:04:13 +02:00
if(!sharedpreferences.getBoolean(Helper.SET_WIFI_ONLY, false) || Helper.isOnWIFI(getApplicationContext())) {
2017-09-09 19:05:43 +02:00
Thread readThread = new Thread(new Runnable() {
@Override
public void run() {
try {
boolean connectionAlive = false;
2017-09-12 18:41:13 +02:00
isConnectingHashMap = true;
if( httpsURLConnection != null) {
2017-09-09 19:05:43 +02:00
try {
2017-09-12 18:41:13 +02:00
connectionAlive = (httpsURLConnection.getResponseCode() == 200);
2017-09-09 19:05:43 +02:00
} catch (Exception e) {
connectionAlive = false;
}
}
if( connectionAlive) {
if( httpsURLConnection != null)
httpsURLConnection.disconnect();
}
try {
URL url = new URL("https://" + account.getInstance() + "/api/v1/streaming/user");
2017-09-12 18:41:13 +02:00
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + account.getToken());
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpsURLConnection.setRequestProperty("Keep-Alive", "header");
httpsURLConnection.setRequestProperty("Connection", "close");
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
InputStream inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
2017-09-09 19:05:43 +02:00
readStream(inputStream, account);
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
forceRestart(account);
}
} catch (Exception ignored) {
}
}
});
readThread.start();
}
}
2017-08-28 19:04:13 +02:00
2017-09-04 19:01:41 +02:00
@SuppressWarnings("ConstantConditions")
2017-09-09 19:05:43 +02:00
private String readStream(InputStream inputStream, final Account account) {
BufferedReader reader = null;
try{
reader = new BufferedReader(new InputStreamReader(inputStream));
String event;
2017-09-12 18:41:13 +02:00
EventStreaming eventStreaming;
//noinspection InfiniteLoopStatement
while(true){
try {
event = reader.readLine();
}catch (Exception e){
e.printStackTrace();
2017-09-09 19:05:43 +02:00
forceRestart(account);
break;
}
if (event !=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: ")){
lastEvent = EventStreaming.NONE;
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-08-30 10:01:34 +02:00
lastEvent = EventStreaming.NONE;
try {
JSONObject eventJson = new JSONObject(event);
2017-09-12 18:41:13 +02:00
onRetrieveStreaming(eventStreaming, eventJson, account.getId());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(reader != null){
try{
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
2017-09-09 19:05:43 +02:00
forceRestart(account);
}
return null;
}
2017-09-09 19:05:43 +02:00
private void forceRestart(Account account){
2017-09-12 18:41:13 +02:00
isConnectingHashMap = false;
2017-08-30 15:24:18 +02:00
SystemClock.sleep(1000);
Intent intent = new Intent(getApplicationContext(), StreamingService.class);
2017-09-09 19:05:43 +02:00
intent.putExtra("accountId", account.getId());
intent.putExtra("accountAcct", account.getAcct());
2017-08-30 15:24:18 +02:00
startService(intent);
}
2017-08-29 15:57:51 +02:00
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent intent = new Intent(getApplicationContext(), StreamingService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 5000, pendingIntent);
super.onTaskRemoved(rootIntent);
}
2017-08-28 19:04:13 +02:00
2017-09-12 18:41:13 +02:00
public void onRetrieveStreaming(EventStreaming event, JSONObject response, String userId) {
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;
if( event == EventStreaming.NOTIFICATION){
2017-08-28 19:04:13 +02:00
notification = API.parseNotificationResponse(getApplicationContext(), response);
2017-09-09 19:05:43 +02:00
Helper.cacheNotifications(getApplicationContext(), notification, userId);
}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);
2017-09-09 19:05:43 +02:00
Helper.cacheStatus(getApplicationContext(), status, userId);
}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-12 18:41:13 +02:00
Intent intentBC = new Intent(Helper.RECEIVE_DATA);
intentBC.putExtra("eventStreaming", event);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentBC);
2017-08-29 08:32:09 +02:00
}
2017-08-28 19:04:13 +02:00
}