Refine Push services (thanks Tfoss Team)

This commit is contained in:
luvletter2333 2021-11-17 20:23:59 +08:00
parent fb0a01b4d1
commit 99c7ba1302
No known key found for this signature in database
GPG Key ID: A26A8880836E1978
1 changed files with 21 additions and 6 deletions

View File

@ -54,6 +54,7 @@ import tw.nekomimi.nekogram.utils.UIUtil;
import static android.os.Build.VERSION.SDK_INT;
public class ApplicationLoader extends Application {
private static PendingIntent pendingIntent;
@SuppressLint("StaticFieldLeak")
public static volatile Context applicationContext;
@ -417,23 +418,37 @@ public class ApplicationLoader extends Application {
ConnectionsManager.getInstance(UserConfig.selectedAccount).setPushConnectionEnabled(true);
}
if (enabled) {
try {
UIUtil.runOnUIThread(() -> {
UIUtil.runOnUIThread(() -> {
try {
Log.d("TFOSS", "Trying to start push service every 10 minutes");
// Telegram-FOSS: unconditionally enable push service
AlarmManager am = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(applicationContext, NotificationsService.class);
pendingIntent = PendingIntent.getBroadcast(applicationContext, 0, i, 0);
am.cancel(pendingIntent);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 60 * 1000, pendingIntent);
Log.d("TFOSS", "Starting push service...");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
applicationContext.startForegroundService(new Intent(applicationContext, NotificationsService.class));
} else {
applicationContext.startService(new Intent(applicationContext, NotificationsService.class));
}
});
} catch (Throwable e) {
Log.d("TFOSS", "Failed to start push service");
}
} catch (Throwable e) {
Log.d("TFOSS", "Failed to start push service");
}
});
} else UIUtil.runOnUIThread(() -> {
applicationContext.stopService(new Intent(applicationContext, NotificationsService.class));
PendingIntent pintent = PendingIntent.getService(applicationContext, 0, new Intent(applicationContext, NotificationsService.class), 0);
AlarmManager alarm = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
if (pendingIntent != null) {
alarm.cancel(pendingIntent);
}
});
}