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-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-21 07:21:04 +02:00
|
|
|
import android.os.AsyncTask;
|
2017-09-12 18:14:41 +02:00
|
|
|
import android.os.Binder;
|
2017-09-13 08:38:08 +02:00
|
|
|
import android.os.Bundle;
|
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;
|
2017-09-12 19:18:55 +02:00
|
|
|
import android.util.Log;
|
|
|
|
|
2017-08-28 19:04:13 +02:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2017-08-30 08:59:15 +02:00
|
|
|
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-08-30 08:59:15 +02:00
|
|
|
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;
|
2017-08-30 08:59:15 +02:00
|
|
|
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
|
|
|
|
2017-09-17 15:34:41 +02:00
|
|
|
private static HttpsURLConnection httpsURLConnection;
|
2017-09-21 07:21:04 +02:00
|
|
|
private static boolean isRunning = false;
|
|
|
|
private AsyncTask<Void, Void, Void> callToStreaming;
|
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-09-21 07:21:04 +02:00
|
|
|
protected Account account;
|
2017-08-30 15:24:18 +02:00
|
|
|
|
2017-09-13 08:38:08 +02:00
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
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-17 10:55:55 +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-09-21 07:21:04 +02:00
|
|
|
this.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);
|
2017-09-17 10:55:55 +02:00
|
|
|
boolean notif_home = sharedpreferences.getBoolean(Helper.SET_NOTIF_HOMETIMELINE, true);
|
2017-08-28 19:04:13 +02:00
|
|
|
//User disagree with all notifications
|
2017-09-17 10:55:55 +02:00
|
|
|
if( !notif_home && !notif_follow && !notif_add && !notif_ask && !notif_mention && !notif_share) {
|
2017-08-28 19:04:13 +02:00
|
|
|
return; //Nothing is done
|
2017-09-17 10:55:55 +02:00
|
|
|
|
|
|
|
}
|
2017-09-21 07:21:04 +02:00
|
|
|
if( callToStreaming != null)
|
|
|
|
callToStreaming.cancel(true);
|
2017-08-28 19:04:13 +02:00
|
|
|
//No account connected, the service is stopped
|
|
|
|
//If WIFI only and on WIFI OR user defined any connections to use the service.
|
2017-09-21 07:21:04 +02:00
|
|
|
if (!sharedpreferences.getBoolean(Helper.SET_WIFI_ONLY, false) || Helper.isOnWIFI(getApplicationContext())) {
|
|
|
|
if (!isRunning)
|
|
|
|
callToStreaming = new CallToStreaming().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-08-30 08:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 07:21:04 +02:00
|
|
|
@Override
|
|
|
|
public void onDestroy(){
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
2017-08-30 08:59:15 +02:00
|
|
|
|
2017-09-21 07:21:04 +02:00
|
|
|
public void longPolling() {
|
2017-09-17 10:55:55 +02:00
|
|
|
//noinspection InfiniteLoopStatement
|
2017-09-17 15:34:41 +02:00
|
|
|
InputStream inputStream;
|
|
|
|
BufferedReader reader = null;
|
|
|
|
try {
|
2017-09-21 07:21:04 +02:00
|
|
|
URL url = new URL("https://" + this.account.getInstance() + "/api/v1/streaming/user");
|
2017-09-17 15:34:41 +02:00
|
|
|
if( httpsURLConnection != null){
|
|
|
|
httpsURLConnection.disconnect();
|
|
|
|
}
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
|
|
|
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
|
2017-09-21 07:21:04 +02:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + this.account.getToken());
|
2017-09-17 15:34:41 +02:00
|
|
|
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);
|
|
|
|
// httpsURLConnection.connect();
|
|
|
|
inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
|
|
|
|
reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
|
String event;
|
|
|
|
EventStreaming eventStreaming;
|
|
|
|
//noinspection InfiniteLoopStatement
|
2017-09-21 07:21:04 +02:00
|
|
|
while((event = reader.readLine()) != null) {
|
|
|
|
if ((lastEvent == EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) {
|
2017-09-17 15:34:41 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-09-21 07:21:04 +02:00
|
|
|
} else {
|
|
|
|
if (!event.startsWith("data: ")) {
|
2017-09-17 15:34:41 +02:00
|
|
|
lastEvent = EventStreaming.NONE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
event = event.substring(6);
|
2017-09-21 07:21:04 +02:00
|
|
|
if (lastEvent == EventStreaming.UPDATE) {
|
2017-09-17 15:34:41 +02:00
|
|
|
eventStreaming = EventStreaming.UPDATE;
|
2017-09-21 07:21:04 +02:00
|
|
|
} else if (lastEvent == EventStreaming.NOTIFICATION) {
|
2017-09-17 15:34:41 +02:00
|
|
|
eventStreaming = EventStreaming.NOTIFICATION;
|
2017-09-21 07:21:04 +02:00
|
|
|
} else if (lastEvent == EventStreaming.DELETE) {
|
2017-09-17 15:34:41 +02:00
|
|
|
eventStreaming = EventStreaming.DELETE;
|
|
|
|
event = "{id:" + event + "}";
|
2017-09-21 07:21:04 +02:00
|
|
|
} else {
|
2017-09-17 15:34:41 +02:00
|
|
|
eventStreaming = EventStreaming.UPDATE;
|
|
|
|
}
|
|
|
|
lastEvent = EventStreaming.NONE;
|
|
|
|
try {
|
|
|
|
JSONObject eventJson = new JSONObject(event);
|
2017-09-21 07:21:04 +02:00
|
|
|
onRetrieveStreaming(eventStreaming, eventJson);
|
2017-09-17 15:34:41 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-09-01 16:28:40 +02:00
|
|
|
}
|
2017-09-17 15:34:41 +02:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}finally {
|
|
|
|
if(reader != null){
|
2017-09-17 10:55:55 +02:00
|
|
|
try{
|
2017-09-17 15:34:41 +02:00
|
|
|
reader.close();
|
|
|
|
}catch (IOException e){
|
2017-09-17 10:55:55 +02:00
|
|
|
e.printStackTrace();
|
2017-08-30 08:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-21 07:21:04 +02:00
|
|
|
isRunning = false;
|
|
|
|
connect(this.account);
|
2017-08-30 08:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 10:55:55 +02:00
|
|
|
|
2017-09-21 07:21:04 +02:00
|
|
|
public void onRetrieveStreaming(EventStreaming event, 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;
|
2017-09-13 08:38:08 +02:00
|
|
|
|
|
|
|
Bundle b = new Bundle();
|
2017-08-30 08:59:15 +02:00
|
|
|
if( event == EventStreaming.NOTIFICATION){
|
2017-08-28 19:04:13 +02:00
|
|
|
notification = API.parseNotificationResponse(getApplicationContext(), response);
|
2017-09-13 08:38:08 +02:00
|
|
|
b.putParcelable("data", notification);
|
2017-08-30 08:59:15 +02:00
|
|
|
}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-13 08:38:08 +02:00
|
|
|
b.putParcelable("data", status);
|
2017-08-30 08:59:15 +02:00
|
|
|
}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-22 07:22:38 +02:00
|
|
|
b.putString("userIdService",account.getId());
|
2017-09-12 18:41:13 +02:00
|
|
|
Intent intentBC = new Intent(Helper.RECEIVE_DATA);
|
|
|
|
intentBC.putExtra("eventStreaming", event);
|
2017-09-13 08:38:08 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
private class CallToStreaming extends AsyncTask<Void, Void, Void> {
|
|
|
|
|
|
|
|
|
|
|
|
CallToStreaming(){
|
|
|
|
isRunning = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
|
|
|
if(!Helper.isConnectedToInternet(getApplicationContext(), account.getInstance())) {
|
|
|
|
SystemClock.sleep(2000);
|
|
|
|
isRunning = false;
|
|
|
|
connect(account);
|
|
|
|
}else {
|
|
|
|
longPolling();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void result) {
|
|
|
|
isRunning = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-08-28 19:04:13 +02:00
|
|
|
}
|