From f6eac5cbceb25ddc7ade8a6fbe58ae39a9e709b8 Mon Sep 17 00:00:00 2001 From: DrKLO Date: Wed, 18 Jun 2014 00:43:57 +0400 Subject: [PATCH] Fixed resuming network when it not need --- TMessagesProj/build.gradle | 2 +- .../messenger/ConnectionsManager.java | 44 +++++++++++++++---- .../messenger/MessagesController.java | 24 +++++----- .../telegram/messenger/ScreenReceiver.java | 6 ++- .../org/telegram/ui/ApplicationLoader.java | 12 +---- .../java/org/telegram/ui/LaunchActivity.java | 4 +- .../src/main/res/values-de/strings.xml | 2 +- .../src/main/res/values-es/strings.xml | 2 +- 8 files changed, 58 insertions(+), 38 deletions(-) diff --git a/TMessagesProj/build.gradle b/TMessagesProj/build.gradle index d195bd872..ed6cbcd73 100644 --- a/TMessagesProj/build.gradle +++ b/TMessagesProj/build.gradle @@ -81,7 +81,7 @@ android { defaultConfig { minSdkVersion 8 targetSdkVersion 19 - versionCode 260 + versionCode 261 versionName "1.5.6" } } diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java b/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java index 9b6161bde..4d5cea5a8 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java @@ -68,6 +68,9 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. private int nextSleepTimeout = 30000; private long nextPingId = 0; + public static long lastPauseTime = System.currentTimeMillis(); + public static boolean appPaused = true; + private static volatile ConnectionsManager Instance = null; public static ConnectionsManager getInstance() { ConnectionsManager localInstance = Instance; @@ -103,7 +106,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. } long currentTime = System.currentTimeMillis(); - if (ApplicationLoader.lastPauseTime != 0 && ApplicationLoader.lastPauseTime < currentTime - nextSleepTimeout) { + if (lastPauseTime != 0 && lastPauseTime < currentTime - nextSleepTimeout) { boolean dontSleep = false; for (RPCRequest request : runningRequests) { if (request.retryCount < 10 && (request.runningStartTime + 60 > (int)(currentTime / 1000)) && ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0 || (request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0)) { @@ -142,7 +145,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. FileLog.e("tmessages", e); } } else { - ApplicationLoader.lastPauseTime += 30 * 1000; + lastPauseTime += 30 * 1000; FileLog.e("tmessages", "don't sleep 30 seconds because of upload or download request"); } } @@ -206,11 +209,11 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. @Override public void run() { if (paused) { - ApplicationLoader.lastPauseTime = System.currentTimeMillis(); + lastPauseTime = System.currentTimeMillis(); nextSleepTimeout = 30000; FileLog.e("tmessages", "wakeup network in background by received push"); - } else if (ApplicationLoader.lastPauseTime != 0) { - ApplicationLoader.lastPauseTime = System.currentTimeMillis(); + } else if (lastPauseTime != 0) { + lastPauseTime = System.currentTimeMillis(); FileLog.e("tmessages", "reset sleep timeout by received push"); } } @@ -230,6 +233,28 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. }); } + public static void resetLastPauseTime() { + if (appPaused) { + return; + } + FileLog.e("tmessages", "reset app pause time"); + if (lastPauseTime != 0 && System.currentTimeMillis() - lastPauseTime > 5000) { + ContactsController.getInstance().checkContacts(); + } + lastPauseTime = 0; + ConnectionsManager.getInstance().applicationMovedToForeground(); + } + + public static void setAppPaused(boolean value) { + appPaused = value; + FileLog.e("tmessages", "app paused = " + value); + if (!appPaused) { + resetLastPauseTime(); + } else { + lastPauseTime = System.currentTimeMillis(); + } + } + //================================================================================ // Config and session manage //================================================================================ @@ -816,7 +841,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. requestQueue.add(request); if (paused && ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0 || (request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0)) { - ApplicationLoader.lastPauseTime = System.currentTimeMillis(); + lastPauseTime = System.currentTimeMillis(); nextSleepTimeout = 30000; FileLog.e("tmessages", "wakeup by download or upload request"); } @@ -887,11 +912,11 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); - if (netInfo != null && (netInfo.isConnectedOrConnecting() || netInfo.isRoaming() || netInfo.isAvailable())) { + if (netInfo != null && (netInfo.isConnectedOrConnecting() || netInfo.isRoaming())) { return true; } else { netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); - if(netInfo != null && (netInfo.isConnectedOrConnecting() || netInfo.isRoaming() || netInfo.isAvailable())) { + if(netInfo != null && (netInfo.isConnectedOrConnecting() || netInfo.isRoaming())) { return true; } } @@ -2588,6 +2613,9 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. sendingPushPing = false; lastPushPingTime = System.currentTimeMillis() - 60000 * 3 + 10000; } else { + if (paused && connection.getDatacenterId() == currentDatacenterId && (connection.transportRequestClass & RPCRequest.RPCRequestClassGeneric) != 0) { + resumeNetworkMaybe(); + } processRequestQueue(connection.transportRequestClass, connection.getDatacenterId()); } } diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java index e52b92471..b971db652 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java @@ -760,7 +760,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter checkDeletingTask(); if (UserConfig.isClientActivated()) { - if (ApplicationLoader.lastPauseTime == 0) { + if (ConnectionsManager.lastPauseTime == 0) { if (statusSettingState != 1 && (lastStatusUpdateTime == 0 || lastStatusUpdateTime <= System.currentTimeMillis() - 55000 || offlineSent)) { statusSettingState = 1; @@ -786,7 +786,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter } }, null, true, RPCRequest.RPCRequestClassGeneric); } - } else if (statusSettingState != 2 && !offlineSent && ApplicationLoader.lastPauseTime <= System.currentTimeMillis() - 2000) { + } else if (statusSettingState != 2 && !offlineSent && ConnectionsManager.lastPauseTime <= System.currentTimeMillis() - 2000) { statusSettingState = 2; if (statusRequest != 0) { ConnectionsManager.getInstance().cancelRpc(statusRequest, true); @@ -3323,7 +3323,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter } if (!(res instanceof TLRPC.TL_updates_differenceSlice)) { - if ((dialog_id != openned_dialog_id || ApplicationLoader.lastPauseTime != 0) && !obj.isOut() && obj.messageOwner.unread && (lastMessage == null || lastMessage.messageOwner.date < obj.messageOwner.date)) { + if ((dialog_id != openned_dialog_id || ConnectionsManager.lastPauseTime != 0) && !obj.isOut() && obj.messageOwner.unread && (lastMessage == null || lastMessage.messageOwner.date < obj.messageOwner.date)) { if (!readMessages.contains(obj.messageOwner.id)) { lastMessage = obj; } @@ -3471,7 +3471,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter } else { dialog_id = obj.messageOwner.to_id.user_id; } - if (dialog_id != openned_dialog_id || ApplicationLoader.lastPauseTime != 0 || !ApplicationLoader.isScreenOn) { + if (dialog_id != openned_dialog_id || ConnectionsManager.lastPauseTime != 0 || !ApplicationLoader.isScreenOn) { showInAppNotification(obj); } } @@ -3534,7 +3534,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter } else { dialog_id = obj.messageOwner.to_id.user_id; } - if (dialog_id != openned_dialog_id || ApplicationLoader.lastPauseTime != 0 || !ApplicationLoader.isScreenOn) { + if (dialog_id != openned_dialog_id || ConnectionsManager.lastPauseTime != 0 || !ApplicationLoader.isScreenOn) { showInAppNotification(obj); } } @@ -3738,7 +3738,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter arr.add(obj); MessagesStorage.lastPtsValue = update.pts; if (upd.message.from_id != UserConfig.getClientUserId() && upd.message.to_id != null) { - if (uid != openned_dialog_id || ApplicationLoader.lastPauseTime != 0) { + if (uid != openned_dialog_id || ConnectionsManager.lastPauseTime != 0) { lastMessage = obj; } } @@ -3846,7 +3846,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter } arr.add(obj); if (newMessage.from_id != UserConfig.getClientUserId() && newMessage.to_id != null) { - if (newMessage.dialog_id != openned_dialog_id || ApplicationLoader.lastPauseTime != 0) { + if (newMessage.dialog_id != openned_dialog_id || ConnectionsManager.lastPauseTime != 0) { lastMessage = obj; } } @@ -3895,7 +3895,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter } arr.add(obj); if (newMessage.from_id != UserConfig.getClientUserId() && newMessage.to_id != null) { - if (newMessage.dialog_id != openned_dialog_id || ApplicationLoader.lastPauseTime != 0) { + if (newMessage.dialog_id != openned_dialog_id || ConnectionsManager.lastPauseTime != 0) { lastMessage = obj; } } @@ -3916,7 +3916,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter } arr.add(obj); if (message.from_id != UserConfig.getClientUserId() && message.to_id != null) { - if (uid != openned_dialog_id || ApplicationLoader.lastPauseTime != 0) { + if (uid != openned_dialog_id || ConnectionsManager.lastPauseTime != 0) { lastMessage = obj; } } @@ -4261,8 +4261,8 @@ public class MessagesController implements NotificationCenter.NotificationCenter if (!UserConfig.isClientActivated()) { return; } - if (ApplicationLoader.lastPauseTime != 0) { - ApplicationLoader.lastPauseTime = System.currentTimeMillis(); + if (ConnectionsManager.lastPauseTime != 0) { + ConnectionsManager.lastPauseTime = System.currentTimeMillis(); FileLog.e("tmessages", "reset sleep timeout by received message"); } if (messageObject == null) { @@ -4308,7 +4308,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter int vibrate_override = preferences.getInt("vibrate_" + dialog_id, 0); - if (ApplicationLoader.lastPauseTime == 0 && ApplicationLoader.isScreenOn) { + if (ConnectionsManager.lastPauseTime == 0 && ApplicationLoader.isScreenOn) { boolean inAppSounds = preferences.getBoolean("EnableInAppSounds", true); boolean inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true); boolean inAppPreview = preferences.getBoolean("EnableInAppPreview", true); diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/ScreenReceiver.java b/TMessagesProj/src/main/java/org/telegram/messenger/ScreenReceiver.java index 3776de40f..b557d0e5f 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/ScreenReceiver.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/ScreenReceiver.java @@ -19,11 +19,13 @@ public class ScreenReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { FileLog.e("tmessages", "screen off"); - ApplicationLoader.lastPauseTime = System.currentTimeMillis(); + if (ConnectionsManager.lastPauseTime == 0) { + ConnectionsManager.lastPauseTime = System.currentTimeMillis(); + } ApplicationLoader.isScreenOn = false; } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { FileLog.e("tmessages", "screen on"); - ApplicationLoader.resetLastPauseTime(); + ConnectionsManager.resetLastPauseTime(); ApplicationLoader.isScreenOn = true; } } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ApplicationLoader.java b/TMessagesProj/src/main/java/org/telegram/ui/ApplicationLoader.java index 48f1c2331..7d5ba1aa9 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ApplicationLoader.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ApplicationLoader.java @@ -29,7 +29,6 @@ import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.gcm.GoogleCloudMessaging; -import org.telegram.messenger.ContactsController; import org.telegram.messenger.NotificationsService; import org.telegram.messenger.BuildVars; import org.telegram.messenger.ConnectionsManager; @@ -52,12 +51,12 @@ public class ApplicationLoader extends Application { public static final String PROPERTY_REG_ID = "registration_id"; private static final String PROPERTY_APP_VERSION = "appVersion"; private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; - public static long lastPauseTime; public static Drawable cachedWallpaper = null; public static volatile Context applicationContext = null; public static volatile Handler applicationHandler = null; private static volatile boolean applicationInited = false; + public static volatile boolean isScreenOn = false; public static void postInitApplication() { @@ -135,7 +134,6 @@ public class ApplicationLoader extends Application { @Override public void onCreate() { super.onCreate(); - lastPauseTime = System.currentTimeMillis(); applicationContext = getApplicationContext(); applicationHandler = new Handler(applicationContext.getMainLooper()); @@ -182,14 +180,6 @@ public class ApplicationLoader extends Application { } } - public static void resetLastPauseTime() { - if (lastPauseTime != 0 && System.currentTimeMillis() - lastPauseTime > 5000) { - ContactsController.getInstance().checkContacts(); - } - lastPauseTime = 0; - ConnectionsManager.getInstance().applicationMovedToForeground(); - } - private void initPlayServices() { if (checkPlayServices()) { gcm = GoogleCloudMessaging.getInstance(this); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java index d0d91672f..93802b824 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java @@ -535,7 +535,7 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen @Override protected void onPause() { super.onPause(); - ApplicationLoader.lastPauseTime = System.currentTimeMillis(); + ConnectionsManager.setAppPaused(true); if (notificationView != null) { notificationView.hide(false); } @@ -559,7 +559,7 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen } Utilities.checkForCrashes(this); Utilities.checkForUpdates(this); - ApplicationLoader.resetLastPauseTime(); + ConnectionsManager.setAppPaused(false); actionBar.setBackOverlayVisible(currentConnectionState != 0); try { NotificationManager mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); diff --git a/TMessagesProj/src/main/res/values-de/strings.xml b/TMessagesProj/src/main/res/values-de/strings.xml index 7abcfc206..16e4137e4 100644 --- a/TMessagesProj/src/main/res/values-de/strings.xml +++ b/TMessagesProj/src/main/res/values-de/strings.xml @@ -273,7 +273,7 @@ nur über WLAN Vorname Nachname - LED Color + LED Farbe Noch keine geteilten Medien vorhanden diff --git a/TMessagesProj/src/main/res/values-es/strings.xml b/TMessagesProj/src/main/res/values-es/strings.xml index 96d29256f..0b101766c 100644 --- a/TMessagesProj/src/main/res/values-es/strings.xml +++ b/TMessagesProj/src/main/res/values-es/strings.xml @@ -273,7 +273,7 @@ Sólo vía WiFi Nombre Apellido - LED Color + Color del LED No hay fotos ni vídeos compartidos aún