15683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal/*
25683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal * Copyright (C) 2015 The Android Open Source Project
35683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal *
45683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
55683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal * you may not use this file except in compliance with the License.
65683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal * You may obtain a copy of the License at
75683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal *
85683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
95683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal *
105683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal * Unless required by applicable law or agreed to in writing, software
115683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
125683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal * See the License for the specific language governing permissions and
145683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal * limitations under the License.
155683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal */
16d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyalpackage com.android.launcher3.allapps;
175683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal
185683f871722254e4e357cf3fb77cd28156278e51Sunny Goyalimport android.content.Context;
197c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyalimport android.os.Process;
207c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyalimport android.os.UserHandle;
215683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal
22d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyalimport com.android.launcher3.AppInfo;
235683f871722254e4e357cf3fb77cd28156278e51Sunny Goyalimport com.android.launcher3.compat.UserManagerCompat;
24d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyalimport com.android.launcher3.util.LabelComparator;
255683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal
265683f871722254e4e357cf3fb77cd28156278e51Sunny Goyalimport java.util.Comparator;
275683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal
285683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal/**
295683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal * A comparator to arrange items based on user profiles.
305683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal */
31d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyalpublic class AppInfoComparator implements Comparator<AppInfo> {
325683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal
335683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal    private final UserManagerCompat mUserManager;
347c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal    private final UserHandle mMyUser;
35d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal    private final LabelComparator mLabelComparator;
365683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal
37d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal    public AppInfoComparator(Context context) {
385683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal        mUserManager = UserManagerCompat.getInstance(context);
397c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal        mMyUser = Process.myUserHandle();
40d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal        mLabelComparator = new LabelComparator();
415683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal    }
425683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal
435683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal    @Override
44d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal    public int compare(AppInfo a, AppInfo b) {
45d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal        // Order by the title in the current locale
46d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal        int result = mLabelComparator.compare(a.title.toString(), b.title.toString());
47d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal        if (result != 0) {
48d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal            return result;
49d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal        }
50d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal
51d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal        // If labels are same, compare component names
52d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal        result = a.componentName.compareTo(b.componentName);
53d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal        if (result != 0) {
54d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal            return result;
55d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal        }
56d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal
57d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal        if (mMyUser.equals(a.user)) {
585683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal            return -1;
595683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal        } else {
60d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal            Long aUserSerial = mUserManager.getSerialNumberForUser(a.user);
61d164b7f4abcba6cc965c2264257569f88ad5e4a5Sunny Goyal            Long bUserSerial = mUserManager.getSerialNumberForUser(b.user);
625683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal            return aUserSerial.compareTo(bUserSerial);
635683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal        }
645683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal    }
655683f871722254e4e357cf3fb77cd28156278e51Sunny Goyal}
66