Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 18 | import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_COLLAPSED; |
| 19 | import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED; |
| 20 | |
chihhangchuang | 7feb375 | 2020-04-24 02:48:56 +0800 | [diff] [blame] | 21 | import android.app.Activity; |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 26 | import android.view.ViewGroup; |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 27 | import android.widget.FrameLayout; |
| 28 | |
| 29 | import androidx.annotation.NonNull; |
| 30 | import androidx.annotation.Nullable; |
| 31 | |
| 32 | import com.android.wallpaper.R; |
Santiago Etchebehere | 53c6343 | 2020-05-07 18:55:35 -0700 | [diff] [blame] | 33 | import com.android.wallpaper.util.SizeCalculator; |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 34 | |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 35 | import com.google.android.material.bottomsheet.BottomSheetBehavior; |
| 36 | import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback; |
| 37 | |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 38 | import java.util.ArrayDeque; |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 39 | import java.util.Arrays; |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 40 | import java.util.Deque; |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 41 | import java.util.EnumMap; |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 42 | import java.util.HashSet; |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 43 | import java.util.Map; |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 44 | import java.util.Set; |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 45 | |
| 46 | /** A {@code ViewGroup} which provides the specific actions for the user to interact with. */ |
| 47 | public class BottomActionBar extends FrameLayout { |
| 48 | |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 49 | /** |
| 50 | * Interface to be implemented by an Activity hosting a {@link BottomActionBar} |
| 51 | */ |
| 52 | public interface BottomActionBarHost { |
| 53 | /** Gets {@link BottomActionBar}. */ |
| 54 | BottomActionBar getBottomActionBar(); |
| 55 | } |
| 56 | |
Chuck Liao | 829d4d2 | 2020-05-07 17:00:10 +0800 | [diff] [blame] | 57 | /** |
| 58 | * The listener for {@link BottomActionBar} visibility change notification. |
| 59 | */ |
| 60 | public interface VisibilityChangeListener { |
| 61 | /** |
| 62 | * Called when {@link BottomActionBar} visibility changes. |
| 63 | * |
| 64 | * @param isVisible {@code true} if it's visible; {@code false} otherwise. |
| 65 | */ |
| 66 | void onVisibilityChange(boolean isVisible); |
| 67 | } |
| 68 | |
Tracy Zhou | 16112e3 | 2020-05-27 13:13:23 -0700 | [diff] [blame] | 69 | /** This listens to changes to an action view's selected state. */ |
| 70 | public interface OnActionSelectedListener { |
| 71 | |
| 72 | /** |
| 73 | * This is called when an action view's selected state changes. |
| 74 | * @param selected whether the action view is selected. |
| 75 | */ |
| 76 | void onActionSelected(boolean selected); |
| 77 | } |
| 78 | |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 79 | /** |
| 80 | * A Callback to notify the registrant to change it's accessibility param when |
| 81 | * {@link BottomActionBar} state changes. |
| 82 | */ |
| 83 | public interface AccessibilityCallback { |
| 84 | /** |
| 85 | * Called when {@link BottomActionBar} collapsed. |
| 86 | */ |
| 87 | void onBottomSheetCollapsed(); |
| 88 | |
| 89 | /** |
| 90 | * Called when {@link BottomActionBar} expanded. |
| 91 | */ |
| 92 | void onBottomSheetExpanded(); |
| 93 | } |
| 94 | |
Ching-Sung Li | 073812b | 2020-04-07 21:19:21 +0800 | [diff] [blame] | 95 | // TODO(b/154299462): Separate downloadable related actions from WallpaperPicker. |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 96 | /** The action items in the bottom action bar. */ |
| 97 | public enum BottomAction { |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 98 | ROTATION, DELETE, INFORMATION, EDIT, CUSTOMIZE, DOWNLOAD, PROGRESS, APPLY |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 99 | } |
| 100 | |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 101 | private final Map<BottomAction, View> mActionMap = new EnumMap<>(BottomAction.class); |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 102 | private final Map<BottomAction, View> mContentViewMap = new EnumMap<>(BottomAction.class); |
Tracy Zhou | 16112e3 | 2020-05-27 13:13:23 -0700 | [diff] [blame] | 103 | private final Map<BottomAction, OnActionSelectedListener> mActionSelectedListeners = |
| 104 | new EnumMap<>(BottomAction.class); |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 105 | |
| 106 | private final ViewGroup mBottomSheetView; |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 107 | private final QueueStateBottomSheetBehavior<ViewGroup> mBottomSheetBehavior; |
Chuck Liao | 829d4d2 | 2020-05-07 17:00:10 +0800 | [diff] [blame] | 108 | private final Set<VisibilityChangeListener> mVisibilityChangeListeners = new HashSet<>(); |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 109 | |
Tracy Zhou | c36633c | 2020-05-26 01:38:37 -0700 | [diff] [blame] | 110 | // The current selected action in the BottomActionBar, can be null when no action is selected. |
chihhangchuang | 90b4522 | 2020-06-17 01:55:29 +0800 | [diff] [blame] | 111 | @Nullable private BottomAction mSelectedAction; |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 112 | @Nullable private AccessibilityCallback mAccessibilityCallback; |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 113 | |
| 114 | public BottomActionBar(@NonNull Context context, @Nullable AttributeSet attrs) { |
| 115 | super(context, attrs); |
| 116 | LayoutInflater.from(context).inflate(R.layout.bottom_actions_layout, this, true); |
| 117 | |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 118 | mActionMap.put(BottomAction.ROTATION, findViewById(R.id.action_rotation)); |
| 119 | mActionMap.put(BottomAction.DELETE, findViewById(R.id.action_delete)); |
| 120 | mActionMap.put(BottomAction.INFORMATION, findViewById(R.id.action_information)); |
| 121 | mActionMap.put(BottomAction.EDIT, findViewById(R.id.action_edit)); |
chihhangchuang | 08abb58 | 2020-04-27 17:20:31 +0800 | [diff] [blame] | 122 | mActionMap.put(BottomAction.CUSTOMIZE, findViewById(R.id.action_customize)); |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 123 | mActionMap.put(BottomAction.DOWNLOAD, findViewById(R.id.action_download)); |
| 124 | mActionMap.put(BottomAction.PROGRESS, findViewById(R.id.action_progress)); |
| 125 | mActionMap.put(BottomAction.APPLY, findViewById(R.id.action_apply)); |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 126 | |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 127 | mBottomSheetView = findViewById(R.id.action_bottom_sheet); |
Santiago Etchebehere | 53c6343 | 2020-05-07 18:55:35 -0700 | [diff] [blame] | 128 | SizeCalculator.adjustBackgroundCornerRadius(mBottomSheetView); |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 129 | |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 130 | mBottomSheetBehavior = (QueueStateBottomSheetBehavior<ViewGroup>) BottomSheetBehavior.from( |
| 131 | mBottomSheetView); |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 132 | mBottomSheetBehavior.setState(STATE_COLLAPSED); |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 133 | mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetCallback() { |
| 134 | @Override |
| 135 | public void onStateChanged(@NonNull View bottomSheet, int newState) { |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 136 | if (mBottomSheetBehavior.isQueueProcessing()) { |
| 137 | // Avoid button and bottom sheet mismatching from quick tapping buttons when |
| 138 | // bottom sheet is changing state. |
| 139 | disableActions(); |
| 140 | // If bottom sheet is going with expanded-collapsed-expanded, the new content |
| 141 | // will be updated in collapsed state. The first state change from expanded to |
| 142 | // collapsed should still show the previous content view. |
| 143 | if (mSelectedAction != null && newState == STATE_COLLAPSED) { |
| 144 | updateContentViewFor(mSelectedAction); |
| 145 | } |
chihhangchuang | 34ba412 | 2020-05-04 11:25:48 +0800 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 149 | notifyAccessibilityCallback(newState); |
| 150 | |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 151 | // Enable all buttons when queue is not processing. |
| 152 | enableActions(); |
Chihhang Chuang | 85f099a | 2020-06-16 18:04:39 +0800 | [diff] [blame] | 153 | if (!isExpandable(mSelectedAction)) { |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 154 | return; |
| 155 | } |
Chihhang Chuang | 85f099a | 2020-06-16 18:04:39 +0800 | [diff] [blame] | 156 | // Ensure the button state is the same as bottom sheet state to catch up the state |
| 157 | // change from dragging or some unexpected bottom sheet state changes. |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 158 | if (newState == STATE_COLLAPSED) { |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 159 | updateSelectedState(mSelectedAction, /* selected= */ false); |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 160 | } else if (newState == STATE_EXPANDED) { |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 161 | updateSelectedState(mSelectedAction, /* selected= */ true); |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 162 | } |
| 163 | } |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 164 | @Override |
Tracy Zhou | c36633c | 2020-05-26 01:38:37 -0700 | [diff] [blame] | 165 | public void onSlide(@NonNull View bottomSheet, float slideOffset) { } |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 166 | }); |
Chuck Liao | ca97c57 | 2020-05-08 17:13:25 +0800 | [diff] [blame] | 167 | |
| 168 | setOnApplyWindowInsetsListener((v, windowInsets) -> { |
| 169 | v.setPadding(v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), |
| 170 | windowInsets.getSystemWindowInsetBottom()); |
| 171 | return windowInsets; |
| 172 | }); |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void onVisibilityAggregated(boolean isVisible) { |
| 177 | super.onVisibilityAggregated(isVisible); |
| 178 | if (!isVisible) { |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 179 | hideBottomSheetAndDeselectButtonIfExpanded(); |
| 180 | mBottomSheetBehavior.reset(); |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 181 | } |
Chuck Liao | 829d4d2 | 2020-05-07 17:00:10 +0800 | [diff] [blame] | 182 | mVisibilityChangeListeners.forEach(listener -> listener.onVisibilityChange(isVisible)); |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
Ching-Sung Li | e7d5f99 | 2020-05-05 22:14:51 +0800 | [diff] [blame] | 186 | * Adds content view to the bottom sheet and binds with a {@code BottomAction} to |
| 187 | * expand / collapse the bottom sheet. |
| 188 | * |
| 189 | * @param contentView the view with content to be added on the bottom sheet |
| 190 | * @param action the action to be bound to expand / collapse the bottom sheet |
| 191 | */ |
| 192 | public void attachViewToBottomSheetAndBindAction(View contentView, BottomAction action) { |
chihhangchuang | 7bef656 | 2020-05-14 21:19:20 +0800 | [diff] [blame] | 193 | contentView.setVisibility(GONE); |
chihhangchuang | fd5326f | 2020-06-09 14:14:00 +0800 | [diff] [blame] | 194 | contentView.setFocusable(true); |
Ching-Sung Li | e7d5f99 | 2020-05-05 22:14:51 +0800 | [diff] [blame] | 195 | mContentViewMap.put(action, contentView); |
| 196 | mBottomSheetView.addView(contentView); |
chihhangchuang | fd5326f | 2020-06-09 14:14:00 +0800 | [diff] [blame] | 197 | setActionClickListener(action, actionView -> { |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 198 | if (mBottomSheetBehavior.getState() == STATE_COLLAPSED) { |
| 199 | updateContentViewFor(action); |
| 200 | } |
chihhangchuang | fd5326f | 2020-06-09 14:14:00 +0800 | [diff] [blame] | 201 | mBottomSheetView.setAccessibilityTraversalAfter(actionView.getId()); |
Ching-Sung Li | e7d5f99 | 2020-05-05 22:14:51 +0800 | [diff] [blame] | 202 | }); |
| 203 | } |
| 204 | |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 205 | /** Collapses the bottom sheet. */ |
| 206 | public void collapseBottomSheetIfExpanded() { |
| 207 | hideBottomSheetAndDeselectButtonIfExpanded(); |
Ching-Sung Li | b940462 | 2020-05-18 22:38:45 +0800 | [diff] [blame] | 208 | } |
| 209 | |
Chihhang Chuang | 1e8bece | 2021-05-17 15:08:06 +0800 | [diff] [blame^] | 210 | /** Enables or disables action buttons that show the bottom sheet. */ |
| 211 | public void enableActionButtonsWithBottomSheet(boolean enabled) { |
| 212 | if (enabled) { |
| 213 | enableActions(mContentViewMap.keySet().toArray(new BottomAction[0])); |
| 214 | } else { |
| 215 | disableActions(mContentViewMap.keySet().toArray(new BottomAction[0])); |
| 216 | } |
| 217 | } |
| 218 | |
Ching-Sung Li | b940462 | 2020-05-18 22:38:45 +0800 | [diff] [blame] | 219 | /** |
Tracy Zhou | 16112e3 | 2020-05-27 13:13:23 -0700 | [diff] [blame] | 220 | * Sets a click listener to a specific action. |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 221 | * |
| 222 | * @param bottomAction the specific action |
| 223 | * @param actionClickListener the click listener for the action |
| 224 | */ |
| 225 | public void setActionClickListener( |
| 226 | BottomAction bottomAction, OnClickListener actionClickListener) { |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 227 | View buttonView = mActionMap.get(bottomAction); |
| 228 | if (buttonView.hasOnClickListeners()) { |
| 229 | throw new IllegalStateException( |
| 230 | "Had already set a click listener to button: " + bottomAction); |
| 231 | } |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 232 | buttonView.setOnClickListener(view -> { |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 233 | if (mSelectedAction != null && isActionSelected(mSelectedAction)) { |
Tracy Zhou | c36633c | 2020-05-26 01:38:37 -0700 | [diff] [blame] | 234 | updateSelectedState(mSelectedAction, /* selected= */ false); |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 235 | if (isExpandable(mSelectedAction)) { |
| 236 | mBottomSheetBehavior.enqueue(STATE_COLLAPSED); |
Tracy Zhou | c36633c | 2020-05-26 01:38:37 -0700 | [diff] [blame] | 237 | } |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 238 | } else { |
| 239 | // Error handling, set to null if the action is not selected. |
| 240 | mSelectedAction = null; |
Tracy Zhou | c36633c | 2020-05-26 01:38:37 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (bottomAction == mSelectedAction) { |
| 244 | // Deselect the selected action. |
| 245 | mSelectedAction = null; |
| 246 | } else { |
| 247 | // Select a different action from the current selected action. |
| 248 | mSelectedAction = bottomAction; |
| 249 | updateSelectedState(mSelectedAction, /* selected= */ true); |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 250 | if (isExpandable(mSelectedAction)) { |
| 251 | mBottomSheetBehavior.enqueue(STATE_EXPANDED); |
Tracy Zhou | c36633c | 2020-05-26 01:38:37 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 254 | actionClickListener.onClick(view); |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 255 | mBottomSheetBehavior.processQueueForStateChange(); |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 256 | }); |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 257 | } |
| 258 | |
Tracy Zhou | 16112e3 | 2020-05-27 13:13:23 -0700 | [diff] [blame] | 259 | /** |
| 260 | * Sets a selected listener to a specific action. This is triggered each time the bottom |
| 261 | * action's selected state changes. |
| 262 | * |
| 263 | * @param bottomAction the specific action |
| 264 | * @param actionSelectedListener the selected listener for the action |
| 265 | */ |
| 266 | public void setActionSelectedListener( |
| 267 | BottomAction bottomAction, OnActionSelectedListener actionSelectedListener) { |
| 268 | if (mActionSelectedListeners.containsKey(bottomAction)) { |
| 269 | throw new IllegalStateException( |
| 270 | "Had already set a selected listener to button: " + bottomAction); |
| 271 | } |
| 272 | mActionSelectedListeners.put(bottomAction, actionSelectedListener); |
| 273 | } |
| 274 | |
Kunhung Li | c670149 | 2021-01-28 17:35:56 +0800 | [diff] [blame] | 275 | /** Set back button visibility. */ |
| 276 | public void setBackButtonVisibility(int visibility) { |
| 277 | findViewById(R.id.action_back).setVisibility(visibility); |
| 278 | } |
| 279 | |
| 280 | /** Get back button visibility. */ |
| 281 | public int getBackButtonVisibility() { |
| 282 | return findViewById(R.id.action_back).getVisibility(); |
| 283 | } |
| 284 | |
chihhangchuang | 7feb375 | 2020-04-24 02:48:56 +0800 | [diff] [blame] | 285 | /** Binds the cancel button to back key. */ |
chihhangchuang | 08abb58 | 2020-04-27 17:20:31 +0800 | [diff] [blame] | 286 | public void bindBackButtonToSystemBackKey(Activity activity) { |
| 287 | findViewById(R.id.action_back).setOnClickListener(v -> activity.onBackPressed()); |
chihhangchuang | 7feb375 | 2020-04-24 02:48:56 +0800 | [diff] [blame] | 288 | } |
| 289 | |
chihhangchuang | 2aa79fa | 2020-04-15 19:29:30 +0800 | [diff] [blame] | 290 | /** Returns {@code true} if visible. */ |
| 291 | public boolean isVisible() { |
| 292 | return getVisibility() == VISIBLE; |
| 293 | } |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 294 | |
Chuck Liao | 4f05931 | 2020-03-13 23:09:46 +0800 | [diff] [blame] | 295 | /** Shows {@link BottomActionBar}. */ |
| 296 | public void show() { |
| 297 | setVisibility(VISIBLE); |
| 298 | } |
| 299 | |
| 300 | /** Hides {@link BottomActionBar}. */ |
| 301 | public void hide() { |
| 302 | setVisibility(GONE); |
| 303 | } |
| 304 | |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 305 | /** |
Chuck Liao | 829d4d2 | 2020-05-07 17:00:10 +0800 | [diff] [blame] | 306 | * Adds the visibility change listener. |
| 307 | * |
| 308 | * @param visibilityChangeListener the listener to be notified. |
| 309 | */ |
| 310 | public void addVisibilityChangeListener(VisibilityChangeListener visibilityChangeListener) { |
| 311 | if (visibilityChangeListener == null) { |
| 312 | return; |
| 313 | } |
| 314 | mVisibilityChangeListeners.add(visibilityChangeListener); |
| 315 | visibilityChangeListener.onVisibilityChange(isVisible()); |
| 316 | } |
| 317 | |
| 318 | /** |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 319 | * Sets a AccessibilityCallback. |
| 320 | * |
| 321 | * @param accessibilityCallback the callback to be notified. |
| 322 | */ |
| 323 | public void setAccessibilityCallback(@Nullable AccessibilityCallback accessibilityCallback) { |
| 324 | mAccessibilityCallback = accessibilityCallback; |
| 325 | } |
| 326 | |
| 327 | /** |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 328 | * Shows the specific actions. |
| 329 | * |
| 330 | * @param actions the specific actions |
| 331 | */ |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 332 | public void showActions(BottomAction... actions) { |
| 333 | for (BottomAction action : actions) { |
| 334 | mActionMap.get(action).setVisibility(VISIBLE); |
| 335 | } |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Hides the specific actions. |
| 340 | * |
| 341 | * @param actions the specific actions |
| 342 | */ |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 343 | public void hideActions(BottomAction... actions) { |
| 344 | for (BottomAction action : actions) { |
| 345 | mActionMap.get(action).setVisibility(GONE); |
| 346 | |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 347 | if (isExpandable(action) && mSelectedAction == action) { |
| 348 | hideBottomSheetAndDeselectButtonIfExpanded(); |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 349 | } |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 350 | } |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Shows the specific actions only. In other words, the other actions will be hidden. |
| 355 | * |
| 356 | * @param actions the specific actions which will be shown. Others will be hidden. |
| 357 | */ |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 358 | public void showActionsOnly(BottomAction... actions) { |
| 359 | final Set<BottomAction> actionsSet = new HashSet<>(Arrays.asList(actions)); |
| 360 | |
chihhangchuang | 90b4522 | 2020-06-17 01:55:29 +0800 | [diff] [blame] | 361 | mActionMap.keySet().forEach(action -> { |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 362 | if (actionsSet.contains(action)) { |
| 363 | showActions(action); |
| 364 | } else { |
| 365 | hideActions(action); |
| 366 | } |
| 367 | }); |
| 368 | } |
| 369 | |
| 370 | /** |
Chuck Liao | 58e4a1c | 2020-05-22 11:35:35 +0800 | [diff] [blame] | 371 | * Checks if the specific actions are shown. |
| 372 | * |
| 373 | * @param actions the specific actions to be verified |
| 374 | * @return {@code true} if the actions are shown; {@code false} otherwise |
| 375 | */ |
| 376 | public boolean areActionsShown(BottomAction... actions) { |
| 377 | final Set<BottomAction> actionsSet = new HashSet<>(Arrays.asList(actions)); |
| 378 | return actionsSet.stream().allMatch(bottomAction -> { |
| 379 | View view = mActionMap.get(bottomAction); |
| 380 | return view != null && view.getVisibility() == VISIBLE; |
| 381 | }); |
| 382 | } |
| 383 | |
| 384 | /** |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 385 | * All actions will be hidden. |
| 386 | */ |
| 387 | public void hideAllActions() { |
| 388 | showActionsOnly(/* No actions to show */); |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 389 | } |
| 390 | |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 391 | /** Enables all the actions' {@link View}. */ |
| 392 | public void enableActions() { |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 393 | enableActions(BottomAction.values()); |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /** Disables all the actions' {@link View}. */ |
| 397 | public void disableActions() { |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 398 | disableActions(BottomAction.values()); |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 399 | } |
| 400 | |
Ching-Sung Li | 073812b | 2020-04-07 21:19:21 +0800 | [diff] [blame] | 401 | /** |
| 402 | * Enables specified actions' {@link View}. |
| 403 | * |
| 404 | * @param actions the specified actions to enable their views |
| 405 | */ |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 406 | public void enableActions(BottomAction... actions) { |
| 407 | for (BottomAction action : actions) { |
| 408 | mActionMap.get(action).setEnabled(true); |
| 409 | } |
Ching-Sung Li | 073812b | 2020-04-07 21:19:21 +0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Disables specified actions' {@link View}. |
| 414 | * |
| 415 | * @param actions the specified actions to disable their views |
| 416 | */ |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 417 | public void disableActions(BottomAction... actions) { |
| 418 | for (BottomAction action : actions) { |
| 419 | mActionMap.get(action).setEnabled(false); |
| 420 | } |
Ching-Sung Li | 073812b | 2020-04-07 21:19:21 +0800 | [diff] [blame] | 421 | } |
| 422 | |
Chihhang Chuang | ca7c825 | 2020-06-17 17:32:46 +0800 | [diff] [blame] | 423 | /** Sets a default selected action button. */ |
| 424 | public void setDefaultSelectedButton(BottomAction action) { |
| 425 | if (mSelectedAction == null) { |
| 426 | mSelectedAction = action; |
| 427 | updateSelectedState(mSelectedAction, /* selected= */ true); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /** Deselects an action button. */ |
| 432 | public void deselectAction(BottomAction action) { |
| 433 | if (isExpandable(action)) { |
| 434 | mBottomSheetBehavior.setState(STATE_COLLAPSED); |
| 435 | } |
| 436 | updateSelectedState(action, /* selected= */ false); |
| 437 | if (action == mSelectedAction) { |
| 438 | mSelectedAction = null; |
| 439 | } |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 442 | public boolean isActionSelected(BottomAction action) { |
| 443 | return mActionMap.get(action).isSelected(); |
| 444 | } |
| 445 | |
Chihhang Chuang | 1e8bece | 2021-05-17 15:08:06 +0800 | [diff] [blame^] | 446 | /** Returns {@code true} if the state of bottom sheet is collapsed. */ |
| 447 | public boolean isBottomSheetCollapsed() { |
| 448 | return mBottomSheetBehavior.getState() == STATE_COLLAPSED; |
| 449 | } |
| 450 | |
chihhangchuang | 90b4522 | 2020-06-17 01:55:29 +0800 | [diff] [blame] | 451 | /** Resets {@link BottomActionBar} to initial state. */ |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 452 | public void reset() { |
chihhangchuang | 90b4522 | 2020-06-17 01:55:29 +0800 | [diff] [blame] | 453 | // Not visible by default, see res/layout/bottom_action_bar.xml |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 454 | hide(); |
chihhangchuang | 90b4522 | 2020-06-17 01:55:29 +0800 | [diff] [blame] | 455 | // All actions are hide and enabled by default, see res/layout/bottom_action_bar.xml |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 456 | hideAllActions(); |
chihhangchuang | 803ea9a | 2020-04-21 13:03:10 +0800 | [diff] [blame] | 457 | enableActions(); |
chihhangchuang | 90b4522 | 2020-06-17 01:55:29 +0800 | [diff] [blame] | 458 | // Clears all the actions' click listeners |
| 459 | mActionMap.values().forEach(v -> v.setOnClickListener(null)); |
| 460 | findViewById(R.id.action_back).setOnClickListener(null); |
| 461 | // Deselect all buttons. |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 462 | mActionMap.keySet().forEach(a -> updateSelectedState(a, /* selected= */ false)); |
chihhangchuang | 90b4522 | 2020-06-17 01:55:29 +0800 | [diff] [blame] | 463 | // Clear values. |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 464 | mContentViewMap.clear(); |
chihhangchuang | 90b4522 | 2020-06-17 01:55:29 +0800 | [diff] [blame] | 465 | mActionSelectedListeners.clear(); |
| 466 | mBottomSheetView.removeAllViews(); |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 467 | mBottomSheetBehavior.reset(); |
| 468 | mSelectedAction = null; |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 469 | } |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 470 | |
Tracy Zhou | 16112e3 | 2020-05-27 13:13:23 -0700 | [diff] [blame] | 471 | private void updateSelectedState(BottomAction bottomAction, boolean selected) { |
| 472 | View bottomActionView = mActionMap.get(bottomAction); |
| 473 | if (bottomActionView.isSelected() == selected) { |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | OnActionSelectedListener listener = mActionSelectedListeners.get(bottomAction); |
| 478 | if (listener != null) { |
| 479 | listener.onActionSelected(selected); |
| 480 | } |
| 481 | bottomActionView.setSelected(selected); |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 482 | } |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 483 | |
| 484 | private void hideBottomSheetAndDeselectButtonIfExpanded() { |
| 485 | if (isExpandable(mSelectedAction) && mBottomSheetBehavior.getState() == STATE_EXPANDED) { |
| 486 | mBottomSheetBehavior.setState(STATE_COLLAPSED); |
| 487 | updateSelectedState(mSelectedAction, /* selected= */ false); |
| 488 | mSelectedAction = null; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | private void updateContentViewFor(BottomAction action) { |
| 493 | mContentViewMap.forEach((a, v) -> v.setVisibility(a.equals(action) ? VISIBLE : GONE)); |
| 494 | } |
| 495 | |
| 496 | private boolean isExpandable(BottomAction action) { |
| 497 | return action != null && mContentViewMap.containsKey(action); |
| 498 | } |
| 499 | |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 500 | private void notifyAccessibilityCallback(int state) { |
chihhangchuang | af7e49d | 2020-06-24 22:46:04 +0800 | [diff] [blame] | 501 | if (mAccessibilityCallback == null) { |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 502 | return; |
| 503 | } |
| 504 | |
| 505 | if (state == STATE_COLLAPSED) { |
| 506 | mAccessibilityCallback.onBottomSheetCollapsed(); |
| 507 | } else if (state == STATE_EXPANDED) { |
| 508 | mAccessibilityCallback.onBottomSheetExpanded(); |
| 509 | } |
| 510 | } |
| 511 | |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 512 | /** A {@link BottomSheetBehavior} that can process a queue of bottom sheet states.*/ |
| 513 | public static class QueueStateBottomSheetBehavior<V extends View> |
| 514 | extends BottomSheetBehavior<V> { |
| 515 | |
| 516 | private final Deque<Integer> mStateQueue = new ArrayDeque<>(); |
| 517 | private boolean mIsQueueProcessing; |
| 518 | |
| 519 | public QueueStateBottomSheetBehavior(Context context, @Nullable AttributeSet attrs) { |
| 520 | super(context, attrs); |
| 521 | // Binds the default callback for processing queue. |
| 522 | setBottomSheetCallback(null); |
| 523 | } |
| 524 | |
| 525 | /** Enqueues the bottom sheet states. */ |
| 526 | public void enqueue(int state) { |
| 527 | if (!mStateQueue.isEmpty() && mStateQueue.getLast() == state) { |
| 528 | return; |
| 529 | } |
| 530 | mStateQueue.add(state); |
| 531 | } |
| 532 | |
| 533 | /** Processes the queue of bottom sheet state that was set via {@link #enqueue}. */ |
| 534 | public void processQueueForStateChange() { |
| 535 | if (mStateQueue.isEmpty()) { |
| 536 | return; |
| 537 | } |
| 538 | setState(mStateQueue.getFirst()); |
| 539 | mIsQueueProcessing = true; |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Returns {@code true} if the queue is processing. For example, if the bottom sheet is |
| 544 | * going with expanded-collapsed-expanded, it would return {@code true} until last expanded |
Chihhang Chuang | 85f099a | 2020-06-16 18:04:39 +0800 | [diff] [blame] | 545 | * state is finished. |
| 546 | */ |
Chihhang Chuang | 541e075 | 2020-06-11 19:14:40 +0800 | [diff] [blame] | 547 | public boolean isQueueProcessing() { |
| 548 | return mIsQueueProcessing; |
| 549 | } |
| 550 | |
| 551 | /** Resets the queue state. */ |
| 552 | public void reset() { |
| 553 | mStateQueue.clear(); |
| 554 | mIsQueueProcessing = false; |
| 555 | } |
| 556 | |
| 557 | @Override |
| 558 | public void setBottomSheetCallback(BottomSheetCallback callback) { |
| 559 | super.setBottomSheetCallback(new BottomSheetCallback() { |
| 560 | @Override |
| 561 | public void onStateChanged(@NonNull View bottomSheet, int newState) { |
| 562 | if (!mStateQueue.isEmpty()) { |
| 563 | if (newState == mStateQueue.getFirst()) { |
| 564 | mStateQueue.removeFirst(); |
| 565 | if (mStateQueue.isEmpty()) { |
| 566 | mIsQueueProcessing = false; |
| 567 | } else { |
| 568 | setState(mStateQueue.getFirst()); |
| 569 | } |
| 570 | } else { |
| 571 | setState(mStateQueue.getFirst()); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (callback != null) { |
| 576 | callback.onStateChanged(bottomSheet, newState); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public void onSlide(@NonNull View bottomSheet, float slideOffset) { |
| 582 | if (callback != null) { |
| 583 | callback.onSlide(bottomSheet, slideOffset); |
| 584 | } |
| 585 | } |
| 586 | }); |
| 587 | } |
| 588 | } |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 589 | } |