diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/services/BackGroundTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/services/BackGroundTask.java index 258df61b8..c8dc6ad64 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/services/BackGroundTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/services/BackGroundTask.java @@ -7,6 +7,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; +import android.os.SystemClock; import android.support.v4.content.LocalBroadcastManager; import android.view.View; @@ -58,7 +59,7 @@ public class BackGroundTask extends AsyncTask { private Account account; private WeakReference contextReference; - public BackGroundTask(Context context, Account account){ + BackGroundTask(Context context, Account account){ this.contextReference = new WeakReference<>(context); this.account = account; } @@ -156,10 +157,18 @@ public class BackGroundTask extends AsyncTask { httpsURLConnection.disconnect(); } } - + SystemClock.sleep(5000); return null; } + @Override + protected void onPostExecute(Void result) { + Intent streamingServiceIntent = new Intent(contextReference.get(), LiveNotificationService.class); + streamingServiceIntent.putExtra("userId", account.getId()); + try { + contextReference.get().startService(streamingServiceIntent); + }catch (Exception ignored){} + } private void onRetrieveStreaming(Helper.EventStreaming event, final Account account, JSONObject response) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/services/LiveNotificationService.java b/app/src/main/java/fr/gouv/etalab/mastodon/services/LiveNotificationService.java index 791e196cf..eece51f14 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/services/LiveNotificationService.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/services/LiveNotificationService.java @@ -51,17 +51,6 @@ public class LiveNotificationService extends IntentService { public void onCreate() { super.onCreate(); - } - - - @Nullable - @Override - public IBinder onBind(Intent intent) { - return null; - } - - @Override - protected void onHandleIntent(@Nullable Intent intent) { SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true); boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true); @@ -82,4 +71,46 @@ public class LiveNotificationService extends IntentService { } + @Nullable + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + protected void onHandleIntent(@Nullable Intent intent) { + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true); + boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true); + String userId; + if( liveNotifications && notify){ + SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + if( intent == null || intent.getExtras() == null) { + List accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccount(); + if (accountStreams != null){ + for (final Account accountStream : accountStreams) { + if (backGroundTaskHashMap.containsKey(accountStream.getAcct() + accountStream.getInstance())) + if (!backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).isCancelled()) + backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).cancel(true); + BackGroundTask task = new BackGroundTask(getApplicationContext(), accountStream); + backGroundTaskHashMap.put(accountStream.getAcct() + accountStream.getInstance(), task); + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + + } + }else if((userId = intent.getStringExtra("userId")) != null){ + Account accountStream = new AccountDAO(getApplicationContext(), db).getAccountByID(userId); + if (accountStream != null) { + if (backGroundTaskHashMap.containsKey(accountStream.getAcct() + accountStream.getInstance())) + if (!backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).isCancelled()) + backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).cancel(true); + BackGroundTask task = new BackGroundTask(getApplicationContext(), accountStream); + backGroundTaskHashMap.put(accountStream.getAcct() + accountStream.getInstance(), task); + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + } + } + } + + }