1 /* 2 * Copyright (C) 2016 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; 17 18 import android.os.Process; 19 import android.os.UserHandle; 20 21 import com.android.launcher3.model.WidgetItem; 22 23 import java.text.Collator; 24 import java.util.Comparator; 25 26 /** 27 * Comparator for sorting WidgetItem based on their user, title and size. 28 */ 29 public class WidgetItemComparator implements Comparator<WidgetItem> { 30 31 private final UserHandle mMyUserHandle = Process.myUserHandle(); 32 private final Collator mCollator = Collator.getInstance(); 33 34 @Override compare(WidgetItem a, WidgetItem b)35 public int compare(WidgetItem a, WidgetItem b) { 36 // Independent of how the labels compare, if only one of the two widget info belongs to 37 // work profile, put that one in the back. 38 boolean thisWorkProfile = !mMyUserHandle.equals(a.user); 39 boolean otherWorkProfile = !mMyUserHandle.equals(b.user); 40 if (thisWorkProfile ^ otherWorkProfile) { 41 return thisWorkProfile ? 1 : -1; 42 } 43 44 int labelCompare = mCollator.compare(a.label, b.label); 45 if (labelCompare != 0) { 46 return labelCompare; 47 } 48 49 // If the label is same, put the smaller widget before the larger widget. If the area is 50 // also same, put the widget with smaller height before. 51 int thisArea = a.spanX * a.spanY; 52 int otherArea = b.spanX * b.spanY; 53 return thisArea == otherArea 54 ? Integer.compare(a.spanY, b.spanY) 55 : Integer.compare(thisArea, otherArea); 56 } 57 } 58