diff --git a/TMessagesProj/src/main/AndroidManifest.xml b/TMessagesProj/src/main/AndroidManifest.xml index dfe4ee762..92ec69af4 100644 --- a/TMessagesProj/src/main/AndroidManifest.xml +++ b/TMessagesProj/src/main/AndroidManifest.xml @@ -387,15 +387,15 @@ - - - - - + + + + + + + + + diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java b/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java index 9080f2bf7..fb837bf43 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java @@ -394,31 +394,23 @@ public class ApplicationLoader extends Application { } private static void startPushServiceInternal() { - if (ExternalGcm.checkPlayServices() || (SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && isNotificationListenerEnabled())) { + if (ExternalGcm.checkPlayServices()) { return; } - SharedPreferences preferences = MessagesController.getGlobalNotificationsSettings(); + SharedPreferences preferences = MessagesController.getNotificationsSettings(UserConfig.selectedAccount); boolean enabled; if (preferences.contains("pushService")) { - enabled = preferences.getBoolean("pushService", true); - if (SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - if (!preferences.getBoolean("pushConnection", true)) return; - } + enabled = preferences.getBoolean("pushService", false); } else { - enabled = MessagesController.getMainSettings(UserConfig.selectedAccount).getBoolean("keepAliveService", true); + enabled = MessagesController.getMainSettings(UserConfig.selectedAccount).getBoolean("keepAliveService", false); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("pushService", enabled); editor.putBoolean("pushConnection", enabled); editor.apply(); - SharedPreferences preferencesCA = MessagesController.getNotificationsSettings(UserConfig.selectedAccount); - SharedPreferences.Editor editorCA = preferencesCA.edit(); - editorCA.putBoolean("pushConnection", enabled); - editorCA.putBoolean("pushService", enabled); - editorCA.apply(); - ConnectionsManager.getInstance(UserConfig.selectedAccount).setPushConnectionEnabled(true); + ConnectionsManager.getInstance(UserConfig.selectedAccount).setPushConnectionEnabled(enabled); } if (enabled) { - UIUtil.runOnUIThread(() -> { + AndroidUtilities.runOnUIThread(() -> { try { Log.d("TFOSS", "Trying to start push service every 10 minutes"); // Telegram-FOSS: unconditionally enable push service @@ -440,7 +432,7 @@ public class ApplicationLoader extends Application { } }); - } else UIUtil.runOnUIThread(() -> { + } else AndroidUtilities.runOnUIThread(() -> { applicationContext.stopService(new Intent(applicationContext, NotificationsService.class)); PendingIntent pintent = PendingIntent.getService(applicationContext, 0, new Intent(applicationContext, NotificationsService.class), 0); @@ -452,14 +444,6 @@ public class ApplicationLoader extends Application { }); } - public static boolean isNotificationListenerEnabled() { - Set packageNames = NotificationManagerCompat.getEnabledListenerPackages(applicationContext); - if (packageNames.contains(applicationContext.getPackageName())) { - return true; - } - return false; - } - @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/NotificationsService.java b/TMessagesProj/src/main/java/org/telegram/messenger/NotificationsService.java index ec1785d16..15cd4b49f 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/NotificationsService.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/NotificationsService.java @@ -16,6 +16,7 @@ import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Build; import android.os.IBinder; import android.provider.Settings; @@ -31,6 +32,23 @@ public class NotificationsService extends Service { public void onCreate() { super.onCreate(); ApplicationLoader.postInitApplication(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + String CHANNEL_ID = "push_service_channel"; + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, LocaleController.getString("NekoXPushService", R.string.NekoXPushService), NotificationManager.IMPORTANCE_DEFAULT); + notificationManager.createNotificationChannel(channel); +// Intent explainIntent = new Intent("android.intent.action.VIEW"); +// explainIntent.setData(Uri.parse("https://github.com/Telegram-FOSS-Team/Telegram-FOSS/blob/master/Notifications.md")); +// PendingIntent explainPendingIntent = PendingIntent.getActivity(this, 0, explainIntent, 0); + Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) +// .setContentIntent(explainPendingIntent) + .setShowWhen(false) + .setOngoing(true) + .setSmallIcon(R.drawable.notification) + .setContentText(LocaleController.getString("NekoXPushService", R.string.NekoXPushService)) + .build(); + startForeground(9999, notification); + } } @Override diff --git a/TMessagesProj/src/main/java/org/telegram/ui/NotificationsSettingsActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/NotificationsSettingsActivity.java index 9326f4d9d..6191ccd59 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/NotificationsSettingsActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/NotificationsSettingsActivity.java @@ -545,23 +545,26 @@ public class NotificationsSettingsActivity extends BaseFragment implements Notif editor.putBoolean("badgeNumberMessages", getNotificationsController().showBadgeMessages); editor.commit(); getNotificationsController().updateBadge(); + } else if (position == notificationsServiceRow) { + SharedPreferences preferences = MessagesController.getNotificationsSettings(currentAccount); + enabled = preferences.getBoolean("pushService", getMessagesController().keepAliveService); + SharedPreferences.Editor editor = preferences.edit(); + editor.putBoolean("pushService", !enabled); + editor.apply(); + ApplicationLoader.startPushService(); } else if (position == notificationsServiceConnectionRow) { SharedPreferences preferences = MessagesController.getNotificationsSettings(currentAccount); enabled = preferences.getBoolean("pushConnection", getMessagesController().backgroundConnection); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("pushConnection", !enabled); - editor.commit(); - if (!enabled) { - ConnectionsManager.getInstance(currentAccount).setPushConnectionEnabled(true); - } else { - ConnectionsManager.getInstance(currentAccount).setPushConnectionEnabled(false); - } + editor.apply(); + ConnectionsManager.getInstance(currentAccount).setPushConnectionEnabled(!enabled); } else if (position == accountsAllRow) { SharedPreferences preferences = MessagesController.getGlobalNotificationsSettings(); enabled = preferences.getBoolean("AllAccounts", true); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("AllAccounts", !enabled); - editor.commit(); + editor.apply(); SharedConfig.showNotificationsForAllAccounts = !enabled; for (int a : SharedConfig.activeAccounts) { if (SharedConfig.showNotificationsForAllAccounts) { @@ -574,22 +577,6 @@ public class NotificationsSettingsActivity extends BaseFragment implements Notif } } } - } else if (position == notificationsServiceRow) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - if (ApplicationLoader.isNotificationListenerEnabled()) { - AlertUtil.showToast(LocaleController.getString("DisablePushAlert", R.string.DisablePushAlert)); - } else { - AlertUtil.showToast(LocaleController.getString("EnablePushAlert", R.string.EnablePushAlert)); - } - AndroidUtilities.runOnUIThread(this::openNotificationListenSettings, 500L); - } else { - SharedPreferences preferences = MessagesController.getNotificationsSettings(currentAccount); - enabled = preferences.getBoolean("pushService", getMessagesController().keepAliveService); - SharedPreferences.Editor editor = preferences.edit(); - editor.putBoolean("pushService", !enabled); - editor.apply(); - ApplicationLoader.startPushService(); - } } else if (position == callsVibrateRow) { if (getParentActivity() == null) { return; @@ -859,7 +846,7 @@ public class NotificationsSettingsActivity extends BaseFragment implements Notif } else if (position == androidAutoAlertRow) { checkCell.setTextAndCheck("Android Auto", preferences.getBoolean("EnableAutoNotifications", false), true); } else if (position == notificationsServiceRow) { - checkCell.setTextAndValueAndCheck(LocaleController.getString("NotificationsService", R.string.NotificationsService), LocaleController.getString("NotificationsServiceInfo", R.string.NotificationsServiceInfo), Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ? ApplicationLoader.isNotificationListenerEnabled() : preferences.getBoolean("pushService", getMessagesController().keepAliveService), true, true); + checkCell.setTextAndValueAndCheck(LocaleController.getString("NotificationsService", R.string.NotificationsService), LocaleController.getString("NotificationsServiceInfo", R.string.NotificationsServiceInfo), preferences.getBoolean("pushService", getMessagesController().keepAliveService), true, true); } else if (position == notificationsServiceConnectionRow) { checkCell.setTextAndValueAndCheck(LocaleController.getString("NotificationsServiceConnection", R.string.NotificationsServiceConnection), LocaleController.getString("NotificationsServiceConnectionInfo", R.string.NotificationsServiceConnectionInfo), preferences.getBoolean("pushConnection", getMessagesController().backgroundConnection), true, true); } else if (position == badgeNumberShowRow) {