summaryrefslogtreecommitdiff
path: root/src/com/android/launcher3/widget/picker/WidgetsListHeader.java
blob: 932e06d57b7a0a5530dfbd7ebbc09cbaf8a84239 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright (C) 2021 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.launcher3.widget.picker;

import static com.android.launcher3.widget.WidgetSections.NO_CATEGORY;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.UiThread;

import com.android.launcher3.DeviceProfile;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.R;
import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
import com.android.launcher3.icons.PlaceHolderIconDrawable;
import com.android.launcher3.icons.cache.HandlerRunnable;
import com.android.launcher3.model.data.ItemInfoWithIcon;
import com.android.launcher3.model.data.PackageItemInfo;
import com.android.launcher3.util.PluralMessageFormat;
import com.android.launcher3.views.ActivityContext;
import com.android.launcher3.widget.WidgetSections;
import com.android.launcher3.widget.WidgetSections.WidgetSection;
import com.android.launcher3.widget.model.WidgetsListHeaderEntry;
import com.android.launcher3.widget.model.WidgetsListSearchHeaderEntry;

import java.util.stream.Collectors;

/**
 * A UI represents a header of an app shown in the full widgets tray.
 *
 * It is a {@link LinearLayout} which contains an app icon, an app name, a subtitle and a checkbox
 * which indicates if the widgets content view underneath this header should be shown.
 */
public final class WidgetsListHeader extends LinearLayout implements ItemInfoUpdateReceiver {

    private boolean mEnableIconUpdateAnimation = false;

    @Nullable private HandlerRunnable mIconLoadRequest;
    @Nullable private Drawable mIconDrawable;
    private final int mIconSize;

    private ImageView mAppIcon;
    private TextView mTitle;
    private TextView mSubtitle;

    private CheckBox mExpandToggle;
    private boolean mIsExpanded = false;
    @Nullable private WidgetsListDrawableState mListDrawableState;

    public WidgetsListHeader(Context context) {
        this(context, /* attrs= */ null);
    }

