Improves streaming API + fixes an issue with blank text at the end of toots

This commit is contained in:
tom79 2017-09-01 16:28:40 +02:00
parent a82db61579
commit 7ec14069fd
5 changed files with 85 additions and 48 deletions

View File

@ -141,7 +141,7 @@ public class MainActivity extends AppCompatActivity
if( eventStreaming == StreamingService.EventStreaming.NOTIFICATION){
Notification notification = b.getParcelable("data");
if(notificationsFragment != null){
if(notificationsFragment.getUserVisibleHint()){
if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){
notificationsFragment.updateData(notification);
}else{
newNotif++;
@ -155,7 +155,7 @@ public class MainActivity extends AppCompatActivity
}else if(eventStreaming == StreamingService.EventStreaming.UPDATE){
Status status = b.getParcelable("data");
if( homeFragment != null){
if(homeFragment.getUserVisibleHint()){
if(homeFragment.getUserVisibleHint() && isActivityVisible()){
homeFragment.updateData(status);
}else{
newHome++;

View File

@ -503,7 +503,10 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
}
}
final String content, displayName, username, ppurl;
String content;
final String displayName;
final String username;
final String ppurl;
if( status.getReblog() != null){
content = status.getReblog().getContent();
displayName = Helper.shortnameToUnicode(status.getReblog().getAccount().getDisplay_name(), true);
@ -569,7 +572,10 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
});
holder.status_content_translated.setMovementMethod(LinkMovementMethod.getInstance());
}
content = content.replaceAll("</p>","<br/>");
content = content.replaceAll("<p>","");
if( content.endsWith("<br/>") )
content = content.substring(0,content.length() -5);
final SpannableString spannableString = Helper.clickableElements(context,content,
status.getReblog() != null?status.getReblog().getMentions():status.getMentions(), true);
holder.status_content.setText(spannableString, TextView.BufferType.SPANNABLE);

View File

@ -180,10 +180,11 @@ public class StreamingService extends Service {
urlConnection.setRequestProperty("Authorization", "Bearer " + account.getToken());
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Keep-Alive", "header");
urlConnection.setRequestProperty("Connection", "close");
urlConnection.setSSLSocketFactory(new TLSSocketFactory());
connectionHashMap.put(account.getAcct()+account.getId(), urlConnection);
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
readStream(inputStream, account);
readStream(inputStream, urlConnection, account);
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
forceRestart();
@ -192,53 +193,78 @@ public class StreamingService extends Service {
private String readStream(InputStream inputStream, final Account account) {
private String readStream(InputStream inputStream, HttpsURLConnection urlConnection, final Account account) {
BufferedReader reader = null;
try{
reader = new BufferedReader(new InputStreamReader(inputStream));
String event;
EventStreaming eventStreaming = null;
while((event = reader.readLine()) != null){
if( lastEvent == EventStreaming.NONE || lastEvent == null) {
switch (event.trim()) {
case "event: update":
lastEvent = EventStreaming.UPDATE;
break;
case "event: notification":
lastEvent = EventStreaming.NOTIFICATION;
break;
case "event: delete":
lastEvent = EventStreaming.DELETE;
break;
default:
lastEvent = EventStreaming.NONE;
}
}else{
//event = event.replace("data: ","");
if( !event.startsWith("data: ")){
lastEvent = EventStreaming.NONE;
continue;
}
event = event.substring(6);
if(lastEvent == EventStreaming.UPDATE) {
eventStreaming = EventStreaming.UPDATE;
}else if(lastEvent == EventStreaming.NOTIFICATION) {
eventStreaming = EventStreaming.NOTIFICATION;
}else if( lastEvent == EventStreaming.DELETE) {
eventStreaming = EventStreaming.DELETE;
event = "{id:" + event + "}";
}
lastEvent = EventStreaming.NONE;
//noinspection InfiniteLoopStatement
while(true){
try {
event = reader.readLine();
}catch (Exception e){
e.printStackTrace();
urlConnection.disconnect();
URL url;
try {
JSONObject eventJson = new JSONObject(event);
onRetrieveStreaming(eventStreaming, eventJson, account.getAcct(), account.getId());
} catch (JSONException e) {
e.printStackTrace();
url = new URL("https://" + account.getInstance() + "/api/v1/streaming/user");
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Authorization", "Bearer " + account.getToken());
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Keep-Alive", "header");
urlConnection.setRequestProperty("Connection", "close");
urlConnection.setSSLSocketFactory(new TLSSocketFactory());
connectionHashMap.put(account.getAcct()+account.getId(), urlConnection);
inputStream = new BufferedInputStream(urlConnection.getInputStream());
reader = new BufferedReader(new InputStreamReader(inputStream));
} catch (NoSuchAlgorithmException | KeyManagementException | IOException ee) {
ee.printStackTrace();
}
SystemClock.sleep(5000);
event = null;
}
if (event !=null){
if( lastEvent == EventStreaming.NONE || lastEvent == null) {
switch (event.trim()) {
case "event: update":
lastEvent = EventStreaming.UPDATE;
break;
case "event: notification":
lastEvent = EventStreaming.NOTIFICATION;
break;
case "event: delete":
lastEvent = EventStreaming.DELETE;
break;
default:
lastEvent = EventStreaming.NONE;
}
}else{
if( !event.startsWith("data: ")){
lastEvent = EventStreaming.NONE;
continue;
}
event = event.substring(6);
if(lastEvent == EventStreaming.UPDATE) {
eventStreaming = EventStreaming.UPDATE;
}else if(lastEvent == EventStreaming.NOTIFICATION) {
eventStreaming = EventStreaming.NOTIFICATION;
}else if( lastEvent == EventStreaming.DELETE) {
eventStreaming = EventStreaming.DELETE;
event = "{id:" + event + "}";
}
lastEvent = EventStreaming.NONE;
try {
JSONObject eventJson = new JSONObject(event);
onRetrieveStreaming(eventStreaming, eventJson, account.getAcct(), account.getId());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}catch (Exception e){
e.printStackTrace();
@ -409,7 +435,8 @@ public class StreamingService extends Service {
//User receiving the notification is connected and application is to front, notification won't be pushed
//Instead, the interaction is done in the activity
if( activityVisible && isCurrentAccountLoggedIn(acct, userId)){
if( isCurrentAccountLoggedIn(acct, userId)){
notify = false;
Intent intentBC = new Intent(Helper.RECEIVE_DATA);
intentBC.putExtra("eventStreaming", event);
@ -422,6 +449,10 @@ public class StreamingService extends Service {
b.putString("id", dataId);
intentBC.putExtras(b);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentBC);
}
if( activityVisible && isCurrentAccountLoggedIn(acct, userId)){
notify = false;
}else if(event == EventStreaming.NOTIFICATION ){
notify = true;

View File

@ -342,7 +342,7 @@
android:id="@+id/status_action_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginTop="5dp"
android:padding="@dimen/drawer_padding"
android:layout_marginStart="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_vertical_margin"

View File

@ -145,7 +145,7 @@ public class MainActivity extends AppCompatActivity
if( eventStreaming == StreamingService.EventStreaming.NOTIFICATION){
Notification notification = b.getParcelable("data");
if(notificationsFragment != null){
if(notificationsFragment.getUserVisibleHint()){
if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){
notificationsFragment.updateData(notification);
}else{
newNotif++;
@ -159,7 +159,7 @@ public class MainActivity extends AppCompatActivity
}else if(eventStreaming == StreamingService.EventStreaming.UPDATE){
Status status = b.getParcelable("data");
if( homeFragment != null){
if(homeFragment.getUserVisibleHint()){
if(homeFragment.getUserVisibleHint() && isActivityVisible()){
homeFragment.updateData(status);
}else{
newHome++;