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.launcher3.widget.picker;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.widget.TableLayout;
21 
22 import androidx.annotation.Nullable;
23 import androidx.annotation.UiThread;
24 
25 /**
26  * Extension of {@link TableLayout} to support the drawable states used by
27  * {@link WidgetsListDrawableState}.
28  */
29 public class WidgetsListTableView extends TableLayout {
30 
31     @Nullable private WidgetsListDrawableState mListDrawableState;
32 
WidgetsListTableView(Context context)33     public WidgetsListTableView(Context context) {
34         super(context);
35     }
36 
WidgetsListTableView(Context context, AttributeSet attrs)37     public WidgetsListTableView(Context context, AttributeSet attrs) {
38         super(context, attrs);
39     }
40 
41     /** Sets the {@link WidgetsListDrawableState} and refreshes the background drawable. */
42     @UiThread
setListDrawableState(WidgetsListDrawableState state)43     public void setListDrawableState(WidgetsListDrawableState state) {
44         if (state == mListDrawableState) return;
45         mListDrawableState = state;
46         refreshDrawableState();
47     }
48 
49     @Override
onCreateDrawableState(int extraSpace)50     protected int[] onCreateDrawableState(int extraSpace) {
51         if (mListDrawableState == null) return super.onCreateDrawableState(extraSpace);
52         // Augment the state set from the super implementation with the custom states from
53         // mListDrawableState.
54         int[] drawableState =
55                 super.onCreateDrawableState(extraSpace + mListDrawableState.mStateSet.length);
56         mergeDrawableStates(drawableState, mListDrawableState.mStateSet);
57         return drawableState;
58     }
59 }
60