summaryrefslogtreecommitdiff
path: root/src/com/android/wallpaper/widget/WallpaperControlButtonGroup.java
blob: c5c8f7eaffb4950491b3094f87ae37dc835b543c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.wallpaper.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ToggleButton;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;

import com.android.wallpaper.R;

/**
 * Custom layout for a group of wallpaper control buttons.
 */
public final class WallpaperControlButtonGroup extends FrameLayout {

    public static final int DELETE = 0;
    public static final int EDIT = 1;
    public static final int CUSTOMIZE = 2;
    public static final int EFFECTS = 3;
    public static final int INFORMATION = 4;
    public static final int SHARE = 5;

    /**
     * Overlay tab
     */
    @IntDef({DELETE, EDIT, CUSTOMIZE, EFFECTS, SHARE, INFORMATION})
    public @interface WallpaperControlType {
    }

    final int[] mFloatingSheetControlButtonTypes = { CUSTOMIZE, EFFECTS, SHARE, INFORMATION };

    ToggleButton mDeleteButton;
    ToggleButton mEditButton;
    ToggleButton mCustomizeButton;
    ToggleButton mEffectsButton;
    ToggleButton mShareButton;
    ToggleButton mInformationButton;

    /**
     * Constructor
     */
    public WallpaperControlButtonGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.wallpaper_control_button_group, this, true);
        mDeleteButton = findViewById(R.id.delete_button);
        mEditButton = findViewById(R.id.edit_button);
        mCustomizeButton = findViewById(R.id.customize_button);
        mEffectsButton = findViewById(R.id.effects_button);
        mShareButton = findViewById(R.id.share_button);
        mInformationButton = findViewById(R.id.information_button);
    }

    /**
     * Show a button by giving a correspondent listener
     */
    public void showButton(@WallpaperControlType int type,
            CompoundButton.OnCheckedChangeListener listener) {
        ToggleButton button = getActionButton(type);
        if (button != null) {
            button.setVisibility(VISIBLE);
            button.setOnCheckedChangeListener(listener);
        }
    }

    private ToggleButton getActionButton(@WallpaperControlType int type) {
        switch (type) {
            case DELETE:
                return mDeleteButton;
            case EDIT:
                return mEditButton;
            case CUSTOMIZE:
                return mCustomizeButton;
            case EFFECTS:
                return mEffectsButton;
            case SHARE:
                return mShareButton;
            case INFORMATION:
                return mInformationButton;
            default:
                return null;
        }
    }

    /**
     * Hide a button
     */
    public void hideButton(@WallpaperControlType int type) {
        getActionButton(type).setVisibility(GONE);
    }

    /**
     * Set checked for a button
     */
    public void setChecked(@WallpaperControlType int type, boolean checked) {
        getActionButton(type).setChecked(checked);
    }

    /**
     * Update the background color in case the context theme has changed.
     */
    public void updateBackgroundColor() {
        Context context = getContext();
        if (context == null) {
            return;
        }
        mDeleteButton.setForeground(null);
        mEditButton.setForeground(null);
        mCustomizeButton.setForeground(null);
        mEffectsButton.setForeground(null);
        mShareButton.setForeground(null);
        mInformationButton.setForeground(null);
        mDeleteButton.setForeground(AppCompatResources.getDrawable(context,
                R.drawable.wallpaper_control_button_delete));
        mEditButton.setForeground(
                AppCompatResources.getDrawable(context, R.drawable.wallpaper_control_button_edit));
        mCustomizeButton.setForeground(AppCompatResources.getDrawable(context,
                R.drawable.wallpaper_control_button_customize));
        mEffectsButton.setForeground(AppCompatResources.getDrawable(context,
                R.drawable.wallpaper_control_button_effect));
        mShareButton.setForeground(AppCompatResources.getDrawable(context,
                R.drawable.wallpaper_control_button_share));
        mInformationButton.setForeground(
                AppCompatResources.getDrawable(context, R.drawable.wallpaper_control_button_info));
    }

    /**
     * Ensures only one toggle button with a floating sheet is selected at a time
     */
    public void deselectOtherFloatingSheetControlButtons(@WallpaperControlType int selectedType) {
        for (int type : mFloatingSheetControlButtonTypes) {
            if (type != selectedType) {
                getActionButton(type).setChecked(false);
            }
        }
    }

    /**
     * Returns true if there is a floating sheet button selected, and false if not
     */
    public boolean isFloatingSheetControlButtonSelected() {
        for (int type : mFloatingSheetControlButtonTypes) {
            if (getActionButton(type).isChecked()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Deselects all floating sheet toggle buttons in the Wallpaper Control Button Group
     */
    public void deselectAllFloatingSheetControlButtons() {
        for (int type : mFloatingSheetControlButtonTypes) {
            getActionButton(type).setChecked(false);
        }
    }
}