现在的位置: 首页 > 综合 > 正文

自动设置时间

2013年10月11日 ⁄ 综合 ⁄ 共 6075字 ⁄ 字号 评论关闭
I/GsmServiceStateTracker(  164): Auto time state changed
./res/values/strings.xml:    <string name="date_time_auto_summaryOn">Use network-provided values</string>
./res/xml/date_time_prefs.xml:        android:summaryOn="@string/date_time_auto_summaryOn"
<CheckBoxPreference android:key="auto_time" 
./src/com/android/settings/DateTimeSettings.java:    private static final String KEY_AUTO_TIME = "auto_time";
        } else if (key.equals(KEY_AUTO_TIME)) {
            boolean autoEnabled = preferences.getBoolean(key, true);
            Settings.System.putInt(getContentResolver(),
                    Settings.System.AUTO_TIME,
                    autoEnabled ? 1 : 0);
            mTimePref.setEnabled(!autoEnabled);
            mDatePref.setEnabled(!autoEnabled);
            mTimeZone.setEnabled(!autoEnabled);
        }
frameworks/base/telephony/java/com/android/internal/telephony/gsm/GsmServiceStateTracker.java:                Settings.System.getUriFor(Settings.System.AUTO_TIME), true,
frameworks/base/telephony/java/com/android/internal/telephony/gsm/GsmServiceStateTracker.java:                    Settings.System.AUTO_TIME) > 0;
frameworks/base/telephony/java/com/android/internal/telephony/gsm/GsmServiceStateTracker.java:                Settings.System.AUTO_TIME, 0) == 0) {
public class SntpClient
public boolean requestTime(String host, int timeout) {
jeff@translogic-desktop:jeff-2.3.7_r1$ grep "SntpClient" frameworks/base/ -r --include=*.java
frameworks/base/services/java/com/android/server/ThrottleService.java:import android.net.SntpClient;
frameworks/base/services/java/com/android/server/ThrottleService.java:            SntpClient client = new SntpClient();
frameworks/base/services/java/com/android/server/location/GpsLocationProvider.java:import android.net.SntpClient;
frameworks/base/services/java/com/android/server/location/GpsLocationProvider.java:        SntpClient client = new SntpClient();
            Properties properties = new Properties();
            File file = new File(PROPERTIES_FILE);
            stream = new FileInputStream(file);
            properties.load(stream);
            mNtpServer = properties.getProperty("NTP_SERVER", null);
D/gps_gns (   95): gns_gps_get_extension:  822  called 
D/GpsLocationProvider(   95): NTP server returned: 1328409970993 (Sun Feb 05 10:46:10 GMT+08:00 2012) reference: 33397 certainty: 40 system time offset: -27681758038
D/gps_gns (   95): gns_gps_inject_time:  801  called 
D/SntpClient(   95): request time failed: java.net.UnknownHostException: cn.pool.ntp.org
            Log.d(TAG, "NTP server returned: "
                    + time + " (" + new Date(time)
                    + ") reference: " + timeReference
                    + " certainty: " + certainty
                    + " system time offset: " + (time - now));
            native_inject_time(time, timeReference, certainty);
    const GpsInterface* interface = GetGpsInterface(env, obj);
    if (interface)
        interface->inject_time(time, timeReference, uncertainty);
static int
gns_gps_inject_time(GpsUtcTime time, int64_t timeReference, int uncertainty)
{
    //uncertainty       ntp transmitTime 
    //timeReference     when we got the ntp times.
    //time              ntp time
    //gnx call GN_GPS_Read_UTC to get the rtc times. so this func is unuseful.
    RUNTO;
    return 0;
}
丢掉了。
                case UPDATE_NETWORK_STATE:
                    handleUpdateNetworkState(msg.arg1, (NetworkInfo)msg.obj);
    public void updateNetworkState(int state, NetworkInfo info) {
        sendMessage(UPDATE_NETWORK_STATE, state, info);
    }
private long getBestTime() {
D/ThrottleService(   82): found Authoritative time - reseting alarm
V/ThrottleService(   82): using Authoritative time: 1328424173598
D/ThrottleService(   82): NTP server returned: 1328424173598 (Sun Feb 05 14:42:53 ?????—?°??2????????—?é—′+0800 2012) reference: 102416 certainty: 54 system time offset: -27627759453
SystemClock.setCurrentTimeMillis(cachedNtp); //jeff. set as local time.
在packages/apps/Settings/src/com/android/settings/DateTimeSettings.java
onSharedPreferenceChanged(...)
{
        } else if (key.equals(KEY_AUTO_TIME)) {
            boolean autoEnabled = preferences.getBoolean(key, true);
            Settings.System.putInt(getContentResolver(),
                    Settings.System.AUTO_TIME,
                    autoEnabled ? 1 : 0);
            mTimePref.setEnabled(!autoEnabled);
            mDatePref.setEnabled(!autoEnabled);
            mTimeZone.setEnabled(!autoEnabled);
            //jeff. if auto time. send broadcast.
            if(autoEnabled){
                Intent autoTime = new Intent(Intent.ACTION_AUTO_TIME);
                sendBroadcast(autoTime);
            }
        }
}
ACTION_AUTO_TIME为新添加,在
frameworks/base/core/java/android/content/Intent.java
    /**
     * Broadcast Action: To get time from ntp server, jeff.
     * @hide
     */
    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String ACTION_AUTO_TIME = "android.intent.action.AUTO_TIME";
而在frameworks/base/services/java/com/android/server/ThrottleService.java中响应ACTION_AUTO_TIME:
import android.provider.Settings.SettingNotFoundException; //jeff
public ThrottleService(Context context) {
        //jeff. add auto time.
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_AUTO_TIME);
        mContext.registerReceiver(mIntentReceiver, filter);
}
    //jeff add.
    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(Intent.ACTION_AUTO_TIME.equals(action)){
                getNtpTime();
            }
        }
    };
    //jeff add.
    private boolean getAutoTime() {
        try {
            return Settings.System.getInt(mContext.getContentResolver(), Settings.System.AUTO_TIME) > 0;
        } catch (SettingNotFoundException snfe) {
            return true;
        }
    }
    private void getNtpTime() {
        if (mNtpServer != null) {
            /*if (mNtpActive) {
                long ntpAge = SystemClock.elapsedRealtime() - cachedNtpTimestamp;
                if (ntpAge < mMaxNtpCacheAgeSec * 1000) {
                    if (VDBG) Slog.v(TAG, "using cached time");
                    return cachedNtp + ntpAge;
                }
            }*/
            SntpClient client = new SntpClient();
            if (client.requestTime(mNtpServer, MAX_NTP_FETCH_WAIT)) {
                cachedNtp = client.getNtpTime();
                cachedNtpTimestamp = SystemClock.elapsedRealtime();
                /*if (!mNtpActive) {
                    mNtpActive = true;
                    if (VDBG) Slog.d(TAG, "found Authoritative time - reseting alarm");
                    mHandler.obtainMessage(EVENT_RESET_ALARM).sendToTarget();
                }*/
                if (VDBG) Slog.v(TAG, "using Authoritative time: " + cachedNtp);
                if(getAutoTime())
                        SystemClock.setCurrentTimeMillis(cachedNtp); //jeff. set as local time.
                //return cachedNtp;
            }
        }
    }

抱歉!评论已关闭.