Skip to content

Commit

Permalink
Optimized battery consumption, fixed blurry photos on some devices, a…
Browse files Browse the repository at this point in the history
…dded option to auto download media only via WiFi, changed way of contacts changes monitoring

DrKLO#422
rubenlagus@d96ce3c140bd49c5c8c63cea2b
c9afad8a16b8bb
  • Loading branch information
DrKLO committed May 24, 2014
1 parent 261b12c commit f5dc961
Show file tree
Hide file tree
Showing 27 changed files with 457 additions and 301 deletions.
4 changes: 2 additions & 2 deletions TMessagesProj/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ android {
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 231
versionName "1.4.13"
versionCode 233
versionName "1.4.14"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public PyroClient connect(InetSocketAddress host, InetSocketAddress bind) throws
}

public void select() {
this.select(10);
this.select(0);
}

public void select(long eventTimeout) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ public BuffersStorage() {
for (int a = 0; a < 5; a++) {
freeBuffers128.add(new ByteBufferDesc(128));
}
for (int a = 0; a < 5; a++) {
freeBuffers1024.add(new ByteBufferDesc(1024 + 200));
}
for (int a = 0; a < 2; a++) {
freeBuffers4096.add(new ByteBufferDesc(4096 + 200));
}
for (int a = 0; a < 2; a++) {
freeBuffers16384.add(new ByteBufferDesc(16384 + 200));
}
for (int a = 0; a < 2; a++) {
freeBuffers32768.add(new ByteBufferDesc(40000));
}
// for (int a = 0; a < 5; a++) {
// freeBuffers1024.add(new ByteBufferDesc(1024 + 200));
// }
// for (int a = 0; a < 2; a++) {
// freeBuffers4096.add(new ByteBufferDesc(4096 + 200));
// }
// for (int a = 0; a < 2; a++) {
// freeBuffers16384.add(new ByteBufferDesc(16384 + 200));
// }
// for (int a = 0; a < 2; a++) {
// freeBuffers32768.add(new ByteBufferDesc(40000));
// }
}

public ByteBufferDesc getFreeBuffer(int size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -64,8 +62,6 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
private boolean registeringForPush = false;

private boolean paused = false;
private Runnable stageRunnable;
private Runnable pingRunnable;
private long lastPingTime = System.currentTimeMillis();
private long lastPushPingTime = System.currentTimeMillis();
private int nextSleepTimeout = 30000;
Expand All @@ -84,6 +80,89 @@ public static ConnectionsManager getInstance() {
return localInstance;
}

static long t = System.currentTimeMillis();
private Runnable stageRunnable = new Runnable() {
@Override
public void run() {
Utilities.stageQueue.handler.removeCallbacks(stageRunnable);
t = System.currentTimeMillis();
if (datacenters != null) {
if (lastPushPingTime < System.currentTimeMillis() - 29000) {
lastPushPingTime = System.currentTimeMillis();
Datacenter datacenter = datacenterWithId(currentDatacenterId);
if (datacenter != null) {
generatePing(datacenter, true);
}
}
}

long currentTime = System.currentTimeMillis();
if (ApplicationLoader.lastPauseTime != 0 && ApplicationLoader.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)) {
dontSleep = true;
break;
}
}
if (!dontSleep) {
for (RPCRequest request : requestQueue) {
if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0 || (request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0) {
dontSleep = true;
break;
}
}
}
if (!dontSleep) {
if (!paused) {
FileLog.e("tmessages", "pausing network and timers by sleep time = " + nextSleepTimeout);
for (Datacenter datacenter : datacenters.values()) {
if (datacenter.connection != null) {
datacenter.connection.suspendConnection(true);
}
if (datacenter.uploadConnection != null) {
datacenter.uploadConnection.suspendConnection(true);
}
if (datacenter.downloadConnection != null) {
datacenter.downloadConnection.suspendConnection(true);
}
}
}
try {
paused = true;
Utilities.stageQueue.postRunnable(stageRunnable, 1000);
return;
} catch (Exception e) {
FileLog.e("tmessages", e);
}
} else {
ApplicationLoader.lastPauseTime += 30 * 1000;
FileLog.e("tmessages", "don't sleep 30 seconds because of upload or download request");
}
}
if (paused) {
paused = false;
FileLog.e("tmessages", "resume network and timers");
}

