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