您的位置:首页 > 移动开发 > Android开发

android4.4 系统时间同步,添加NTP备份地址功能

2016-12-07 10:44 836 查看
添加NTP备份地址功能,同步cn.pool.ntp.org服务器时间失败的话,可同步129.6.15.28服务器的时间,双保险。

diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 81b388d..ae3e286 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -2867,6 +2867,7 @@ public final class Settings {
MOVED_TO_GLOBAL.add(Settings.Global.NITZ_UPDATE_SPACING);
MOVED_TO_GLOBAL.add(Settings.Global.NTP_SERVER);
MOVED_TO_GLOBAL.add(Settings.Global.NTP_TIMEOUT);
+			MOVED_TO_GLOBAL.add(Settings.Global.NTP_BACKUP_SERVER);
MOVED_TO_GLOBAL.add(Settings.Global.PDP_WATCHDOG_ERROR_POLL_COUNT);
MOVED_TO_GLOBAL.add(Settings.Global.PDP_WATCHDOG_LONG_POLL_INTERVAL_MS);
MOVED_TO_GLOBAL.add(Settings.Global.PDP_WATCHDOG_MAX_PDP_RESET_FAIL_COUNT);
@@ -5074,6 +5075,8 @@ public final class Settings {

/** Preferred NTP server. {@hide} */
public static final String NTP_SERVER = "ntp_server";
+       /** Preferred NTP backup server. {@hide} */
+       public static final String NTP_BACKUP_SERVER = "ntp_server2";
/** Timeout in milliseconds to wait for NTP server. {@hide} */
public static final String NTP_TIMEOUT = "ntp_timeout";

diff --git a/core/java/android/util/NtpTrustedTime.java b/core/java/android/util/NtpTrustedTime.java
index 602a68c..d1bd18c 100644
--- a/core/java/android/util/NtpTrustedTime.java
+++ b/core/java/android/util/NtpTrustedTime.java
@@ -38,6 +38,9 @@ public class NtpTrustedTime implements TrustedTime {
private final String mServer;
private final long mTimeout;

+	private String mBackupServer = null;
+
private boolean mHasCache;
private long mCachedNtpTime;
private long mCachedNtpElapsedRealtime;
@@ -49,6 +52,11 @@ public class NtpTrustedTime implements TrustedTime {
mTimeout = timeout;
}

+	private void setBackupServer(String server)
+	{
+		mBackupServer = server;
+	}
+
public static synchronized NtpTrustedTime getInstance(Context context) {
if (sSingleton == null) {
final Resources res = context.getResources();
@@ -56,6 +64,8 @@ public class NtpTrustedTime implements TrustedTime {

final String defaultServer = res.getString(
com.android.internal.R.string.config_ntpServer);
+            final String defaultServer2 = res.getString(
+                    com.android.internal.R.string.config_ntpServer2);
final long defaultTimeout = res.getInteger(
com.android.internal.R.integer.config_ntpTimeout);

@@ -64,8 +74,16 @@ public class NtpTrustedTime implements TrustedTime {
final long timeout = Settings.Global.getLong(
resolver, Settings.Global.NTP_TIMEOUT, defaultTimeout);

+			final String secureServer2 = Settings.Global.getString(
+                    resolver, Settings.Global.NTP_BACKUP_SERVER);
+
final String server = secureServer != null ? secureServer : defaultServer;
+            final String backupServer = secureServer2 != null ? secureServer2 : defaultServer2;
sSingleton = new NtpTrustedTime(server, timeout);
+			if(sSingleton != null)
+			{
+				sSingleton.setBackupServer(backupServer);
+			}
}

return sSingleton;
@@ -80,7 +98,8 @@ public class NtpTrustedTime implements TrustedTime {

if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
final SntpClient client = new SntpClient();
-        if (client.requestTime(mServer, (int) mTimeout)) {
+        if (client.requestTime(mServer, (int) mTimeout) ||
+			(mBackupServer != null && client.requestTime(mBackupServer, (int) mTimeout))) {
mHasCache = true;
mCachedNtpTime = client.getNtpTime();
mCachedNtpElapsedRealtime = client.getNtpTimeReference();
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index b7dd977..88550e1 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -1060,7 +1060,9 @@
<bool name="config_allowActionMenuItemTextWithIcon">false</bool>

<!-- Remote server that can provide NTP responses. -->
-    <string translatable="false" name="config_ntpServer">2.android.pool.ntp.org</string>
+    <string translatable="false" name="config_ntpServer">cn.pool.ntp.org</string>
+    <!-- Remote server that can provide NTP backup responses. -->
+    <string translatable="false" name="config_ntpServer2">129.6.15.28</string>
<!-- Normal polling frequency in milliseconds -->
<integer name="config_ntpPollingInterval">864000000</integer>
<!-- Try-again polling interval in milliseconds, in case the network request failed -->
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 84e6cce..957a821 100755
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -477,6 +477,7 @@
<java-symbol type="string" name="config_mms_user_agent" />
<java-symbol type="string" name="config_mms_user_agent_profile_url" />
<java-symbol type="string" name="config_ntpServer" />
+  <java-symbol type="string" name="config_ntpServer2" />
<java-symbol type="string" name="config_tether_apndata" />
<java-symbol type="string" name="config_useragentprofile_url" />
<java-symbol type="string" name="config_wifi_p2p_device_type" />

diff --git a/core/java/android/net/SntpClient.java b/core/java/android/net/SntpClient.java
index 316440f..b46b632 100644
--- a/core/java/android/net/SntpClient.java
+++ b/core/java/android/net/SntpClient.java
@@ -120,7 +120,7 @@ public class SntpClient
mNtpTimeReference = responseTicks;
mRoundTripTime = roundTripTime;
} catch (Exception e) {
-            if (false) Log.d(TAG, "request time failed: " + e);
+            if (/*false*/true) Log.d(TAG, "request time failed: " + e);
return false;
} finally {
if (socket != null) {
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android NTP 时间同步