2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.services;
|
2017-09-26 19:19:53 +02:00
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* This file is a part of Fedilab
|
2017-09-26 19:19:53 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-09-26 19:19:53 +02:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
2017-09-26 19:19:53 +02:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
import android.app.IntentService;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2018-10-13 15:59:15 +02:00
|
|
|
import android.net.Uri;
|
2018-11-27 18:18:05 +01:00
|
|
|
import android.os.Build;
|
2017-09-26 19:19:53 +02:00
|
|
|
import android.os.Bundle;
|
2019-06-11 19:38:26 +02:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2018-11-27 18:18:05 +01:00
|
|
|
|
2018-10-13 15:59:15 +02:00
|
|
|
import com.koushikdutta.async.http.AsyncHttpClient;
|
|
|
|
import com.koushikdutta.async.http.AsyncHttpRequest;
|
|
|
|
import com.koushikdutta.async.http.Headers;
|
|
|
|
import com.koushikdutta.async.http.WebSocket;
|
2018-11-27 18:18:05 +01:00
|
|
|
|
2017-09-26 19:19:53 +02:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2018-11-27 18:18:05 +01:00
|
|
|
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
2017-09-26 19:19:53 +02:00
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
2018-11-27 18:18:05 +01:00
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.client.API;
|
|
|
|
import app.fedilab.android.client.Entities.Account;
|
|
|
|
import app.fedilab.android.client.Entities.Status;
|
|
|
|
import app.fedilab.android.client.TLSSocketFactory;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.sqlite.AccountDAO;
|
|
|
|
import app.fedilab.android.sqlite.Sqlite;
|
2017-09-26 19:19:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 26/09/2017.
|
|
|
|
* Manage service for streaming api for federated timeline
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class StreamingFederatedTimelineService extends IntentService {
|
|
|
|
|
2018-10-30 13:46:24 +01:00
|
|
|
static {
|
|
|
|
Helper.installProvider();
|
|
|
|
}
|
2017-09-26 19:19:53 +02:00
|
|
|
/**
|
|
|
|
* Creates an IntentService. Invoked by your subclass's constructor.
|
|
|
|
*
|
|
|
|
* @param name Used to name the worker thread, important only for debugging.
|
|
|
|
*/
|
2017-11-12 08:33:24 +01:00
|
|
|
@SuppressWarnings("unused")
|
2017-09-26 19:19:53 +02:00
|
|
|
public StreamingFederatedTimelineService(String name) {
|
|
|
|
super(name);
|
|
|
|
}
|
2017-11-12 08:33:24 +01:00
|
|
|
@SuppressWarnings("unused")
|
2017-09-26 19:19:53 +02:00
|
|
|
public StreamingFederatedTimelineService() {
|
2017-09-30 08:48:44 +02:00
|
|
|
super("StreamingFederatedTimelineService");
|
2017-09-26 19:19:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static HttpsURLConnection httpsURLConnection;
|
|
|
|
protected Account account;
|
|
|
|
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2017-09-26 19:35:02 +02:00
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
boolean display_global = sharedpreferences.getBoolean(Helper.SET_DISPLAY_GLOBAL, true);
|
|
|
|
if( !display_global){
|
|
|
|
stopSelf();
|
|
|
|
}
|
2017-09-29 14:08:46 +02:00
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
2017-12-28 17:39:02 +01:00
|
|
|
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(getApplicationContext()));
|
|
|
|
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + userId + instance, true);
|
2017-09-29 14:08:46 +02:00
|
|
|
editor.apply();
|
2017-09-26 19:19:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onHandleIntent(@Nullable Intent intent) {
|
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
2019-06-05 14:35:42 +02:00
|
|
|
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
|
2017-09-26 19:19:53 +02:00
|
|
|
Account accountStream = null;
|
|
|
|
if( userId != null) {
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
2019-06-05 14:35:42 +02:00
|
|
|
accountStream = new AccountDAO(getApplicationContext(), db).getUniqAccount(userId, instance);
|
2017-09-26 19:19:53 +02:00
|
|
|
}
|
2018-10-13 15:59:15 +02:00
|
|
|
if( accountStream != null) {
|
|
|
|
Headers headers = new Headers();
|
|
|
|
headers.add("Authorization", "Bearer " + accountStream.getToken());
|
|
|
|
headers.add("Connection", "Keep-Alive");
|
|
|
|
headers.add("method", "GET");
|
|
|
|
headers.add("scheme", "https");
|
|
|
|
Uri url = Uri.parse("wss://" + accountStream.getInstance() + "/api/v1/streaming/?stream=public&access_token="+ accountStream.getToken());
|
|
|
|
AsyncHttpRequest.setDefaultHeaders(headers, url);
|
|
|
|
Account finalAccountStream = accountStream;
|
2018-11-27 18:18:05 +01:00
|
|
|
if( Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ) {
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setSSLContext(new TLSSocketFactory(accountStream.getInstance()).getSSLContext());
|
2018-11-27 18:18:05 +01:00
|
|
|
AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setConnectAllAddresses(true);
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 15:59:15 +02:00
|
|
|
AsyncHttpClient.getDefaultInstance().websocket("wss://" + accountStream.getInstance() + "/api/v1/streaming/?stream=public&access_token="+ accountStream.getToken(),"wss", new AsyncHttpClient.WebSocketConnectCallback() {
|
|
|
|
@Override
|
|
|
|
public void onCompleted(Exception ex, WebSocket webSocket) {
|
|
|
|
if (ex != null) {
|
|
|
|
ex.printStackTrace();
|
2017-09-29 07:19:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-10-13 15:59:15 +02:00
|
|
|
webSocket.setStringCallback(new WebSocket.StringCallback() {
|
|
|
|
public void onStringAvailable(String s) {
|
|
|
|
if (!sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + finalAccountStream.getId() + finalAccountStream.getInstance(), true)) {
|
|
|
|
stopSelf();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
JSONObject eventJson = new JSONObject(s);
|
|
|
|
|
|
|
|
onRetrieveStreaming(finalAccountStream, eventJson);
|
|
|
|
} catch (JSONException ignored) {}
|
|
|
|
}
|
|
|
|
});
|
2017-09-26 19:19:53 +02:00
|
|
|
}
|
2018-10-13 15:59:15 +02:00
|
|
|
});
|
2017-09-26 19:19:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onRetrieveStreaming(Account account, JSONObject response) {
|
|
|
|
if( response == null )
|
|
|
|
return;
|
|
|
|
Status status ;
|
|
|
|
Bundle b = new Bundle();
|
2018-10-13 15:59:15 +02:00
|
|
|
try {
|
|
|
|
if( response.get("event").toString().equals("update")){
|
|
|
|
status = API.parseStatuses(getApplicationContext(), new JSONObject(response.get("payload").toString()));
|
|
|
|
status.setNew(true);
|
|
|
|
b.putParcelable("data", status);
|
|
|
|
if( account != null)
|
|
|
|
b.putString("userIdService",account.getId());
|
|
|
|
Intent intentBC = new Intent(Helper.RECEIVE_FEDERATED_DATA);
|
|
|
|
intentBC.putExtras(b);
|
|
|
|
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentBC);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2017-09-26 19:19:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|