1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.customization.picker.theme;
17 
18 import android.os.Bundle;
19 import android.text.TextUtils;
20 import android.util.Log;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.EditText;
25 import android.widget.ImageView;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 
30 import com.android.customization.model.theme.ThemeBundle.PreviewInfo;
31 import com.android.customization.model.theme.custom.CustomTheme;
32 import com.android.customization.module.CustomizationInjector;
33 import com.android.customization.module.CustomizationPreferences;
34 import com.android.customization.picker.WallpaperPreviewer;
35 import com.android.wallpaper.R;
36 import com.android.wallpaper.module.CurrentWallpaperInfoFactory;
37 import com.android.wallpaper.module.InjectorProvider;
38 import com.android.wallpaper.picker.AppbarFragment;
39 
40 import org.json.JSONArray;
41 import org.json.JSONException;
42 
43 /** Fragment of naming a custom theme. */
44 public class CustomThemeNameFragment extends CustomThemeStepFragment {
45 
46     private static final String TAG = "CustomThemeNameFragment";
47 
newInstance(CharSequence toolbarTitle, int position, int titleResId, int accessibilityResId)48     public static CustomThemeNameFragment newInstance(CharSequence toolbarTitle, int position,
49             int titleResId, int accessibilityResId) {
50         CustomThemeNameFragment fragment = new CustomThemeNameFragment();
51         Bundle arguments = AppbarFragment.createArguments(toolbarTitle);
52         arguments.putInt(ARG_KEY_POSITION, position);
53         arguments.putInt(ARG_KEY_TITLE_RES_ID, titleResId);
54         arguments.putInt(ARG_KEY_ACCESSIBILITY_RES_ID, accessibilityResId);
55         fragment.setArguments(arguments);
56         return fragment;
57     }
58 
59     private EditText mNameEditor;
60     private ImageView mWallpaperImage;
61     private ThemeOptionPreviewer mThemeOptionPreviewer;
62     private CustomizationPreferences mCustomizationPreferences;
63 
64     @Nullable
65     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)66     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
67             @Nullable Bundle savedInstanceState) {
68         View view = super.onCreateView(inflater, container, savedInstanceState);
69         mTitle = view.findViewById(R.id.component_options_title);
70         mTitle.setText(mTitleResId);
71         CurrentWallpaperInfoFactory currentWallpaperFactory = InjectorProvider.getInjector()
72                 .getCurrentWallpaperFactory(getActivity().getApplicationContext());
73         CustomizationInjector injector = (CustomizationInjector) InjectorProvider.getInjector();
74         mCustomizationPreferences = injector.getCustomizationPreferences(getContext());
75 
76         // Set theme option.
77         ViewGroup previewContainer = view.findViewById(R.id.theme_preview_container);
78         mThemeOptionPreviewer = new ThemeOptionPreviewer(getLifecycle(), getContext(),
79                 previewContainer);
80         PreviewInfo previewInfo = mCustomThemeManager.buildCustomThemePreviewInfo(getContext());
81         mThemeOptionPreviewer.setPreviewInfo(previewInfo);
82 
83         // Set wallpaper background.
84         mWallpaperImage = view.findViewById(R.id.wallpaper_preview_image);
85         final WallpaperPreviewer wallpaperPreviewer = new WallpaperPreviewer(
86                 getLifecycle(),
87                 getActivity(),
88                 mWallpaperImage,
89                 view.findViewById(R.id.wallpaper_preview_surface));
90         currentWallpaperFactory.createCurrentWallpaperInfos(
91                 (homeWallpaper, lockWallpaper, presentationMode) -> {
92                     wallpaperPreviewer.setWallpaper(homeWallpaper,
93                             mThemeOptionPreviewer::updateColorForLauncherWidgets);
94                 }, false);
95 
96         // Set theme default name.
97         mNameEditor = view.findViewById(R.id.custom_theme_name);
98         mNameEditor.setText(getOriginalThemeName());
99         return view;
100     }
101 
getOriginalThemeName()102     private String getOriginalThemeName() {
103         CustomTheme originalTheme = mCustomThemeManager.getOriginalTheme();
104         if (originalTheme == null || !originalTheme.isDefined()) {
105             // For new custom theme. use custom themes amount plus 1 as default naming.
106             String serializedThemes = mCustomizationPreferences.getSerializedCustomThemes();
107             int customThemesCount = 0;
108             if (!TextUtils.isEmpty(serializedThemes)) {
109                 try {
110                     JSONArray customThemes = new JSONArray(serializedThemes);
111                     customThemesCount = customThemes.length();
112                 } catch (JSONException e) {
113                     Log.w(TAG, "Couldn't read stored custom theme");
114                 }
115             }
116             return getContext().getString(
117                     R.string.custom_theme_title, customThemesCount + 1);
118         } else {
119             // For existing custom theme, keep its name as default naming.
120             return originalTheme.getTitle();
121         }
122     }
123 
124     @Override
getFragmentLayoutResId()125     protected int getFragmentLayoutResId() {
126         return R.layout.fragment_custom_theme_name;
127     }
128 
getThemeName()129     public String getThemeName() {
130         return mNameEditor.getText().toString();
131     }
132 }
133