Add timestamp to serialized ThemeBundle json

Add timestamp when applying a theme

Bug: 134686741
Change-Id: Ib516f5ceb71d812522baaf1ee862427bbadac75d
diff --git a/src/com/android/customization/model/theme/DefaultThemeProvider.java b/src/com/android/customization/model/theme/DefaultThemeProvider.java
index 87b5b4a..9fdb0c9 100644
--- a/src/com/android/customization/model/theme/DefaultThemeProvider.java
+++ b/src/com/android/customization/model/theme/DefaultThemeProvider.java
@@ -349,7 +349,7 @@
     }
 
     private void addThemeBundleToArray(JSONArray themesArray, ThemeBundle themeBundle) {
-        JSONObject jsonPackages = themeBundle.getJsonPackages();
+        JSONObject jsonPackages = themeBundle.getJsonPackages(false);
         try {
             jsonPackages.put(THEME_TITLE_FIELD, themeBundle.getTitle());
             if (themeBundle instanceof CustomTheme) {
diff --git a/src/com/android/customization/model/theme/ThemeBundle.java b/src/com/android/customization/model/theme/ThemeBundle.java
index b3258de..2fc5d90 100644
--- a/src/com/android/customization/model/theme/ThemeBundle.java
+++ b/src/com/android/customization/model/theme/ThemeBundle.java
@@ -31,7 +31,9 @@
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.ShapeDrawable;
 import android.graphics.drawable.shapes.PathShape;
+import android.icu.text.SimpleDateFormat;
 import android.text.TextUtils;
+import android.util.Log;
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.TextView;
@@ -52,14 +54,17 @@
 import com.android.wallpaper.model.LiveWallpaperInfo;
 import com.android.wallpaper.model.WallpaperInfo;
 
+import org.json.JSONException;
 import org.json.JSONObject;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 
@@ -70,7 +75,10 @@
  */
 public class ThemeBundle implements CustomizationOption<ThemeBundle> {
 
+    private static final String TAG = "ThemeBundle";
     private final static String EMPTY_JSON = "{}";
+    private final static String TIMESTAMP_FIELD = "_applied_timestamp";
+
     private final String mTitle;
     private final PreviewInfo mPreviewInfo;
     private final boolean mIsDefault;
@@ -121,10 +129,10 @@
     @Override
     public boolean isActive(CustomizationManager<ThemeBundle> manager) {
         ThemeManager themeManager = (ThemeManager) manager;
-        String serializedOverlays = themeManager.getStoredOverlays();
 
         if (mIsDefault) {
-            return TextUtils.isEmpty(serializedOverlays);
+            String serializedOverlays = themeManager.getStoredOverlays();
+            return TextUtils.isEmpty(serializedOverlays) || EMPTY_JSON.equals(serializedOverlays);
         } else {
             Map<String, String> currentOverlays = themeManager.getCurrentOverlays();
             return mPackagesByCategory.equals(currentOverlays);
@@ -197,19 +205,27 @@
     }
 
     public String getSerializedPackages() {
-        if (isDefault()) {
-            return "";
-        }
-        return getJsonPackages().toString();
+        return getJsonPackages(false).toString();
     }
 
-    JSONObject getJsonPackages() {
+    public String getSerializedPackagesWithTimestamp() {
+        return getJsonPackages(true).toString();
+    }
+
+    JSONObject getJsonPackages(boolean insertTimestamp) {
         if (isDefault()) {
             return new JSONObject();
         }
         JSONObject json = new JSONObject(mPackagesByCategory);
         // Remove items with null values to avoid deserialization issues.
         removeNullValues(json);
+        if (insertTimestamp) {
+            try {
+                json.put(TIMESTAMP_FIELD, System.currentTimeMillis());
+            } catch (JSONException e) {
+                Log.e(TAG, "Couldn't add timestamp to serialized themebundle");
+            }
+        }
         return json;
     }
 
diff --git a/src/com/android/customization/model/theme/ThemeManager.java b/src/com/android/customization/model/theme/ThemeManager.java
index 5fddc57..e4981b0 100644
--- a/src/com/android/customization/model/theme/ThemeManager.java
+++ b/src/com/android/customization/model/theme/ThemeManager.java
@@ -153,7 +153,7 @@
 
     private void applyOverlays(ThemeBundle theme, Callback callback) {
         boolean allApplied = Settings.Secure.putString(mActivity.getContentResolver(),
-                ResourceConstants.THEME_SETTING, theme.getSerializedPackages());
+                ResourceConstants.THEME_SETTING, theme.getSerializedPackagesWithTimestamp());
         if (theme instanceof CustomTheme) {
             storeCustomTheme((CustomTheme) theme);
         }