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.themedicon;
17 
18 import android.content.Context;
19 import android.os.Bundle;
20 import android.view.LayoutInflater;
21 
22 import androidx.annotation.Nullable;
23 
24 import com.android.customization.picker.themedicon.ThemedIconSectionView;
25 import com.android.wallpaper.R;
26 import com.android.wallpaper.model.CustomizationSectionController;
27 import com.android.wallpaper.model.WorkspaceViewModel;
28 
29 /** The {@link CustomizationSectionController} for themed icon section. */
30 public class ThemedIconSectionController implements
31         CustomizationSectionController<ThemedIconSectionView> {
32 
33     private static final String KEY_THEMED_ICON_ENABLED = "SAVED_THEMED_ICON_ENABLED";
34 
35     private final ThemedIconSwitchProvider mThemedIconOptionsProvider;
36     private final WorkspaceViewModel mWorkspaceViewModel;
37 
38     private ThemedIconSectionView mThemedIconSectionView;
39     private boolean mSavedThemedIconEnabled = false;
40 
41 
ThemedIconSectionController(ThemedIconSwitchProvider themedIconOptionsProvider, WorkspaceViewModel workspaceViewModel, @Nullable Bundle savedInstanceState)42     public ThemedIconSectionController(ThemedIconSwitchProvider themedIconOptionsProvider,
43             WorkspaceViewModel workspaceViewModel, @Nullable Bundle savedInstanceState) {
44         mThemedIconOptionsProvider = themedIconOptionsProvider;
45         mWorkspaceViewModel = workspaceViewModel;
46 
47         if (savedInstanceState != null) {
48             mSavedThemedIconEnabled = savedInstanceState.getBoolean(
49                     KEY_THEMED_ICON_ENABLED, /* defaultValue= */ false);
50         }
51     }
52 
53     @Override
isAvailable(@ullable Context context)54     public boolean isAvailable(@Nullable Context context) {
55         return context != null && mThemedIconOptionsProvider.isThemedIconAvailable();
56     }
57 
58     @Override
createView(Context context)59     public ThemedIconSectionView createView(Context context) {
60         mThemedIconSectionView =
61                 (ThemedIconSectionView) LayoutInflater.from(context).inflate(
62                         R.layout.themed_icon_section_view, /* root= */ null);
63         mThemedIconSectionView.setViewListener(this::onViewActivated);
64         mThemedIconSectionView.getSwitch().setChecked(mSavedThemedIconEnabled);
65         mThemedIconOptionsProvider.fetchThemedIconEnabled(
66                 enabled -> mThemedIconSectionView.getSwitch().setChecked(enabled));
67         return mThemedIconSectionView;
68     }
69 
onViewActivated(Context context, boolean viewActivated)70     private void onViewActivated(Context context, boolean viewActivated) {
71         if (context == null) {
72             return;
73         }
74         mThemedIconOptionsProvider.setThemedIconEnabled(viewActivated);
75         mWorkspaceViewModel.getUpdateWorkspace().setValue(viewActivated);
76     }
77 
78     @Override
onSaveInstanceState(Bundle savedInstanceState)79     public void onSaveInstanceState(Bundle savedInstanceState) {
80         if (mThemedIconSectionView != null) {
81             savedInstanceState.putBoolean(KEY_THEMED_ICON_ENABLED,
82                     mThemedIconSectionView.getSwitch().isChecked());
83         }
84     }
85 }
86