Android 9 NTP GPS time sync logic

diff --git a/build/make/tools/buildinfo.sh b/build/make/tools/buildinfo.sh
index 5a54462..4a20fd8 100755
--- a/build/make/tools/buildinfo.sh
+++ b/build/make/tools/buildinfo.sh
@@ -60,5 +60,6 @@
   echo "ro.build.thumbprint=$BUILD_THUMBPRINT"
 fi
 echo "ro.build.characteristics=$TARGET_AAPT_CHARACTERISTICS"
+echo "persist.sys.timezone=Asia/Shanghai"
 
 echo "# end build properties"
diff --git a/frameworks/base/core/java/android/util/NtpTrustedTime.java b/frameworks/base/core/java/android/util/NtpTrustedTime.java
old mode 100644
new mode 100755
index 30d7b6c..cc6cb27
--- a/frameworks/base/core/java/android/util/NtpTrustedTime.java
+++ b/frameworks/base/core/java/android/util/NtpTrustedTime.java
@@ -26,6 +26,7 @@
 import android.os.SystemClock;
 import android.provider.Settings;
 import android.text.TextUtils;
+import android.os.SystemProperties;
 
 /**
  * {@link TrustedTime} that connects with a remote NTP server as its trusted
@@ -35,13 +36,21 @@
  */
 public class NtpTrustedTime implements TrustedTime {
     private static final String TAG = "NtpTrustedTime";
-    private static final boolean LOGD = false;
+    private static final boolean LOGD = true;
 
     private static NtpTrustedTime sSingleton;
     private static Context sContext;
 
     private final String mServer;
     private final long mTimeout;
+    //[ADD-BEGIN] by dowork,NTP server
+    private String[] ntpServerHost = new String[] { "ntp1.aliyun.com", "ntp2.aliyun.com", 
+        "ntp3.aliyun.com", "ntp4.aliyun.com", "ntp5.aliyun.com", 
+        "ntp6.aliyun.com", "ntp7.aliyun.com", "dns1.synet.edu.cn",
+        "news.neu.edu.cn", "dns.sjtu.edu.cn", "dns2.synet.edu.cn",
+        "ntp.glnet.edu.cn", "ntp-sz.chl.la", "ntp.gwadar.cn",
+        "cn.pool.ntp.org"};
+    //[ADD-END]
 
     private ConnectivityManager mCM;
 
@@ -121,6 +130,26 @@
             mCachedNtpCertainty = client.getRoundTripTime() / 2;
             return true;
         } else {
+            //[ADD-BEGIN] by dowork,NTP server
+            for(String s : ntpServerHost){
+                if (client.requestTime(s, (int) mTimeout, network)){
+                    mHasCache = true;
+                    mCachedNtpTime = client.getNtpTime();
+                    mCachedNtpElapsedRealtime = client.getNtpTimeReference();
+                    mCachedNtpCertainty = client.getRoundTripTime() / 2;
+
+                    if (LOGD){
+                        Log.d(TAG, "forceRefresh() ntpServerHost : " + s);
+                        Log.d(TAG, "forceRefresh() mCachedNtpTime : " + mCachedNtpTime);
+                        Log.d(TAG, "forceRefresh() mCachedNtpElapsedRealtime : " + mCachedNtpElapsedRealtime);
+                        Log.d(TAG, "forceRefresh() mCachedNtpCertainty : " + mCachedNtpCertainty);
+                        Log.d(TAG, "forceRefresh() ,timezone : " + (SystemProperties.get("persist.sys.timezone", "")));
+                    }
+                    return true;
+                }
+            }//[ADD-END] by dowork
+            Log.i(TAG, "forceRefresh() updateNtp failed ");
+            
             return false;
         }
     }
diff --git a/frameworks/base/services/core/java/com/android/server/NetworkTimeUpdateService.java b/frameworks/base/services/core/java/com/android/server/NetworkTimeUpdateService.java
old mode 100644
new mode 100755
index b3a8fb6..92f314f
--- a/frameworks/base/services/core/java/com/android/server/NetworkTimeUpdateService.java
+++ b/frameworks/base/services/core/java/com/android/server/NetworkTimeUpdateService.java
@@ -45,6 +45,14 @@
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 
+import android.location.Location;
+import android.location.LocationListener;
+import android.location.LocationManager;
+import android.os.Bundle;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
 /**
  * Monitors the network time and updates the system time if it is out of sync
  * and there hasn't been any NITZ update from the carrier recently.
@@ -58,11 +66,13 @@
 public class NetworkTimeUpdateService extends Binder {
 
     private static final String TAG = "NetworkTimeUpdateService";
-    private static final boolean DBG = false;
+    private static final boolean DBG = true;
 
     private static final int EVENT_AUTO_TIME_CHANGED = 1;
     private static final int EVENT_POLL_NETWORK_TIME = 2;
     private static final int EVENT_NETWORK_CHANGED = 3;
+
+    private static final int EVENT_POLL_GPS_TIME = 4;//Gps
 
     private static final String ACTION_POLL =
             "com.android.server.NetworkTimeUpdateService.action.POLL";
@@ -97,6 +107,12 @@
     // During bootup, the network may not have been up yet, or it's taking time for the
     // connection to happen.
     private int mTryAgainCounter;
+
+    //[ADD] by dowork, GPS
+    private LocationListener mlocListener;
+    private LocationManager mlocManager;
+    private int mMinDistance = 1; // GPS distance
+    private int mMinTime = 10000; // GPS period
 
     public NetworkTimeUpdateService(Context context) {
         mContext = context;
@@ -133,6 +149,9 @@
 
         mSettingsObserver = new SettingsObserver(mHandler, EVENT_AUTO_TIME_CHANGED);
         mSettingsObserver.observe(mContext);
+
+        //GPS
+        registerLocationListener();
     }
 
     private void registerForTelephonyIntents() {
@@ -326,4 +345,69 @@
         pw.println("NTP cache certainty: " + mTime.getCacheCertainty());
         pw.println();
     }
+
+    //[ADD-BEGIN] by dowork, GPS time sync
+    private void registerLocationListener() {
+        if(!isAutomaticTimeRequested()) {
+            Log.d(TAG, "isAutomaticTimeRequested is false ,return");
+            return;
+        }
+        
+        openGPS();//open GPS
+        
+        mlocManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
+        mlocListener = new LocationListener() {
+            public void onLocationChanged(Location location) {
+                if (location != null) {
+                    Log.d(TAG, "onLocationChanged:" + location.getLatitude() + ","
+                        + location.getLongitude() + ",  time: " + location.getTime());
+                    
+                    long gpsTime = location.getTime();
+                    gpsTime = gpsTime + TimeZone.getDefault().getRawOffset();
+                    Log.d(TAG, "gpsTime: " + gpsTime);
+                    SystemClock.setCurrentTimeMillis(gpsTime);
+                    
+                    printTime(gpsTime);
+                }
+            }
+
+            public void onProviderDisabled(String provider) {
+                Log.d(TAG, "onProviderDisabled: " + provider);
+            }
+
+            public void onProviderEnabled(String provider) {
+                Log.d(TAG, "onProviderEnabled: " + provider);
+            }
+
+            public void onStatusChanged(String provider, int status, Bundle extras) {
+                    Log.d(TAG, "onStatusChanged: " + provider + ",status:" + status);
+                }
+            };
+
+            try {
+                mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, ((long) this.mMinTime),
+                        ((float) this.mMinDistance), this.mlocListener);
+            } catch (Exception e) {
+                Log.e(TAG, "this.mlocManager.requestLocationUpdates null pointer!");
+                e.printStackTrace();
+            }
+    }
+
+    private void printTime(long time) {
+    SimpleDateFormat formatter = new SimpleDateFormat(
+            "yyyy/MM/dd HH:mm:ss");
+    Date curDate = new Date(time);
+    String str = formatter.format(curDate);
+    Log.d(TAG, "time: " + str);
+    }
+
+    public void openGPS() {
+        boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(
+                this.mContext.getContentResolver(),
+                LocationManager.GPS_PROVIDER);
+        if (!gpsEnabled) {
+            Settings.Secure.setLocationProviderEnabled( mContext.getContentResolver(),LocationManager.GPS_PROVIDER, true);
+        }
+    }
+    //[ADD-END]
 }
diff --git a/hardware/libhardware/modules/my-gps/sys_gps.c b/hardware/libhardware/modules/my-gps/sys_gps.c
index c3ff8eb..53d7926 100755
--- a/hardware/libhardware/modules/my-gps/sys_gps.c
+++ b/hardware/libhardware/modules/my-gps/sys_gps.c
@@ -245,7 +245,7 @@
     r->utc_mon  = -1;
     r->utc_day  = -1;
 
-    parse_nmea_reader_update_utc_diff( r );
+    //parse_nmea_reader_update_utc_diff( r );
 }
 
 static int parse_nmea_reader_get_timestamp(NmeaReader*  r, Token  tok, time_t *timestamp)
@@ -275,13 +275,19 @@
     tm.tm_mday = r->utc_day;
     tm.tm_isdst = -1;
 
-    AIP_LOGI("h: %d, m: %d, s: %d", tm.tm_hour, tm.tm_min, tm.tm_sec);
-    AIP_LOGI("Y: %d, M: %d, D: %d", tm.tm_year, tm.tm_mon, tm.tm_mday);
+    //AIP_LOGI("h: %d, m: %d, s: %d", tm.tm_hour, tm.tm_min, tm.tm_sec);
+    //AIP_LOGI("Y: %d, M: %d, D: %d", tm.tm_year, tm.tm_mon, tm.tm_mday);
 
+#if 0
 	parse_nmea_reader_update_utc_diff(r);
 
 	ttime = mktime( &tm );
 	*timestamp = ttime - r->utc_diff;
+#endif
+
+	ttime = mktime( &tm );
+	*timestamp = ttime;
+	AIP_LOGI("timestamp:%s ", ctime(timestamp));
 
     return 0;
 }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值