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.AppbarFragment; 22 23 abstract class CustomThemeStepFragment extends AppbarFragment { 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 static final String ARG_KEY_ACCESSIBILITY_RES_ID = 27 "CustomThemeStepFragment.accessibility_res"; 28 protected CustomThemeComponentStepHost mHost; 29 protected CustomThemeManager mCustomThemeManager; 30 protected int mPosition; 31 protected ViewGroup mPreviewContainer; 32 protected TextView mTitle; 33 @StringRes 34 protected int mTitleResId; 35 @StringRes 36 protected int mAccessibilityResId; 37 38 @Override onAttach(Context context)39 public void onAttach(Context context) { 40 super.onAttach(context); 41 mHost = (CustomThemeComponentStepHost) context; 42 } 43 44 @Override onResume()45 public void onResume() { 46 super.onResume(); 47 mHost.setCurrentStep(mPosition); 48 } 49 50 @Override onCreate(@ullable Bundle savedInstanceState)51 public void onCreate(@Nullable Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 mPosition = getArguments().getInt(ARG_KEY_POSITION); 54 mTitleResId = getArguments().getInt(ARG_KEY_TITLE_RES_ID); 55 mAccessibilityResId = getArguments().getInt(ARG_KEY_ACCESSIBILITY_RES_ID); 56 mCustomThemeManager = mHost.getCustomThemeManager(); 57 } 58 59 @Override onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)60 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 61 @Nullable Bundle savedInstanceState) { 62 View view = inflater.inflate( 63 getFragmentLayoutResId(), container, /* attachToRoot */ false); 64 // No original theme means it's a new one, so no toolbar icon for deleting it is needed 65 if (mCustomThemeManager.getOriginalTheme() == null 66 || !mCustomThemeManager.getOriginalTheme().isDefined()) { 67 setUpToolbar(view); 68 } else { 69 setUpToolbar(view, R.menu.custom_theme_editor_menu); 70 mToolbar.getMenu().getItem(0).setIconTintList( 71 getContext().getColorStateList(R.color.toolbar_icon_tint)); 72 } 73 Drawable closeIcon = getResources().getDrawable(R.drawable.ic_close_24px, null).mutate(); 74 closeIcon.setTintList(getResources().getColorStateList(R.color.toolbar_icon_tint, null)); 75 mToolbar.setNavigationIcon(closeIcon); 76 77 mToolbar.setNavigationContentDescription(R.string.cancel); 78 mToolbar.setNavigationOnClickListener(v -> mHost.cancel()); 79 80 mPreviewContainer = view.findViewById(R.id.component_preview_content); 81 return view; 82 } 83 84 @Override getAccessibilityTitle()85 protected String getAccessibilityTitle() { 86 return getString(mAccessibilityResId); 87 } 88 89 @Override onMenuItemClick(MenuItem item)90 public boolean onMenuItemClick(MenuItem item) { 91 if (item.getItemId() == R.id.custom_theme_delete) { 92 AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 93 builder.setMessage(R.string.delete_custom_theme_confirmation) 94 .setPositiveButton(R.string.delete_custom_theme_button, 95 (dialogInterface, i) -> mHost.delete()) 96 .setNegativeButton(R.string.cancel, null) 97 .create() 98 .show(); 99 return true; 100 } 101 return super.onMenuItemClick(item); 102 } 103 getFragmentLayoutResId()104 protected abstract int getFragmentLayoutResId(); 105 106 public interface CustomThemeComponentStepHost { delete()107 void delete(); cancel()108 void cancel(); getComponentOptionProvider( int position)109 ThemeComponentOptionProvider<? extends ThemeComponentOption> getComponentOptionProvider( 110 int position); 111 getCustomThemeManager()112 CustomThemeManager getCustomThemeManager(); 113 setCurrentStep(int step)114 void setCurrentStep(int step); 115 } 116 } 117