• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.customization.picker.ViewOnlyFullPreviewActivity.SECTION_STYLE;
19 import static com.android.customization.picker.theme.ThemeFullPreviewFragment.EXTRA_THEME_OPTION;
20 import static com.android.customization.picker.theme.ThemeFullPreviewFragment.EXTRA_THEME_OPTION_TITLE;
21 import static com.android.customization.picker.theme.ThemeFullPreviewFragment.EXTRA_WALLPAPER_INFO;
22 
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.text.TextUtils;
26 import android.util.Log;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.EditText;
31 import android.widget.ImageView;
32 
33 import androidx.annotation.NonNull;
34 import androidx.annotation.Nullable;
35 
36 import com.android.customization.model.theme.ThemeBundle.PreviewInfo;
37 import com.android.customization.model.theme.custom.CustomTheme;
38 import com.android.customization.module.CustomizationInjector;
39 import com.android.customization.module.CustomizationPreferences;
40 import com.android.customization.picker.ViewOnlyFullPreviewActivity;
41 import com.android.customization.picker.WallpaperPreviewer;
42 import com.android.wallpaper.R;
43 import com.android.wallpaper.model.WallpaperInfo;
44 import com.android.wallpaper.module.CurrentWallpaperInfoFactory;
45 import com.android.wallpaper.module.InjectorProvider;
46 import com.android.wallpaper.picker.AppbarFragment;
47 
48 import org.json.JSONArray;
49 import org.json.JSONException;
50 
51 /** Fragment of naming a custom theme. */
52 public class CustomThemeNameFragment extends CustomThemeStepFragment {
53 
54     private static final String TAG = "CustomThemeNameFragment";
55 
newInstance(CharSequence toolbarTitle, int position, int titleResId)56     public static CustomThemeNameFragment newInstance(CharSequence toolbarTitle, int position,
57             int titleResId) {
58         CustomThemeNameFragment fragment = new CustomThemeNameFragment();
59         Bundle arguments = AppbarFragment.createArguments(toolbarTitle);
60         arguments.putInt(ARG_KEY_POSITION, position);
61         arguments.putInt(ARG_KEY_TITLE_RES_ID, titleResId);
62         fragment.setArguments(arguments);
63         return fragment;
64     }
65 
66     private EditText mNameEditor;
67     private ImageView mWallpaperImage;
68     private WallpaperInfo mCurrentHomeWallpaper;
69     private ThemeOptionPreviewer mThemeOptionPreviewer;
70     private CustomizationPreferences mCustomizationPreferences;
71 
72     @Nullable
73     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)74     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
75             @Nullable Bundle savedInstanceState) {
76         View view = super.onCreateView(inflater, container, savedInstanceState);
77         mTitle = view.findViewById(R.id.component_options_title);
78         mTitle.setText(mTitleResId);
79         CurrentWallpaperInfoFactory currentWallpaperFactory = InjectorProvider.getInjector()
80                 .getCurrentWallpaperFactory(getActivity().getApplicationContext());
81         CustomizationInjector injector = (CustomizationInjector) InjectorProvider.getInjector();
82         mCustomizationPreferences = injector.getCustomizationPreferences(getContext());
83 
84         // Set theme option.
85         ViewGroup previewContainer = view.findViewById(R.id.theme_preview_container);
86         previewContainer.setOnClickListener(v -> showFullPreview());
87         mThemeOptionPreviewer = new ThemeOptionPreviewer(getLifecycle(), getContext(),
88                 previewContainer);
89         PreviewInfo previewInfo = mCustomThemeManager.buildCustomThemePreviewInfo(getContext());
90         mThemeOptionPreviewer.setPreviewInfo(previewInfo);
91 
92         // Set wallpaper background.
93         mWallpaperImage = view.findViewById(R.id.wallpaper_preview_image);
94         final WallpaperPreviewer wallpaperPreviewer = new WallpaperPreviewer(
95                 getLifecycle(),
96                 getActivity(),
97                 mWallpaperImage,
98                 view.findViewById(R.id.wallpaper_preview_surface));
99         currentWallpaperFactory.createCurrentWallpaperInfos(
100                 (homeWallpaper, lockWallpaper, presentationMode) -> {
101                     mCurrentHomeWallpaper = homeWallpaper;
102                     wallpaperPreviewer.setWallpaper(homeWallpaper,
103                             mThemeOptionPreviewer::updateColorForLauncherWidgets);
104                 }, false);
105 
106         // Set theme default name.
107         mNameEditor = view.findViewById(R.id.custom_theme_name);
108         mNameEditor.setText(getOriginalThemeName());
109         return view;
110     }
111 
getOriginalThemeName()112     private String getOriginalThemeName() {
113         CustomTheme originalTheme = mCustomThemeManager.getOriginalTheme();
114         if (originalTheme == null || !originalTheme.isDefined()) {
115             // For new custom theme. use custom themes amount plus 1 as default naming.
116             String serializedThemes = mCustomizationPreferences.getSerializedCustomThemes();
117             int customThemesCount = 0;
118             if (!TextUtils.isEmpty(serializedThemes)) {
119                 try {
120                     JSONArray customThemes = new JSONArray(serializedThemes);
121                     customThemesCount = customThemes.length();
122                 } catch (JSONException e) {
123                     Log.w(TAG, "Couldn't read stored custom theme");
124                 }
125             }
126             return getContext().getString(
127                     R.string.custom_theme_title, customThemesCount + 1);
128         } else {
129             // For existing custom theme, keep its name as default naming.
130             return originalTheme.getTitle();
131         }
132     }
133 
134     @Override
getFragmentLayoutResId()135     protected int getFragmentLayoutResId() {
136         return R.layout.fragment_custom_theme_name;
137     }
138 
getThemeName()139     public String getThemeName() {
140         return mNameEditor.getText().toString();
141     }
142 
showFullPreview()143     private void showFullPreview() {
144         CustomTheme themeToFullPreview = mCustomThemeManager.buildPartialCustomTheme(
145                 getContext(), /* id= */ "", getThemeName());
146         Bundle bundle = new Bundle();
147         bundle.putParcelable(EXTRA_WALLPAPER_INFO, mCurrentHomeWallpaper);
148         bundle.putString(EXTRA_THEME_OPTION, themeToFullPreview.getSerializedPackages());
149         bundle.putString(EXTRA_THEME_OPTION_TITLE, themeToFullPreview.getTitle());
150         Intent intent = ViewOnlyFullPreviewActivity.newIntent(getContext(), SECTION_STYLE, bundle);
151         startActivity(intent);
152     }
153 }
154