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 {
minSdkVersion 8
targetSdkVersion 19
versionCode 260
versionCode 261
versionName "1.5.6"
}
}

View File

@ -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());
}
}

View File

@ -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);

View File

@ -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;
}
}

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.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);

View File

@ -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);

View File

@ -273,7 +273,7 @@
<string name="WiFiOnly">nur über WLAN</string>
<string name="SortFirstName">Vorname</string>
<string name="SortLastName">Nachname</string>
<string name="LedColor">LED Color</string>
<string name="LedColor">LED Farbe</string>
<!--media view-->
<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="SortFirstName">Nombre</string>
<string name="SortLastName">Apellido</string>
<string name="LedColor">LED Color</string>
<string name="LedColor">Color del LED</string>
<!--media view-->
<string name="NoMedia">No hay fotos ni vídeos compartidos aún</string>