Adjust custom themes default naming

 - Change custom themes default naming, use custom themes amount plus 1
 as naming not "Custom", Doc: https://docs.google.com/presentation/d/15JadcGi5k1_0znUN_XdFpU7UxeXwxjK3Y7LxvhW5ETM/edit?ts=5eb540b5#slide=id.g812429ed29_51_16
 Video: https://drive.google.com/file/d/1Ijin203kGq9D9AG5FogaNtmb2gkcGfcW/view?usp=sharing

 Bug: 151285666
 Test: Manually

Change-Id: Ief25203a7911617d4f6287a44e2833db55ca8b2b
diff --git a/src/com/android/customization/picker/theme/CustomThemeActivity.java b/src/com/android/customization/picker/theme/CustomThemeActivity.java
index df968e5..a5ce222 100644
--- a/src/com/android/customization/picker/theme/CustomThemeActivity.java
+++ b/src/com/android/customization/picker/theme/CustomThemeActivity.java
@@ -64,6 +64,7 @@
     public static final String EXTRA_THEME_ID = "CustomThemeActivity.ThemeId";
     public static final String EXTRA_THEME_TITLE = "CustomThemeActivity.ThemeTitle";
     public static final String EXTRA_THEME_PACKAGES = "CustomThemeActivity.ThemePackages";
+    public static final String CREATE_NEW_THEME = "CustomThemeActivity.NewTheme";
     public static final int REQUEST_CODE_CUSTOM_THEME = 1;
     public static final int RESULT_THEME_DELETED = 10;
     public static final int RESULT_THEME_APPLIED = 20;
@@ -78,6 +79,7 @@
     private ThemeManager mThemeManager;
     private TextView mNextButton;
     private TextView mPreviousButton;
+    private boolean mIsDefinedTheme = true;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -89,6 +91,7 @@
                 && intent.hasExtra(EXTRA_THEME_TITLE) && intent.hasExtra(EXTRA_THEME_ID)) {
             ThemeBundleProvider themeProvider =
                     new DefaultThemeProvider(this, injector.getCustomizationPreferences(this));
+            mIsDefinedTheme = intent.getBooleanExtra(CREATE_NEW_THEME, true);
             try {
                 CustomTheme.Builder themeBuilder = themeProvider.parseCustomTheme(
                         intent.getStringExtra(EXTRA_THEME_PACKAGES));
@@ -384,7 +387,8 @@
             return CustomThemeNameFragment.newInstance(
                     title,
                     position,
-                    titleResId);
+                    titleResId,
+                    mIsDefinedTheme);
         }
     }
 }
diff --git a/src/com/android/customization/picker/theme/CustomThemeNameFragment.java b/src/com/android/customization/picker/theme/CustomThemeNameFragment.java
index 28f0f1b..1a70245 100644
--- a/src/com/android/customization/picker/theme/CustomThemeNameFragment.java
+++ b/src/com/android/customization/picker/theme/CustomThemeNameFragment.java
@@ -16,6 +16,8 @@
 package com.android.customization.picker.theme;
 
 import android.os.Bundle;
+import android.text.TextUtils;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -26,6 +28,8 @@
 import androidx.annotation.Nullable;
 
 import com.android.customization.model.theme.ThemeBundle.PreviewInfo;
+import com.android.customization.module.CustomizationInjector;
+import com.android.customization.module.CustomizationPreferences;
 import com.android.customization.picker.WallpaperPreviewer;
 import com.android.wallpaper.R;
 import com.android.wallpaper.model.WallpaperInfo;
@@ -34,15 +38,22 @@
 import com.android.wallpaper.picker.AppbarFragment;
 import com.android.wallpaper.widget.WallpaperColorsLoader;
 
