New toots are sent directly to activity

This commit is contained in:
tom79 2017-09-13 08:38:08 +02:00
parent 9b8c6635e7
commit 6ffce804e9
5 changed files with 50 additions and 210 deletions

View File

@ -77,6 +77,8 @@ import java.util.regex.Matcher;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveMetaDataAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoByIDAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Notification;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.fragments.DisplayAccountsFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayFollowRequestSentFragment;
@ -131,6 +133,8 @@ public class MainActivity extends AppCompatActivity
private DisplayNotificationsFragment notificationsFragment;
private BroadcastReceiver receive_data;
private boolean display_local, display_global;
private int countNewStatus = 0;
private int countNewNotifications = 0;
public MainActivity() {
}
@ -148,11 +152,13 @@ public class MainActivity extends AppCompatActivity
StreamingService.EventStreaming eventStreaming = (StreamingService.EventStreaming) intent.getSerializableExtra("eventStreaming");
if( eventStreaming == StreamingService.EventStreaming.NOTIFICATION){
if(notificationsFragment != null){
notificationsFragment.refresh();
Notification notification = b.getParcelable("data");
notificationsFragment.refresh(notification);
}
}else if(eventStreaming == StreamingService.EventStreaming.UPDATE){
Status status = b.getParcelable("data");
if( homeFragment != null){
homeFragment.refresh();
homeFragment.refresh(status);
}
}else if(eventStreaming == StreamingService.EventStreaming.DELETE){
String id = b.getString("id");
@ -257,6 +263,12 @@ public class MainActivity extends AppCompatActivity
tabLayout.addTab(tabPublic);
viewPager = (ViewPager) findViewById(R.id.viewpager);
int countPage = 2;
if( sharedpreferences.getBoolean(Helper.SET_DISPLAY_LOCAL, true))
countPage++;
if( sharedpreferences.getBoolean(Helper.SET_DISPLAY_GLOBAL, true))
countPage++;
viewPager.setOffscreenPageLimit(countPage);
main_app_container = (RelativeLayout) findViewById(R.id.main_app_container);
PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
@ -283,21 +295,10 @@ public class MainActivity extends AppCompatActivity
if( tab.getPosition() == 0) {
item = navigationView.getMenu().findItem(R.id.nav_home);
fragmentTag = "HOME_TIMELINE";
if (homeFragment != null && Helper.getUnreadToots(getApplicationContext(), null) > 0) {
homeFragment.refresh();
}
Helper.cacheStatusClear(getApplicationContext(), null);
updateHomeCounter();
}else if( tab.getPosition() == 1) {
fragmentTag = "NOTIFICATIONS";
item = navigationView.getMenu().findItem(R.id.nav_notification);
if (notificationsFragment != null && Helper.getUnreadNotifications(getApplicationContext(), null) > 0) {
notificationsFragment.refresh();
}
Helper.cacheNotificationsClear(getApplicationContext(), null);
updateNotifCounter();
}else if( tab.getPosition() == 2 && display_local) {
fragmentTag = "LOCAL_TIMELINE";
item = navigationView.getMenu().findItem(R.id.nav_local);
}else if( tab.getPosition() == 2 && !display_local) {
@ -345,7 +346,7 @@ public class MainActivity extends AppCompatActivity
DisplayStatusFragment displayStatusFragment = ((DisplayStatusFragment) fragment);
if( displayStatusFragment != null )
displayStatusFragment.scrollToTop();
Helper.cacheStatusClear(getApplicationContext(), null);
countNewStatus = 0;
updateHomeCounter();
break;
case 2:
@ -358,7 +359,7 @@ public class MainActivity extends AppCompatActivity
DisplayNotificationsFragment displayNotificationsFragment = ((DisplayNotificationsFragment) fragment);
if( displayNotificationsFragment != null )
displayNotificationsFragment.scrollToTop();
Helper.cacheNotificationsClear(getApplicationContext(), null);
countNewNotifications = 0;
updateNotifCounter();
break;
}
@ -1157,8 +1158,7 @@ public class MainActivity extends AppCompatActivity
if( tabHome == null)
return;
TextView tabCounterHome = (TextView) tabHome.findViewById(R.id.tab_counter);
tabCounterHome.setText(String.valueOf(Helper.getUnreadToots(getApplicationContext(), null)));
if( Helper.getUnreadToots(getApplicationContext(), null) > 0){
if( countNewStatus> 0){
//New data are available
//The fragment is not displayed, so the counter is displayed
tabCounterHome.setVisibility(View.VISIBLE);
@ -1175,8 +1175,7 @@ public class MainActivity extends AppCompatActivity
if( tabNotif == null)
return;
TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter);
tabCounterNotif.setText(String.valueOf(Helper.getUnreadNotifications(getApplicationContext(), null)));
if( Helper.getUnreadNotifications(getApplicationContext(), null) > 0){
if( countNewNotifications > 0){
tabCounterNotif.setVisibility(View.VISIBLE);
}else {
tabCounterNotif.setVisibility(View.GONE);

View File

@ -20,25 +20,18 @@ import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
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;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
import fr.gouv.etalab.mastodon.drawers.StatusListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
@ -61,7 +54,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
private AsyncTask<Void, Void, Void> asyncTask;
private NotificationsListAdapter notificationsListAdapter;
private String max_id;
private List<Notification> notifications, notificationsTmp;
private List<Notification> notifications;
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout;
@ -160,14 +153,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
@Override
public void onResume() {
super.onResume();
refresh();
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if( isVisibleToUser )
refresh();
}
@Override
@ -195,8 +180,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
else
textviewNoAction.setVisibility(View.GONE);
if( swiped ){
Helper.cacheNotificationsClear(context,null);
((MainActivity) context).updateNotifCounter();
boolean isOnWifi = Helper.isOnWIFI(context);
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments, this.notifications);
@ -238,29 +221,17 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
}
public void refresh(){
public void refresh(Notification notification){
if( context == null)
return;
notificationsTmp = Helper.getTempNotification(context, null);
if( notificationsTmp.size() > 0){
ArrayList<String> added = new ArrayList<>();
for(Notification notification : notifications){
added.add(notification.getId());
}
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();
if( notification != null){
notifications.add(0, notification);
boolean isOnWifi = Helper.isOnWIFI(context);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
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.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notification.getId());
editor.apply();
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments, notifications);
lv_notifications.setAdapter(notificationsListAdapter);

View File

@ -25,7 +25,6 @@ import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -35,8 +34,6 @@ import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRepliesAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
@ -63,7 +60,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
private AsyncTask<Void, Void, Void> asyncTask;
private StatusListAdapter statusListAdapter;
private String max_id;
private List<Status> statuses, statusesTmp;
private List<Status> statuses;
private RetrieveFeedsAsyncTask.Type type;
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
@ -78,6 +75,8 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
private int positionSpinnerTrans;
private boolean hideHeader;
private String instanceValue;
private String lastReadStatus;
private String userId;
public DisplayStatusFragment(){
}
@ -117,7 +116,8 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
lastReadStatus = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, null);
lv_status = (ListView) rootView.findViewById(R.id.lv_status);
mainLoader = (RelativeLayout) rootView.findViewById(R.id.loader);
nextElementLoader = (RelativeLayout) rootView.findViewById(R.id.loading_next_status);
@ -226,7 +226,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
@Override
public void onResume() {
super.onResume();
refresh();
}
@Override
@ -269,14 +268,11 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
else
textviewNoAction.setVisibility(View.GONE);
if( swiped ){
if( type == RetrieveFeedsAsyncTask.Type.HOME ) {
Helper.cacheStatusClear(context,null);
((MainActivity) context).updateHomeCounter();
}
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses);
lv_status.setAdapter(statusListAdapter);
swiped = false;
}
//Avoids to add a second time the same status, can happen due call in on resume
ArrayList<String> added = new ArrayList<>();
for(Status status : this.statuses){
added.add(status.getId());
@ -286,6 +282,11 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
if( !added.contains(tmpStatus.getId())) {
this.statuses.add(tmpStatus);
added.add(tmpStatus.getId());
if( Long.parseLong(tmpStatus.getId()) > Long.parseLong(lastReadStatus)){
tmpStatus.setNew(true);
}else {
tmpStatus.setNew(false);
}
}
}
statusListAdapter.notifyDataSetChanged();
@ -295,7 +296,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
//Store last toot id for home timeline to avoid to notify for those that have been already seen
if(statuses != null && statuses.size() > 0 && type == RetrieveFeedsAsyncTask.Type.HOME ){
//acct is null when used in Fragment, data need to be retrieved via shared preferences and db
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account currentAccount = new AccountDAO(context, db).getAccountByID(userId);
if( currentAccount != null && firstLoad && since_id != null){
@ -316,38 +317,19 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if( isVisibleToUser )
refresh();
}
public void refresh(){
public void refresh(Status status){
//New data are available
if( type == RetrieveFeedsAsyncTask.Type.HOME ) {
if (context == null)
return;
statusesTmp = Helper.getTempStatus(context, null);
if (statusesTmp.size() > 0) {
ArrayList<String> added = new ArrayList<>();
for (Status status : statuses) {
added.add(status.getId());
}
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();
if (status != null) {
statuses.add(0,status);
boolean isOnWifi = Helper.isOnWIFI(context);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
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.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, status.getId());
editor.apply();
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, statuses);
lv_status.setAdapter(statusListAdapter);

View File

@ -1597,125 +1597,4 @@ public class Helper {
}
}
public static int getUnreadNotifications(Context context, String userId){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( userId == null)
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
Gson gson = new Gson();
String json = sharedpreferences.getString(Helper.SET_TEMP_NOTIFICATIONS + userId, null);
Type type = new TypeToken<ArrayList<Notification>>() {}.getType();
ArrayList<Notification> notifications = gson.fromJson(json, type);
return (notifications == null)?0:notifications.size();
}
public static int getUnreadToots(Context context, String userId){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( userId == null)
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
Gson gson = new Gson();
String json = sharedpreferences.getString(Helper.SET_TEMP_STATUS + userId, null);
Type type = new TypeToken<ArrayList<Status>>() {}.getType();
ArrayList<Status> statuses = gson.fromJson(json, type);
return (statuses == null)?0:statuses.size();
}
public static void cacheStatus(Context context, Status status, String userId){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( userId == null)
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SharedPreferences.Editor editor = sharedpreferences.edit();
ArrayList<Status> statuses = getTempStatus(context, userId);
if( statuses == null)
statuses = new ArrayList<>();
if( status != null)
statuses.add(0,status);
Gson gson = new Gson();
String serializedAccounts = gson.toJson(statuses);
editor.putString(Helper.SET_TEMP_STATUS + userId, serializedAccounts);
editor.apply();
}
public static void cacheStatusClear(Context context, String userId){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( userId == null)
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SharedPreferences.Editor editor = sharedpreferences.edit();
ArrayList<Status> statuses = new ArrayList<>();
Gson gson = new Gson();
String serializedAccounts = gson.toJson(statuses);
editor.putString(Helper.SET_TEMP_STATUS + userId, serializedAccounts);
editor.apply();
//noinspection EmptyTryBlock
try {
NotificationManager notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
long notif_id = Long.parseLong(userId);
int notificationId = ((notif_id + 2) > 2147483647) ? (int) (2147483647 - notif_id - 2) : (int) (notif_id + 2);
notificationManager.cancel(notificationId);
}catch (Exception ignored){}
}
public static ArrayList<Status> getTempStatus(Context context, String userId){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( userId == null)
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
Gson gson = new Gson();
String json = sharedpreferences.getString(Helper.SET_TEMP_STATUS + userId, null);
Type type = new TypeToken<ArrayList<Status>>() {}.getType();
ArrayList<Status> statuses = gson.fromJson(json, type);
return (statuses != null)?statuses:new ArrayList<Status>();
}
public static void cacheNotifications(Context context, Notification notification, String userId){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( userId == null)
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SharedPreferences.Editor editor = sharedpreferences.edit();
ArrayList<Notification> notifications = getTempNotification(context, userId);
if( notifications == null)
notifications = new ArrayList<>();
if( notification != null)
notifications.add(0,notification);
Gson gson = new Gson();
String serializedAccounts = gson.toJson(notifications);
editor.putString(Helper.SET_TEMP_NOTIFICATIONS + userId, serializedAccounts);
editor.apply();
}
public static void cacheNotificationsClear(Context context, String userId){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( userId == null)
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SharedPreferences.Editor editor = sharedpreferences.edit();
ArrayList<Notification> notifications = new ArrayList<>();
Gson gson = new Gson();
String serializedAccounts = gson.toJson(notifications);
editor.putString(Helper.SET_TEMP_NOTIFICATIONS + userId, serializedAccounts);
editor.apply();
//noinspection EmptyTryBlock
try {
NotificationManager notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
long notif_id = Long.parseLong(userId);
int notificationId = ((notif_id + 1) > 2147483647) ? (int) (2147483647 - notif_id - 1) : (int) (notif_id + 1);
notificationManager.cancel(notificationId);
}catch (Exception ignored){}
}
public static ArrayList<Notification> getTempNotification(Context context, String userId){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( userId == null)
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
Gson gson = new Gson();
String json = sharedpreferences.getString(Helper.SET_TEMP_NOTIFICATIONS + userId, null);
Type type = new TypeToken<ArrayList<Notification>>() {}.getType();
ArrayList<Notification> notifications = gson.fromJson(json, type);
return (notifications != null)?notifications:new ArrayList<Notification>();
}
}

View File

@ -20,6 +20,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
@ -66,6 +67,11 @@ public class StreamingService extends Service {
}
private final IBinder iBinder = new StreamingServiceBinder();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
public class StreamingServiceBinder extends Binder {
public StreamingService getService() {
return StreamingService.this;
@ -255,14 +261,16 @@ public class StreamingService extends Service {
Status status ;
Notification notification;
String dataId = null;
Bundle b = new Bundle();
if( event == EventStreaming.NOTIFICATION){
notification = API.parseNotificationResponse(getApplicationContext(), response);
Helper.cacheNotifications(getApplicationContext(), notification, userId);
b.putParcelable("data", notification);
}else if ( event == EventStreaming.UPDATE){
status = API.parseStatuses(getApplicationContext(), response);
status.setReplies(new ArrayList<Status>());
status.setNew(true);
Helper.cacheStatus(getApplicationContext(), status, userId);
b.putParcelable("data", status);
}else if( event == EventStreaming.DELETE){
try {
dataId = response.getString("id");
@ -272,6 +280,7 @@ public class StreamingService extends Service {
}
Intent intentBC = new Intent(Helper.RECEIVE_DATA);
intentBC.putExtra("eventStreaming", event);
intentBC.putExtras(b);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentBC);
}
}