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

168 lines
7.0 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;
import android.support.annotation.Nullable;
import android.support.v4.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;
import com.koushikdutta.async.http.WebSocket;
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;
2017-09-30 08:48:44 +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-30 08:48:44 +02:00
/**
* Created by Thomas on 29/09/2017.
* Manage service for streaming api for local timeline
*/
public class StreamingLocalTimelineService extends IntentService {
2018-10-30 13:46:24 +01:00
static {
Helper.installProvider();
}
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")
2017-09-30 08:48:44 +02:00
public StreamingLocalTimelineService(String name) {
super(name);
}
@SuppressWarnings("unused")
2017-09-30 08:48:44 +02:00
public StreamingLocalTimelineService() {
super("StreamingLocalTimelineService");
}
private static HttpsURLConnection httpsURLConnection;
protected Account account;
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);
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);
2017-12-28 17:39:02 +01:00
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(getApplicationContext()));
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);
Account accountStream = null;
if( userId != null) {
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
accountStream = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
}
2018-10-13 16:24:49 +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:local&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 16:24:49 +02:00
AsyncHttpClient.getDefaultInstance().websocket("wss://" + accountStream.getInstance() + "/api/v1/streaming/?stream=public:local&access_token="+ accountStream.getToken(),"wss", new AsyncHttpClient.WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, WebSocket webSocket) {
if (ex != null) {
ex.printStackTrace();
2017-09-30 08:48:44 +02:00
return;
}
2018-10-13 16:24:49 +02:00
webSocket.setStringCallback(new WebSocket.StringCallback() {
public void onStringAvailable(String s) {
if (!sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL + finalAccountStream.getId() + finalAccountStream.getInstance(), true)) {
stopSelf();
return;
}
try {
JSONObject eventJson = new JSONObject(s);
onRetrieveStreaming(finalAccountStream, eventJson);
} catch (JSONException ignored) {}
}
});
2017-09-30 08:48:44 +02:00
}
2018-10-13 16:24:49 +02:00
});
2017-09-30 08:48:44 +02:00
}
}
public void onRetrieveStreaming(Account account, JSONObject response) {
if( response == null )
return;
Status status ;
Bundle b = new Bundle();
2018-10-13 16:24:49 +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_LOCAL_DATA);
intentBC.putExtras(b);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentBC);
}
} catch (Exception e) {
e.printStackTrace();
}
2017-09-30 08:48:44 +02:00
}
}