From 9b8c6635e7adc70886b182fbd9e2ae9f7caa149e Mon Sep 17 00:00:00 2001 From: tom79 Date: Tue, 12 Sep 2017 19:18:55 +0200 Subject: [PATCH] Only connected account receives live events --- .../activities/MainActivity.java | 55 ++++++++++++---- app/src/main/AndroidManifest.xml | 7 +-- .../DisplayNotificationsFragment.java | 57 +---------------- .../fragments/DisplayStatusFragment.java | 63 +------------------ .../etalab/mastodon/services/BootService.java | 38 ----------- .../mastodon/services/StreamingService.java | 13 +++- .../layout-sw600dp/fragment_notifications.xml | 13 ---- .../res/layout-sw600dp/fragment_status.xml | 14 ----- .../res/layout/fragment_notifications.xml | 14 ----- app/src/main/res/layout/fragment_status.xml | 15 ----- .../activities/MainActivity.java | 30 ++++----- 11 files changed, 69 insertions(+), 250 deletions(-) delete mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/services/BootService.java diff --git a/app/src/fdroid/java/fr.gouv.etalab.mastodon/activities/MainActivity.java b/app/src/fdroid/java/fr.gouv.etalab.mastodon/activities/MainActivity.java index dc8566632..fbc91a08d 100644 --- a/app/src/fdroid/java/fr.gouv.etalab.mastodon/activities/MainActivity.java +++ b/app/src/fdroid/java/fr.gouv.etalab.mastodon/activities/MainActivity.java @@ -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); } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f5475c694..355c1c189 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,7 +24,6 @@ - - - - - - + = 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 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); } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java index ffab4fe3d..17608c80b 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java @@ -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 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); } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/services/BootService.java b/app/src/main/java/fr/gouv/etalab/mastodon/services/BootService.java deleted file mode 100644 index 147ef37a0..000000000 --- a/app/src/main/java/fr/gouv/etalab/mastodon/services/BootService.java +++ /dev/null @@ -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 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); - } - } - } - -} \ No newline at end of file diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java b/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java index b41510665..61da454e9 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java @@ -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": diff --git a/app/src/main/res/layout-sw600dp/fragment_notifications.xml b/app/src/main/res/layout-sw600dp/fragment_notifications.xml index 61dda0f44..690ab810f 100644 --- a/app/src/main/res/layout-sw600dp/fragment_notifications.xml +++ b/app/src/main/res/layout-sw600dp/fragment_notifications.xml @@ -80,18 +80,5 @@ android:indeterminate="true" /> - diff --git a/app/src/main/res/layout-sw600dp/fragment_status.xml b/app/src/main/res/layout-sw600dp/fragment_status.xml index 4f819e708..2b9b5bc3e 100644 --- a/app/src/main/res/layout-sw600dp/fragment_status.xml +++ b/app/src/main/res/layout-sw600dp/fragment_status.xml @@ -78,19 +78,5 @@ android:indeterminate="true" /> - - diff --git a/app/src/main/res/layout/fragment_notifications.xml b/app/src/main/res/layout/fragment_notifications.xml index 286913039..d1dd13d21 100644 --- a/app/src/main/res/layout/fragment_notifications.xml +++ b/app/src/main/res/layout/fragment_notifications.xml @@ -79,19 +79,5 @@ android:layout_height="match_parent" android:indeterminate="true" /> - - diff --git a/app/src/main/res/layout/fragment_status.xml b/app/src/main/res/layout/fragment_status.xml index 4d0ac9974..9810aa916 100644 --- a/app/src/main/res/layout/fragment_status.xml +++ b/app/src/main/res/layout/fragment_status.xml @@ -77,20 +77,5 @@ android:layout_height="match_parent" android:indeterminate="true" /> - - - diff --git a/app/src/safetynet/java/fr.gouv.etalab.mastodon/activities/MainActivity.java b/app/src/safetynet/java/fr.gouv.etalab.mastodon/activities/MainActivity.java index ebc6a7d95..3879dac08 100644 --- a/app/src/safetynet/java/fr.gouv.etalab.mastodon/activities/MainActivity.java +++ b/app/src/safetynet/java/fr.gouv.etalab.mastodon/activities/MainActivity.java @@ -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); }