Merge "update time while network is connected"
am: 465f8527dd
Change-Id: I863c775015079a21098efd32b570acf8a8b53efa
This commit is contained in:
@@ -26,6 +26,8 @@ import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.ConnectivityManager.NetworkCallback;
|
||||
import android.net.Network;
|
||||
import android.os.Binder;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
@@ -73,6 +75,7 @@ public class NetworkTimeUpdateService extends Binder {
|
||||
private long mNitzTimeSetTime = NOT_SET;
|
||||
// TODO: Have a way to look up the timezone we are in
|
||||
private long mNitzZoneSetTime = NOT_SET;
|
||||
private Network mDefaultNetwork = null;
|
||||
|
||||
private Context mContext;
|
||||
private TrustedTime mTime;
|
||||
@@ -82,6 +85,8 @@ public class NetworkTimeUpdateService extends Binder {
|
||||
private AlarmManager mAlarmManager;
|
||||
private PendingIntent mPendingPollIntent;
|
||||
private SettingsObserver mSettingsObserver;
|
||||
private ConnectivityManager mCM;
|
||||
private NetworkTimeUpdateCallback mNetworkTimeUpdateCallback;
|
||||
// The last time that we successfully fetched the NTP time.
|
||||
private long mLastNtpFetchTime = NOT_SET;
|
||||
private final PowerManager.WakeLock mWakeLock;
|
||||
@@ -103,6 +108,7 @@ public class NetworkTimeUpdateService extends Binder {
|
||||
mContext = context;
|
||||
mTime = NtpTrustedTime.getInstance(context);
|
||||
mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
|
||||
mCM = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
Intent pollIntent = new Intent(ACTION_POLL, null);
|
||||
mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0);
|
||||
|
||||
@@ -123,13 +129,12 @@ public class NetworkTimeUpdateService extends Binder {
|
||||
public void systemRunning() {
|
||||
registerForTelephonyIntents();
|
||||
registerForAlarms();
|
||||
registerForConnectivityIntents();
|
||||
|
||||
HandlerThread thread = new HandlerThread(TAG);
|
||||
thread.start();
|
||||
mHandler = new MyHandler(thread.getLooper());
|
||||
// Check the network time on the new thread
|
||||
mHandler.obtainMessage(EVENT_POLL_NETWORK_TIME).sendToTarget();
|
||||
mNetworkTimeUpdateCallback = new NetworkTimeUpdateCallback();
|
||||
mCM.registerDefaultNetworkCallback(mNetworkTimeUpdateCallback, mHandler);
|
||||
|
||||
mSettingsObserver = new SettingsObserver(mHandler, EVENT_AUTO_TIME_CHANGED);
|
||||
mSettingsObserver.observe(mContext);
|
||||
@@ -152,15 +157,10 @@ public class NetworkTimeUpdateService extends Binder {
|
||||
}, 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) {
|
||||
// If Automatic time is not set, don't bother.
|
||||
if (!isAutomaticTimeRequested()) return;
|
||||
// If Automatic time is not set, don't bother. Similarly, if we don't
|
||||
// have any default network, don't bother.
|
||||
if (!isAutomaticTimeRequested() || mDefaultNetwork == null) return;
|
||||
mWakeLock.acquire();
|
||||
try {
|
||||
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 */
|
||||
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 */
|
||||
private static class SettingsObserver extends ContentObserver {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user