• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.text.TextUtils;
22 
23 import com.android.launcher3.model.data.AppInfo;
24 import com.android.launcher3.pm.UserCache;
25 import com.android.launcher3.util.LabelComparator;
26 
27 import java.util.Comparator;
28 
29 /**
30  * A comparator to arrange items based on user profiles.
31  */
32 public class AppInfoComparator implements Comparator<AppInfo> {
33 
34     private final UserCache mUserManager;
35     private final UserHandle mMyUser;
36     private final LabelComparator mLabelComparator;
37 
AppInfoComparator(Context context)38     public AppInfoComparator(Context context) {
39         mUserManager = UserCache.INSTANCE.get(context);
40         mMyUser = Process.myUserHandle();
41         mLabelComparator = new LabelComparator();
42     }
43 
44     @Override
compare(AppInfo a, AppInfo b)45     public int compare(AppInfo a, AppInfo b) {
46         // Order by the title in the current locale
47         int result = mLabelComparator.compare(getSortingTitle(a), getSortingTitle(b));
48         if (result != 0) {
49             return result;
50         }
51 
52         // If labels are same, compare component names
53         result = a.componentName.compareTo(b.componentName);
54         if (result != 0) {
55             return result;
56         }
57 
58         if (mMyUser.equals(a.user)) {
59             return -1;
60         } else {
61             Long aUserSerial = mUserManager.getSerialNumberForUser(a.user);
62             Long bUserSerial = mUserManager.getSerialNumberForUser(b.user);
63             return aUserSerial.compareTo(bUserSerial);
64         }
65     }
66 
getSortingTitle(AppInfo info)67     private String getSortingTitle(AppInfo info) {
68         if (!TextUtils.isEmpty(info.appTitle)) {
69             return info.appTitle.toString();
70         }
71         if (info.title != null) {
72             return info.title.toString();
73         }
74         return "";
75     }
76 }
77