Save picker behavior related to launch and apply

- Define key for SharedPreference.
 KEY_APP_LAUNCH_COUNT
 KEY_FIRST_LAUNCH_DATE_SINCE_SETUP
 KEY_FIRST_WALLPAPER_APPLY_DATE_SINCE_SETUP
- Save picker launch count and first launch date.
- Save wallpaper first apply date.

Test: Manual
Bug: 151130766
Change-Id: Id332d97f49763b65d3279e7d1ca5584dd7c38661
diff --git a/src/com/android/wallpaper/module/DefaultWallpaperPreferences.java b/src/com/android/wallpaper/module/DefaultWallpaperPreferences.java
index 88cb72a..a9a6619 100755
--- a/src/com/android/wallpaper/module/DefaultWallpaperPreferences.java
+++ b/src/com/android/wallpaper/module/DefaultWallpaperPreferences.java
@@ -25,17 +25,21 @@
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
+import com.android.wallpaper.module.WallpaperPersister.Destination;
 import com.android.wallpaper.module.WallpaperPreferenceKeys.NoBackupKeys;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 
 import java.io.File;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
 
 /**
  * Default implementation that writes to and reads from SharedPreferences.
@@ -352,6 +356,7 @@
         mNoBackupPrefs.edit().putString(
                 NoBackupKeys.KEY_HOME_WALLPAPER_SERVICE_NAME, serviceName)
                 .apply();
+        setFirstWallpaperApplyDateIfNeeded();
     }
 
     @Override
@@ -379,6 +384,7 @@
         mNoBackupPrefs.edit().putString(
                 NoBackupKeys.KEY_HOME_WALLPAPER_REMOTE_ID, wallpaperRemoteId)
                 .apply();
+        setFirstWallpaperApplyDateIfNeeded();
     }
 
     @Override
@@ -494,6 +500,7 @@
         mNoBackupPrefs.edit().putString(
                 NoBackupKeys.KEY_LOCK_WALLPAPER_REMOTE_ID, wallpaperRemoteId)
                 .apply();
+        setFirstWallpaperApplyDateIfNeeded();
     }
 
     @Override
@@ -832,4 +839,75 @@
                 .putInt(NoBackupKeys.KEY_NUM_DAYS_DAILY_ROTATION_NOT_ATTEMPTED, 0)
                 .apply();
     }
+
+    @Override
+    public int getAppLaunchCount() {
+        return mNoBackupPrefs.getInt(NoBackupKeys.KEY_APP_LAUNCH_COUNT, 0);
+    }
+
+    private void setAppLaunchCount(int count) {
+        mNoBackupPrefs.edit().putInt(NoBackupKeys.KEY_APP_LAUNCH_COUNT, count).apply();
+    }
+
+    @Override
+    public int getFirstLaunchDateSinceSetup() {
+        return mNoBackupPrefs.getInt(NoBackupKeys.KEY_FIRST_LAUNCH_DATE_SINCE_SETUP, 0);
+    }
+
+    private void setFirstLaunchDateSinceSetup(int firstLaunchDate) {
+        mNoBackupPrefs.edit().putInt(NoBackupKeys.KEY_FIRST_LAUNCH_DATE_SINCE_SETUP,
+                firstLaunchDate).apply();
+    }
+
+    @Override
+    public int getFirstWallpaperApplyDateSinceSetup() {
+        return mNoBackupPrefs.getInt(NoBackupKeys.KEY_FIRST_WALLPAPER_APPLY_DATE_SINCE_SETUP, 0);
+    }
+
+    private void setFirstWallpaperApplyDateSinceSetup(int firstApplyDate) {
+        mNoBackupPrefs.edit().putInt(NoBackupKeys.KEY_FIRST_WALLPAPER_APPLY_DATE_SINCE_SETUP,
+                firstApplyDate).apply();
+    }
+
+    @Override
+    public void incrementAppLaunched() {
+        if (getFirstLaunchDateSinceSetup() == 0) {
+            setFirstLaunchDateSinceSetup(getCurrentDate());
+        }
+
+        int appLaunchCount = getAppLaunchCount();
+        if (appLaunchCount < Integer.MAX_VALUE) {
+            setAppLaunchCount(appLaunchCount + 1);
+        }
+    }
+
+    private void setFirstWallpaperApplyDateIfNeeded() {
+        if (getFirstWallpaperApplyDateSinceSetup() == 0) {
+            setFirstWallpaperApplyDateSinceSetup(getCurrentDate());
+        }
+    }
+
+    @Override
+    public void updateDailyWallpaperSet(@Destination int destination, String collectionId,
+            String wallpaperId) {
+        // Assign wallpaper info by destination.
+        if (destination == WallpaperPersister.DEST_HOME_SCREEN) {
+            setHomeWallpaperCollectionId(collectionId);
+            setHomeWallpaperRemoteId(wallpaperId);
+        } else if (destination == WallpaperPersister.DEST_LOCK_SCREEN) {
+            setLockWallpaperCollectionId(collectionId);
+            setLockWallpaperRemoteId(wallpaperId);
+        } else if (destination == WallpaperPersister.DEST_BOTH) {
+            setHomeWallpaperCollectionId(collectionId);
+            setHomeWallpaperRemoteId(wallpaperId);
+            setLockWallpaperCollectionId(collectionId);
+            setLockWallpaperRemoteId(wallpaperId);
+        }
+    }
+
+    private int getCurrentDate() {
+        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.US);
+        return Integer.parseInt(format.format(calendar.getTime()));
+    }
 }
diff --git a/src/com/android/wallpaper/module/WallpaperPreferenceKeys.java b/src/com/android/wallpaper/module/WallpaperPreferenceKeys.java
index eb3aa80..f8f7f2e 100755
--- a/src/com/android/wallpaper/module/WallpaperPreferenceKeys.java
+++ b/src/com/android/wallpaper/module/WallpaperPreferenceKeys.java
@@ -43,6 +43,11 @@
      * Preferences with these keys should not be backed up
      */
     public interface NoBackupKeys {
+        public static final String KEY_APP_LAUNCH_COUNT = "app_launch_count";
+        public static final String KEY_FIRST_LAUNCH_DATE_SINCE_SETUP =
+                "first_launch_date_since_setup";
+        public static final String KEY_FIRST_WALLPAPER_APPLY_DATE_SINCE_SETUP =
+                "first_wallpaper_apply_date_since_setup";
         public static final String KEY_HOME_WALLPAPER_BASE_IMAGE_URL =
                 "home_wallpaper_base_image_url";
         public static final String KEY_HOME_WALLPAPER_MANAGER_ID = "home_wallpaper_id";
diff --git a/src/com/android/wallpaper/module/WallpaperPreferences.java b/src/com/android/wallpaper/module/WallpaperPreferences.java
index 27cd89e..2a0fc39 100755
--- a/src/com/android/wallpaper/module/WallpaperPreferences.java
+++ b/src/com/android/wallpaper/module/WallpaperPreferences.java
@@ -21,6 +21,8 @@
 import androidx.annotation.IntDef;
 import androidx.annotation.Nullable;
 
+import com.android.wallpaper.module.WallpaperPersister.Destination;
+
 import java.util.List;
 
 /**
@@ -455,6 +457,36 @@
     void resetNumDaysDailyRotationNotAttempted();
 
     /**
+     * Return the count of wallpaper picker launch.
+     */
+    int getAppLaunchCount();
+
+    /**
+     * Return the date for the first time to launch wallpaper picker.
+     */
+    int getFirstLaunchDateSinceSetup();
+
+    /**
+     * Increments the number of wallpaper picker launch.
+     */
+    void incrementAppLaunched();
+
+    /**
+     * Returns the date for the first time to apply a wallpaper.
+     */
+    int getFirstWallpaperApplyDateSinceSetup();
+
+    /**
+     * Update currently set daily wallpaper info.
+     *
+     * @param destination  The wallpaper destination, 1: home, 2: lockscreen, 3: both.
+     * @param collectionId wallpaper category.
+     * @param wallpaperId  wallpaper id.
+     */
+    void updateDailyWallpaperSet(@Destination int destination, String collectionId,
+            String wallpaperId);
+
+    /**
      * The possible wallpaper presentation modes, i.e., either "static" or "rotating".
      */
     @IntDef({
diff --git a/src/com/android/wallpaper/picker/TopLevelPickerActivity.java b/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
index 482b77e..d0731e9 100755
--- a/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
+++ b/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
@@ -108,6 +108,7 @@
     private NetworkStatusNotifier mNetworkStatusNotifier;
     private NetworkStatusNotifier.Listener mNetworkStatusListener;
     private WallpaperPersister mWallpaperPersister;
+    private WallpaperPreferences mWallpaperPreferences;
     private boolean mWasCustomPhotoWallpaperSet;
     @WallpaperPosition
     private int mCustomPhotoWallpaperPosition;
@@ -305,6 +306,7 @@
         if (fragment == null) {
             // App launch specific logic: log the "app launched" event and set up daily logging.
             mUserEventLogger.logAppLaunched();
+            mWallpaperPreferences.incrementAppLaunched();
             DailyLoggingAlarmScheduler.setAlarm(getApplicationContext());
 
             CategoryFragment newFragment = CategoryFragment.newInstance(
@@ -367,6 +369,7 @@
         if (fragment == null) {
             // App launch specific logic: log the "app launched" event and set up daily logging.
             mUserEventLogger.logAppLaunched();
+            mWallpaperPreferences.incrementAppLaunched();
             DailyLoggingAlarmScheduler.setAlarm(getApplicationContext());
         }
 
diff --git a/tests/src/com/android/wallpaper/testing/TestWallpaperPreferences.java b/tests/src/com/android/wallpaper/testing/TestWallpaperPreferences.java
index 5af0bba..caef91f 100644
--- a/tests/src/com/android/wallpaper/testing/TestWallpaperPreferences.java
+++ b/tests/src/com/android/wallpaper/testing/TestWallpaperPreferences.java
@@ -15,19 +15,31 @@
  */
 package com.android.wallpaper.testing;
 
+import static com.android.wallpaper.module.WallpaperPersister.DEST_BOTH;
+import static com.android.wallpaper.module.WallpaperPersister.DEST_HOME_SCREEN;
+import static com.android.wallpaper.module.WallpaperPersister.DEST_LOCK_SCREEN;
+
 import androidx.annotation.Nullable;
 
+import com.android.wallpaper.module.WallpaperPersister.Destination;
 import com.android.wallpaper.module.WallpaperPreferences;
 
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Calendar;
 import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
 
 /**
  * Test implementation of the WallpaperPreferences interface. Just keeps prefs in memory.
  */
 public class TestWallpaperPreferences implements WallpaperPreferences {
 
+    private int mAppLaunchCount;
+    private int mFirstLaunchDate;
+    private int mFirstWallpaperApplyDate;
     @PresentationMode
     private int mWallpaperPresentationMode;
 
@@ -202,6 +214,7 @@
     @Override
     public void setHomeWallpaperServiceName(String serviceName) {
         mHomeScreenServiceName = serviceName;
+        setFirstWallpaperApplyDateIfNeeded();
     }
 
     @Override
@@ -222,6 +235,7 @@
     @Override
     public void setHomeWallpaperRemoteId(String wallpaperRemoteId) {
         mHomeWallpaperRemoteId = wallpaperRemoteId;
+        setFirstWallpaperApplyDateIfNeeded();
     }
 
     @Override
@@ -319,6 +333,7 @@
     @Override
     public void setLockWallpaperRemoteId(String wallpaperRemoteId) {
         mLockWallpaperRemoteId = wallpaperRemoteId;
+        setFirstWallpaperApplyDateIfNeeded();
     }
 
     @Override
@@ -476,4 +491,74 @@
     public void resetNumDaysDailyRotationNotAttempted() {
         mNumDaysDailyRotationNotAttempted = 0;
     }
+
+
+    @Override
+    public int getAppLaunchCount() {
+        return mAppLaunchCount;
+    }
+
+    private void setAppLaunchCount(int count) {
+        mAppLaunchCount = count;
+    }
+
+    @Override
+    public int getFirstLaunchDateSinceSetup() {
+        return mFirstLaunchDate;
+    }
+
+    private void setFirstLaunchDateSinceSetup(int firstLaunchDate) {
+        mFirstLaunchDate = firstLaunchDate;
+    }
+
+    @Override
+    public int getFirstWallpaperApplyDateSinceSetup() {
+        return mFirstWallpaperApplyDate;
+    }
+
+    private void setFirstWallpaperApplyDateSinceSetup(int firstWallpaperApplyDate) {
+        mFirstWallpaperApplyDate = firstWallpaperApplyDate;
+    }
+
+    @Override
+    public void incrementAppLaunched() {
+        if (getFirstLaunchDateSinceSetup() == 0) {
+            setFirstLaunchDateSinceSetup(getCurrentDate());
+        }
+
+        int appLaunchCount = getAppLaunchCount();
+        if (appLaunchCount < Integer.MAX_VALUE) {
+            setAppLaunchCount(appLaunchCount + 1);
+        }
+    }
+
+    private void setFirstWallpaperApplyDateIfNeeded() {
+        if (getFirstWallpaperApplyDateSinceSetup() == 0) {
+            setFirstWallpaperApplyDateSinceSetup(getCurrentDate());
+        }
+    }
+
+    @Override
+    public void updateDailyWallpaperSet(@Destination int destination, String collectionId,
+            String wallpaperId) {
+        // Assign wallpaper info by destination.
+        if (destination == DEST_HOME_SCREEN) {
+            setHomeWallpaperCollectionId(collectionId);
+            setHomeWallpaperRemoteId(wallpaperId);
+        } else if (destination == DEST_LOCK_SCREEN) {
+            setLockWallpaperCollectionId(collectionId);
+            setLockWallpaperRemoteId(wallpaperId);
+        } else if (destination == DEST_BOTH) {
+            setHomeWallpaperCollectionId(collectionId);
+            setHomeWallpaperRemoteId(wallpaperId);
+            setLockWallpaperCollectionId(collectionId);
+            setLockWallpaperRemoteId(wallpaperId);
+        }
+    }
+
+    private int getCurrentDate() {
+        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.US);
+        return Integer.parseInt(format.format(calendar.getTime()));
+    }
 }