From 3bb2a89c0386e212565eb3a5f01821fec6ecff30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 2 Jul 2020 03:28:34 +0000 Subject: [PATCH] Dev setting: disableStatusUpdate (cherry picked from commit e4c8827008b9deb15cd8c65e5c2fa0f2c8aeed25) --- .../messenger/MessagesController.java | 94 ++++++++++++++----- .../messenger/NotificationCenter.java | 4 + .../ui/Adapters/DrawerLayoutAdapter.java | 33 ++++++- .../java/org/telegram/ui/LaunchActivity.java | 7 ++ .../tw/nekomimi/nekogram/NekoXConfig.java | 24 +++++ .../nekogram/NekoXSettingActivity.java | 10 ++ .../src/main/res/values/strings_nekox.xml | 2 + 7 files changed, 146 insertions(+), 28 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java index fb2d6f239..f5f0247f0 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java @@ -58,6 +58,7 @@ import java.util.concurrent.CountDownLatch; import tw.nekomimi.nekogram.ExternalGcm; import tw.nekomimi.nekogram.InternalFilters; import tw.nekomimi.nekogram.NekoConfig; +import tw.nekomimi.nekogram.NekoXConfig; import tw.nekomimi.nekogram.utils.AlertUtil; import tw.nekomimi.nekogram.utils.ThreadUtil; import tw.nekomimi.nekogram.utils.UIUtil; @@ -230,6 +231,11 @@ public class MessagesController extends BaseController implements NotificationCe private int statusRequest; private int statusSettingState; private boolean offlineSent; + + public boolean isOnline() { + return !offlineSent; + } + private String uploadingAvatar; private HashMap uploadingThemes = new HashMap<>(); @@ -4362,6 +4368,30 @@ public class MessagesController extends BaseController implements NotificationCe }); } + public void updateStatus(boolean online) { + + statusSettingState = online ? 2 : 1; + + if (statusRequest != 0) { + getConnectionsManager().cancelRequest(statusRequest, true); + } + + offlineSent = !online; + AndroidUtilities.runOnUIThread(() -> NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.updateUserStatus, (Object) null)); + + TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus(); + req.offline = !online; + statusRequest = getConnectionsManager().sendRequest(req, (response, error) -> { + if (error == null) { + lastStatusUpdateTime = System.currentTimeMillis(); + statusSettingState = 0; + } else { + AlertUtil.showToast(error); + } + statusRequest = 0; + }); + } + public void updateTimerProc() { long currentTime = System.currentTimeMillis(); @@ -4379,20 +4409,25 @@ public class MessagesController extends BaseController implements NotificationCe getConnectionsManager().cancelRequest(statusRequest, true); } - TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus(); - req.offline = false; - statusRequest = getConnectionsManager().sendRequest(req, (response, error) -> { - if (error == null) { - lastStatusUpdateTime = System.currentTimeMillis(); - offlineSent = false; - statusSettingState = 0; - } else { - if (lastStatusUpdateTime != 0) { - lastStatusUpdateTime += 5000; + if (NekoXConfig.disableStatusUpdate) { + lastStatusUpdateTime = System.currentTimeMillis(); + statusSettingState = 0; + } else { + TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus(); + req.offline = false; + statusRequest = getConnectionsManager().sendRequest(req, (response, error) -> { + if (error == null) { + lastStatusUpdateTime = System.currentTimeMillis(); + offlineSent = false; + statusSettingState = 0; + } else { + if (lastStatusUpdateTime != 0) { + lastStatusUpdateTime += 5000; + } } - } - statusRequest = 0; - }); + statusRequest = 0; + }); + } } } } else if (statusSettingState != 2 && !offlineSent && Math.abs(System.currentTimeMillis() - getConnectionsManager().getPauseTime()) >= 2000) { @@ -4400,18 +4435,22 @@ public class MessagesController extends BaseController implements NotificationCe if (statusRequest != 0) { getConnectionsManager().cancelRequest(statusRequest, true); } - TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus(); - req.offline = true; - statusRequest = getConnectionsManager().sendRequest(req, (response, error) -> { - if (error == null) { - offlineSent = true; - } else { - if (lastStatusUpdateTime != 0) { - lastStatusUpdateTime += 5000; - } - } + if (NekoXConfig.disableStatusUpdate) { statusRequest = 0; - }); + } else { + TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus(); + req.offline = true; + statusRequest = getConnectionsManager().sendRequest(req, (response, error) -> { + if (error == null) { + offlineSent = true; + } else { + if (lastStatusUpdateTime != 0) { + lastStatusUpdateTime += 5000; + } + } + statusRequest = 0; + }); + } } if (updatesQueueChannels.size() != 0) { @@ -11447,8 +11486,15 @@ public class MessagesController extends BaseController implements NotificationCe toDbUser.status = update.status; dbUsersStatus.add(toDbUser); if (update.user_id == getUserConfig().getClientUserId()) { + boolean offline = !(update.status instanceof TLRPC.TL_userStatusOnline); getNotificationsController().setLastOnlineFromOtherDevice(update.status.expires); + if (NekoXConfig.keepOnlineStatus && offline != offlineSent) { + getMessagesController().updateStatus(offline); + } else { + offlineSent = offline; + } } + NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.updateUserStatus, update); } else if (baseUpdate instanceof TLRPC.TL_updateUserName) { TLRPC.TL_updateUserName update = (TLRPC.TL_updateUserName) baseUpdate; final TLRPC.User currentUser = getUser(update.user_id); diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/NotificationCenter.java b/TMessagesProj/src/main/java/org/telegram/messenger/NotificationCenter.java index 40d3a2703..ab27d3b37 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/NotificationCenter.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/NotificationCenter.java @@ -190,6 +190,10 @@ public class NotificationCenter { public static final int messagePlayingSpeedChanged = totalEvents++; public static final int screenStateChanged = totalEvents++; + // custom + + public static final int updateUserStatus = totalEvents++; + private SparseArray> observers = new SparseArray<>(); private SparseArray> removeAfterBroadcast = new SparseArray<>(); private SparseArray> addAfterBroadcast = new SparseArray<>(); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java b/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java index cf67a163b..34cc1b30a 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java @@ -19,12 +19,14 @@ import androidx.recyclerview.widget.RecyclerView; import org.telegram.messenger.AndroidUtilities; import org.telegram.messenger.ApplicationLoader; +import org.telegram.messenger.FileLog; import org.telegram.messenger.LocaleController; import org.telegram.messenger.MessagesController; import org.telegram.messenger.NotificationCenter; import org.telegram.messenger.R; import org.telegram.messenger.SharedConfig; import org.telegram.messenger.UserConfig; +import org.telegram.tgnet.TLRPC; import org.telegram.ui.ActionBar.Theme; import org.telegram.ui.Cells.DividerCell; import org.telegram.ui.Cells.DrawerActionCell; @@ -40,8 +42,10 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Locale; +import cn.hutool.core.util.StrUtil; import kotlin.jvm.functions.Function0; import tw.nekomimi.nekogram.NekoConfig; +import tw.nekomimi.nekogram.NekoXConfig; public class DrawerLayoutAdapter extends RecyclerListView.SelectionAdapter implements NotificationCenter.NotificationCenterDelegate { @@ -102,8 +106,17 @@ public class DrawerLayoutAdapter extends RecyclerListView.SelectionAdapter imple return accountsShown; } - @Override public void didReceivedNotification(int id, int account, Object... args) { - resetItems(); + @Override + public void didReceivedNotification(int id, int account, Object... args) { + if (id == NotificationCenter.updateUserStatus) { + if (args[0] != null) { + TLRPC.TL_updateUserStatus update = (TLRPC.TL_updateUserStatus) args[0]; + int selectedUserId = UserConfig.getInstance(UserConfig.selectedAccount).getClientUserId(); + if (update.user_id != selectedUserId) { + return; + } + } + } notifyDataSetChanged(); } @@ -262,8 +275,20 @@ public class DrawerLayoutAdapter extends RecyclerListView.SelectionAdapter imple return true; })); } + if (NekoXConfig.disableStatusUpdate && !UserConfig.getInstance(UserConfig.selectedAccount).isBot) { + boolean online = MessagesController.getInstance(UserConfig.selectedAccount).isOnline(); + String message = online ? StrUtil.upperFirst(LocaleController.getString("Online", R.string.Online)) : LocaleController.getString("VoipOfflineTitle",R.string.VoipOfflineTitle); + if (NekoXConfig.keepOnlineStatus) { + message += " (" + LocaleController.getString("Locked",R.string.Locked) + ")"; + } + items.add(new CheckItem(14, message, R.drawable.baseline_visibility_24, () -> online, () -> { + MessagesController controller = MessagesController.getInstance(UserConfig.selectedAccount); + controller.updateStatus(!online); + return true; + })); + } items.add(null); // divider - items.add(new CheckItem(12,LocaleController.getString("DarkMode",R.string.NightMode),R.drawable.baseline_brightness_2_24,() -> Theme.getActiveTheme().isDark(),null)); + items.add(new CheckItem(12, LocaleController.getString("DarkMode", R.string.NightMode), R.drawable.baseline_brightness_2_24, () -> Theme.getActiveTheme().isDark(), null)); } public int getId(int position) { @@ -311,7 +336,7 @@ public class DrawerLayoutAdapter extends RecyclerListView.SelectionAdapter imple public Function0 isChecked; public Function0 doSwitch; - public CheckItem(int id, String text, int icon, Function0 isChecked,@Nullable Function0 doSwitch) { + public CheckItem(int id, String text, int icon, Function0 isChecked, @Nullable Function0 doSwitch) { super(id, text, icon); this.isChecked = isChecked; this.doSwitch = doSwitch; diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java index da7415c56..9a6e1166e 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java @@ -598,6 +598,9 @@ public class LaunchActivity extends Activity implements ActionBarLayout.ActionBa } else if (id == 13) { presentFragment(new ProxyListActivity()); drawerLayoutContainer.closeDrawer(false); + } else if (id == 14) { + NekoXConfig.toggleKeepOnlineStatus();; + drawerLayoutAdapter.notifyDataSetChanged(); } } else { int id = drawerLayoutAdapter.getId(position); @@ -678,7 +681,9 @@ public class LaunchActivity extends Activity implements ActionBarLayout.ActionBa NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.didSetNewWallpapper); NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.notificationsCountUpdated); NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.screenStateChanged); + NotificationCenter.getGlobalInstance().addObserver(drawerLayoutAdapter, NotificationCenter.proxySettingsChanged); + NotificationCenter.getGlobalInstance().addObserver(drawerLayoutAdapter, NotificationCenter.updateUserStatus); if (actionBarLayout.fragmentsStack.isEmpty()) { if (!UserConfig.getInstance(currentAccount).isClientActivated()) { @@ -898,6 +903,7 @@ public class LaunchActivity extends Activity implements ActionBarLayout.ActionBa PrivacyUtil.postCheckAll(this, account); } updateCurrentConnectionState(currentAccount); + NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.updateUserStatus, (Object) null); } private void switchToAvailableAccountOrLogout() { @@ -2861,6 +2867,7 @@ public class LaunchActivity extends Activity implements ActionBarLayout.ActionBa NotificationCenter.getGlobalInstance().removeObserver(this, NotificationCenter.notificationsCountUpdated); NotificationCenter.getGlobalInstance().removeObserver(this, NotificationCenter.screenStateChanged); NotificationCenter.getGlobalInstance().removeObserver(drawerLayoutAdapter, NotificationCenter.proxySettingsChanged); + NotificationCenter.getGlobalInstance().removeObserver(drawerLayoutAdapter, NotificationCenter.updateUserStatus); } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/NekoXConfig.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/NekoXConfig.java index a0b04eeb8..37553f724 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/NekoXConfig.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/NekoXConfig.java @@ -4,6 +4,7 @@ import android.content.SharedPreferences; import org.telegram.messenger.BuildConfig; import org.telegram.messenger.BuildVars; +import org.telegram.messenger.NotificationCenter; import tw.nekomimi.nekogram.database.NitritesKt; @@ -19,6 +20,10 @@ public class NekoXConfig { public static boolean disableFlagSecure = preferences.getBoolean("disable_flag_secure", false); public static boolean disableScreenshotDetection = preferences.getBoolean("disable_screenshot_detection", false); + public static boolean disableStatusUpdate = preferences.getBoolean("disable_status_update", false); + public static boolean keepOnlineStatus = preferences.getBoolean("keepOnlineStatus", false); + + public static void toggleDeveloperMode() { preferences.edit().putBoolean("developer_mode", developerMode = !developerMode).apply(); @@ -28,8 +33,10 @@ public class NekoXConfig { preferences.edit() .putBoolean("disable_flag_secure", disableFlagSecure = false) .putBoolean("disable_screenshot_detection", disableScreenshotDetection = false) + .putBoolean("disable_status_update", disableStatusUpdate = false) .apply(); + } } @@ -115,4 +122,21 @@ public class NekoXConfig { } + public static void toggleDisableStatusUpdate() { + + preferences.edit().putBoolean("disable_status_update", disableStatusUpdate = !disableStatusUpdate).apply(); + + NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.updateUserStatus, (Object) null); + + } + + public static void toggleKeepOnlineStatus() { + + preferences.edit().putBoolean("keepOnlineStatus", keepOnlineStatus = !keepOnlineStatus).apply(); + + NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.updateUserStatus, (Object) null); + + } + + } \ No newline at end of file diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/NekoXSettingActivity.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/NekoXSettingActivity.java index 905f88005..062806486 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/NekoXSettingActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/NekoXSettingActivity.java @@ -51,6 +51,7 @@ public class NekoXSettingActivity extends BaseFragment { private int enableRow; private int disableFlagSecureRow; private int disableScreenshotDetectionRow; + private int disableStatusUpdateRow; private int fetchAndExportLangRow; private int checkUpdateRepoForceRow; @@ -119,6 +120,11 @@ public class NekoXSettingActivity extends BaseFragment { if (view instanceof TextCheckCell) { ((TextCheckCell) view).setChecked(NekoXConfig.disableScreenshotDetection); } + } else if (position == disableStatusUpdateRow) { + NekoXConfig.toggleDisableStatusUpdate(); + if (view instanceof TextCheckCell) { + ((TextCheckCell) view).setChecked(NekoXConfig.disableStatusUpdate); + } } }); @@ -143,6 +149,7 @@ public class NekoXSettingActivity extends BaseFragment { disableFlagSecureRow = rowCount++; disableScreenshotDetectionRow = rowCount++; + disableStatusUpdateRow = rowCount++; fetchAndExportLangRow = rowCount++; checkUpdateRepoForceRow = rowCount++; @@ -282,6 +289,9 @@ public class NekoXSettingActivity extends BaseFragment { textCell.setTextAndCheck("Disable Flag Secure", NekoXConfig.disableFlagSecure, true); } else if (position == disableScreenshotDetectionRow) { textCell.setTextAndCheck("Disable Screenshot Detection", NekoXConfig.disableScreenshotDetection, false); + } else if (position == disableStatusUpdateRow) { + textCell.setTextAndCheck("Disable Status Update", NekoXConfig.disableStatusUpdate, false); + } } break; diff --git a/TMessagesProj/src/main/res/values/strings_nekox.xml b/TMessagesProj/src/main/res/values/strings_nekox.xml index 48e4b63e5..e96ae3192 100644 --- a/TMessagesProj/src/main/res/values/strings_nekox.xml +++ b/TMessagesProj/src/main/res/values/strings_nekox.xml @@ -3,6 +3,8 @@ Nekogram X + Locked + Custom API Log in using the custom api, if you are unable to register or log in, this may help.\n\nNote: fcm will not work if you are using the release version. Don\'t use custom API