1 package com.android.customization.picker.theme;
2 
3 import android.app.AlertDialog;
4 import android.content.Context;
5 import android.graphics.drawable.Drawable;
6 import android.os.Bundle;
7 import android.view.LayoutInflater;
8 import android.view.MenuItem;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.TextView;
12 
13 import androidx.annotation.NonNull;
14 import androidx.annotation.Nullable;
15 import androidx.annotation.StringRes;
16 
17 import com.android.customization.model.theme.custom.CustomThemeManager;
18 import com.android.customization.model.theme.custom.ThemeComponentOption;
19 import com.android.customization.model.theme.custom.ThemeComponentOptionProvider;
20 import com.android.wallpaper.R;
21 import com.android.wallpaper.picker.ToolbarFragment;
22 
23 abstract class CustomThemeStepFragment extends ToolbarFragment {
24     protected static final String ARG_KEY_POSITION = "CustomThemeStepFragment.position";
25     protected static final String ARG_KEY_TITLE_RES_ID = "CustomThemeStepFragment.title_res";
26     protected CustomThemeComponentStepHost mHost;
27     protected CustomThemeManager mCustomThemeManager;
28     protected int mPosition;
29     protected ViewGroup mPreviewContainer;
30     protected TextView mTitle;
31     @StringRes
32     protected int mTitleResId;
33 
34     @Override
onAttach(Context context)35     public void onAttach(Context context) {
36         super.onAttach(context);
37         mHost = (CustomThemeComponentStepHost) context;
38     }
39 
40     @Override
onResume()41     public void onResume() {
42         super.onResume();
43         mHost.setCurrentStep(mPosition);
44     }
45 
46     @Override
onCreate(@ullable Bundle savedInstanceState)47     public void onCreate(@Nullable Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         mPosition = getArguments().getInt(ARG_KEY_POSITION);
50         mTitleResId = getArguments().getInt(ARG_KEY_TITLE_RES_ID);
51         mCustomThemeManager = mHost.getCustomThemeManager();
52     }
53 
54     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)55     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
56             @Nullable Bundle savedInstanceState) {
57         View view = inflater.inflate(
58                 getFragmentLayoutResId(), container, /* attachToRoot */ false);
59         // No original theme means it's a new one, so no toolbar icon for deleting it is needed
60         if (mCustomThemeManager.getOriginalTheme() == null
61                 || !mCustomThemeManager.getOriginalTheme().isDefined()) {
62             setUpToolbar(view);
63         } else {
64             setUpToolbar(view, R.menu.custom_theme_editor_menu);
65             mToolbar.getMenu().getItem(0).setIconTintList(
66                     getContext().getColorStateList(R.color.toolbar_icon_color));
67         }
68         Drawable closeIcon = getResources().getDrawable(R.drawable.ic_close_24px, null).mutate();
69         closeIcon.setTintList(getResources().getColorStateList(R.color.toolbar_icon_color, null));
70         mToolbar.setNavigationIcon(closeIcon);
71 
72         mToolbar.setNavigationContentDescription(R.string.cancel);
73         mToolbar.setNavigationOnClickListener(v -> mHost.cancel());
74 
75         mPreviewContainer = view.findViewById(R.id.component_preview_content);
76         return view;
77     }
78 
79     @Override
onMenuItemClick(MenuItem item)80     public boolean onMenuItemClick(MenuItem item) {
81         if (item.getItemId() == R.id.custom_theme_delete) {
82             AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
83             builder.setMessage(R.string.delete_custom_theme_confirmation)
84                     .setPositiveButton(R.string.delete_custom_theme_button,
85                             (dialogInterface, i) -> mHost.delete())
86                     .setNegativeButton(R.string.cancel, null)
87                     .create()
88                     .show();
89             return true;
90         }
91         return super.onMenuItemClick(item);
92     }
93 
getFragmentLayoutResId()94     protected abstract int getFragmentLayoutResId();
95 
96     public interface CustomThemeComponentStepHost {
delete()97         void delete();
cancel()98         void cancel();
getComponentOptionProvider( int position)99         ThemeComponentOptionProvider<? extends ThemeComponentOption> getComponentOptionProvider(
100                 int position);
101 
getCustomThemeManager()102         CustomThemeManager getCustomThemeManager();
103 
setCurrentStep(int step)104         void setCurrentStep(int step);
105     }
106 }
107