1. 程式人生 > >android7.0修改系統預設時間

android7.0修改系統預設時間

    客戶需求:修改預設系統時間為2017/1/1,即燒機後開機顯示的時間。

Index: SystemServer.java
===================================================================
--- SystemServer.java	(revision 5703)
+++ SystemServer.java	(revision 5704)
@@ -129,8 +129,8 @@
 
     // The earliest supported time.  We pick one day into 1970, to
     // give any timezone code room without going into negative time.   
-    //wangxx modify for default time 20170705 start
-    //private static final long EARLIEST_SUPPORTED_TIME = 1451577600L * 1000;
+    //wangxx modify for default time 20170706 start
+    private static final long EARLIEST_SUPPORTED_TIME_TEMP = 1451577600L * 1000;
     private static final long EARLIEST_SUPPORTED_TIME =  1483200000000L; //20170101 00:00  /*86400 * 1000*/
     //wangxx modify for default time 20170705 end
 
@@ -242,10 +242,14 @@
             // APIs crash dealing with negative numbers, notably
             // java.io.File#setLastModified, so instead we fake it and
             // hope that time from cell towers or NTP fixes it shortly.
-            /*if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) { //wangxx modify for default time 20170705 start
-                Slog.w(TAG, "System clock is before 1970; setting to 1970.");
-                SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
-            }*/ //wangxx modify for default time 20170705 end
+            //wangxx modify for default time 20170706 start
+			boolean firstBoot = SystemProperties.getBoolean("persist.sys.firstboot", true);
+		    if(!firstBoot){//wangxx modify for default time 20170705 start
+			    if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME_TEMP) {
+					Slog.w(TAG, "System clock is before 1970; setting to 1970.");
+                    SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME_TEMP);
+                }
+		    }//wangxx modify for default time 20170706 end
 
             // If the system has "persist.sys.language" and friends set, replace them with
             // "persist.sys.locale". Note that the default locale at this point is calculated
@@ -350,11 +354,16 @@
         } finally {
             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
         }
-        //wangxx modify for default time 20170705 start
+        //wangxx modify for default time 20170706 start
+		boolean firstBoot = SystemProperties.getBoolean("persist.sys.firstboot", true);
         Slog.w("XXX", "SystemServer ==> System.currentTimeMillis()=" + System.currentTimeMillis()); 
-        if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {//1220093030 
-            SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
-        }//wangxx modify for default time 20170705 end
+		if(firstBoot){
+            if (System.currentTimeMillis() != EARLIEST_SUPPORTED_TIME) {//1220093030 
+               SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
+            }
+			SystemProperties.set("persist.sys.firstboot", "false");
+		}//wangxx modify for default time 20170706 end
+        
         // For debug builds, log event loop stalls to dropbox for analysis.
         if (StrictMode.conditionallyEnableDebugLogging()) {
             Slog.i(TAG, "Enabled StrictMode for system server main thread.");

    解決方案:費了很大週摺才弄好,貼出來,希望能幫助到網友

android\vendor\qcom\proprietary\time-services\time_daemon_qmi.c

#define DEFAULT_TIME 1
static int genoff_post_init(time_genoff_ptr time_genoff)
{
	int rc;

	if (time_genoff->init_func != NULL) {
		rc = time_genoff->init_func();
		if (rc) {
			TIME_LOGE("Daemon:%s:Init func failed\n", __func__);
			return -EINVAL;
		}
	}

	if (time_genoff->per_storage_spec.initialized == 1) {
		/* Read from the generic offset */
		rc = time_persistent_memory_opr(
				time_genoff->per_storage_spec.f_name,
				TIME_READ_MEMORY, &(time_genoff->generic_offset));
		if (rc) {
			TIME_LOGD("Daemon:%s:Error in accessing storage\n",
					__func__);
			#if DEFAULT_TIME ///force system time = 2017/1/1
            time_genoff->generic_offset = (uint64_t)SEC_TO_MSEC (((117-70) * 365 * 86400) -
            (3 * 86400)) - time_genoff->generic_offset;
			#else
			time_genoff->generic_offset = 0;
			#endif
		}
	}

	time_genoff->initialized = 1;

	return 0;
}


其中黃色為新增程式碼(及修改程式碼)

做這個需求很是折騰,專案要量產,快量產時需求才給出,網上搜索了好多沒有一個比較好的或者完整的修改方案,為此加班了大半夜。之前使用的是高通給的修改5.0的patch,結果引起問題,後面會給出5.0的方案。這是高通給的7.0的patch,被坑了一把,剛開始時給的時候少了個括號,沒太注意,一直編譯不過,但是編譯報錯又沒報這個地方的錯誤,也沒想到是修改這段程式碼的問題,一直以為是伺服器的問題,再加上7.0之後編譯系統原始碼使用jack-server特別慢,很少費勁,有編譯好的方案,希望網友可以提供下,感謝。time_genoff->generic_offset這個值不好打log,無法確定是多少,所以改為2017/1/1號的時候費了些周折(先改一遍,看時間A,然後根據想要的時間B和A差幾天,再做調整),網友在修改的時候需根據自己的原始碼情況適當修改時間日期。

117為2017年意思,

70為1970,365天,每天86400秒

再修改時,根據實際情況適當調整 3 * 86400這個時間段(3天)

5.0時高通給的pathch(邏輯上是有問題的,不過當時能用<不知道為啥沒出錯>,就沒管,上面7.0的方案5.0應該也是適用的,最好使用上面的方案)

./frameworks/base/services/java/com/android/server/SystemServer.java
1)EARLIEST_SUPPORTED_TIME 賦值為 1199275200000L;
即 private static final long EARLIEST_SUPPORTED_TIME = 1199275200000L;
把 SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME) 這句程式碼放到startOtherServices()後面;