am 09dff843: Merge "Only try to fetch time from NTP if there is a network connection." into lmp-mr1-dev
* commit '09dff843b8ed402564c4c89d7e1cabceb715e458': Only try to fetch time from NTP if there is a network connection.
This commit is contained in:
@@ -19,6 +19,8 @@ package android.util;
|
|||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.NetworkInfo;
|
||||||
import android.net.SntpClient;
|
import android.net.SntpClient;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
@@ -34,10 +36,13 @@ public class NtpTrustedTime implements TrustedTime {
|
|||||||
private static final boolean LOGD = false;
|
private static final boolean LOGD = false;
|
||||||
|
|
||||||
private static NtpTrustedTime sSingleton;
|
private static NtpTrustedTime sSingleton;
|
||||||
|
private static Context sContext;
|
||||||
|
|
||||||
private final String mServer;
|
private final String mServer;
|
||||||
private final long mTimeout;
|
private final long mTimeout;
|
||||||
|
|
||||||
|
private ConnectivityManager mCM;
|
||||||
|
|
||||||
private boolean mHasCache;
|
private boolean mHasCache;
|
||||||
private long mCachedNtpTime;
|
private long mCachedNtpTime;
|
||||||
private long mCachedNtpElapsedRealtime;
|
private long mCachedNtpElapsedRealtime;
|
||||||
@@ -66,6 +71,7 @@ public class NtpTrustedTime implements TrustedTime {
|
|||||||
|
|
||||||
final String server = secureServer != null ? secureServer : defaultServer;
|
final String server = secureServer != null ? secureServer : defaultServer;
|
||||||
sSingleton = new NtpTrustedTime(server, timeout);
|
sSingleton = new NtpTrustedTime(server, timeout);
|
||||||
|
sContext = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sSingleton;
|
return sSingleton;
|
||||||
@@ -78,6 +84,20 @@ public class NtpTrustedTime implements TrustedTime {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We can't do this at initialization time: ConnectivityService might not be running yet.
|
||||||
|
synchronized (this) {
|
||||||
|
if (mCM == null) {
|
||||||
|
mCM = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final NetworkInfo ni = mCM == null ? null : mCM.getActiveNetworkInfo();
|
||||||
|
if (ni == null || !ni.isConnected()) {
|
||||||
|
if (LOGD) Log.d(TAG, "forceRefresh: no connectivity");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
|
if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
|
||||||
final SntpClient client = new SntpClient();
|
final SntpClient client = new SntpClient();
|
||||||
if (client.requestTime(mServer, (int) mTimeout)) {
|
if (client.requestTime(mServer, (int) mTimeout)) {
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public class NetworkTimeUpdateService {
|
|||||||
|
|
||||||
private static final int EVENT_AUTO_TIME_CHANGED = 1;
|
private static final int EVENT_AUTO_TIME_CHANGED = 1;
|
||||||
private static final int EVENT_POLL_NETWORK_TIME = 2;
|
private static final int EVENT_POLL_NETWORK_TIME = 2;
|
||||||
private static final int EVENT_NETWORK_CONNECTED = 3;
|
private static final int EVENT_NETWORK_CHANGED = 3;
|
||||||
|
|
||||||
private static final String ACTION_POLL =
|
private static final String ACTION_POLL =
|
||||||
"com.android.server.NetworkTimeUpdateService.action.POLL";
|
"com.android.server.NetworkTimeUpdateService.action.POLL";
|
||||||
@@ -248,18 +248,8 @@ public class NetworkTimeUpdateService {
|
|||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
|
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
|
||||||
// There is connectivity
|
// Don't bother checking if we have connectivity, NtpTrustedTime does that for us.
|
||||||
final ConnectivityManager connManager = (ConnectivityManager) context
|
mHandler.obtainMessage(EVENT_NETWORK_CHANGED).sendToTarget();
|
||||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
||||||
final NetworkInfo netInfo = connManager.getActiveNetworkInfo();
|
|
||||||
if (netInfo != null) {
|
|
||||||
// Verify that it's a WIFI connection
|
|
||||||
if (netInfo.getState() == NetworkInfo.State.CONNECTED &&
|
|
||||||
(netInfo.getType() == ConnectivityManager.TYPE_WIFI ||
|
|
||||||
netInfo.getType() == ConnectivityManager.TYPE_ETHERNET) ) {
|
|
||||||
mHandler.obtainMessage(EVENT_NETWORK_CONNECTED).sendToTarget();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -276,7 +266,7 @@ public class NetworkTimeUpdateService {
|
|||||||
switch (msg.what) {
|
switch (msg.what) {
|
||||||
case EVENT_AUTO_TIME_CHANGED:
|
case EVENT_AUTO_TIME_CHANGED:
|
||||||
case EVENT_POLL_NETWORK_TIME:
|
case EVENT_POLL_NETWORK_TIME:
|
||||||
case EVENT_NETWORK_CONNECTED:
|
case EVENT_NETWORK_CHANGED:
|
||||||
onPollNetworkTime(msg.what);
|
onPollNetworkTime(msg.what);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user