Fixed resuming network when it not need

This commit is contained in:
DrKLO 2014-06-18 00:43:57 +04:00
parent 99bdc317ac
commit f6eac5cbce
8 changed files with 58 additions and 38 deletions

View File

@ -81,7 +81,7 @@ android {
defaultConfig { defaultConfig {
minSdkVersion 8 minSdkVersion 8
targetSdkVersion 19 targetSdkVersion 19
versionCode 260 versionCode 261
versionName "1.5.6" versionName "1.5.6"
} }
} }

View File

@ -68,6 +68,9 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
private int nextSleepTimeout = 30000; private int nextSleepTimeout = 30000;
private long nextPingId = 0; private long nextPingId = 0;
public static long lastPauseTime = System.currentTimeMillis();
public static boolean appPaused = true;
private static volatile ConnectionsManager Instance = null; private static volatile ConnectionsManager Instance = null;
public static ConnectionsManager getInstance() { public static ConnectionsManager getInstance() {
ConnectionsManager localInstance = Instance; ConnectionsManager localInstance = Instance;
@ -103,7 +106,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
} }
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
if (ApplicationLoader.lastPauseTime != 0 && ApplicationLoader.lastPauseTime < currentTime - nextSleepTimeout) { if (lastPauseTime != 0 && lastPauseTime < currentTime - nextSleepTimeout) {
boolean dontSleep = false; boolean dontSleep = false;
for (RPCRequest request : runningRequests) { for (RPCRequest request : runningRequests) {
if (request.retryCount < 10 && (request.runningStartTime + 60 > (int)(currentTime / 1000)) && ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0 || (request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0)) { 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); FileLog.e("tmessages", e);
} }
} else { } else {
ApplicationLoader.lastPauseTime += 30 * 1000; lastPauseTime += 30 * 1000;
FileLog.e("tmessages", "don't sleep 30 seconds because of upload or download request"); 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 @Override
public void run() { public void run() {
if (paused) { if (paused) {
ApplicationLoader.lastPauseTime = System.currentTimeMillis(); lastPauseTime = System.currentTimeMillis();
nextSleepTimeout = 30000; nextSleepTimeout = 30000;
FileLog.e("tmessages", "wakeup network in background by received push"); FileLog.e("tmessages", "wakeup network in background by received push");
} else if (ApplicationLoader.lastPauseTime != 0) { } else if (lastPauseTime != 0) {
ApplicationLoader.lastPauseTime = System.currentTimeMillis(); lastPauseTime = System.currentTimeMillis();
FileLog.e("tmessages", "reset sleep timeout by received push"); 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 // Config and session manage
//================================================================================ //================================================================================
@ -816,7 +841,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
requestQueue.add(request); requestQueue.add(request);
if (paused && ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0 || (request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0)) { if (paused && ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0 || (request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0)) {
ApplicationLoader.lastPauseTime = System.currentTimeMillis(); lastPauseTime = System.currentTimeMillis();
nextSleepTimeout = 30000; nextSleepTimeout = 30000;
FileLog.e("tmessages", "wakeup by download or upload request"); 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); netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (netInfo != null && (netInfo.isConnectedOrConnecting() || netInfo.isRoaming() || netInfo.isAvailable())) { if (netInfo != null && (netInfo.isConnectedOrConnecting() || netInfo.isRoaming())) {
return true; return true;
} else { } else {
netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(netInfo != null && (netInfo.isConnectedOrConnecting() || netInfo.isRoaming() || netInfo.isAvailable())) { if(netInfo != null && (netInfo.isConnectedOrConnecting() || netInfo.isRoaming())) {
return true; return true;
} }
} }
@ -2588,6 +2613,9 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
sendingPushPing = false; sendingPushPing = false;
lastPushPingTime = System.currentTimeMillis() - 60000 * 3 + 10000; lastPushPingTime = System.currentTimeMillis() - 60000 * 3 + 10000;
} else { } else {
if (paused && connection.getDatacenterId() == currentDatacenterId && (connection.transportRequestClass & RPCRequest.RPCRequestClassGeneric) != 0) {
resumeNetworkMaybe();
}
processRequestQueue(connection.transportRequestClass, connection.getDatacenterId()); processRequestQueue(connection.transportRequestClass, connection.getDatacenterId());
} }
} }

View File

@ -760,7 +760,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
checkDeletingTask(); checkDeletingTask();
if (UserConfig.isClientActivated()) { if (UserConfig.isClientActivated()) {
if (ApplicationLoader.lastPauseTime == 0) { if (ConnectionsManager.lastPauseTime == 0) {
if (statusSettingState != 1 && (lastStatusUpdateTime == 0 || lastStatusUpdateTime <= System.currentTimeMillis() - 55000 || offlineSent)) { if (statusSettingState != 1 && (lastStatusUpdateTime == 0 || lastStatusUpdateTime <= System.currentTimeMillis() - 55000 || offlineSent)) {
statusSettingState = 1; statusSettingState = 1;
@ -786,7 +786,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
} }
}, null, true, RPCRequest.RPCRequestClassGeneric); }, 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; statusSettingState = 2;
if (statusRequest != 0) { if (statusRequest != 0) {
ConnectionsManager.getInstance().cancelRpc(statusRequest, true); ConnectionsManager.getInstance().cancelRpc(statusRequest, true);
@ -3323,7 +3323,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
} }
if (!(res instanceof TLRPC.TL_updates_differenceSlice)) { 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)) { if (!readMessages.contains(obj.messageOwner.id)) {
lastMessage = obj; lastMessage = obj;
} }
@ -3471,7 +3471,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
} else { } else {
dialog_id = obj.messageOwner.to_id.user_id; 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); showInAppNotification(obj);
} }
} }
@ -3534,7 +3534,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
} else { } else {
dialog_id = obj.messageOwner.to_id.user_id; 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); showInAppNotification(obj);
} }
} }
@ -3738,7 +3738,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
arr.add(obj); arr.add(obj);
MessagesStorage.lastPtsValue = update.pts; MessagesStorage.lastPtsValue = update.pts;
if (upd.message.from_id != UserConfig.getClientUserId() && upd.message.to_id != null) { 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; lastMessage = obj;
} }
} }
@ -3846,7 +3846,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
} }
arr.add(obj); arr.add(obj);
if (newMessage.from_id != UserConfig.getClientUserId() && newMessage.to_id != null) { 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; lastMessage = obj;
} }
} }
@ -3895,7 +3895,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
} }
arr.add(obj); arr.add(obj);
if (newMessage.from_id != UserConfig.getClientUserId() && newMessage.to_id != null) { 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; lastMessage = obj;
} }
} }
@ -3916,7 +3916,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
} }
arr.add(obj); arr.add(obj);
if (message.from_id != UserConfig.getClientUserId() && message.to_id != null) { 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; lastMessage = obj;
} }
} }
@ -4261,8 +4261,8 @@ public class MessagesController implements NotificationCenter.NotificationCenter
if (!UserConfig.isClientActivated()) { if (!UserConfig.isClientActivated()) {
return; return;
} }
if (ApplicationLoader.lastPauseTime != 0) { if (ConnectionsManager.lastPauseTime != 0) {
ApplicationLoader.lastPauseTime = System.currentTimeMillis(); ConnectionsManager.lastPauseTime = System.currentTimeMillis();
FileLog.e("tmessages", "reset sleep timeout by received message"); FileLog.e("tmessages", "reset sleep timeout by received message");
} }
if (messageObject == null) { if (messageObject == null) {
@ -4308,7 +4308,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
int vibrate_override = preferences.getInt("vibrate_" + dialog_id, 0); 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 inAppSounds = preferences.getBoolean("EnableInAppSounds", true);
boolean inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true); boolean inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true);
boolean inAppPreview = preferences.getBoolean("EnableInAppPreview", true); boolean inAppPreview = preferences.getBoolean("EnableInAppPreview", true);

