Fix for federate timeline
This commit is contained in:
parent
5d421dbfb1
commit
e767ed8beb
|
@ -70,6 +70,7 @@ dependencies {
|
||||||
implementation "com.gongwen:swipeback:$swipebackLibraryVersion"
|
implementation "com.gongwen:swipeback:$swipebackLibraryVersion"
|
||||||
implementation 'com.github.stom79:country-picker-android:1.2.0'
|
implementation 'com.github.stom79:country-picker-android:1.2.0'
|
||||||
implementation 'com.github.stom79:mytransl:1.5'
|
implementation 'com.github.stom79:mytransl:1.5'
|
||||||
|
implementation "com.koushikdutta.async:androidasync:2.+"
|
||||||
playstoreImplementation "io.github.kobakei:ratethisapp:$ratethisappLibraryVersion"
|
playstoreImplementation "io.github.kobakei:ratethisapp:$ratethisappLibraryVersion"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -19,29 +19,23 @@ import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.SystemClock;
|
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
|
import android.util.Log;
|
||||||
|
import com.koushikdutta.async.http.AsyncHttpClient;
|
||||||
|
import com.koushikdutta.async.http.AsyncHttpRequest;
|
||||||
|
import com.koushikdutta.async.http.Headers;
|
||||||
|
import com.koushikdutta.async.http.WebSocket;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import javax.net.ssl.HttpsURLConnection;
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
|
||||||
import fr.gouv.etalab.mastodon.client.API;
|
import fr.gouv.etalab.mastodon.client.API;
|
||||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||||
import fr.gouv.etalab.mastodon.client.Entities.Status;
|
import fr.gouv.etalab.mastodon.client.Entities.Status;
|
||||||
import fr.gouv.etalab.mastodon.client.TLSSocketFactory;
|
|
||||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||||
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
||||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||||
|
@ -98,55 +92,37 @@ public class StreamingFederatedTimelineService extends IntentService {
|
||||||
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||||
accountStream = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
|
accountStream = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
|
||||||
}
|
}
|
||||||
if( accountStream != null){
|
if( accountStream != null) {
|
||||||
try {
|
Headers headers = new Headers();
|
||||||
if(!Helper.isConnectedToInternet(StreamingFederatedTimelineService.this, accountStream.getInstance()))
|
headers.add("Authorization", "Bearer " + accountStream.getToken());
|
||||||
return;
|
headers.add("Connection", "Keep-Alive");
|
||||||
URL url = new URL("https://" + accountStream.getInstance() + "/api/v1/streaming/public");
|
headers.add("method", "GET");
|
||||||
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
headers.add("scheme", "https");
|
||||||
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
|
Uri url = Uri.parse("wss://" + accountStream.getInstance() + "/api/v1/streaming/?stream=public&access_token="+ accountStream.getToken());
|
||||||
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + accountStream.getToken());
|
AsyncHttpRequest.setDefaultHeaders(headers, url);
|
||||||
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
|
Account finalAccountStream = accountStream;
|
||||||
httpsURLConnection.setRequestProperty("Keep-Alive", "header");
|
AsyncHttpClient.getDefaultInstance().websocket("wss://" + accountStream.getInstance() + "/api/v1/streaming/?stream=public&access_token="+ accountStream.getToken(),"wss", new AsyncHttpClient.WebSocketConnectCallback() {
|
||||||
httpsURLConnection.setRequestProperty("Connection", "close");
|
@Override
|
||||||
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
|
public void onCompleted(Exception ex, WebSocket webSocket) {
|
||||||
httpsURLConnection.setRequestMethod("GET");
|
if (ex != null) {
|
||||||
httpsURLConnection.setConnectTimeout(70000);
|
ex.printStackTrace();
|
||||||
httpsURLConnection.setReadTimeout(70000);
|
|
||||||
inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
|
|
||||||
reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
||||||
String event;
|
|
||||||
while((event = reader.readLine()) != null) {
|
|
||||||
if (!sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + accountStream.getId() + accountStream.getInstance(), true)) {
|
|
||||||
stopSelf();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!event.startsWith("data: ")) {
|
webSocket.setStringCallback(new WebSocket.StringCallback() {
|
||||||
continue;
|
public void onStringAvailable(String s) {
|
||||||
}
|
if (!sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + finalAccountStream.getId() + finalAccountStream.getInstance(), true)) {
|
||||||
event = event.substring(6);
|
stopSelf();
|
||||||
if( event.matches("^[0-9]{1,}$"))
|
return;
|
||||||
continue;
|
}
|
||||||
try {
|
try {
|
||||||
JSONObject eventJson = new JSONObject(event);
|
JSONObject eventJson = new JSONObject(s);
|
||||||
onRetrieveStreaming(accountStream, eventJson);
|
|
||||||
} catch (JSONException ignored) {}
|
onRetrieveStreaming(finalAccountStream, eventJson);
|
||||||
|
} catch (JSONException ignored) {}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {
|
});
|
||||||
}finally {
|
|
||||||
if(reader != null){
|
|
||||||
try{
|
|
||||||
reader.close();
|
|
||||||
}catch (IOException ignored){}
|
|
||||||
}
|
|
||||||
if( sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + accountStream.getId() + accountStream.getInstance(), true)) {
|
|
||||||
SystemClock.sleep(1000);
|
|
||||||
Intent streamingFederatedTimelineService = new Intent(this, StreamingFederatedTimelineService.class);
|
|
||||||
try {
|
|
||||||
startService(streamingFederatedTimelineService);
|
|
||||||
}catch (Exception ignored){}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,14 +131,22 @@ public class StreamingFederatedTimelineService extends IntentService {
|
||||||
return;
|
return;
|
||||||
Status status ;
|
Status status ;
|
||||||
Bundle b = new Bundle();
|
Bundle b = new Bundle();
|
||||||
status = API.parseStatuses(getApplicationContext(), response);
|
try {
|
||||||
status.setNew(true);
|
if( response.get("event").toString().equals("update")){
|
||||||
b.putParcelable("data", status);
|
status = API.parseStatuses(getApplicationContext(), new JSONObject(response.get("payload").toString()));
|
||||||
if( account != null)
|
status.setNew(true);
|
||||||
b.putString("userIdService",account.getId());
|
Log.v(Helper.TAG,"status: " + status);
|
||||||
Intent intentBC = new Intent(Helper.RECEIVE_FEDERATED_DATA);
|
b.putParcelable("data", status);
|
||||||
intentBC.putExtras(b);
|
if( account != null)
|
||||||
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentBC);
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue