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()后面;










评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值