    public WidgetsListHeader(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, /* defStyle= */ 0);
    }

    public WidgetsListHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        ActivityContext activity = ActivityContext.lookupContext(context);
        DeviceProfile grid = activity.getDeviceProfile();
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.WidgetsListRowHeader, defStyleAttr, /* defStyleRes= */ 0);
        mIconSize = a.getDimensionPixelSize(R.styleable.WidgetsListRowHeader_appIconSize,
                grid.iconSizePx);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mAppIcon = findViewById(R.id.app_icon);
        mTitle = findViewById(R.id.app_title);
        mSubtitle = findViewById(R.id.app_subtitle);
        mExpandToggle = findViewById(R.id.toggle);
        findViewById(R.id.app_container).setAccessibilityDelegate(new AccessibilityDelegate() {

            @Override
            public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
                if (mIsExpanded) {
                    info.removeAction(AccessibilityNodeInfo.ACTION_EXPAND);
                    info.addAction(AccessibilityNodeInfo.ACTION_COLLAPSE);
                } else {
                    info.removeAction(AccessibilityNodeInfo.ACTION_COLLAPSE);
                    info.addAction(AccessibilityNodeInfo.ACTION_EXPAND);
                }
                super.onInitializeAccessibilityNodeInfo(host, info);
            }

            @Override
            public boolean performAccessibilityAction(View host, int action, Bundle args) {
                switch (action) {
                    case AccessibilityNodeInfo.ACTION_EXPAND:
                    case AccessibilityNodeInfo.ACTION_COLLAPSE:
                        callOnClick();
                        return true;
                    default:
                        return super.performAccessibilityAction(host, action, args);
                }
            }
        });
    }

    /**
     * Sets a {@link OnExpansionChangeListener} to get a callback when this app widgets section
     * expands / collapses.
     */
    @UiThread
    public void setOnExpandChangeListener(
            @Nullable OnExpansionChangeListener onExpandChangeListener) {
        // Use the entire touch area of this view to expand / collapse an app widgets section.
        setOnClickListener(view -> {
            setExpanded(!mIsExpanded);
            if (onExpandChangeListener != null) {
                onExpandChangeListener.onExpansionChange(mIsExpanded);
            }
        });
    }

    /** Sets the expand toggle to expand / collapse. */
    @UiThread
    public void setExpanded(boolean isExpanded) {
        this.mIsExpanded = isExpanded;
        mExpandToggle.setChecked(isExpanded);
    }

    /** Sets the {@link WidgetsListDrawableState} and refreshes the background drawable. */
    @UiThread
    public void setListDrawableState(WidgetsListDrawableState state) {
        if (state == mListDrawableState) return;
        this.mListDrawableState = state;
        refreshDrawableState();
    }

    /** Apply app icon, labels and tag using a generic {@link WidgetsListHeaderEntry}. */
    @UiThread
    public void applyFromItemInfoWithIcon(WidgetsListHeaderEntry entry) {
        applyIconAndLabel(entry);
    }

    @UiThread
    private void applyIconAndLabel(WidgetsListHeaderEntry entry) {
        PackageItemInfo info = entry.mPkgItem;
        setIcon(info);
        setTitles(entry);
        setExpanded(entry.isWidgetListShown());

        super.setTag(info);

        verifyHighRes();
    }

    private void setIcon(PackageItemInfo info) {
        Drawable icon;
        if (info.widgetCategory == NO_CATEGORY) {
            icon = info.newIcon(getContext());
        } else {
            WidgetSection widgetSection = WidgetSections.getWidgetSections(getContext())
                    .get(info.widgetCategory);
            icon = getContext().getDrawable(widgetSection.mSectionDrawable);
        }
        applyDrawables(icon);
        mIconDrawable = icon;
        if (mIconDrawable != null) {
            mIconDrawable.setVisible(
                    /* visible= */ getWindowVisibility() == VISIBLE && isShown(),
                    /* restart= */ false);
        }
    }

    private void applyDrawables(Drawable icon) {
        icon.setBounds(0, 0, mIconSize, mIconSize);

        LinearLayout.LayoutParams layoutParams =
                (LinearLayout.LayoutParams) mAppIcon.getLayoutParams();
        layoutParams.width = mIconSize;
        layoutParams.height = mIconSize;
        mAppIcon.setLayoutParams(layoutParams);
        mAppIcon.setImageDrawable(icon);

        // If the current icon is a placeholder color, animate its update.
        if (mIconDrawable != null
                && mIconDrawable instanceof PlaceHolderIconDrawable
                && mEnableIconUpdateAnimation) {
            ((PlaceHolderIconDrawable) mIconDrawable).animateIconUpdate(icon);
        }
    }

    private void setTitles(WidgetsListHeaderEntry entry) {
        mTitle.setText(entry.mPkgItem.title);

        Resources resources = getContext().getResources();
        if (entry.widgetsCount == 0 && entry.shortcutsCount == 0) {
            mSubtitle.setVisibility(GONE);
            return;
        }

        String subtitle;
        if (entry.widgetsCount > 0 && entry.shortcutsCount > 0) {
            String widgetsCount = PluralMessageFormat.getIcuPluralString(getContext(),
                    R.string.widgets_count, entry.widgetsCount);
            String shortcutsCount = PluralMessageFormat.getIcuPluralString(getContext(),
                    R.string.shortcuts_count, entry.shortcutsCount);
            subtitle = resources.getString(R.string.widgets_and_shortcuts_count, widgetsCount,
                    shortcutsCount);
        } else if (entry.widgetsCount > 0) {
            subtitle = PluralMessageFormat.getIcuPluralString(getContext(),
                    R.string.widgets_count, entry.widgetsCount);
        } else {
            subtitle = PluralMessageFormat.getIcuPluralString(getContext(),
                    R.string.shortcuts_count, entry.shortcutsCount);
        }
        mSubtitle.setText(subtitle);
        mSubtitle.setVisibility(VISIBLE);
    }

    /** Apply app icon, labels and tag using a generic {@link WidgetsListSearchHeaderEntry}. */
    @UiThread
    public void applyFromItemInfoWithIcon(WidgetsListSearchHeaderEntry entry) {
        applyIconAndLabel(entry);
    }

    @UiThread
    private void applyIconAndLabel(WidgetsListSearchHeaderEntry entry) {
        PackageItemInfo info = entry.mPkgItem;
        setIcon(info);
        setTitles(entry);
        setExpanded(entry.isWidgetListShown());

        super.setTag(info);

        verifyHighRes();
    }

    private void setTitles(WidgetsListSearchHeaderEntry entry) {
        mTitle.setText(entry.mPkgItem.title);

        mSubtitle.setText(entry.mWidgets.stream()
                .map(item -> item.label).sorted().collect(Collectors.joining(", ")));
        mSubtitle.setVisibility(VISIBLE);
    }

    @Override
    public void reapplyItemInfo(ItemInfoWithIcon info) {
        if (getTag() == info) {
            mIconLoadRequest = null;
            mEnableIconUpdateAnimation = true;

            // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
            info.bitmap.icon.prepareToDraw();

            setIcon((PackageItemInfo) info);

            mEnableIconUpdateAnimation = false;
        }
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        if (mListDrawableState == null) return super.onCreateDrawableState(extraSpace);
        // Augment the state set from the super implementation with the custom states from
        // mListDrawableState.
        int[] drawableState =
                super.onCreateDrawableState(extraSpace + mListDrawableState.mStateSet.length);
        mergeDrawableStates(drawableState, mListDrawableState.mStateSet);
        return drawableState;
    }

    /** Verifies that the current icon is high-res otherwise posts a request to load the icon. */
    public void verifyHighRes() {
        if (mIconLoadRequest != null) {
            mIconLoadRequest.cancel();
            mIconLoadRequest = null;
        }
        if (getTag() instanceof ItemInfoWithIcon) {
            ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
            if (info.usingLowResIcon()) {
                mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
                        .updateIconInBackground(this, info);
            }
        }
    }

    /** A listener for the widget section expansion / collapse events. */
    public interface OnExpansionChangeListener {
        /** Notifies that the widget section is expanded or collapsed. */
        void onExpansionChange(boolean isExpanded);
    }
}