1 /*
2  * Copyright (C) 2021 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.model.grid;
17 
18 import android.content.Context;
19 import android.util.Log;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.widget.TextView;
23 
24 import androidx.annotation.Nullable;
25 
26 import com.android.customization.model.CustomizationManager.OptionsFetchedListener;
27 import com.android.customization.picker.grid.GridFragment;
28 import com.android.customization.picker.grid.GridSectionView;
29 import com.android.wallpaper.R;
30 import com.android.wallpaper.model.CustomizationSectionController;
31 
32 import java.util.List;
33 
34 /** A {@link CustomizationSectionController} for app grid. */
35 public class GridSectionController implements CustomizationSectionController<GridSectionView> {
36 
37     private static final String TAG = "GridSectionController";
38 
39     private final GridOptionsManager mGridOptionsManager;
40     private final CustomizationSectionNavigationController mSectionNavigationController;
41 
GridSectionController(GridOptionsManager gridOptionsManager, CustomizationSectionNavigationController sectionNavigationController)42     public GridSectionController(GridOptionsManager gridOptionsManager,
43             CustomizationSectionNavigationController sectionNavigationController) {
44         mGridOptionsManager = gridOptionsManager;
45         mSectionNavigationController = sectionNavigationController;
46     }
47 
48     @Override
isAvailable(Context context)49     public boolean isAvailable(Context context) {
50         return mGridOptionsManager.isAvailable();
51     }
52 
53     @Override
createView(Context context)54     public GridSectionView createView(Context context) {
55         GridSectionView gridSectionView = (GridSectionView) LayoutInflater.from(context)
56                 .inflate(R.layout.grid_section_view, /* root= */ null);
57         TextView sectionDescription = gridSectionView.findViewById(R.id.grid_section_description);
58         View sectionTile = gridSectionView.findViewById(R.id.grid_section_tile);
59 
60         // Fetch grid options to show currently set grid.
61         mGridOptionsManager.fetchOptions(new OptionsFetchedListener<GridOption>() {
62             @Override
63             public void onOptionsLoaded(List<GridOption> options) {
64                 sectionDescription.setText(getActiveOption(options).getTitle());
65             }
66 
67             @Override
68             public void onError(@Nullable Throwable throwable) {
69                 if (throwable != null) {
70                     Log.e(TAG, "Error loading grid options", throwable);
71                 }
72                 sectionDescription.setText(R.string.something_went_wrong);
73                 sectionTile.setVisibility(View.GONE);
74             }
75         }, /* reload= */ true);
76 
77         gridSectionView.setOnClickListener(
78                 v -> mSectionNavigationController.navigateTo(new GridFragment()));
79 
80         return gridSectionView;
81     }
82 
getActiveOption(List<GridOption> options)83     private GridOption getActiveOption(List<GridOption> options) {
84         return options.stream()
85                 .filter(option -> option.isActive(mGridOptionsManager))
86                 .findAny()
87                 // For development only, as there should always be a grid set.
88                 .orElse(options.get(0));
89     }
90 }
91