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