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.app.AlertDialog; 19 import android.content.Context; 20 import android.graphics.drawable.Drawable; 21 import android.os.Bundle; 22 import android.view.LayoutInflater; 23 import android.view.MenuItem; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import android.widget.TextView; 27 28 import androidx.annotation.NonNull; 29 import androidx.annotation.Nullable; 30 import androidx.annotation.StringRes; 31 import androidx.recyclerview.widget.RecyclerView; 32 33 import com.android.customization.model.theme.custom.CustomThemeManager; 34 import com.android.customization.model.theme.custom.ThemeComponentOption; 35 import com.android.customization.model.theme.custom.ThemeComponentOptionProvider; 36 import com.android.customization.widget.OptionSelectorController; 37 import com.android.wallpaper.R; 38 import com.android.wallpaper.picker.ToolbarFragment; 39 40 public class CustomThemeComponentFragment extends CustomThemeStepFragment { 41 private static final String ARG_USE_GRID_LAYOUT = "CustomThemeComponentFragment.use_grid";; 42 newInstance(CharSequence toolbarTitle, int position, int titleResId)43 public static CustomThemeComponentFragment newInstance(CharSequence toolbarTitle, int position, 44 int titleResId) { 45 return newInstance(toolbarTitle, position, titleResId, false); 46 } 47 newInstance(CharSequence toolbarTitle, int position, int titleResId, boolean allowGridLayout)48 public static CustomThemeComponentFragment newInstance(CharSequence toolbarTitle, int position, 49 int titleResId, boolean allowGridLayout) { 50 CustomThemeComponentFragment fragment = new CustomThemeComponentFragment(); 51 Bundle arguments = ToolbarFragment.createArguments(toolbarTitle); 52 arguments.putInt(ARG_KEY_POSITION, position); 53 arguments.putInt(ARG_KEY_TITLE_RES_ID, titleResId); 54 arguments.putBoolean(ARG_USE_GRID_LAYOUT, allowGridLayout); 55 fragment.setArguments(arguments); 56 return fragment; 57 } 58 59 private ThemeComponentOptionProvider<? extends ThemeComponentOption> mProvider; 60 private boolean mUseGridLayout; 61 62 private RecyclerView mOptionsContainer; 63 private OptionSelectorController<ThemeComponentOption> mOptionsController; 64 private ThemeComponentOption mSelectedOption; 65 66 @Override onCreate(@ullable Bundle savedInstanceState)67 public void onCreate(@Nullable Bundle savedInstanceState) { 68 super.onCreate(savedInstanceState); 69 mUseGridLayout = getArguments().getBoolean(ARG_USE_GRID_LAYOUT); 70 mProvider = mHost.getComponentOptionProvider(mPosition); 71 } 72 73 @Nullable 74 @Override onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)75 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 76 @Nullable Bundle savedInstanceState) { 77 View view = super.onCreateView(inflater, container, savedInstanceState); 78 mOptionsContainer = view.findViewById(R.id.options_container); 79 mPreviewContainer = view.findViewById(R.id.component_preview_content); 80 mTitle = view.findViewById(R.id.component_options_title); 81 mTitle.setText(mTitleResId); 82 setUpOptions(); 83 84 return view; 85 } 86 87 @Override getFragmentLayoutResId()88 protected int getFragmentLayoutResId() { 89 return R.layout.fragment_custom_theme_component; 90 } 91 getSelectedOption()92 public ThemeComponentOption getSelectedOption() { 93 return mSelectedOption; 94 } 95 bindPreview()96 private void bindPreview() { 97 mSelectedOption.bindPreview(mPreviewContainer); 98 } 99 setUpOptions()100 private void setUpOptions() { 101 mProvider.fetch(options -> { 102 mOptionsController = new OptionSelectorController( 103 mOptionsContainer, options, mUseGridLayout, false); 104 105 mOptionsController.addListener(selected -> { 106 mSelectedOption = (ThemeComponentOption) selected; 107 bindPreview(); 108 }); 109 mOptionsController.initOptions(mCustomThemeManager); 110 111 for (ThemeComponentOption option : options) { 112 if (option.isActive(mCustomThemeManager)) { 113 mSelectedOption = option; 114 break; 115 } 116 } 117 if (mSelectedOption == null) { 118 mSelectedOption = options.get(0); 119 } 120 mOptionsController.setSelectedOption(mSelectedOption); 121 }, false); 122 } 123 } 124