2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.services;
|
2018-12-18 19:08:59 +01:00
|
|
|
/* Copyright 2018 Thomas Schneider
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* This file is a part of Fedilab
|
2018-12-18 19:08:59 +01: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
|
2018-12-18 19:08:59 +01: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,
|
2018-12-18 19:08:59 +01: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;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2019-06-11 19:38:26 +02:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2018-12-18 19:08:59 +01:00
|
|
|
|
|
|
|
import com.koushikdutta.async.http.AsyncHttpClient;
|
|
|
|
import com.koushikdutta.async.http.AsyncHttpRequest;
|
|
|
|
import com.koushikdutta.async.http.Headers;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
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;
|
2018-12-18 19:08:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 18/12/2018.
|
|
|
|
* Manage service for streaming api for home and notifications
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class StreamingHomeTimelineService extends IntentService {
|
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
|
2018-12-18 19:08:59 +01:00
|
|
|
static {
|
|
|
|
Helper.installProvider();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
protected Account account;
|
|
|
|
|
2018-12-18 19:08:59 +01:00
|
|
|
/**
|
|
|
|
* Creates an IntentService. Invoked by your subclass's constructor.
|
|
|
|
*
|
|
|
|
* @param name Used to name the worker thread, important only for debugging.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public StreamingHomeTimelineService(String name) {
|
|
|
|
super(name);
|
|
|
|
}
|
2020-03-08 10:29:06 +01:00
|
|
|
|
2018-12-18 19:08:59 +01:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public StreamingHomeTimelineService() {
|
|
|
|
super("StreamingHomeTimelineService");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
boolean display_global = sharedpreferences.getBoolean(Helper.SET_DISPLAY_GLOBAL, true);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (!display_global) {
|
2018-12-18 19:08:59 +01:00
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
2020-04-08 12:42:15 +02:00
|
|
|
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(StreamingHomeTimelineService.this));
|
2018-12-18 19:08:59 +01:00
|
|
|
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_HOME + userId + instance, true);
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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);
|
2018-12-18 19:08:59 +01:00
|
|
|
Account accountStream = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (userId != null) {
|
2020-04-09 18:57:12 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
2020-04-08 12:42:15 +02:00
|
|
|
accountStream = new AccountDAO(StreamingHomeTimelineService.this, db).getUniqAccount(userId, instance);
|
2018-12-18 19:08:59 +01:00
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
if (accountStream != null) {
|
2018-12-18 19:08:59 +01:00
|
|
|
Headers headers = new Headers();
|
|
|
|
headers.add("Authorization", "Bearer " + accountStream.getToken());
|
|
|
|
headers.add("Connection", "Keep-Alive");
|
|
|
|
headers.add("method", "GET");
|
|
|
|
headers.add("scheme", "https");
|
2019-09-06 17:55:14 +02:00
|
|
|
Uri url = Uri.parse("wss://" + accountStream.getInstance() + "/api/v1/streaming/?stream=user&access_token=" + accountStream.getToken());
|
2018-12-18 19:08:59 +01:00
|
|
|
AsyncHttpRequest.setDefaultHeaders(headers, url);
|
|
|
|
Account finalAccountStream = accountStream;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
|
2018-12-18 19:08:59 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setSSLContext(new TLSSocketFactory(accountStream.getInstance()).getSSLContext());
|
2018-12-18 19:08:59 +01:00
|
|
|
AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setConnectAllAddresses(true);
|
2020-05-16 11:32:09 +02:00
|
|
|
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
2018-12-18 19:08:59 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2020-05-16 11:32:09 +02:00
|
|
|
AsyncHttpClient.getDefaultInstance().websocket("wss://" + accountStream.getInstance() + "/api/v1/streaming/?stream=user&access_token=" + accountStream.getToken(), "wss", (ex, webSocket) -> {
|
|
|
|
if (ex != null) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
webSocket.setStringCallback(s -> {
|
|
|
|
if (!sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING_HOME + finalAccountStream.getId() + finalAccountStream.getInstance(), true)) {
|
|
|
|
stopSelf();
|
2018-12-18 19:08:59 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-05-16 11:32:09 +02:00
|
|
|
try {
|
|
|
|
JSONObject eventJson = new JSONObject(s);
|
|
|
|
|
|
|
|
onRetrieveStreaming(finalAccountStream, eventJson);
|
|
|
|
} catch (JSONException ignored) {
|
|
|
|
}
|
|
|
|
});
|
2018-12-18 19:08:59 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onRetrieveStreaming(Account account, JSONObject response) {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (response == null)
|
2018-12-18 19:08:59 +01:00
|
|
|
return;
|
2019-09-06 17:55:14 +02:00
|
|
|
Status status;
|
2018-12-18 19:08:59 +01:00
|
|
|
Bundle b = new Bundle();
|
|
|
|
try {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (response.get("event").toString().equals("update")) {
|
2020-04-08 12:42:15 +02:00
|
|
|
status = API.parseStatuses(StreamingHomeTimelineService.this, new JSONObject(response.get("payload").toString()));
|
2018-12-18 19:08:59 +01:00
|
|
|
status.setNew(true);
|
|
|
|
b.putParcelable("data", status);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (account != null)
|
|
|
|
b.putString("userIdService", account.getId());
|
2018-12-19 10:54:26 +01:00
|
|
|
Intent intentBC = new Intent(Helper.RECEIVE_HOME_DATA);
|
2018-12-18 19:08:59 +01:00
|
|
|
intentBC.putExtras(b);
|
2020-04-08 12:42:15 +02:00
|
|
|
LocalBroadcastManager.getInstance(StreamingHomeTimelineService.this).sendBroadcast(intentBC);
|
2018-12-18 19:08:59 +01:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|