Merge "update time while network is connected"
This commit is contained in:
@@ -26,6 +26,8 @@ import android.content.IntentFilter;
|
|||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.database.ContentObserver;
|
import android.database.ContentObserver;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.ConnectivityManager.NetworkCallback;
|
||||||
|
import android.net.Network;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.HandlerThread;
|
import android.os.HandlerThread;
|
||||||
@@ -73,6 +75,7 @@ public class NetworkTimeUpdateService extends Binder {
|
|||||||
private long mNitzTimeSetTime = NOT_SET;
|
private long mNitzTimeSetTime = NOT_SET;
|
||||||
// TODO: Have a way to look up the timezone we are in
|
// TODO: Have a way to look up the timezone we are in
|
||||||
private long mNitzZoneSetTime = NOT_SET;
|
private long mNitzZoneSetTime = NOT_SET;
|
||||||
|
private Network mDefaultNetwork = null;
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private TrustedTime mTime;
|
private TrustedTime mTime;
|
||||||
@@ -82,6 +85,8 @@ public class NetworkTimeUpdateService extends Binder {
|
|||||||
private AlarmManager mAlarmManager;
|
private AlarmManager mAlarmManager;
|
||||||
private PendingIntent mPendingPollIntent;
|
private PendingIntent mPendingPollIntent;
|
||||||
private SettingsObserver mSettingsObserver;
|
private SettingsObserver mSettingsObserver;
|
||||||
|
private ConnectivityManager mCM;
|
||||||
|
private NetworkTimeUpdateCallback mNetworkTimeUpdateCallback;
|
||||||
// The last time that we successfully fetched the NTP time.
|
// The last time that we successfully fetched the NTP time.
|
||||||
private long mLastNtpFetchTime = NOT_SET;
|
private long mLastNtpFetchTime = NOT_SET;
|
||||||
private final PowerManager.WakeLock mWakeLock;
|
private final PowerManager.WakeLock mWakeLock;
|
||||||
@@ -103,6 +108,7 @@ public class NetworkTimeUpdateService extends Binder {
|
|||||||
mContext = context;
|
mContext = context;
|
||||||
mTime = NtpTrustedTime.getInstance(context);
|
mTime = NtpTrustedTime.getInstance(context);
|
||||||
mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
|
mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
|
||||||
|
mCM = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
Intent pollIntent = new Intent(ACTION_POLL, null);
|
Intent pollIntent = new Intent(ACTION_POLL, null);
|
||||||
mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0);
|
mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0);
|
||||||
|
|
||||||
@@ -123,13 +129,12 @@ public class NetworkTimeUpdateService extends Binder {
|
|||||||
public void systemRunning() {
|
public void systemRunning() {
|
||||||
registerForTelephonyIntents();
|
registerForTelephonyIntents();
|
||||||
registerForAlarms();
|
registerForAlarms();
|
||||||
registerForConnectivityIntents();
|
|
||||||
|
|
||||||
HandlerThread thread = new HandlerThread(TAG);
|
HandlerThread thread = new HandlerThread(TAG);
|
||||||
thread.start();
|
thread.start();
|
||||||
mHandler = new MyHandler(thread.getLooper());
|
mHandler = new MyHandler(thread.getLooper());
|
||||||
// Check the network time on the new thread
|
mNetworkTimeUpdateCallback = new NetworkTimeUpdateCallback();
|
||||||
mHandler.obtainMessage(EVENT_POLL_NETWORK_TIME).sendToTarget();
|
mCM.registerDefaultNetworkCallback(mNetworkTimeUpdateCallback, mHandler);
|
||||||
|
|
||||||
mSettingsObserver = new SettingsObserver(mHandler, EVENT_AUTO_TIME_CHANGED);
|
mSettingsObserver = new SettingsObserver(mHandler, EVENT_AUTO_TIME_CHANGED);
|
||||||
mSettingsObserver.observe(mContext);
|
mSettingsObserver.observe(mContext);
|
||||||
@@ -152,15 +157,10 @@ public class NetworkTimeUpdateService extends Binder {
|
|||||||
}, new IntentFilter(ACTION_POLL));
|
}, new IntentFilter(ACTION_POLL));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerForConnectivityIntents() {
|
|
||||||
IntentFilter intentFilter = new IntentFilter();
|
|
||||||
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
|
||||||
mContext.registerReceiver(mConnectivityReceiver, intentFilter);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onPollNetworkTime(int event) {
|
private void onPollNetworkTime(int event) {
|
||||||
// If Automatic time is not set, don't bother.
|
// If Automatic time is not set, don't bother. Similarly, if we don't
|
||||||
if (!isAutomaticTimeRequested()) return;
|
// have any default network, don't bother.
|
||||||
|
if (!isAutomaticTimeRequested() || mDefaultNetwork == null) return;
|
||||||
mWakeLock.acquire();
|
mWakeLock.acquire();
|
||||||
try {
|
try {
|
||||||
onPollNetworkTimeUnderWakeLock(event);
|
onPollNetworkTimeUnderWakeLock(event);
|
||||||
@@ -262,22 +262,6 @@ public class NetworkTimeUpdateService extends Binder {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Receiver for ConnectivityManager events */
|
|
||||||
private BroadcastReceiver mConnectivityReceiver = new BroadcastReceiver() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
String action = intent.getAction();
|
|
||||||
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
|
|
||||||
if (DBG) Log.d(TAG, "Received CONNECTIVITY_ACTION ");
|
|
||||||
// Don't bother checking if we have connectivity, NtpTrustedTime does that for us.
|
|
||||||
Message message = mHandler.obtainMessage(EVENT_NETWORK_CHANGED);
|
|
||||||
// Send with a short delay to make sure the network is ready for use
|
|
||||||
mHandler.sendMessageDelayed(message, NETWORK_CHANGE_EVENT_DELAY_MS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Handler to do the network accesses on */
|
/** Handler to do the network accesses on */
|
||||||
private class MyHandler extends Handler {
|
private class MyHandler extends Handler {
|
||||||
|
|
||||||
@@ -297,6 +281,21 @@ public class NetworkTimeUpdateService extends Binder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class NetworkTimeUpdateCallback extends NetworkCallback {
|
||||||
|
@Override
|
||||||
|
public void onAvailable(Network network) {
|
||||||
|
Log.d(TAG, String.format("New default network %s; checking time.", network));
|
||||||
|
mDefaultNetwork = network;
|
||||||
|
// Running on mHandler so invoke directly.
|
||||||
|
onPollNetworkTime(EVENT_NETWORK_CHANGED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLost(Network network) {
|
||||||
|
if (network.equals(mDefaultNetwork)) mDefaultNetwork = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Observer to watch for changes to the AUTO_TIME setting */
|
/** Observer to watch for changes to the AUTO_TIME setting */
|
||||||
private static class SettingsObserver extends ContentObserver {
|
private static class SettingsObserver extends ContentObserver {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user