Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.util.AttributeSet; |
| 5 | import android.widget.FrameLayout; |
| 6 | |
| 7 | import java.util.ArrayList; |
| 8 | import java.util.LinkedHashMap; |
| 9 | import java.util.List; |
| 10 | import java.util.Map; |
| 11 | |
| 12 | |
| 13 | class SectionedWidgetsRow { |
| 14 | String section; |
| 15 | List<List<Object>> widgets; |
| 16 | |
| 17 | public SectionedWidgetsRow(String sc) { |
| 18 | section = sc; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | class SectionedWidgetsAlgorithm { |
| 23 | public List<SectionedWidgetsRow> computeSectionedWidgetRows(List<Object> sortedWidgets, |
| 24 | int widgetsPerRow) { |
| 25 | List<SectionedWidgetsRow> rows = new ArrayList<>(); |
| 26 | LinkedHashMap<String, List<Object>> sections = computeSectionedApps(sortedWidgets); |
| 27 | for (Map.Entry<String, List<Object>> sectionEntry : sections.entrySet()) { |
| 28 | String section = sectionEntry.getKey(); |
| 29 | SectionedWidgetsRow row = new SectionedWidgetsRow(section); |
| 30 | List<Object> widgets = sectionEntry.getValue(); |
| 31 | int numRows = (int) Math.ceil((float) widgets.size() / widgetsPerRow); |
| 32 | for (int i = 0; i < numRows; i++) { |
| 33 | List<Object> widgetsInRow = new ArrayList<>(); |
| 34 | int offset = i * widgetsPerRow; |
| 35 | for (int j = 0; j < widgetsPerRow; j++) { |
| 36 | widgetsInRow.add(widgets.get(offset + j)); |
| 37 | } |
| 38 | row.widgets.add(widgetsInRow); |
| 39 | } |
| 40 | } |
| 41 | return rows; |
| 42 | } |
| 43 | |
| 44 | private LinkedHashMap<String, List<Object>> computeSectionedApps(List<Object> sortedWidgets) { |
| 45 | LinkedHashMap<String, List<Object>> sections = new LinkedHashMap<>(); |
| 46 | for (Object info : sortedWidgets) { |
| 47 | String section = getSection(info); |
| 48 | List<Object> sectionedWidgets = sections.get(section); |
| 49 | if (sectionedWidgets == null) { |
| 50 | sectionedWidgets = new ArrayList<>(); |
| 51 | sections.put(section, sectionedWidgets); |
| 52 | } |
| 53 | sectionedWidgets.add(info); |
| 54 | } |
| 55 | return sections; |
| 56 | } |
| 57 | |
| 58 | private String getSection(Object widgetOrShortcut) { |
| 59 | return "UNKNOWN"; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * The widgets list view container. |
| 65 | */ |
| 66 | public class WidgetsContainerView extends FrameLayout { |
| 67 | |
| 68 | |
| 69 | public WidgetsContainerView(Context context) { |
| 70 | this(context, null); |
| 71 | } |
| 72 | |
| 73 | public WidgetsContainerView(Context context, AttributeSet attrs) { |
| 74 | this(context, attrs, 0); |
| 75 | } |
| 76 | |
| 77 | public WidgetsContainerView(Context context, AttributeSet attrs, int defStyleAttr) { |
Winson Chung | 3d9490a | 2015-03-24 17:38:42 -0700 | [diff] [blame] | 78 | super(context, attrs, defStyleAttr); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected void onFinishInflate() { |
| 83 | } |
| 84 | } |