1 /* 2 * Copyright (C) 2015 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.allapps; 17 18 import android.content.Context; 19 import android.os.Process; 20 import android.os.UserHandle; 21 22 import com.android.launcher3.model.data.AppInfo; 23 import com.android.launcher3.pm.UserCache; 24 import com.android.launcher3.util.LabelComparator; 25 26 import java.util.Comparator; 27 28 /** 29 * A comparator to arrange items based on user profiles. 30 */ 31 public class AppInfoComparator implements Comparator<AppInfo> { 32 33 private final UserCache mUserManager; 34 private final UserHandle mMyUser; 35 private final LabelComparator mLabelComparator; 36 AppInfoComparator(Context context)37 public AppInfoComparator(Context context) { 38 mUserManager = UserCache.INSTANCE.get(context); 39 mMyUser = Process.myUserHandle(); 40 mLabelComparator = new LabelComparator(); 41 } 42 43 @Override compare(AppInfo a, AppInfo b)44 public int compare(AppInfo a, AppInfo b) { 45 // Order by the title in the current locale 46 int result = mLabelComparator.compare(a.title.toString(), b.title.toString()); 47 if (result != 0) { 48 return result; 49 } 50 51 // If labels are same, compare component names 52 result = a.componentName.compareTo(b.componentName); 53 if (result != 0) { 54 return result; 55 } 56 57 if (mMyUser.equals(a.user)) { 58 return -1; 59 } else { 60 Long aUserSerial = mUserManager.getSerialNumberForUser(a.user); 61 Long bUserSerial = mUserManager.getSerialNumberForUser(b.user); 62 return aUserSerial.compareTo(bUserSerial); 63 } 64 } 65 } 66