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.content.Context;
19 import android.content.res.Resources;
20 import android.graphics.Bitmap;
21 import android.graphics.drawable.BitmapDrawable;
22 import android.graphics.drawable.Drawable;
23 import android.os.Bundle;
24 import android.text.Editable;
25 import android.text.TextWatcher;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.View.OnLayoutChangeListener;
29 import android.view.ViewGroup;
30 import android.widget.EditText;
31 import android.widget.TextView;
32 
33 import androidx.annotation.NonNull;
34 import androidx.annotation.Nullable;
35 import androidx.cardview.widget.CardView;
36 
37 import com.android.customization.model.theme.ThemeBundle.PreviewInfo;
38 import com.android.customization.picker.TimeTicker;
39 import com.android.customization.picker.theme.ThemePreviewPage.ThemeCoverPage;
40 import com.android.wallpaper.R;
41 import com.android.wallpaper.asset.Asset;
42 import com.android.wallpaper.asset.BitmapCachingAsset;
43 import com.android.wallpaper.module.CurrentWallpaperInfoFactory;
44 import com.android.wallpaper.module.InjectorProvider;
45 import com.android.wallpaper.picker.ToolbarFragment;
46 
47 public class CustomThemeNameFragment extends CustomThemeStepFragment {
48 
newInstance(CharSequence toolbarTitle, int position, int titleResId)49     public static CustomThemeNameFragment newInstance(CharSequence toolbarTitle, int position,
50             int titleResId) {
51         CustomThemeNameFragment fragment = new CustomThemeNameFragment();
52         Bundle arguments = ToolbarFragment.createArguments(toolbarTitle);
53         arguments.putInt(ARG_KEY_POSITION, position);
54         arguments.putInt(ARG_KEY_TITLE_RES_ID, titleResId);
55         fragment.setArguments(arguments);
56         return fragment;
57     }
58 
59 
60     private int[] mColorButtonIds = {
61             R.id.preview_check_selected, R.id.preview_radio_selected, R.id.preview_toggle_selected
62     };
63     private int[] mColorTileIds = {
64             R.id.preview_color_qs_0_bg, R.id.preview_color_qs_1_bg, R.id.preview_color_qs_2_bg
65     };
66     private int[][] mColorTileIconIds = {
67             new int[]{ R.id.preview_color_qs_0_icon, 0},
68             new int[]{ R.id.preview_color_qs_1_icon, 1},
69             new int[] { R.id.preview_color_qs_2_icon, 3}
70     };
71 
72     private int[] mShapeIconIds = {
73             R.id.shape_preview_icon_0, R.id.shape_preview_icon_1, R.id.shape_preview_icon_2,
74             R.id.shape_preview_icon_3, R.id.shape_preview_icon_4, R.id.shape_preview_icon_5
75     };
76 
77     private Asset mWallpaperAsset;
78     private ThemeCoverPage mCoverPage;
79     private TimeTicker mTicker;
80     private EditText mNameEditor;
81 
82     @Override
onCreate(@ullable Bundle savedInstanceState)83     public void onCreate(@Nullable Bundle savedInstanceState) {
84         super.onCreate(savedInstanceState);
85         CurrentWallpaperInfoFactory currentWallpaperFactory = InjectorProvider.getInjector()
86                 .getCurrentWallpaperFactory(getActivity().getApplicationContext());
87         currentWallpaperFactory.createCurrentWallpaperInfos(
88                 (homeWallpaper, lockWallpaper, presentationMode) -> {
89                     mWallpaperAsset = new BitmapCachingAsset(getContext(),
90                             homeWallpaper.getThumbAsset(getContext()));
91                     if (mCoverPage != null) {
92                         mCoverPage.bindBody(true);
93                     }
94                 }, false);
95     }
96 
97     @Nullable
98     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)99     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
100             @Nullable Bundle savedInstanceState) {
101         View view = super.onCreateView(inflater, container, savedInstanceState);
102         mTitle = view.findViewById(R.id.component_options_title);
103         mTitle.setText(mTitleResId);
104         mNameEditor = view.findViewById(R.id.custom_theme_name);
105         mNameEditor.setText(mCustomThemeManager.getOriginalTheme().getTitle());
106         bindCover(view.findViewById(R.id.component_preview_content));
107 
108         return view;
109     }
110 
111     @Override
onResume()112     public void onResume() {
113         super.onResume();
114         mTicker = TimeTicker.registerNewReceiver(getContext(), this::updateTime);
115         updateTime();
116     }
117 
updateTime()118     private void updateTime() {
119         if (mCoverPage != null) {
120             mCoverPage.updateTime();
121         }
122     }
123 
124     @Override
onPause()125     public void onPause() {
126         super.onPause();
127         if (getContext() != null) {
128             getContext().unregisterReceiver(mTicker);
129         }
130     }
131 
bindCover(CardView card)132     private void bindCover(CardView card) {
133         Context context = getContext();
134         PreviewInfo previewInfo = mCustomThemeManager.buildCustomThemePreviewInfo(context);
135         mCoverPage = new ThemeCoverPage(context, getThemeName(),
136                 previewInfo.resolveAccentColor(getResources()), previewInfo.icons,
137                 previewInfo.headlineFontFamily, previewInfo.bottomSheeetCornerRadius,
138                 previewInfo.shapeDrawable, previewInfo.shapeAppIcons, null,
139                 mColorButtonIds, mColorTileIds, mColorTileIconIds, mShapeIconIds,
140                 new WallpaperLayoutListener());
141         mCoverPage.setCard(card);
142         mCoverPage.bindPreviewContent();
143         mNameEditor.addTextChangedListener(new TextWatcher() {
144             @Override
145             public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
146 
147             }
148 
149             @Override
150             public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
151                 ((TextView)card.findViewById(R.id.theme_preview_card_header)).setText(charSequence);
152             }
153 
154             @Override
155             public void afterTextChanged(Editable editable) {
156 
157             }
158         });
159     }
160 
161     private class WallpaperLayoutListener implements OnLayoutChangeListener {
162 
163         @Override
onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)164         public void onLayoutChange(View view, int left, int top, int right,
165                 int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
166             int targetWidth = right - left;
167             int targetHeight = bottom - top;
168             if (targetWidth > 0 && targetHeight > 0) {
169                 if (mWallpaperAsset != null) {
170                     mWallpaperAsset.decodeBitmap(
171                             targetWidth, targetHeight,
172                             bitmap -> setWallpaperBitmap(view, bitmap));
173                 }
174                 view.removeOnLayoutChangeListener(this);
175             }
176         }
177 
setWallpaperBitmap(View view, Bitmap bitmap)178         private void setWallpaperBitmap(View view, Bitmap bitmap) {
179             Resources res = view.getContext().getResources();
180             Drawable background = new BitmapDrawable(res, bitmap);
181             background.setAlpha(ThemeCoverPage.COVER_PAGE_WALLPAPER_ALPHA);
182 
183             view.findViewById(R.id.theme_preview_card_background).setBackground(background);
184         }
185     }
186 
187     @Override
onDestroyView()188     public void onDestroyView() {
189         super.onDestroyView();
190         mCoverPage = null;
191     }
192 
193     @Override
getFragmentLayoutResId()194     protected int getFragmentLayoutResId() {
195         return R.layout.fragment_custom_theme_name;
196     }
197 
getThemeName()198     public String getThemeName() {
199         return mNameEditor.getText().toString();
200     }
201 }
202