Only connected account receives live events

This commit is contained in:
tom79 2017-09-12 19:18:55 +02:00
parent 0fc3214390
commit 9b8c6635e7
11 changed files with 69 additions and 250 deletions

View File

@ -16,10 +16,12 @@ package fr.gouv.etalab.mastodon.activities;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.PorterDuff;
@ -27,6 +29,7 @@ import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
@ -38,7 +41,6 @@ import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
@ -146,19 +148,11 @@ public class MainActivity extends AppCompatActivity
StreamingService.EventStreaming eventStreaming = (StreamingService.EventStreaming) intent.getSerializableExtra("eventStreaming");
if( eventStreaming == StreamingService.EventStreaming.NOTIFICATION){
if(notificationsFragment != null){
if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){
notificationsFragment.showNewContent();
}else{
notificationsFragment.refresh();
}
notificationsFragment.refresh();
}
}else if(eventStreaming == StreamingService.EventStreaming.UPDATE){
if( homeFragment != null){
if(homeFragment.getUserVisibleHint() && isActivityVisible()){
homeFragment.showNewContent();
}else{
homeFragment.refresh();
}
homeFragment.refresh();
}
}else if(eventStreaming == StreamingService.EventStreaming.DELETE){
String id = b.getString("id");
@ -174,6 +168,8 @@ public class MainActivity extends AppCompatActivity
updateHomeCounter();
}
};
Intent intentService = new Intent(this, StreamingService.class);
bindService(intentService, serviceConnection, Context.BIND_AUTO_CREATE);
LocalBroadcastManager.getInstance(this).registerReceiver(receive_data, new IntentFilter(Helper.RECEIVE_DATA));
@ -900,6 +896,37 @@ public class MainActivity extends AppCompatActivity
new UpdateAccountInfoByIDAsyncTask(getApplicationContext(), MainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
StreamingService streamingService = null;
boolean mBound = false;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
StreamingService.StreamingServiceBinder binder = (StreamingService.StreamingServiceBinder) service;
streamingService = binder.getService();
mBound = true;
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SQLiteDatabase db = Sqlite.getInstance(MainActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
streamingService.connect(account);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onPause() {
super.onPause();
@ -909,6 +936,12 @@ public class MainActivity extends AppCompatActivity
@Override
public void onDestroy(){
super.onDestroy();
if( streamingService != null)
streamingService.disconnect();
if (mBound) {
unbindService(serviceConnection);
mBound = false;
}
LocalBroadcastManager.getInstance(this).unregisterReceiver(receive_data);
}

View File

@ -24,7 +24,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="fr.gouv.etalab.mastodon.activities.MainApplication"
@ -38,11 +37,7 @@
<service
android:name="fr.gouv.etalab.mastodon.services.StreamingService"
android:exported="false"/>
<receiver android:name="fr.gouv.etalab.mastodon.services.BootService" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name="fr.gouv.etalab.mastodon.activities.MainActivity"
android:label="@string/app_name"

View File

@ -67,7 +67,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
private SwipeRefreshLayout swipeRefreshLayout;
private boolean swiped;
private ListView lv_notifications;
private TextView new_data;
public DisplayNotificationsFragment(){
}
@ -86,7 +85,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
lv_notifications = (ListView) rootView.findViewById(R.id.lv_notifications);
new_data = (TextView) rootView.findViewById(R.id.new_data);
mainLoader = (RelativeLayout) rootView.findViewById(R.id.loader);
nextElementLoader = (RelativeLayout) rootView.findViewById(R.id.loading_next_notifications);
textviewNoAction = (RelativeLayout) rootView.findViewById(R.id.no_action);
@ -123,7 +121,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
firstLoad = true;
flag_loading = true;
swiped = true;
new_data.setVisibility(View.GONE);
asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
@ -133,33 +130,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notificationsTmp = Helper.getTempNotification(context, null);
if( notificationsTmp != null){
for(int i = notificationsTmp.size() -1 ; i >= 0 ; i--){
notifications.add(0,notificationsTmp.get(i));
}
boolean isOnWifi = Helper.isOnWIFI(context);
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments, notifications);
lv_notifications.setAdapter(notificationsListAdapter);
if( notificationsTmp.size() > 0){
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notificationsTmp.get(0).getId());
editor.apply();
}
if( notificationsTmp.size() > 0 && textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE);
}
new_data.setVisibility(View.GONE);
notificationsTmp = new ArrayList<>();
Helper.cacheNotificationsClear(context, null);
((MainActivity) context).updateNotifCounter();
}
});
return rootView;
}
@ -190,27 +160,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
@Override
public void onResume() {
super.onResume();
//New data are available
notificationsTmp = Helper.getTempNotification(context, null);
if (getUserVisibleHint() && notificationsTmp != null && notificationsTmp.size() > 0 && notifications.size() > 0) {
ArrayList<String> added = new ArrayList<>();
for(Notification notification : notifications){
added.add(notification.getId());
}
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean isOnWifi = Helper.isOnWIFI(context);
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
for(int i = notificationsTmp.size() -1 ; i >= 0 ; i--){
if( !added.contains(notificationsTmp.get(i).getId())) {
this.notifications.add(0, notificationsTmp.get(i));
added.add(notificationsTmp.get(i).getId());
}
}
if( this.notifications.size() > 0 )
max_id = this.notifications.get(this.notifications.size()-1).getId();
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments, notifications);
lv_notifications.setAdapter(notificationsListAdapter);
}
refresh();
}
@Override
@ -288,10 +238,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
}
public void showNewContent(){
new_data.setVisibility(View.VISIBLE);
}
public void refresh(){
if( context == null)
return;
@ -321,6 +267,5 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
if( textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE);
}
new_data.setVisibility(View.GONE);
}
}

