• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.launcher3.folder;
18 
19 import android.util.Log;
20 
21 import com.android.launcher3.FolderInfo;
22 import com.android.launcher3.InvariantDeviceProfile;
23 
24 import static com.android.launcher3.folder.ClippedFolderIconLayoutRule.MAX_NUM_ITEMS_IN_PREVIEW;
25 
26 /**
27  * Verifies whether an item in a Folder is displayed in the FolderIcon preview.
28  */
29 public class FolderIconPreviewVerifier {
30 
31     private static final String TAG = "FolderPreviewVerifier";
32 
33     private final int mMaxGridCountX;
34     private final int mMaxGridCountY;
35     private final int mMaxItemsPerPage;
36     private final int[] mGridSize = new int[] { 1, 1 };
37 
38     private int mNumItemsInFolder;
39     private int mGridCountX;
40     private boolean mDisplayingUpperLeftQuadrant = false;
41 
42     /**
43      * Note: must call {@link #setFolderInfo(FolderInfo)} manually for verifier to work.
44      */
FolderIconPreviewVerifier(InvariantDeviceProfile profile)45     public FolderIconPreviewVerifier(InvariantDeviceProfile profile) {
46         mMaxGridCountX = profile.numFolderColumns;
47         mMaxGridCountY = profile.numFolderRows;
48         mMaxItemsPerPage = mMaxGridCountX * mMaxGridCountY;
49     }
50 
setFolderInfo(FolderInfo info)51     public void setFolderInfo(FolderInfo info) {
52         int numItemsInFolder = info.contents.size();
53         if (numItemsInFolder != mNumItemsInFolder) {
54             FolderPagedView.calculateGridSize(numItemsInFolder, 0, 0, mMaxGridCountX,
55                     mMaxGridCountY, mMaxItemsPerPage, mGridSize);
56             mGridCountX = mGridSize[0];
57 
58             mDisplayingUpperLeftQuadrant = numItemsInFolder > MAX_NUM_ITEMS_IN_PREVIEW;
59             mNumItemsInFolder = numItemsInFolder;
60         }
61     }
62 
63     /**
64      * Returns whether the item with {@param rank} is in the default Folder icon preview.
65      */
isItemInPreview(int rank)66     public boolean isItemInPreview(int rank) {
67         return isItemInPreview(0, rank);
68     }
69 
70     /**
71      * @param page The page the item is on.
72      * @param rank The rank of the item.
73      * @return True iff the icon is in the 2x2 upper left quadrant of the Folder.
74      */
isItemInPreview(int page, int rank)75     public boolean isItemInPreview(int page, int rank) {
76         if (mGridSize[0] == 1) {
77             Log.w(TAG, "setFolderInfo not called before checking if item is in preview.");
78         }
79 
80         // First page items are laid out such that the first 4 items are always in the upper
81         // left quadrant. For all other pages, we need to check the row and col.
82         if (page > 0 || mDisplayingUpperLeftQuadrant) {
83             int col = rank % mGridCountX;
84             int row = rank / mGridCountX;
85             return col < 2 && row < 2;
86         }
87         return rank < MAX_NUM_ITEMS_IN_PREVIEW;
88     }
89 }
90