• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import androidx.lifecycle.Observer;
24 
25 import com.android.customization.model.themedicon.domain.interactor.ThemedIconInteractor;
26 import com.android.customization.model.themedicon.domain.interactor.ThemedIconSnapshotRestorer;
27 import com.android.customization.module.logging.ThemesUserEventLogger;
28 import com.android.customization.picker.themedicon.ThemedIconSectionView;
29 import com.android.themepicker.R;
30 import com.android.wallpaper.model.CustomizationSectionController;
31 
32 // TODO (b/311712452): Refactor CustomizationSectionController to use recommended arch UI components
33 /** The {@link CustomizationSectionController} for themed icon section. */
34 public class ThemedIconSectionController implements
35         CustomizationSectionController<ThemedIconSectionView> {
36 
37     private static final String KEY_THEMED_ICON_ENABLED = "SAVED_THEMED_ICON_ENABLED";
38 
39     private final ThemedIconSwitchProvider mThemedIconOptionsProvider;
40     private final ThemedIconInteractor mInteractor;
41     private final ThemedIconSnapshotRestorer mSnapshotRestorer;
42     private final Observer<Boolean> mIsActivatedChangeObserver;
43     private final ThemesUserEventLogger mThemesUserEventLogger;
44 
45     private ThemedIconSectionView mThemedIconSectionView;
46     private boolean mSavedThemedIconEnabled = false;
47 
ThemedIconSectionController( ThemedIconSwitchProvider themedIconOptionsProvider, ThemedIconInteractor interactor, @Nullable Bundle savedInstanceState, ThemedIconSnapshotRestorer snapshotRestorer, ThemesUserEventLogger themesUserEventLogger)48     public ThemedIconSectionController(
49             ThemedIconSwitchProvider themedIconOptionsProvider,
50             ThemedIconInteractor interactor,
51             @Nullable Bundle savedInstanceState,
52             ThemedIconSnapshotRestorer snapshotRestorer,
53             ThemesUserEventLogger themesUserEventLogger) {
54         mThemedIconOptionsProvider = themedIconOptionsProvider;
55         mInteractor = interactor;
56         mSnapshotRestorer = snapshotRestorer;
57         mIsActivatedChangeObserver = isActivated -> {
58             if (mThemedIconSectionView.isAttachedToWindow()) {
59                 mThemedIconSectionView.getSwitch().setChecked(isActivated);
60             }
61         };
62         mThemesUserEventLogger = themesUserEventLogger;
63 
64         if (savedInstanceState != null) {
65             mSavedThemedIconEnabled = savedInstanceState.getBoolean(
66                     KEY_THEMED_ICON_ENABLED, /* defaultValue= */ false);
67         }
68     }
69 
70     @Override
isAvailable(@ullable Context context)71     public boolean isAvailable(@Nullable Context context) {
72         return context != null && mThemedIconOptionsProvider.isThemedIconAvailable();
73     }
74 
75     @Override
createView(Context context)76     public ThemedIconSectionView createView(Context context) {
77         mThemedIconSectionView =
78                 (ThemedIconSectionView) LayoutInflater.from(context).inflate(
79                         R.layout.themed_icon_section_view, /* root= */ null);
80         mThemedIconSectionView.setViewListener(this::onViewActivated);
81         mThemedIconSectionView.getSwitch().setChecked(mSavedThemedIconEnabled);
82         mThemedIconOptionsProvider.fetchThemedIconEnabled(
83                 enabled -> {
84                     mInteractor.setActivated(enabled);
85                     mThemedIconSectionView.getSwitch().setChecked(enabled);
86                 });
87         mInteractor.isActivatedAsLiveData().observeForever(mIsActivatedChangeObserver);
88         return mThemedIconSectionView;
89     }
90 
91     @Override
release()92     public void release() {
93         mInteractor.isActivatedAsLiveData().removeObserver(mIsActivatedChangeObserver);
94     }
95 
onViewActivated(Context context, boolean viewActivated)96     private void onViewActivated(Context context, boolean viewActivated) {
97         if (context == null) {
98             return;
99         }
100         mThemedIconOptionsProvider.setThemedIconEnabled(viewActivated);
101         mInteractor.setActivated(viewActivated);
102         mThemesUserEventLogger.logThemedIconApplied(viewActivated);
103         mSnapshotRestorer.store(viewActivated);
104     }
105 
106     @Override
onSaveInstanceState(Bundle savedInstanceState)107     public void onSaveInstanceState(Bundle savedInstanceState) {
108         if (mThemedIconSectionView != null) {
109             savedInstanceState.putBoolean(KEY_THEMED_ICON_ENABLED,
110                     mThemedIconSectionView.getSwitch().isChecked());
111         }
112     }
113 }
114