Skip to content

Commit

Permalink
misc: better UX and guide for UnifiedPush
Browse files Browse the repository at this point in the history
  • Loading branch information
dic1911 committed Dec 4, 2024
1 parent 3c68015 commit 4d2dd23
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public static void startPushService() {

private static void startPushServiceInternal() {
if (PushListenerController.getProvider().hasServices()) {
if (NekoConfig.enableUnifiedPush.Bool()) stopPushService();
return;
}
SharedPreferences preferences = MessagesController.getNotificationsSettings(UserConfig.selectedAccount);
Expand Down Expand Up @@ -400,16 +401,18 @@ private static void startPushServiceInternal() {
}
});

} else AndroidUtilities.runOnUIThread(() -> {
applicationContext.stopService(new Intent(applicationContext, NotificationsService.class));
} else AndroidUtilities.runOnUIThread(ApplicationLoader::stopPushService);
}

PendingIntent pintent = PendingIntent.getService(applicationContext, 0, new Intent(applicationContext, NotificationsService.class), PendingIntent.FLAG_MUTABLE);
AlarmManager alarm = (AlarmManager)applicationContext.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
if (pendingIntent != null) {
alarm.cancel(pendingIntent);
}
});
private static void stopPushService() {
applicationContext.stopService(new Intent(applicationContext, NotificationsService.class));

PendingIntent pintent = PendingIntent.getService(applicationContext, 0, new Intent(applicationContext, NotificationsService.class), PendingIntent.FLAG_MUTABLE);
AlarmManager alarm = (AlarmManager)applicationContext.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
if (pendingIntent != null) {
alarm.cancel(pendingIntent);
}
}

private static void initPushServices() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,7 @@ public static UnifiedPushListenerServiceProvider getInstance() {

@Override
public boolean hasServices() {
if (!NekoConfig.enableUnifiedPush.Bool()) return false;
return !UnifiedPush.getDistributors(ApplicationLoader.applicationContext, new ArrayList()).isEmpty();
}

Expand Down
19 changes: 19 additions & 0 deletions TMessagesProj/src/main/java/tw/nekomimi/nekogram/NekoConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import org.telegram.messenger.BuildVars;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MessagesController;
import org.telegram.messenger.PushListenerController;
import org.telegram.messenger.R;
import org.telegram.messenger.SharedConfig;
import org.telegram.messenger.Utilities;
import org.telegram.messenger.browser.Browser;
import org.telegram.ui.ActionBar.AlertDialog;
import org.telegram.ui.LaunchActivity;
import org.telegram.ui.LauncherIconController;
import org.unifiedpush.android.connector.UnifiedPush;

import java.io.ByteArrayInputStream;
import java.io.File;
Expand Down Expand Up @@ -45,6 +48,7 @@
import tw.nekomimi.nekogram.utils.ShareUtil;

import static tw.nekomimi.nekogram.config.ConfigItem.*;
import static tw.nekomimi.nekogram.utils.StrUtil.getAppName;

@SuppressLint("ApplySharedPref")
public class NekoConfig {
Expand Down Expand Up @@ -794,6 +798,21 @@ public static void init() {
applyCustomGetQueryBlacklist();
applySearchBlacklist();
applyPerformanceClassOverride(null);

if (!NekoConfig.enableUnifiedPush.Bool() || UnifiedPush.getSavedDistributor(ApplicationLoader.applicationContext) != null)
return;

AndroidUtilities.runOnUIThread(() -> {
Context context = LaunchActivity.getLastFragment().getContext();
new AlertDialog.Builder(context)
.setTitle(LocaleController.getString(R.string.SetupUnifiedPush))
.setMessage(LocaleController.getString(R.string.SetupUnifiedPushInfo))
.setNeutralButton(LocaleController.getString(R.string.SettingsHelp), (__, ___) -> {
Browser.openUrl(context, "https://github.com/dic1911/Momogram/tree/test?tab=readme-ov-file#how-do-i-get-notifications-working");
})
.setPositiveButton(LocaleController.getString(R.string.Close), null)
.create().show();
});
} catch (Exception ex) {
Log.e("030-neko", "failed to load part of neko config", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.graphics.Canvas;
import android.graphics.Paint;
Expand All @@ -28,6 +29,7 @@
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.util.OpenPgpApi;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.ApplicationLoader;
import org.telegram.messenger.BuildVars;
import org.telegram.messenger.ContactsController;
import org.telegram.messenger.LocaleController;
Expand Down Expand Up @@ -492,6 +494,14 @@ public void onItemClick(int id) {
restartTooltip.showWithAction(0, UndoView.ACTION_NEED_RESTART, null, null);
} else if (key.equals(NekoConfig.useOldName.getKey())) {
LauncherIconController.switchAppName((boolean) newValue);
} else if (key.equals(NekoConfig.enableUnifiedPush.getKey())) {
boolean enabled = ((boolean) newValue);
// start original service if enabled
SharedPreferences accountSettings = MessagesController.getNotificationsSettings(currentAccount);
boolean service = accountSettings.getBoolean("pushService", false);
boolean bgConn = accountSettings.getBoolean("pushConnection", false);
ApplicationLoader.startPushService();
ConnectionsManager.getInstance(currentAccount).setPushConnectionEnabled(!enabled && bgConn);
}
};

Expand Down
2 changes: 2 additions & 0 deletions TMessagesProj/src/main/res/values/strings_neko.xml
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,6 @@
<string name="NameAsTitleText">Use name as title text</string>
<string name="EnableUnifiedPush">Enable UnifiedPush</string>
<string name="RestartApp">Restart App</string>
<string name="SetupUnifiedPush">Setup UnifiedPush</string>
<string name="SetupUnifiedPushInfo">UnifiedPush is enabled, but you need to have at least one distributor installed in order to make it work.</string>
</resources>

0 comments on commit 4d2dd23

Please sign in to comment.