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.picker.mode;
17 
18 import android.content.Context;
19 import android.content.res.Configuration;
20 import android.util.AttributeSet;
21 import android.widget.Switch;
22 
23 import androidx.annotation.Nullable;
24 
25 import com.android.themepicker.R;
26 import com.android.wallpaper.picker.SectionView;
27 
28 /** The view of section in the customization picker fragment. */
29 public final class DarkModeSectionView extends SectionView {
30 
31     private boolean mIsDarkModeActivated;
32 
DarkModeSectionView(Context context, @Nullable AttributeSet attrs)33     public DarkModeSectionView(Context context, @Nullable AttributeSet attrs) {
34         super(context, attrs);
35         setTitle(context.getString(R.string.mode_title));
36         mIsDarkModeActivated = (context.getResources().getConfiguration().uiMode
37                 & Configuration.UI_MODE_NIGHT_YES) != 0;
38     }
39 
40     @Override
onFinishInflate()41     protected void onFinishInflate() {
42         super.onFinishInflate();
43         Switch switchView = findViewById(R.id.dark_mode_toggle);
44         switchView.setChecked(mIsDarkModeActivated);
45         switchView.setOnCheckedChangeListener((buttonView, isChecked) ->
46                 switchView.setChecked(mIsDarkModeActivated)
47         );
48         setOnClickListener(view -> modeToggleClicked());
49     }
50 
modeToggleClicked()51     private void modeToggleClicked() {
52         mIsDarkModeActivated = !mIsDarkModeActivated;
53         viewActivated(mIsDarkModeActivated);
54     }
55 
viewActivated(boolean isChecked)56     private void viewActivated(boolean isChecked) {
57         if (mSectionViewListener != null) {
58             mSectionViewListener.onViewActivated(getContext(), isChecked);
59         }
60     }
61 
62     @Override
setEnabled(boolean enabled)63     public void setEnabled(boolean enabled) {
64         final int numOfChildViews = getChildCount();
65         for (int i = 0; i < numOfChildViews; i++) {
66             getChildAt(i).setEnabled(enabled);
67         }
68     }
69 }
70