Tweak exemption for sync requests made by FG apps

Exemption given to a sync request made by a foreground app (including
PROCESS_STATE_IMPORTANT_FOREGROUND).
At the schedule time, we promote the sync adapter app for a higher bucket:
- If the device is not dozing (so the sync will start right away)
  promote to ACTIVE for 1 hour.
- If the device is dozing (so the sync *won't* start right away),
promote to WORKING_SET for 4 hours, so it'll get a higher chance to be started once the
device comes out of doze.
- When the sync actually starts, we promote the sync adapter app to ACTIVE for 10 minutes,
so it can schedule and start more syncs without getting throttled, even when the first
operation was canceled and now we're retrying.

Test: atest cts/tests/tests/syncmanager/
Test: Manual test with "requestsync -f" and "am set-standby-bucket", while checking
"dumpsys usagestats"
Test: settings put global app_idle_constants \
  exempted_sync_scheduled_nd_duration=1,exempted_sync_scheduled_d_duration=2,exempted_sync_start_duration=3
  and check "dumpsys usagestats" and make sure the constants are properly updated.
Fixes: 72443754

Change-Id: I233d8e4be85769150830bac798abc04810f4cc7b
diff --git a/services/core/java/com/android/server/content/SyncStorageEngine.java b/services/core/java/com/android/server/content/SyncStorageEngine.java
index 6a343f8..11f0701 100644
--- a/services/core/java/com/android/server/content/SyncStorageEngine.java
+++ b/services/core/java/com/android/server/content/SyncStorageEngine.java
@@ -341,6 +341,7 @@
         boolean initialization;
         Bundle extras;
         int reason;
+        int syncExemptionFlag;
     }
 
     public static class DayStats {
@@ -1142,6 +1143,7 @@
             item.reason = op.reason;
             item.extras = op.extras;
             item.event = EVENT_START;
+            item.syncExemptionFlag = op.syncExemptionFlag;
             mSyncHistory.add(0, item);
             while (mSyncHistory.size() > MAX_HISTORY) {
                 mSyncHistory.remove(mSyncHistory.size()-1);
@@ -1262,6 +1264,20 @@
             SyncManager.formatDurationHMS(event, elapsedTime);
             event.append(" Reason=");
             event.append(SyncOperation.reasonToString(null, item.reason));
+            if (item.syncExemptionFlag != ContentResolver.SYNC_EXEMPTION_NONE) {
+                event.append(" Exemption=");
+                switch (item.syncExemptionFlag) {
+                    case ContentResolver.SYNC_EXEMPTION_PROMOTE_BUCKET:
+                        event.append("fg");
+                        break;
+                    case ContentResolver.SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP:
+                        event.append("top");
+                        break;
+                    default:
+                        event.append(item.syncExemptionFlag);
+                        break;
+                }
+            }
             event.append(" Extras=");
             SyncOperation.extrasToStringBuilder(item.extras, event);