[RK3399][Android7.1.1]自动同步日期和时间无效

测试平台

Platform: RK3399
OS: Android 7.1.1

现象

Android自动同步时间是通过NtpServer来同步时间的,但是默认的服务器使用的是Google的所以无法连接.

解决方案

  1. 修改默认的Ntp服务器
	diff --git a/frameworks/base/packages/SettingsProvider/packages/SettingsProvider/res/values/defaults.xml 	
	b/frameworks/base/packages/SettingsProvider/packages/SettingsProvider/res/values/defaults.xml
	index 07d968a..782f613 100644
	--- a/frameworks/base/packages/SettingsProvider/packages/SettingsProvider/res/values/defaults.xml
	+++ b/frameworks/base/packages/SettingsProvider/packages/SettingsProvider/res/values/defaults.xml
	@@ -231,4 +231,6 @@
	     <item name="def_font_scale" format="float" type="dimen">1.15item>
 
	+    <string name="def_ntp_server" translatable="false">time.windows.comstring>
	+
 	resources>
diff --git a/frameworks/base/packages/SettingsProvider/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java b/frameworks/base/packages/SettingsProvider/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
index 57776e2..d9fcefd 100644
--- a/frameworks/base/packages/SettingsProvider/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
+++ b/frameworks/base/packages/SettingsProvider/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
@@ -2727,6 +2727,10 @@ class DatabaseHelper extends SQLiteOpenHelper {
             loadSetting(stmt, Settings.Global.ENHANCED_4G_MODE_ENABLED,
                     ImsConfig.FeatureValueConstants.ON);
 
+
+            loadStringSetting(stmt, Settings.Global.NTP_SERVER,
+                    R.string.def_ntp_server);
+
             /*
              * IMPORTANT: Do not add any more upgrade steps here as the global,
              * secure, and system settings are no longer stored in a database
  1. 添加多个Ntp服务器用于轮询,避免默认Ntp服务器失效
diff --git a/frameworks/base/core/java/android/util/NtpTrustedTime.java b/frameworks/base/core/java/android/util/NtpTrustedTime.java

index ed2d3c6..36e5b11 100644 (file)

--- a/frameworks/base/core/java/android/util/NtpTrustedTime.java
+++ b/frameworks/base/core/java/android/util/NtpTrustedTime.java
@@ -39,7 +39,7 @@ public class NtpTrustedTime implements TrustedTime {
    private static NtpTrustedTime sSingleton;
    private static Context sContext;

-    private final String mServer;
+    private String mServer;
    private final long mTimeout;

    private ConnectivityManager mCM;
@@ -49,6 +49,13 @@ public class NtpTrustedTime implements TrustedTime {
    private long mCachedNtpElapsedRealtime;
    private long mCachedNtpCertainty;

+    String[] backupNtpServers = new String[]{
+            "tw.pool.ntp.org",
+            "time.nist.gov",
+            "time-a.nist.gov"
+    };
+    int index = -1;
+
    private NtpTrustedTime(String server, long timeout) {
        if (LOGD) Log.d(TAG, "creating NtpTrustedTime using " + server);
        mServer = server;
@@ -101,15 +108,28 @@ 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)) {
-            mHasCache = true;
-            mCachedNtpTime = client.getNtpTime();
-            mCachedNtpElapsedRealtime = client.getNtpTimeReference();
-            mCachedNtpCertainty = client.getRoundTripTime() / 2;
-            return true;
-        } else {
-            return false;
-        }
+               boolean result = false;
+               while(!(result = client.requestTime(mServer, (int)mTimeout)) && index < (backupNtpServers.length-1)){
+               index++;
+               mServer = backupNtpServers[index];
+        }  
+        index = -1;
+               Resources res = sContext.getResources();
+               String defaultServer = res.getString(
+                    com.android.internal.R.string.config_ntpServer);
+               String secureServer = Settings.Global.getString(
+                     sContext.getContentResolver(), Settings.Global.NTP_SERVER);
+
+               mServer  = secureServer != null ? secureServer : defaultServer;
+               
+               if(result){
+                       mHasCache = true;
+                       mCachedNtpTime = client.getNtpTime();
+                       mCachedNtpElapsedRealtime = client.getNtpTimeReference();
+                       mCachedNtpCertainty = client.getRoundTripTime() / 2;
+               }
+            
+               return result;
    }

    @Override

你可能感兴趣的:([RK3399][Android7.1.1]自动同步日期和时间无效)