if (datacenters != null) {
MessagesController.getInstance().updateTimerProc();
if (datacenterWithId(currentDatacenterId).authKey != null) {
if (lastPingTime < System.currentTimeMillis() - 19000) {
lastPingTime = System.currentTimeMillis();
generatePing();
}
if (!updatingDcSettings && lastDcUpdateTime < (int)(System.currentTimeMillis() / 1000) - DC_UPDATE_TIME) {
updateDcSettings(0);
}
processRequestQueue(0, 0);
}
}

Utilities.stageQueue.postRunnable(stageRunnable, 1000);
}
};

public ConnectionsManager() {
currentAppVersion = ApplicationLoader.getAppVersion();
lastOutgoingMessageId = 0;
Expand All @@ -94,89 +173,7 @@ public ConnectionsManager() {
connectionState = 1;
}

Timer serviceTimer = new Timer();
serviceTimer.schedule(new TimerTask() {
@Override
public void run() {
Utilities.stageQueue.postRunnable(new Runnable() {
@Override
public void run() {
if (datacenters != null) {
if (lastPushPingTime < System.currentTimeMillis() - 29000) {
lastPushPingTime = System.currentTimeMillis();
Datacenter datacenter = datacenterWithId(currentDatacenterId);
if (datacenter != null) {
generatePing(datacenter, true);
}
}
}

long currentTime = System.currentTimeMillis();
if (ApplicationLoader.lastPauseTime != 0 && ApplicationLoader.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)) {
dontSleep = true;
break;
}
}
if (!dontSleep) {
for (RPCRequest request : requestQueue) {
if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0 || (request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0) {
dontSleep = true;
break;
}
}
}
if (!dontSleep) {
if (!paused) {
FileLog.e("tmessages", "pausing network and timers by sleep time = " + nextSleepTimeout);
for (Datacenter datacenter : datacenters.values()) {
if (datacenter.connection != null) {
datacenter.connection.suspendConnection(true);
}
if (datacenter.uploadConnection != null) {
datacenter.uploadConnection.suspendConnection(true);
}
if (datacenter.downloadConnection != null) {
datacenter.downloadConnection.suspendConnection(true);
}
}
}
try {
paused = true;
Thread.sleep(500);
return;
} catch (Exception e) {
FileLog.e("tmessages", e);
}
} else {
ApplicationLoader.lastPauseTime += 30 * 1000;
FileLog.e("tmessages", "don't sleep 30 seconds because of upload or download request");
}
}
if (paused) {
paused = false;
FileLog.e("tmessages", "resume network and timers");
}

if (datacenters != null) {
MessagesController.getInstance().updateTimerProc();
if (datacenterWithId(currentDatacenterId).authKey != null) {
if (lastPingTime < System.currentTimeMillis() - 19000) {
lastPingTime = System.currentTimeMillis();
generatePing();
}
if (!updatingDcSettings && lastDcUpdateTime < (int)(System.currentTimeMillis() / 1000) - DC_UPDATE_TIME) {
updateDcSettings(0);
}
processRequestQueue(0, 0);
}
}
}
});
}
}, 1000, 1000);
Utilities.stageQueue.postRunnable(stageRunnable, 1000);
}

public void resumeNetworkMaybe() {
Expand All @@ -196,6 +193,7 @@ public void run() {
}

public void applicationMovedToForeground() {
Utilities.stageQueue.postRunnable(stageRunnable);
Utilities.stageQueue.postRunnable(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -848,23 +846,36 @@ public void run() {
}

public static boolean isNetworkOnline() {
boolean status = false;
try {
ConnectivityManager cm = (ConnectivityManager)ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getNetworkInfo(0);
NetworkInfo netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
status = true;
return true;
} else {
netInfo = cm.getNetworkInfo(1);
netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
status = true;
return true;
}
}
} catch(Exception e) {
FileLog.e("tmessages", e);
return true;
}
return status;
return false;
}

public static boolean isConnectedToWiFi() {
try {
ConnectivityManager cm = (ConnectivityManager)ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
} catch(Exception e) {
FileLog.e("tmessages", e);
return true;
}
return false;
}

public int getCurrentTime() {
Expand Down
Loading

0 comments on commit f5dc961

Please sign in to comment.