+import org.json.JSONArray;
+import org.json.JSONException;
+
 /** Fragment of naming a custom theme. */
 public class CustomThemeNameFragment extends CustomThemeStepFragment {
 
+    private static final String TAG = "CustomThemeNameFragment";
+    private static final String ARG_IS_DEFINED_THEME = "CustomThemeNameFragment.new_theme";
+
     public static CustomThemeNameFragment newInstance(CharSequence toolbarTitle, int position,
-            int titleResId) {
+            int titleResId, boolean mIsDefinedTheme) {
         CustomThemeNameFragment fragment = new CustomThemeNameFragment();
         Bundle arguments = AppbarFragment.createArguments(toolbarTitle);
         arguments.putInt(ARG_KEY_POSITION, position);
         arguments.putInt(ARG_KEY_TITLE_RES_ID, titleResId);
+        arguments.putBoolean(ARG_IS_DEFINED_THEME, mIsDefinedTheme);
         fragment.setArguments(arguments);
         return fragment;
     }
@@ -51,6 +62,7 @@
     private ImageView mWallpaperImage;
     private WallpaperInfo mCurrentHomeWallpaper;
     private ThemeOptionPreviewer mThemeOptionPreviewer;
+    private CustomizationPreferences mCustomizationPreferences;
 
     @Nullable
     @Override
@@ -59,10 +71,10 @@
         View view = super.onCreateView(inflater, container, savedInstanceState);
         mTitle = view.findViewById(R.id.component_options_title);
         mTitle.setText(mTitleResId);
-        mNameEditor = view.findViewById(R.id.custom_theme_name);
-        mNameEditor.setText(mCustomThemeManager.getOriginalTheme().getTitle());
         CurrentWallpaperInfoFactory currentWallpaperFactory = InjectorProvider.getInjector()
                 .getCurrentWallpaperFactory(getActivity().getApplicationContext());
+        CustomizationInjector injector = (CustomizationInjector) InjectorProvider.getInjector();
+        mCustomizationPreferences = injector.getCustomizationPreferences(getContext());
 
         // Set wallpaper background.
         mWallpaperImage = view.findViewById(R.id.wallpaper_preview_image);
@@ -86,6 +98,11 @@
         PreviewInfo previewInfo = mCustomThemeManager.buildCustomThemePreviewInfo(getContext());
         mThemeOptionPreviewer.setPreviewInfo(previewInfo);
 
+        // Set theme default name.
+        mNameEditor = view.findViewById(R.id.custom_theme_name);
+        mNameEditor.setText(
+                getCustomThemeDefaultName(getArguments().getBoolean(ARG_IS_DEFINED_THEME, true)));
+
         view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
             @Override
             public void onLayoutChange(View v, int left, int top, int right, int bottom,
@@ -110,6 +127,28 @@
         }
     }
 
+    private String getCustomThemeDefaultName(boolean mIsDefinedTheme) {
+        if (mIsDefinedTheme) {
+            // For new custom theme. use custom themes amount plus 1 as default naming.
+            String serializedThemes = mCustomizationPreferences.getSerializedCustomThemes();
+            int customThemesCount = 0;
+            if (!TextUtils.isEmpty(serializedThemes)) {
+                try {
+                    JSONArray customThemes = new JSONArray(serializedThemes);
+                    customThemesCount = customThemes.length();
+                } catch (JSONException e) {
+                    Log.w(TAG, "Couldn't read stored custom theme");
+                }
+            }
+            return getContext().getString(
+                    R.string.custom_theme_title, customThemesCount + 1);
+        } else {
+            // For existing custom theme, keep its name as default naming.
+            return mCustomThemeManager.getOriginalTheme().getTitle();
+        }
+
+    }
+
     @Override
     protected int getFragmentLayoutResId() {
         return R.layout.fragment_custom_theme_name;
diff --git a/src/com/android/customization/picker/theme/ThemeFragment.java b/src/com/android/customization/picker/theme/ThemeFragment.java
index 132ce18..32bab20 100644
--- a/src/com/android/customization/picker/theme/ThemeFragment.java
+++ b/src/com/android/customization/picker/theme/ThemeFragment.java
@@ -362,6 +362,7 @@
         intent.putExtra(CustomThemeActivity.EXTRA_THEME_ID, themeToEdit.getId());
         intent.putExtra(CustomThemeActivity.EXTRA_THEME_PACKAGES,
                 themeToEdit.getSerializedPackages());
+        intent.putExtra(CustomThemeActivity.CREATE_NEW_THEME, !themeToEdit.isDefined());
         startActivityForResult(intent, CustomThemeActivity.REQUEST_CODE_CUSTOM_THEME);
     }