View File

@ -19,11 +19,13 @@ public class ScreenReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
FileLog.e("tmessages", "screen off"); FileLog.e("tmessages", "screen off");
ApplicationLoader.lastPauseTime = System.currentTimeMillis(); if (ConnectionsManager.lastPauseTime == 0) {
ConnectionsManager.lastPauseTime = System.currentTimeMillis();
}
ApplicationLoader.isScreenOn = false; ApplicationLoader.isScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
FileLog.e("tmessages", "screen on"); FileLog.e("tmessages", "screen on");
ApplicationLoader.resetLastPauseTime(); ConnectionsManager.resetLastPauseTime();
ApplicationLoader.isScreenOn = true; ApplicationLoader.isScreenOn = true;
} }
} }

View File

@ -29,7 +29,6 @@ import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging; import com.google.android.gms.gcm.GoogleCloudMessaging;
import org.telegram.messenger.ContactsController;
import org.telegram.messenger.NotificationsService; import org.telegram.messenger.NotificationsService;
import org.telegram.messenger.BuildVars; import org.telegram.messenger.BuildVars;
import org.telegram.messenger.ConnectionsManager; import org.telegram.messenger.ConnectionsManager;
@ -52,12 +51,12 @@ public class ApplicationLoader extends Application {
public static final String PROPERTY_REG_ID = "registration_id"; public static final String PROPERTY_REG_ID = "registration_id";
private static final String PROPERTY_APP_VERSION = "appVersion"; private static final String PROPERTY_APP_VERSION = "appVersion";
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
public static long lastPauseTime;
public static Drawable cachedWallpaper = null; public static Drawable cachedWallpaper = null;
public static volatile Context applicationContext = null; public static volatile Context applicationContext = null;
public static volatile Handler applicationHandler = null; public static volatile Handler applicationHandler = null;
private static volatile boolean applicationInited = false; private static volatile boolean applicationInited = false;
public static volatile boolean isScreenOn = false; public static volatile boolean isScreenOn = false;
public static void postInitApplication() { public static void postInitApplication() {
@ -135,7 +134,6 @@ public class ApplicationLoader extends Application {
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
lastPauseTime = System.currentTimeMillis();
applicationContext = getApplicationContext(); applicationContext = getApplicationContext();
applicationHandler = new Handler(applicationContext.getMainLooper()); 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() { private void initPlayServices() {
if (checkPlayServices()) { if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(this); gcm = GoogleCloudMessaging.getInstance(this);

View File

@ -535,7 +535,7 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
ApplicationLoader.lastPauseTime = System.currentTimeMillis(); ConnectionsManager.setAppPaused(true);
if (notificationView != null) { if (notificationView != null) {
notificationView.hide(false); notificationView.hide(false);
} }
@ -559,7 +559,7 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen
} }
Utilities.checkForCrashes(this); Utilities.checkForCrashes(this);
Utilities.checkForUpdates(this); Utilities.checkForUpdates(this);
ApplicationLoader.resetLastPauseTime(); ConnectionsManager.setAppPaused(false);
actionBar.setBackOverlayVisible(currentConnectionState != 0); actionBar.setBackOverlayVisible(currentConnectionState != 0);
try { try {
NotificationManager mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);

View File

@ -273,7 +273,7 @@
<string name="WiFiOnly">nur über WLAN</string> <string name="WiFiOnly">nur über WLAN</string>
<string name="SortFirstName">Vorname</string> <string name="SortFirstName">Vorname</string>
<string name="SortLastName">Nachname</string> <string name="SortLastName">Nachname</string>
<string name="LedColor">LED Color</string> <string name="LedColor">LED Farbe</string>
<!--media view--> <!--media view-->
<string name="NoMedia">Noch keine geteilten Medien vorhanden</string> <string name="NoMedia">Noch keine geteilten Medien vorhanden</string>

View File

@ -273,7 +273,7 @@
<string name="WiFiOnly">Sólo vía WiFi</string> <string name="WiFiOnly">Sólo vía WiFi</string>
<string name="SortFirstName">Nombre</string> <string name="SortFirstName">Nombre</string>
<string name="SortLastName">Apellido</string> <string name="SortLastName">Apellido</string>
<string name="LedColor">LED Color</string> <string name="LedColor">Color del LED</string>
<!--media view--> <!--media view-->
<string name="NoMedia">No hay fotos ni vídeos compartidos aún</string> <string name="NoMedia">No hay fotos ni vídeos compartidos aún</string>