From abf6953beb8616aebdc7fba40392cb25bed0b40a Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 10 Aug 2020 16:45:28 +0200 Subject: [PATCH] Some fixes --- .../android/activities/SettingsActivity.java | 29 +++++++++---------- .../android/activities/BaseMainActivity.java | 9 ++---- .../fedilab/android/helper/BaseHelper.java | 3 +- .../LiveNotificationDelayedService.java | 7 +++-- .../RestartLiveNotificationReceiver.java | 18 +++--------- .../StopLiveNotificationReceiver.java | 1 + .../android/services/UpgradeReceiver.java | 1 + 7 files changed, 28 insertions(+), 40 deletions(-) diff --git a/app/src/lite/java/app/fedilab/android/activities/SettingsActivity.java b/app/src/lite/java/app/fedilab/android/activities/SettingsActivity.java index 97fafc3fa..90598d210 100644 --- a/app/src/lite/java/app/fedilab/android/activities/SettingsActivity.java +++ b/app/src/lite/java/app/fedilab/android/activities/SettingsActivity.java @@ -21,6 +21,7 @@ import android.database.sqlite.SQLiteDatabase; import android.graphics.drawable.ColorDrawable; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; @@ -56,6 +57,8 @@ import app.fedilab.android.sqlite.AccountDAO; import app.fedilab.android.sqlite.Sqlite; import es.dmoral.toasty.Toasty; +import static app.fedilab.android.helper.BaseHelper.startStreaming; + /** * Created by Thomas on 01/07/2019. * Settings activity @@ -332,19 +335,7 @@ public class SettingsActivity extends BaseActivity { editor.putBoolean(Helper.SET_NOTIFY, isChecked); editor.apply(); if (isChecked) { - try { - switch (Helper.liveNotifType(getApplicationContext())) { - case Helper.NOTIF_LIVE: - Intent streamingIntent = new Intent(getApplicationContext(), LiveNotificationService.class); - startService(streamingIntent); - break; - case Helper.NOTIF_DELAYED: - streamingIntent = new Intent(getApplicationContext(), LiveNotificationDelayedService.class); - startService(streamingIntent); - break; - } - } catch (Exception ignored) { - } + startStreaming(SettingsActivity.this); } else { sendBroadcast(new Intent(getApplicationContext(), StopLiveNotificationReceiver.class)); if (Build.VERSION.SDK_INT >= 26) { @@ -380,7 +371,7 @@ public class SettingsActivity extends BaseActivity { } else { LiveNotificationDelayedService.totalAccount--; } - Helper.startStreaming(SettingsActivity.this); + startStreaming(SettingsActivity.this); }); final ImageButton set_allow_live_notifications_others = findViewById(R.id.set_allow_live_notifications_others); @@ -430,7 +421,10 @@ public class SettingsActivity extends BaseActivity { live_notif_per_account.setVisibility(View.VISIBLE); editor.commit(); set_live_type_indication.setText(R.string.live_notif_indication); - Helper.startStreaming(SettingsActivity.this); + Handler handler = new Handler(); + handler.postDelayed(() -> { + startStreaming(SettingsActivity.this); + }, 1000); break; case Helper.NOTIF_DELAYED: editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, false); @@ -438,7 +432,10 @@ public class SettingsActivity extends BaseActivity { live_notif_per_account.setVisibility(View.VISIBLE); set_live_type_indication.setText(R.string.set_live_type_indication); editor.commit(); - Helper.startStreaming(SettingsActivity.this); + handler = new Handler(); + handler.postDelayed(() -> { + startStreaming(SettingsActivity.this); + }, 1000); break; case Helper.NOTIF_NONE: editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, false); diff --git a/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java index 92b13e869..c531e564f 100644 --- a/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java @@ -150,8 +150,6 @@ import app.fedilab.android.interfaces.OnRetrieveRemoteAccountInterface; import app.fedilab.android.interfaces.OnSyncTimelineInterface; import app.fedilab.android.interfaces.OnUpdateAccountInfoInterface; import app.fedilab.android.services.BackupStatusService; -import app.fedilab.android.services.LiveNotificationDelayedService; -import app.fedilab.android.services.LiveNotificationService; import app.fedilab.android.sqlite.AccountDAO; import app.fedilab.android.sqlite.Sqlite; import app.fedilab.android.sqlite.TempMuteDAO; @@ -161,6 +159,7 @@ import es.dmoral.toasty.Toasty; import static app.fedilab.android.activities.WebviewActivity.trackingDomains; import static app.fedilab.android.asynctasks.ManageFiltersAsyncTask.action.GET_ALL_FILTER; +import static app.fedilab.android.helper.BaseHelper.startStreaming; import static app.fedilab.android.helper.Helper.changeDrawableColor; @@ -1211,15 +1210,13 @@ public abstract class BaseMainActivity extends BaseActivity editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, true); editor.putBoolean(Helper.SET_DELAYED_NOTIFICATIONS, false); editor.apply(); - Intent streamingIntent = new Intent(BaseMainActivity.this, LiveNotificationService.class); - startService(streamingIntent); + startStreaming(BaseMainActivity.this); break; case Helper.NOTIF_DELAYED: editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, false); editor.putBoolean(Helper.SET_DELAYED_NOTIFICATIONS, true); - streamingIntent = new Intent(BaseMainActivity.this, LiveNotificationDelayedService.class); - startService(streamingIntent); editor.apply(); + startStreaming(BaseMainActivity.this); break; case Helper.NOTIF_NONE: editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, false); diff --git a/app/src/main/java/app/fedilab/android/helper/BaseHelper.java b/app/src/main/java/app/fedilab/android/helper/BaseHelper.java index 3e9d84d0c..01ed49e52 100644 --- a/app/src/main/java/app/fedilab/android/helper/BaseHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/BaseHelper.java @@ -4340,11 +4340,12 @@ public class BaseHelper { if (streamingIntent != null) { try { if (Build.VERSION.SDK_INT >= 26) { - context.startService(streamingIntent); + context.startForegroundService(streamingIntent); } else { context.startService(streamingIntent); } } catch (Exception ignored) { + context.startService(streamingIntent); } } } diff --git a/app/src/main/java/app/fedilab/android/services/LiveNotificationDelayedService.java b/app/src/main/java/app/fedilab/android/services/LiveNotificationDelayedService.java index 02522e06e..6669457f9 100644 --- a/app/src/main/java/app/fedilab/android/services/LiveNotificationDelayedService.java +++ b/app/src/main/java/app/fedilab/android/services/LiveNotificationDelayedService.java @@ -91,7 +91,6 @@ public class LiveNotificationDelayedService extends Service { super.onCreate(); final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true); - if (Build.VERSION.SDK_INT >= 26) { channel = new NotificationChannel(CHANNEL_ID, "Live notifications", @@ -261,7 +260,8 @@ public class LiveNotificationDelayedService extends Service { gnuApi = new GNUAPI(LiveNotificationDelayedService.this, account.getInstance(), account.getToken()); apiResponse = gnuApi.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, last_notifid); } - } catch (Exception ignored) { + } catch (Exception e) { + e.printStackTrace(); } if (apiResponse != null && apiResponse.getNotifications() != null && apiResponse.getNotifications().size() > 0) { @@ -490,7 +490,8 @@ public class LiveNotificationDelayedService extends Service { editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), notification.getId()); editor.apply(); } - } catch (Exception ignored) { + } catch (Exception e) { + e.printStackTrace(); } } diff --git a/app/src/main/java/app/fedilab/android/services/RestartLiveNotificationReceiver.java b/app/src/main/java/app/fedilab/android/services/RestartLiveNotificationReceiver.java index fd2ce42de..4faea1f83 100644 --- a/app/src/main/java/app/fedilab/android/services/RestartLiveNotificationReceiver.java +++ b/app/src/main/java/app/fedilab/android/services/RestartLiveNotificationReceiver.java @@ -18,10 +18,11 @@ import android.annotation.SuppressLint; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; - import app.fedilab.android.helper.Helper; import app.fedilab.android.jobs.NotificationsSyncJob; +import static app.fedilab.android.helper.BaseHelper.startStreaming; + /** * Created by Thomas on 22/09/2017. * BroadcastReceiver for restarting the service @@ -34,21 +35,10 @@ public class RestartLiveNotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int type = Helper.liveNotifType(context); - if (type == Helper.NOTIF_DELAYED) { - Intent streamingServiceIntent = new Intent(context, LiveNotificationDelayedService.class); - try { - context.startService(streamingServiceIntent); - } catch (Exception ignored) { - } - } else if (type == Helper.NOTIF_LIVE) { - Intent streamingServiceIntent = new Intent(context, LiveNotificationService.class); - try { - context.startService(streamingServiceIntent); - } catch (Exception ignored) { - } + if (type == Helper.NOTIF_DELAYED || type == Helper.NOTIF_LIVE) { + startStreaming(context); } else { NotificationsSyncJob.schedule(false); } } - } \ No newline at end of file diff --git a/app/src/main/java/app/fedilab/android/services/StopLiveNotificationReceiver.java b/app/src/main/java/app/fedilab/android/services/StopLiveNotificationReceiver.java index 7c4b924e8..5a04558ff 100644 --- a/app/src/main/java/app/fedilab/android/services/StopLiveNotificationReceiver.java +++ b/app/src/main/java/app/fedilab/android/services/StopLiveNotificationReceiver.java @@ -20,6 +20,7 @@ import android.content.Context; import android.content.Intent; + /** * Created by Thomas on 22/09/2017. * BroadcastReceiver for restarting the service diff --git a/app/src/main/java/app/fedilab/android/services/UpgradeReceiver.java b/app/src/main/java/app/fedilab/android/services/UpgradeReceiver.java index 98bd9f980..c086c095a 100644 --- a/app/src/main/java/app/fedilab/android/services/UpgradeReceiver.java +++ b/app/src/main/java/app/fedilab/android/services/UpgradeReceiver.java @@ -19,6 +19,7 @@ import android.content.Context; import android.content.Intent; + public class UpgradeReceiver extends BroadcastReceiver { @Override