View File

@ -32,7 +32,6 @@ import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
@ -76,7 +75,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
private boolean isOnWifi;
private int behaviorWithAttachments;
private boolean showMediaOnly;
private TextView new_data;
private int positionSpinnerTrans;
private boolean hideHeader;
private String instanceValue;
@ -128,7 +126,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
nextElementLoader.setVisibility(View.GONE);
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses);
lv_status.setAdapter(statusListAdapter);
new_data = (TextView) rootView.findViewById(R.id.new_data);
if( !comesFromSearch){
//Hide account header when scrolling for ShowAccountActivity
@ -188,7 +185,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
firstLoad = true;
flag_loading = true;
swiped = true;
new_data.setVisibility(View.GONE);
if( type == RetrieveFeedsAsyncTask.Type.USER)
asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else if( type == RetrieveFeedsAsyncTask.Type.TAG)
@ -215,35 +211,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
textviewNoAction.setVisibility(View.VISIBLE);
}
new_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
statusesTmp = Helper.getTempStatus(context, null);
if( statusesTmp != null){
for(int i = statusesTmp.size() -1 ; i >= 0 ; i--){
statuses.add(0,statusesTmp.get(i));
}
boolean isOnWifi = Helper.isOnWIFI(context);
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, statuses);
lv_status.setAdapter(statusListAdapter);
if( statusesTmp.size() > 0){
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, statusesTmp.get(0).getId());
editor.apply();
}
if( statusesTmp.size() > 0 && textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE);
}
new_data.setVisibility(View.GONE);
statusesTmp = new ArrayList<>();
Helper.cacheStatusClear(context, null);
((MainActivity) context).updateHomeCounter();
}
});
return rootView;
}
@ -259,30 +226,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
@Override
public void onResume() {
super.onResume();
if( type == RetrieveFeedsAsyncTask.Type.HOME ) {
//New data are available
statusesTmp = Helper.getTempStatus(context, null);
if (getUserVisibleHint() && statusesTmp != null && statusesTmp.size() > 0 && statuses.size() > 0) {
ArrayList<String> added = new ArrayList<>();
for (Status status : statuses) {
added.add(status.getId());
}
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean isOnWifi = Helper.isOnWIFI(context);
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
int positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
for (int i = statusesTmp.size() - 1; i >= 0; i--) {
if (!added.contains(statusesTmp.get(i).getId())) {
this.statuses.add(0, statusesTmp.get(i));
added.add(statusesTmp.get(i).getId());
}
}
if (this.statuses.size() > 0)
max_id = this.statuses.get(this.statuses.size() - 1).getId();
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, statuses);
lv_status.setAdapter(statusListAdapter);
}
}
refresh();
}
@Override
@ -372,10 +316,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
}
}
public void showNewContent(){
new_data.setVisibility(View.VISIBLE);
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
@ -414,7 +354,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
if (textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE);
}
new_data.setVisibility(View.GONE);
}
}

View File

@ -1,38 +0,0 @@
package fr.gouv.etalab.mastodon.services;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import java.util.List;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
/**
* Created by Thomas on 29/08/2017.
* BroadcastReceiver to start service when device boot
*/
public class BootService extends BroadcastReceiver {
public BootService() {
}
@Override
public void onReceive(Context context, Intent intent) {
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accounts = new AccountDAO(context, db).getAllAccount();
if( accounts != null){
for (Account account: accounts) {
Intent intentService = new Intent(context, StreamingService.class);
intentService.putExtra("acccountId", account.getId());
intentService.putExtra("accountAcct", account.getAcct());
context.startService(intentService);
}
}
}
}

View File

@ -24,6 +24,8 @@ import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
@ -78,8 +80,14 @@ public class StreamingService extends Service {
public void disconnect(){
if( httpsURLConnection != null)
httpsURLConnection.disconnect();
Thread readThread = new Thread(new Runnable() {
@Override
public void run() {
if( httpsURLConnection != null){
httpsURLConnection.disconnect();
}
}});
readThread.start();
}
@ -165,6 +173,7 @@ public class StreamingService extends Service {
break;
}
if (event !=null){
if( (lastEvent == EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) {
switch (event.trim()) {
case "event: update":

View File

@ -80,18 +80,5 @@
android:indeterminate="true" />
</RelativeLayout>
<TextView
android:visibility="gone"
android:background="@color/mastodonC3"
android:textColor="@color/mastodonC1"
android:clickable="true"
android:padding="10dp"
android:gravity="center"
android:text="@string/new_data"
android:layout_alignParentBottom="true"
android:id="@+id/new_data"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"/>
</RelativeLayout>

View File

@ -78,19 +78,5 @@
android:indeterminate="true" />
</RelativeLayout>
<TextView
android:visibility="gone"
android:padding="10dp"
android:background="@color/mastodonC3"
android:textColor="@color/mastodonC1"
android:clickable="true"
android:gravity="center"
android:text="@string/new_data"
android:layout_alignParentBottom="true"
android:id="@+id/new_data"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"/>
</RelativeLayout>

View File

@ -79,19 +79,5 @@
android:layout_height="match_parent"
android:indeterminate="true" />
</RelativeLayout>
<TextView
android:background="@color/mastodonC3"
android:textColor="@color/mastodonC1"
android:clickable="true"
android:visibility="gone"
android:padding="10dp"
android:gravity="center"
android:text="@string/new_data"
android:layout_alignParentBottom="true"
android:id="@+id/new_data"
android:layout_width="match_parent"
android:textSize="16sp"
android:layout_height="wrap_content"/>
</RelativeLayout>

View File

@ -77,20 +77,5 @@
android:layout_height="match_parent"
android:indeterminate="true" />
</RelativeLayout>
<TextView
android:visibility="gone"
android:background="@color/mastodonC3"
android:textColor="@color/mastodonC1"
android:clickable="true"
android:padding="10dp"
android:gravity="center"
android:text="@string/new_data"
android:layout_alignParentBottom="true"
android:id="@+id/new_data"
android:layout_width="match_parent"
android:textSize="16sp"
android:layout_height="wrap_content"/>
</RelativeLayout>

View File

@ -30,7 +30,6 @@ import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Messenger;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
@ -152,19 +151,11 @@ public class MainActivity extends AppCompatActivity
StreamingService.EventStreaming eventStreaming = (StreamingService.EventStreaming) intent.getSerializableExtra("eventStreaming");
if( eventStreaming == StreamingService.EventStreaming.NOTIFICATION){
if(notificationsFragment != null){
if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){
notificationsFragment.showNewContent();
}else{
notificationsFragment.refresh();
}
notificationsFragment.refresh();
}
}else if(eventStreaming == StreamingService.EventStreaming.UPDATE){
if( homeFragment != null){
if(homeFragment.getUserVisibleHint() && isActivityVisible()){
homeFragment.showNewContent();
}else{
homeFragment.refresh();
}
homeFragment.refresh();
}
}else if(eventStreaming == StreamingService.EventStreaming.DELETE){
String id = b.getString("id");
@ -180,6 +171,8 @@ public class MainActivity extends AppCompatActivity
updateHomeCounter();
}
};
Intent intentService = new Intent(this, StreamingService.class);
bindService(intentService, serviceConnection, Context.BIND_AUTO_CREATE);
LocalBroadcastManager.getInstance(this).registerReceiver(receive_data, new IntentFilter(Helper.RECEIVE_DATA));
@ -931,29 +924,28 @@ public class MainActivity extends AppCompatActivity
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, StreamingService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if (mBound) {
unbindService(serviceConnection);
mBound = false;
}
}
@Override
protected void onPause() {
super.onPause();
MainActivity.activityPaused();
if( streamingService != null)
streamingService.disconnect();
}
@Override
public void onDestroy(){
super.onDestroy();
if( streamingService != null)
streamingService.disconnect();
if (mBound) {
unbindService(serviceConnection);
mBound = false;
}
LocalBroadcastManager.getInstance(this).unregisterReceiver(receive_data);
}