1/*
2 * Copyright (C) 2017 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
17package com.android.launcher3.folder;
18
19import com.android.launcher3.FolderInfo;
20import com.android.launcher3.InvariantDeviceProfile;
21import com.android.launcher3.config.FeatureFlags;
22
23/**
24 * Verifies whether an item in a Folder is displayed in the FolderIcon preview.
25 */
26public class FolderIconPreviewVerifier {
27
28    private final int mMaxGridCountX;
29    private final int mMaxGridCountY;
30    private final int mMaxItemsPerPage;
31    private final int[] mGridSize = new int[2];
32
33    private int mGridCountX;
34    private boolean mDisplayingUpperLeftQuadrant = false;
35
36    public FolderIconPreviewVerifier(InvariantDeviceProfile profile) {
37        mMaxGridCountX = profile.numFolderColumns;
38        mMaxGridCountY = profile.numFolderRows;
39        mMaxItemsPerPage = mMaxGridCountX * mMaxGridCountY;
40    }
41
42    public void setFolderInfo(FolderInfo info) {
43        int numItemsInFolder = info.contents.size();
44        FolderPagedView.calculateGridSize(numItemsInFolder, 0, 0, mMaxGridCountX,
45                mMaxGridCountY, mMaxItemsPerPage, mGridSize);
46        mGridCountX = mGridSize[0];
47
48        mDisplayingUpperLeftQuadrant = FeatureFlags.LAUNCHER3_NEW_FOLDER_ANIMATION
49                && !FeatureFlags.LAUNCHER3_LEGACY_FOLDER_ICON
50                && numItemsInFolder > FolderIcon.NUM_ITEMS_IN_PREVIEW;
51    }
52
53    /**
54     * Returns whether the item with {@param rank} is in the default Folder icon preview.
55     */
56    public boolean isItemInPreview(int rank) {
57        return isItemInPreview(0, rank);
58    }
59
60    /**
61     * @param page The page the item is on.
62     * @param rank The rank of the item.
63     * @return True iff the icon is in the 2x2 upper left quadrant of the Folder.
64     */
65    public boolean isItemInPreview(int page, int rank) {
66        // First page items are laid out such that the first 4 items are always in the upper
67        // left quadrant. For all other pages, we need to check the row and col.
68        if (page > 0 || mDisplayingUpperLeftQuadrant) {
69            int col = rank % mGridCountX;
70            int row = rank / mGridCountX;
71            return col < 2 && row < 2;
72        }
73        return rank < FolderIcon.NUM_ITEMS_IN_PREVIEW;
74    }
75}
76