fedilab-Android-App/app/src/main/java/app/fedilab/android/services/BaseStreamingLocalTimelineS...

150 lines
6.1 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.services;
2017-09-30 08:48:44 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-09-30 08:48:44 +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-30 08:48:44 +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-30 08:48:44 +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 16:24:49 +02:00
import android.net.Uri;
2018-11-27 18:18:05 +01:00
import android.os.Build;
2017-09-30 08:48:44 +02:00
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-11-27 18:18:05 +01:00
2018-10-13 16:24:49 +02:00
import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.AsyncHttpRequest;
import com.koushikdutta.async.http.Headers;
2018-11-27 18:18:05 +01:00
2017-09-30 08:48:44 +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;
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-30 08:48:44 +02:00
/**
* Created by Thomas on 29/09/2017.
* Manage service for streaming api for local timeline
*/
2020-07-14 17:25:10 +02:00
public abstract class BaseStreamingLocalTimelineService extends IntentService {
2019-09-06 17:55:14 +02:00
2019-11-15 16:32:25 +01:00
protected Account account;
2017-09-30 08:48:44 +02:00
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
@SuppressWarnings("unused")
2020-07-14 17:25:10 +02:00
public BaseStreamingLocalTimelineService(String name) {
2017-09-30 08:48:44 +02:00
super(name);
}
2020-03-08 10:29:06 +01:00
@SuppressWarnings("unused")
2020-07-14 17:25:10 +02:00
public BaseStreamingLocalTimelineService() {
2017-09-30 08:48:44 +02:00
super("StreamingLocalTimelineService");
}
public void onCreate() {
super.onCreate();
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
2017-09-30 09:01:36 +02:00
boolean display_local = sharedpreferences.getBoolean(Helper.SET_DISPLAY_LOCAL, true);
2019-09-06 17:55:14 +02:00
if (!display_local) {
2017-09-30 08:48:44 +02:00
stopSelf();
}
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2020-07-14 17:25:10 +02:00
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(BaseStreamingLocalTimelineService.this));
2017-12-28 17:39:02 +01:00
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL + userId + instance, true);
2017-09-30 08:48:44 +02:00
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);
2017-09-30 08:48:44 +02: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-07-14 17:25:10 +02:00
accountStream = new AccountDAO(BaseStreamingLocalTimelineService.this, db).getUniqAccount(userId, instance);
2017-09-30 08:48:44 +02:00
}
2018-10-13 16:24:49 +02:00
2019-09-06 17:55:14 +02:00
if (accountStream != null) {
2018-10-13 16:24:49 +02: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=public:local&access_token=" + accountStream.getToken());
2018-10-13 16:24:49 +02:00
AsyncHttpRequest.setDefaultHeaders(headers, url);
Account finalAccountStream = accountStream;
2020-05-16 11:32:09 +02:00
AsyncHttpClient.getDefaultInstance().websocket("wss://" + accountStream.getInstance() + "/api/v1/streaming/?stream=public:local&access_token=" + accountStream.getToken(), "wss", (ex, webSocket) -> {
if (ex != null) {
ex.printStackTrace();
return;
}
webSocket.setStringCallback(s -> {
if (!sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL + finalAccountStream.getId() + finalAccountStream.getInstance(), true)) {
stopSelf();
2017-09-30 08:48:44 +02:00
return;
}
2020-05-16 11:32:09 +02:00
try {
JSONObject eventJson = new JSONObject(s);
onRetrieveStreaming(finalAccountStream, eventJson);
} catch (JSONException ignored) {
}
});
2018-10-13 16:24:49 +02:00
});
2017-09-30 08:48:44 +02:00
}
}
public void onRetrieveStreaming(Account account, JSONObject response) {
2019-09-06 17:55:14 +02:00
if (response == null)
2017-09-30 08:48:44 +02:00
return;
2019-09-06 17:55:14 +02:00
Status status;
2017-09-30 08:48:44 +02:00
Bundle b = new Bundle();
2018-10-13 16:24:49 +02:00
try {
2019-09-06 17:55:14 +02:00
if (response.get("event").toString().equals("update")) {
2020-07-14 17:25:10 +02:00
status = API.parseStatuses(BaseStreamingLocalTimelineService.this, new JSONObject(response.get("payload").toString()));
2018-10-13 16:24:49 +02: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-10-13 16:24:49 +02:00
Intent intentBC = new Intent(Helper.RECEIVE_LOCAL_DATA);
intentBC.putExtras(b);
2020-07-14 17:25:10 +02:00
LocalBroadcastManager.getInstance(BaseStreamingLocalTimelineService.this).sendBroadcast(intentBC);
2018-10-13 16:24:49 +02:00
}
} catch (Exception e) {
e.printStackTrace();
}
2017-09-30 08:48:44 +02:00
}
}