Some work to delay when no activity

This commit is contained in:
tom79 2019-10-19 15:51:07 +02:00
parent ac97851f42
commit 34a69133f7
6 changed files with 91 additions and 102 deletions

View File

@ -763,7 +763,7 @@ public abstract class BaseMainActivity extends BaseActivity
}
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA || social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
startSreaming();
Helper.startSreaming(BaseMainActivity.this);
}
if (hidde_menu != null)
@ -2655,40 +2655,6 @@ public abstract class BaseMainActivity extends BaseActivity
}
public void startSreaming() {
int liveNotifications = Helper.liveNotifType(getApplicationContext());
switch (liveNotifications){
case Helper.NOTIF_LIVE:
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
assert manager != null;
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (LiveNotificationDelayedService.class.getName().equals(service.service.getClassName())) {
return;
}
}
try {
Intent streamingIntent = new Intent(this, LiveNotificationService.class);
startService(streamingIntent);
} catch (Exception ignored) {
}
break;
case Helper.NOTIF_DELAYED:
manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
assert manager != null;
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (LiveNotificationDelayedService.class.getName().equals(service.service.getClassName())) {
return;
}
}
try {
Intent streamingIntent = new Intent(this, LiveNotificationDelayedService.class);
startService(streamingIntent);
} catch (Exception ignored) {ignored.printStackTrace();
}
break;
}
}
public void manageFloatingButton(boolean display) {
if (display) {
tootShow();

View File

@ -15,6 +15,7 @@
package app.fedilab.android.asynctasks;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
@ -28,6 +29,7 @@ import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Error;
import app.fedilab.android.client.GNUAPI;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.services.StopLiveNotificationReceiver;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.TagsCacheDAO;
import app.fedilab.android.R;
@ -126,6 +128,7 @@ public class PostStatusAsyncTask extends AsyncTask<Void, Void, Void> {
}
};
thread.start();
Helper.startSreaming(contextReference.get());
}
}

View File

@ -80,18 +80,7 @@ public class AccountLiveAdapter extends RecyclerView.Adapter {
} else {
LiveNotificationDelayedService.totalAccount--;
}
int type = Helper.liveNotifType(context);
context.sendBroadcast(new Intent(context, StopLiveNotificationReceiver.class));
switch (type) {
case Helper.NOTIF_LIVE:
Intent streamingIntent = new Intent(context, LiveNotificationService.class);
context.startService(streamingIntent);
break;
case Helper.NOTIF_DELAYED:
streamingIntent = new Intent(context, LiveNotificationDelayedService.class);
context.startService(streamingIntent);
break;
}
Helper.startSreaming(context);
}
});
}

View File

@ -1240,22 +1240,10 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SET_NOTIFY, isChecked);
editor.apply();
context.sendBroadcast(new Intent(context, StopLiveNotificationReceiver.class));
if (isChecked) {
notification_settings.setVisibility(View.VISIBLE);
try {
switch (Helper.liveNotifType(context)) {
case Helper.NOTIF_LIVE:
Intent streamingIntent = new Intent(context, LiveNotificationService.class);
context.startService(streamingIntent);
break;
case Helper.NOTIF_DELAYED:
streamingIntent = new Intent(context, LiveNotificationDelayedService.class);
context.startService(streamingIntent);
break;
}
} catch (Exception ignored) {}
}
Helper.startSreaming(context);
}
});
@ -1298,15 +1286,11 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
editor.putBoolean(Helper.SET_DELAYED_NOTIFICATIONS, false);
live_notif_per_account.setVisibility(View.VISIBLE);
editor.apply();
Intent streamingIntent = new Intent(context, LiveNotificationService.class);
context.startService(streamingIntent);
break;
case Helper.NOTIF_DELAYED:
editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, false);
editor.putBoolean(Helper.SET_DELAYED_NOTIFICATIONS, true);
live_notif_per_account.setVisibility(View.VISIBLE);
streamingIntent = new Intent(context, LiveNotificationDelayedService.class);
context.startService(streamingIntent);
editor.apply();
break;
case Helper.NOTIF_NONE:
@ -1316,6 +1300,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
editor.apply();
break;
}
Helper.startSreaming(context);
switch (Helper.liveNotifType(context)){
case Helper.NOTIF_LIVE:
set_live_type_indication.setText(R.string.live_notif_indication);
@ -1706,19 +1691,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
} else {
LiveNotificationDelayedService.totalAccount--;
}
context.sendBroadcast(new Intent(context, StopLiveNotificationReceiver.class));
try {
switch (Helper.liveNotifType(context)) {
case Helper.NOTIF_LIVE:
Intent streamingIntent = new Intent(context, LiveNotificationService.class);
context.startService(streamingIntent);
break;
case Helper.NOTIF_DELAYED:
streamingIntent = new Intent(context, LiveNotificationDelayedService.class);
context.startService(streamingIntent);
break;
}
} catch (Exception ignored) {}
Helper.startSreaming(context);
}
});

View File

@ -212,6 +212,9 @@ import app.fedilab.android.client.Entities.TagTimeline;
import app.fedilab.android.client.Entities.Version;
import app.fedilab.android.client.Tls12SocketFactory;
import app.fedilab.android.fragments.ContentSettingsFragment;
import app.fedilab.android.services.LiveNotificationDelayedService;
import app.fedilab.android.services.LiveNotificationService;
import app.fedilab.android.services.StopLiveNotificationReceiver;
import app.fedilab.android.sqlite.MainMenuDAO;
import app.fedilab.android.sqlite.StatusCacheDAO;
import app.fedilab.android.sqlite.TimelineCacheDAO;
@ -4899,4 +4902,21 @@ public class Helper {
return bitmap;
}
public static HashMap<String, Integer> sleeps = new HashMap<>();
public static void startSreaming(Context context) {
context.sendBroadcast(new Intent(context, StopLiveNotificationReceiver.class));
int liveNotifications = Helper.liveNotifType(context);
switch (liveNotifications){
case Helper.NOTIF_LIVE:
Intent streamingIntent = new Intent(context, LiveNotificationService.class);
context.startService(streamingIntent);
break;
case Helper.NOTIF_DELAYED:
streamingIntent = new Intent(context, LiveNotificationDelayedService.class);
context.startService(streamingIntent);
break;
}
}
}

View File

@ -34,6 +34,7 @@ import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.Html;
import android.text.SpannableString;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -65,6 +66,7 @@ import app.fedilab.android.sqlite.Sqlite;
import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY;
import static app.fedilab.android.helper.Helper.getMainLogo;
import static app.fedilab.android.helper.Helper.sleeps;
/**
@ -81,7 +83,8 @@ public class LiveNotificationDelayedService extends Service {
public static int totalAccount = 0;
public static int eventsCount = 0;
public static HashMap<String, String> since_ids = new HashMap<>();
private static Thread thread;
public static HashMap<String, Thread> threads = new HashMap<>();
private boolean fetch;
@ -107,6 +110,7 @@ public class LiveNotificationDelayedService extends Service {
}
}
Log.v(Helper.TAG,"totalAccount -> " + totalAccount);
if( totalAccount > 0) {
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
@ -152,33 +156,53 @@ public class LiveNotificationDelayedService extends Service {
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
if (Helper.liveNotifType(getApplicationContext()) == Helper.NOTIF_DELAYED) {
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
fetch = true;
if (accountStreams != null) {
if( thread != null && !thread.isInterrupted()){
thread.interrupt();
}
fetch = true;
thread = new Thread() {
@Override
public void run() {
while (fetch) {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
Thread thread;
for (final Account accountStream : accountStreams) {
String key = accountStream.getAcct() + "@" + accountStream.getInstance();
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + accountStream.getId() + accountStream.getInstance(), true);
Log.v(Helper.TAG,key + " -> " + allowStream);
if( !allowStream){
continue;
}
if(!sleeps.containsKey(key)) {
sleeps.put(key, 30000);
}
for (final Account accountStream : accountStreams) {
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + accountStream.getId() + accountStream.getInstance(), true);
if (allowStream && (accountStream.getSocial() == null || accountStream.getSocial().equals("MASTODON") || accountStream.getSocial().equals("PLEROMA"))) {
taks(accountStream);
}
}
fetch = (Helper.liveNotifType(getApplicationContext()) == Helper.NOTIF_DELAYED);
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
SystemClock.sleep(60000);
}
if( threads.containsKey(key) && threads.get(key) != null) {
thread = threads.get(key);
if (thread != null && !thread.isInterrupted()) {
thread.interrupt();
}
}
};
thread.start();
thread = new Thread() {
@Override
public void run() {
while (fetch) {
taks(accountStream);
fetch = (Helper.liveNotifType(getApplicationContext()) == Helper.NOTIF_DELAYED);
if( sleeps.containsKey(key) && sleeps.get(key) != null){
try {
Thread.sleep(sleeps.get(key));
} catch (InterruptedException e) {
SystemClock.sleep(sleeps.get(key));
}
}else{
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
SystemClock.sleep(30000);
}
}
}
}
};
thread.start();
threads.put(key, thread);
}
}
}
}
@ -222,7 +246,21 @@ public class LiveNotificationDelayedService extends Service {
last_notifid = since_ids.get(key);
}
apiResponse = api.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, last_notifid, false);
if( apiResponse.getNotifications() != null && apiResponse.getNotifications().size() > 0){
if( apiResponse == null || apiResponse.getNotifications() == null || apiResponse.getNotifications().size() == 0){
if( sleeps.containsKey(key) && sleeps.get(key) != null){
int newWaitTime = sleeps.get(key) + 30000;
if( newWaitTime > 900000){
newWaitTime = 900000;
}
sleeps.put(key, newWaitTime);
}else{
sleeps.put(key, 60000);
}
}else{
sleeps.put(key, 30000);
}
Log.v(Helper.TAG,key + " -> " + sleeps.get(key));
if( apiResponse != null && apiResponse.getNotifications() != null && apiResponse.getNotifications().size() > 0){
since_ids.put(key, apiResponse.getNotifications().get(0).getId());
for (Notification notification : apiResponse.getNotifications()) {
if( last_notifid != null && notification.getId().compareTo(last_notifid) > 0) {