1 /* 2 * Copyright (C) 2022 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.wallpaper.widget; 17 18 import android.content.Context; 19 import android.util.AttributeSet; 20 import android.view.LayoutInflater; 21 import android.widget.CompoundButton; 22 import android.widget.FrameLayout; 23 import android.widget.ToggleButton; 24 25 import androidx.annotation.IntDef; 26 import androidx.annotation.NonNull; 27 import androidx.annotation.Nullable; 28 import androidx.appcompat.content.res.AppCompatResources; 29 30 import com.android.wallpaper.R; 31 32 /** 33 * Custom layout for a group of wallpaper control buttons. 34 */ 35 public final class WallpaperControlButtonGroup extends FrameLayout { 36 37 public static final int DELETE = 0; 38 public static final int EDIT = 1; 39 public static final int CUSTOMIZE = 2; 40 public static final int EFFECTS = 3; 41 public static final int INFORMATION = 4; 42 public static final int SHARE = 5; 43 44 /** 45 * Overlay tab 46 */ 47 @IntDef({DELETE, EDIT, CUSTOMIZE, EFFECTS, SHARE, INFORMATION}) 48 public @interface WallpaperControlType { 49 } 50 51 final int[] mFloatingSheetControlButtonTypes = { CUSTOMIZE, EFFECTS, SHARE, INFORMATION }; 52 53 ToggleButton mDeleteButton; 54 ToggleButton mEditButton; 55 ToggleButton mCustomizeButton; 56 ToggleButton mEffectsButton; 57 ToggleButton mShareButton; 58 ToggleButton mInformationButton; 59 60 /** 61 * Constructor 62 */ WallpaperControlButtonGroup(@onNull Context context, @Nullable AttributeSet attrs)63 public WallpaperControlButtonGroup(@NonNull Context context, @Nullable AttributeSet attrs) { 64 super(context, attrs); 65 LayoutInflater.from(context).inflate(R.layout.wallpaper_control_button_group, this, true); 66 mDeleteButton = findViewById(R.id.delete_button); 67 mEditButton = findViewById(R.id.edit_button); 68 mCustomizeButton = findViewById(R.id.customize_button); 69 mEffectsButton = findViewById(R.id.effects_button); 70 mShareButton = findViewById(R.id.share_button); 71 mInformationButton = findViewById(R.id.information_button); 72 } 73 74 /** 75 * Show a button by giving a correspondent listener 76 */ showButton(@allpaperControlType int type, CompoundButton.OnCheckedChangeListener listener)77 public void showButton(@WallpaperControlType int type, 78 CompoundButton.OnCheckedChangeListener listener) { 79 ToggleButton button = getActionButton(type); 80 if (button != null) { 81 button.setVisibility(VISIBLE); 82 button.setOnCheckedChangeListener(listener); 83 } 84 } 85 getActionButton(@allpaperControlType int type)86 private ToggleButton getActionButton(@WallpaperControlType int type) { 87 switch (type) { 88 case DELETE: 89 return mDeleteButton; 90 case EDIT: 91 return mEditButton; 92 case CUSTOMIZE: 93 return mCustomizeButton; 94 case EFFECTS: 95 return mEffectsButton; 96 case SHARE: 97 return mShareButton; 98 case INFORMATION: 99 return mInformationButton; 100 default: 101 return null; 102 } 103 } 104 105 /** 106 * Hide a button 107 */ hideButton(@allpaperControlType int type)108 public void hideButton(@WallpaperControlType int type) { 109 getActionButton(type).setVisibility(GONE); 110 } 111 112 /** 113 * Set checked for a button 114 */ setChecked(@allpaperControlType int type, boolean checked)115 public void setChecked(@WallpaperControlType int type, boolean checked) { 116 getActionButton(type).setChecked(checked); 117 } 118 119 /** 120 * Update the background color in case the context theme has changed. 121 */ updateBackgroundColor()122 public void updateBackgroundColor() { 123 Context context = getContext(); 124 if (context == null) { 125 return; 126 } 127 mDeleteButton.setForeground(null); 128 mEditButton.setForeground(null); 129 mCustomizeButton.setForeground(null); 130 mEffectsButton.setForeground(null); 131 mShareButton.setForeground(null); 132 mInformationButton.setForeground(null); 133 mDeleteButton.setForeground(AppCompatResources.getDrawable(context, 134 R.drawable.wallpaper_control_button_delete)); 135 mEditButton.setForeground( 136 AppCompatResources.getDrawable(context, R.drawable.wallpaper_control_button_edit)); 137 mCustomizeButton.setForeground(AppCompatResources.getDrawable(context, 138 R.drawable.wallpaper_control_button_customize)); 139 mEffectsButton.setForeground(AppCompatResources.getDrawable(context, 140 R.drawable.wallpaper_control_button_effect)); 141 mShareButton.setForeground(AppCompatResources.getDrawable(context, 142 R.drawable.wallpaper_control_button_share)); 143 mInformationButton.setForeground( 144 AppCompatResources.getDrawable(context, R.drawable.wallpaper_control_button_info)); 145 } 146 147 /** 148 * Ensures only one toggle button with a floating sheet is selected at a time 149 */ deselectOtherFloatingSheetControlButtons(@allpaperControlType int selectedType)150 public void deselectOtherFloatingSheetControlButtons(@WallpaperControlType int selectedType) { 151 for (int type : mFloatingSheetControlButtonTypes) { 152 if (type != selectedType) { 153 getActionButton(type).setChecked(false); 154 } 155 } 156 } 157 158 /** 159 * Returns true if there is a floating sheet button selected, and false if not 160 */ isFloatingSheetControlButtonSelected()161 public boolean isFloatingSheetControlButtonSelected() { 162 for (int type : mFloatingSheetControlButtonTypes) { 163 if (getActionButton(type).isChecked()) { 164 return true; 165 } 166 } 167 return false; 168 } 169 170 /** 171 * Deselects all floating sheet toggle buttons in the Wallpaper Control Button Group 172 */ deselectAllFloatingSheetControlButtons()173 public void deselectAllFloatingSheetControlButtons() { 174 for (int type : mFloatingSheetControlButtonTypes) { 175 getActionButton(type).setChecked(false); 176 } 177 } 178 } 179