Remove spurious syncmanager wake-ups

BUG: 22287469
1) Fix SyncManager waking up every 2 hrs if there is nothing
to do.
2) Fix sync wake-up alarm not being properly updated if the new
alarm was in the future.
2a) Due to staus bar sync signal that were removed post-K
one of the wake-ups was for 30s in the future, removed this
@hide intent completely.
3) The SyncManager will still set a timeout alarm for 5mins after
the start of a sync. Leaving this in as to post to a handler is
less expensive but more complex, and the alarm update is
correctly working now.

Change-Id: If51c9dd68391ccaeb480a17eb5a1364c4afe4c2a
diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java
index aaaa745..ec443cd 100644
--- a/core/java/android/content/Intent.java
+++ b/core/java/android/content/Intent.java
@@ -1782,14 +1782,6 @@
     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
     public static final String ACTION_ALARM_CHANGED = "android.intent.action.ALARM_CHANGED";
     /**
-     * Sync State Changed Action: This is broadcast when the sync starts or stops or when one has
-     * been failing for a long time.  It is used by the SyncManager and the StatusBar service.
-     * @hide
-     */
-    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
-    public static final String ACTION_SYNC_STATE_CHANGED
-            = "android.intent.action.SYNC_STATE_CHANGED";
-    /**
      * Broadcast Action: This is broadcast once, after the system has finished
      * booting.  It can be used to perform application-specific initialization,
      * such as installing alarms.  You must hold the
diff --git a/services/core/java/com/android/server/content/SyncManager.java b/services/core/java/com/android/server/content/SyncManager.java
index 658f6f8..c998c2c 100644
--- a/services/core/java/com/android/server/content/SyncManager.java
+++ b/services/core/java/com/android/server/content/SyncManager.java
@@ -297,7 +297,6 @@
     private final UserManager mUserManager;
 
     private static final long SYNC_ALARM_TIMEOUT_MIN = 30 * 1000; // 30 seconds
-    private static final long SYNC_ALARM_TIMEOUT_MAX = 2 * 60 * 60 * 1000; // two hours
 
     private List<UserInfo> getAllUsers() {
         return mUserManager.getUsers();
@@ -1478,9 +1477,9 @@
         final long now = SystemClock.elapsedRealtime();
         pw.print("now: "); pw.print(now);
         pw.println(" (" + formatTime(System.currentTimeMillis()) + ")");
-        pw.print("offset: "); pw.print(DateUtils.formatElapsedTime(mSyncRandomOffsetMillis/1000));
+        pw.print("offset: "); pw.print(DateUtils.formatElapsedTime(mSyncRandomOffsetMillis / 1000));
         pw.println(" (HH:MM:SS)");
-        pw.print("uptime: "); pw.print(DateUtils.formatElapsedTime(now/1000));
+        pw.print("uptime: "); pw.print(DateUtils.formatElapsedTime(now / 1000));
                 pw.println(" (HH:MM:SS)");
         pw.print("time spent syncing: ");
                 pw.print(DateUtils.formatElapsedTime(
@@ -1497,11 +1496,6 @@
             pw.println("no alarm is scheduled (there had better not be any pending syncs)");
         }
 
-        pw.print("notification info: ");
-        final StringBuilder sb = new StringBuilder();
-        mSyncHandler.mSyncNotificationInfo.toString(sb);
-        pw.println(sb.toString());
-
         pw.println();
         pw.println("Active Syncs: " + mActiveSyncContexts.size());
         final PackageManager pm = mContext.getPackageManager();
@@ -1514,8 +1508,8 @@
             pw.println();
         }
 
+        final StringBuilder sb = new StringBuilder();
         synchronized (mSyncQueue) {
-            sb.setLength(0);
             mSyncQueue.dump(sb);
             // Dump Pending Operations.
             getSyncStorageEngine().dumpPendingOperations(sb);
@@ -2349,7 +2343,6 @@
 
                 }
             } finally {
-                manageSyncNotificationLocked();
                 manageSyncAlarmLocked(earliestFuturePollTime, nextPendingSyncTime);
                 mSyncTimeTracker.update();
                 mSyncManagerWakeLock.release();
@@ -3169,67 +3162,6 @@
             throw new IllegalStateException("we are not in an error state, " + syncResult);
         }
 
-        private void manageSyncNotificationLocked() {
-            boolean shouldCancel;
-            boolean shouldInstall;
-
-            if (mActiveSyncContexts.isEmpty()) {
-                mSyncNotificationInfo.startTime = null;
-
-                // we aren't syncing. if the notification is active then remember that we need
-                // to cancel it and then clear out the info
-                shouldCancel = mSyncNotificationInfo.isActive;
-                shouldInstall = false;
-            } else {
-                // we are syncing
-                final long now = SystemClock.elapsedRealtime();
-                if (mSyncNotificationInfo.startTime == null) {
-                    mSyncNotificationInfo.startTime = now;
-                }
-
-                // there are three cases:
-                // - the notification is up: do nothing
-                // - the notification is not up but it isn't time yet: don't install
-                // - the notification is not up and it is time: need to install
-
-                if (mSyncNotificationInfo.isActive) {
-                    shouldInstall = shouldCancel = false;
-                } else {
-                    // it isn't currently up, so there is nothing to cancel
-                    shouldCancel = false;
-
-                    final boolean timeToShowNotification =
-                            now > mSyncNotificationInfo.startTime + SYNC_NOTIFICATION_DELAY;
-                    if (timeToShowNotification) {
-                        shouldInstall = true;
-                    } else {
-                        // show the notification immediately if this is a manual sync
-                        shouldInstall = false;
-                        for (ActiveSyncContext activeSyncContext : mActiveSyncContexts) {
-                            final boolean manualSync = activeSyncContext.mSyncOperation.extras
-                                    .getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
-                            if (manualSync) {
-                                shouldInstall = true;
-                                break;
-                            }
-                        }
-                    }
-                }
-            }
-
-            if (shouldCancel && !shouldInstall) {
-                mNeedSyncActiveNotification = false;
-                sendSyncStateIntent();
-                mSyncNotificationInfo.isActive = false;
-            }
-
-            if (shouldInstall) {
-                mNeedSyncActiveNotification = true;
-                sendSyncStateIntent();
-                mSyncNotificationInfo.isActive = true;
-            }
-        }
-
         private void manageSyncAlarmLocked(long nextPeriodicEventElapsedTime,
                 long nextPendingEventElapsedTime) {
             // in each of these cases the sync loop will be kicked, which will cause this
@@ -3238,13 +3170,6 @@
             if (mStorageIsLow) return;
             if (mDeviceIsIdle) return;
 
-            // When the status bar notification should be raised
-            final long notificationTime =
-                    (!mSyncHandler.mSyncNotificationInfo.isActive
-                            && mSyncHandler.mSyncNotificationInfo.startTime != null)
-                            ? mSyncHandler.mSyncNotificationInfo.startTime + SYNC_NOTIFICATION_DELAY
-                            : Long.MAX_VALUE;
-
             // When we should consider canceling an active sync
             long earliestTimeoutTime = Long.MAX_VALUE;
             for (ActiveSyncContext currentSyncContext : mActiveSyncContexts) {
@@ -3260,24 +3185,14 @@
             }
 
             if (Log.isLoggable(TAG, Log.VERBOSE)) {
-                Log.v(TAG, "manageSyncAlarm: notificationTime is " + notificationTime);
-            }
-
-            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                 Log.v(TAG, "manageSyncAlarm: earliestTimeoutTime is " + earliestTimeoutTime);
-            }
-
-            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                 Log.v(TAG, "manageSyncAlarm: nextPeriodicEventElapsedTime is "
                         + nextPeriodicEventElapsedTime);
-            }
-            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                 Log.v(TAG, "manageSyncAlarm: nextPendingEventElapsedTime is "
                         + nextPendingEventElapsedTime);
             }
 
-            long alarmTime = Math.min(notificationTime, earliestTimeoutTime);
-            alarmTime = Math.min(alarmTime, nextPeriodicEventElapsedTime);
+            long alarmTime = Math.min(earliestTimeoutTime, nextPeriodicEventElapsedTime);
             alarmTime = Math.min(alarmTime, nextPendingEventElapsedTime);
 
             // Bound the alarm time.
@@ -3288,24 +3203,16 @@
                             + alarmTime + ", setting to " + (now + SYNC_ALARM_TIMEOUT_MIN));
                 }
                 alarmTime = now + SYNC_ALARM_TIMEOUT_MIN;
-            } else if (alarmTime > now + SYNC_ALARM_TIMEOUT_MAX) {
-                if (Log.isLoggable(TAG, Log.VERBOSE)) {
-                    Log.v(TAG, "manageSyncAlarm: the alarmTime is too large, "
-                            + alarmTime + ", setting to " + (now + SYNC_ALARM_TIMEOUT_MIN));
-                }
-                alarmTime = now + SYNC_ALARM_TIMEOUT_MAX;
             }
 
-            // determine if we need to set or cancel the alarm
+            // Determine if we need to set or cancel the alarm
             boolean shouldSet = false;
             boolean shouldCancel = false;
             final boolean alarmIsActive = (mAlarmScheduleTime != null) && (now < mAlarmScheduleTime);
-            final boolean needAlarm = alarmTime != Long.MAX_VALUE;
-            if (needAlarm) {
-                // Need the alarm if
-                //  - it's currently not set
-                //  - if the alarm is set in the past.
-                if (!alarmIsActive || alarmTime < mAlarmScheduleTime) {
+
+            if (alarmTime != Long.MAX_VALUE) {
+                // Need the alarm if it isn't set or has changed.
+                if (!alarmIsActive || alarmTime != mAlarmScheduleTime) {
                     shouldSet = true;
                 }
             } else {
@@ -3329,14 +3236,6 @@
             }
         }
 
-        private void sendSyncStateIntent() {
-            Intent syncStateIntent = new Intent(Intent.ACTION_SYNC_STATE_CHANGED);
-            syncStateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
-            syncStateIntent.putExtra("active", mNeedSyncActiveNotification);
-            syncStateIntent.putExtra("failing", false);
-            mContext.sendBroadcastAsUser(syncStateIntent, UserHandle.OWNER);
-        }
-
         private void installHandleTooManyDeletesNotification(Account account, String authority,
                 long numDeletes, int userId) {
             if (mNotificationMgr == null) return;